TMR0 is an 8 byte timer/counter that can be pre-scaled to change vary its incrementing cycle. It is quite different in regards to setting up compared to the 16F's, but here's an example.
Pre-scaling is important as we may want the Timer Register to increment faster/slower in different applications. The speed in which it does cycle is dependent on both the external crystal speed and the pre-scaling options, or the Prescaler and external clock if an external source is used.
Now to make a timer that can be used to count mS very accurately. An easy way to find out how much time each clock cycle will take is to use a pic timer calculator. Configure the oscillator speed (20Mhz for this example), along with the pre-scaler (1:32 for this example). The TMR0 period should now be 1.638mS (when TMR0 reaches 255 and rolls over to 0, 1.638mS has elapsed). To make things easier, I will work with a scale of 1000 for my mS counter. Now when mS = 10000, 10mS has elapsed, so there is no need to use Floats.
I have a question about the Swordfish Timer0 Example.
Make sure that the Internal Check Box is selected, now be sure that 20Mhz is the clock speed, and the pre-scaler is set to 1]
I have downloaded the timer calc. linked in the article and it doesn't have a tick box for 'Internal'. Also when set as above it doesn't show 1.638 unless the 'Timer Offset' slide is moved from 0 to 1. Then it shows 1638.4 which is correct... What is the Timer Offset for ?
Re: Timer0 Code Examples
8 years 5 months ago #6874
The Swordfish Code Snippet - TMR0, as an Interrupt routine which Inc(mS, 1638)
Now if it takes 7 roll overs of TMR0 to Increase mS, so that mS equals over 10000?
(actual figure of mS = 11466 after 7 roll overs of TMR0)
then why is mS = mS - 10000? because correct me if im wrong mS now equals 1466?
So that means that TMR0 has to only roll over 6 times for mS to equal mS >= 10000 ?.
Then I wouldn't have thought that this would be very accurate over time?.
Save(0) // Backup system variables
If TMR0IF = 1 Then // Check if a TMR0 Interrupt occurred
TMR0IF = 0 // Clear the interrupt
Inc(mS, 1638) // Increment the mS counter (scale of 1000)
If mS >= 10000 Then // Working with a scale of 1000, so this
mS = mS - 10000 // checks if 10mS has elapsed
TMR0_Event = True
Restore // Backup system variables