![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
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 |
|
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 10:40 AM. | |
| |
| | #3 |
|
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 10:50 AM. | |
| |
| | #4 |
|
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 02:23 PM. | |
| |
| | #5 |
|
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
| |
| |
| | #6 |
|
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 03:25 AM. | |
| |
| | #7 |
|
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 |
|
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 |
|
i'm now trying to write the code in assembly and try the interrupt again.
| |
| |
| | #10 |
|
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 |
|
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 | |
| |
| | #12 |
|
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 |
|
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 |
|
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 |
|
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 You can also use dt to write a string of bytes, Code: dt 0x01,0x02,0x03 OR, dt "Hello world!",0 Mike. Last edited by Pommie; 8th January 2009 at 08:16 AM. | |
| |
|
| Tags |
| facing, problem, timer0 |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| Timer0 problem | littletransistor | Micro Controllers | 8 | 27th November 2008 06:55 AM |
| LCD & CHECK CODE (facing problem) | smileguitar | Micro Controllers | 17 | 20th March 2008 05:30 PM |
| Problem in LED Flasher in pic 16F877A | rakan | Micro Controllers | 6 | 7th April 2007 11:45 AM |
| info about computes im facing some hardware problem | salman007 | Chit-Chat | 6 | 29th March 2007 07:23 PM |
| XTAL Problem in PIC 16F877A | Hesam Kamalan | Micro Controllers | 7 | 16th July 2006 07:41 PM |