https://www.romanblack.com/one_sec.htmSimple and fast system to get reliable timer periods from
a PIC controller.
This system (PIC assembler source is provided) gives a simple,
fast way to generate regular periods with a PIC at any clock speed.
Great for one second events like simple clocks. You can use any
crystal you have, 4.0 MHz, 12.137253 MHz, (ANY crystal) and ANY
prescaler value, and still get perfect one second timing.
It will generate reliable periods from milliseconds to many seconds,
with very fast code execution.
zahidkhan said:bUT TMR0 can hold maximum of 256 i.e it is an 8-bit timer. Isnt it ?.how can we load it with 0xFC17.
Device 16F84A XTAL = 4.0 Declare Lcd_DTPin PortB.4 Declare Lcd_ENPin Portb.3 Declare Lcd_RSPin Portb.2 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 T0CS = OPTION_REG.5 DIM MS AS WORD DIM SECOND AS BYTE DIM MINUTE AS BYTE DIM HOUR AS BYTE PS0 = 1 PS1 = 0 PS2 = 0 PSA = 0 T0CS = 0 'set the clock source for internal oscillator T0IF = 0 'clear the interrupt flag T0IE = 1 'enable tmr0 interrupt GIE = 1 'enable Global interrupts TMR0 = 6 On Interrupt goto InterruptServiceRoutine GoTo Main 'start of program, jumps straight to main Disable 'disable interrupts InterruptServiceRoutine: T0IF = 0 'clear the interrupt flag TMR0 = 6 INC MS IF MS >= 999 THEN MS = 0 INC SECOND IF SECOND >= 59 THEN SECOND = 0 INC MINUTE IF MINUTE >= 59 THEN MINUTE = 0 INC HOUR ENDIF ENDIF ENDIF Resume 'return back to where the main code was running 'before the interrupt Enable 're enanble interupts Main: Print at 1,1, DEC HOUR," ",DEC MINUTE," ", DEC SECOND," ",DEC MS GoTo Main
Here's the equation to calculate:
Period = (256 - TMR0)*(4/fosc)*(Prescaler)
For example, if Period = 1 ms and fosc = 4 MHz and using Prescaler 1:4
1ms = (256 - TMR0)(1us)(1)
TMR0 = 6 = 0x06
This is thevalue you must load to the timer 0 register. Remember to turn on the timer and clear the timer interrupt flag each time after the timer interrupt occurs. Careful with the global interrupt also because any unclear interrupt flag would trigger another interrupt. For 1 s of period, choose proper prescaler value which is suitable. Remember* timer 0 is only 8-bit. In order to have more precise timing, use 16 bit timer.
All the best.
what about for getting 50mSec timer for 20MHz crystal at a 256 prescaler