Go search for OLYMPIC TIMER in this forum for an example of TMR1 and PICBASIC Interrupts. The text describes how it all works.
Go search for OLYMPIC TIMER in this forum for an example of TMR1 and PICBASIC Interrupts. The text describes how it all works.
I posted two examples in the "Code Examples" section that show how to make accurate clocks using polling of TMR0. You could switch to TMR1 with a little effort - Look for "Easy and Accurate Clocks without RTC IC"
The example programs require a 4.000MHz Crystal, use TMRO, but do not require preloading counters. Although pollling is used, you have over 65mSec to do other stuff before needing to return to your overflow check (polling routine). The programs are short as well. Good Luck.
Paul Borgmeier
Salt Lake City, Utah
USA
Thanks a lot.
By the way, what is really the significance of PRESCALER in Timer0? I found examples that use 1:32, then another example that uses 1:8. The goal was to achieve a 1 second increment in clock display. Could you please show to me the proper way to compute the 1 second interval using 4MHz Cristal. I saw some PIC Basic examples that loads certain figures to the TMR0. How did they compute that number?
Emmanuel
Don Bosco-Victorias,
Philippines
With the code I posted in the Example Section (referenced above), the goal was to give the user as much time as possible to do other stuff before needing to update the LCD display, so I went with a 256 prescaler.
Most TMR0 timers are 8 bit, meaning 256 instructions per flag rollover. A 4.000 Mhz crystal gives a 1.000 uS instruction (uS=microSecond). This means the default time for TMRO to rollover is 256 uS.
The prescaler increases the TMR0 rollover Flag time:
Prescale 2 – rollover flag is 2 * 256 = 512 uS
Prescale 8 – rollover flag is 8 * 256 = 2048 uS
Prescale 256 - rollover flag is 256 * 256 = 65536 uS.
At 1.000 uS per instruction, there are 1,000,000 instructions per second. If you do not use a prescaler, then TMRO overflows 1,000,000/256=3906.25 times per second. In code, you just need to keep track of 3906.25 overflows, and 1 second has passed.
If you use a prescaler of 256, then TMR0 overflows 1,000,000/65,636 = 15.235541 times per second.
Because neither of these divisions give integers, some people preload the TMRO to start at a given time rather than 0. You can do this but it is more work - it also takes processor time to load the timer which must be adjusted out in order to maintain high accuracy. See Melanie's code (referenced above) for a perfect example of how to make this approach work.
Since my code allows for colon blink, I tracked 0.5 second time intervals rather than 1.0 second time intervals. Details of the TMRO overflow tracking and math involved are in the code comments at the bottom of the post in the Code Examples Section. The approach I used does not require preloading the timer, but does require keeping track of the residual from the timer overflows (and leads to zero error).
Again, Good Luck,
Paul Borgmeier
Salt Lake City, Utah
USA
Bookmarks