+ Reply to Thread
Page 1 of 2
1 2 Last
Results 1 to 15 of 26

Thread: Problem facing when using Timer0 of 16f877a

  1. #1
    simpsonss Newbie
    Join Date
    Jul 2008
    Posts
    20

    Question Problem facing when using Timer0 of 16f877a

    hi guys,

    i'm now trying to use Timer0 of 16f877a to overflow every 6.25ms. So this is how i calculate the TMR0 value. i'm using 4Mhz XTAL. and my prescaler is set to 1:256.

    4Mhz/4= 1Mhz

    1Mhz/256 = 3906.25hz

    1/3906.25hz = 256us

    6.25ms/256us=24.41

    256 - 24.41 = 231.58

    so i decided to use 232 as my TMR0 value. So am i correct?

    in my program, i complement my LEDs everytime interrupt occur which mean every 6.25ms. But when i measure the period from my logic analyzer i found that the period time is not correct. SO i'm wonder maybe is my calculation problem or other .

    thank you.


  2. #2
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,805

    Default

    If you set TMR0 to 232 then you will get an overflow every (256-232)*256 = 6144 instruction cycles.

    1,000,000/6144 = 162.7 Hz

    1/162.7 = 0.006114 Seconds or 6.114mS

    Looks good to me. Even if it is kinda circular.

    However, you will get better accuracy if you set the prescaler to 32 and TMR0 to 61.

    <edit> Note that this time is for half a period and so a frequency meter will register 80Hz.</edit>

    Mike.
    Last edited by Pommie; 7th January 2009 at 09:40 AM.

  3. #3
    Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent Gayan Soyza Excellent
    Join Date
    Oct 2006
    Location
    Colombo
    Posts
    1,664
    Blog Entries
    1

    Default

    The gap is too big.

    Better set pre scaller to 32 & load 195(61) to TMR0.

    195*32 = 6240uS

    So you need to add another 10uS to your routine.

    EDIT:Mike answered before
    Last edited by Gayan Soyza; 7th January 2009 at 09:50 AM.

  4. #4
    simpsonss Newbie
    Join Date
    Jul 2008
    Posts
    20

    Default

    Seems like my calculation is correct. The following is my picBasic code. Where am i go wrong? because the period of one logic high or logic low is wrong.


    'DECLARE SYMBOLS
    Symbol T0IF = INTCON.2 ' TMR0 Overflow Interrupt Flag
    Symbol T0IE = INTCON.5 ' TMR0 Overflow Interrupt Enable
    Symbol GIE = INTCON.7 ' Global Interrupt Enable
    Symbol PS0 = OPTION_REG.0 ' Prescaler Rate Select
    Symbol PS1 = OPTION_REG.1 ' Prescaler Rate Select
    Symbol PS2 = OPTION_REG.2 ' Prescaler Rate Select
    Symbol PSA = OPTION_REG.3 ' Prescaler Assignment
    Symbol TOCS = OPTION_REG.5
    symbol LED = PORTB

    TRISB = 0
    PORTB = 0

    PS0 = 1
    PS1 = 1
    PS2 = 1





    thank you for the reply.
    Last edited by simpsonss; 8th January 2009 at 01:23 PM.

  5. #5
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,805

    Default

    I can't see anything wrong. Can you try a non-interrupt version.

    Something like,
    Code:
        OPTION_REG=7
        while(1)
            while(INTCON.TMR0IF=0)
            wend
            INTCON.TMR0IF=0
            TMR0=232
            LED = ~LED
        wend
    
    Mike.

  6. #6
    simpsonss Newbie
    Join Date
    Jul 2008
    Posts
    20

    Default

    HI,
    yeah~ the code u given works.i can get half period for 6.15ms. So what wrong with my part?

    After i troubleshooting my code. i found that when i enable global interrupt, it wont work. but once i disable it. i cant get 6.20ms back for half period.

    thank you.
    Last edited by simpsonss; 8th January 2009 at 02:25 AM.

  7. #7
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,805

    Default

    I'm afraid I'm not familiar with the compiler that you are using so can't really help. I suspect it is something to do with the interrupt code. Is there a forum for your compiler?

    If you state which compiler it is then maybe someone else who is familiar with it might help.

    Mike.

  8. #8
    simpsonss Newbie
    Join Date
    Jul 2008
    Posts
    20

    Default

    i'm using PIC basic pro compiler.

    anyway thanks.so instead of using the interrupt i also can use the code u write to generate the frequency i want.Am i correct?

  9. #9
    simpsonss Newbie
    Join Date
    Jul 2008
    Posts
    20

    Default

    i'm now trying to write the code in assembly and try the interrupt again.

  10. #10
    simpsonss Newbie
    Join Date
    Jul 2008
    Posts
    20

    Default

    Hi,

    anything wrong with my code?

    list p=16F877A
    #include <p16F877A.inc>


    ORG 0x00
    goto Main
    org 0x04
    goto T0_INT

    Main

    bsf STATUS,RP0 ;switch 2 bank1
    clrf TRISB
    movlw b'00000111'
    movwf OPTION_REG

    bcf STATUS,RP0 ;switch 2 bank0

    movlw 0xe8
    movwf TMR0

    movlw 0xa0
    movwf INTCON

    clrf PORTB ;clear PORTB

    goto $

    ;interrupt subroutine
    T0_INT
    clrf T0IE
    clrf T0IF
    movlw 0xe8
    movwf TMR0
    comf PORTB
    bsf INTCON,5
    retfie

    END

    thanks.

  11. #11
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,805

    Default

    You're code is very close to working, you need a config line and I changed the INTCON bits just in case.

    try,
    Code:
    		list	p=16F877A
    		#include <p16F877A.inc> 
    
    		__config _XT_OSC & _PWRTE_OFF & _WDT_OFF &_LVP_OFF 
    
    
    		org	0x00
    		goto	Main
    		org	0x04
    		goto	T0_INT
    
    Main
    		bsf	STATUS,RP0	;switch 2 bank1
    		clrf	TRISB
    		movlw	b'00000111'
    		movwf	OPTION_REG
    		bcf	STATUS,RP0	;switch 2 bank0
    		movlw	0xe8
    		movwf	TMR0
    		bsf	INTCON,T0IE	;enable timer interrupt
    		bsf	INTCON,GIE
    		clrf	PORTB		;clear PORTB
    		goto	$
    
    ;interrupt subroutine 
    T0_INT
    		clrf	INTCON,T0IF	;clear int flag
    		movlw	0xe8
    		movwf	TMR0
    		comf	PORTB,f
    		retfie
    		end
    
    Mike.

  12. #12
    simpsonss Newbie
    Join Date
    Jul 2008
    Posts
    20

    Default

    hi pommie,
    since the idea and concept of the code for PicBasic is same as the assmebly which both i written. How come the result is different? ehm...

    regards the config line u added in. since everything is off. So why i can get the correct result without adding in the config line?

    thanks for the reply.

  13. #13
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,805

    Default

    As you state, the code is identical and so it must be the compiler that does something strange. I would check the manual especially the interrupt section.

    With regard to the config line, the default is all ones and so,
    LVP would be enabled - rb3 must be tied high.
    WDT is enabled - it will reset every few mS.
    Oscillator is RC - won't work with a crystal.

    Actually, this could be the problem with your basic program. The watchdog timer could be reseting the processor.

    Mike.

  14. #14
    simpsonss Newbie
    Join Date
    Jul 2008
    Posts
    20

    Default

    hi pommie,
    ic..really learn alot.thanks. i would like to ask something regard programming style. if i want to read data from a table using assembly. how am i gonna do?

    .data 0x01, 0x02, 0x03

    some sort like dis. i want to display the data 1 by 1 to the led. how is the coding idea?

    thank you.

  15. #15
    Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent Pommie Excellent
    Join Date
    Mar 2005
    Location
    Brisbane Australia
    Posts
    6,805

    Default

    You use an instruction called retlw and do a jump into the middle of the message,

    Code:
    Message		addwf	PCL,F
    		retlw	0x01
    		retlw	0x02
    		retlw	0x03
    
    To use it you put the item number in W and call Message.

    You can also use dt to write a string of bytes,
    Code:
    		dt	0x01,0x02,0x03
    OR,
    		dt	"Hello world!",0
    
    <edit> forgot to mention, these types of tables must reside in 1 page of memory.</edit>

    Mike.
    Last edited by Pommie; 8th January 2009 at 07:16 AM.

+ Reply to Thread
Page 1 of 2
1 2 Last

Similar Threads

  1. Timer0 problem
    By littletransistor in forum Micro Controllers
    Replies: 8
    Latest: 27th November 2008, 05:55 AM
  2. LCD & CHECK CODE (facing problem)
    By smileguitar in forum Micro Controllers
    Replies: 17
    Latest: 20th March 2008, 04:30 PM
  3. Problem in LED Flasher in pic 16F877A
    By rakan in forum Micro Controllers
    Replies: 6
    Latest: 7th April 2007, 10:45 AM
  4. info about computes im facing some hardware problem
    By salman007 in forum Chit-Chat
    Replies: 6
    Latest: 29th March 2007, 06:23 PM
  5. XTAL Problem in PIC 16F877A
    By Hesam Kamalan in forum Micro Controllers
    Replies: 7
    Latest: 16th July 2006, 06:41 PM

Tags for this Thread