Welcome, Guest
Username: Password: Secret Key Remember me

TOPIC: Swordfish Code Snippet - TMR0

Re: Swordfish Code Snippet - TMR0 10 years 5 months ago #15360

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.

Read full article...

Timer0 Code Examples 8 years 5 months ago #6873

  • odessa
  • odessa's Avatar
  • Offline
  • Fresh Boarder
Hi Guys,

I have a question about the Swordfish Timer0 Example.

Its says
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 ?

Many Thanks

Jay

Re: Timer0 Code Examples 8 years 5 months ago #6874

  • be80be
  • be80be's Avatar
  • Offline
  • Senior Boarder
  • Posts: 79
  • Thanks received: 90
Your preloading timer0 with a 1

Timer Offset is set at 1 and counts up
The following user(s) said Thank You: odessa

Re: Timer0 Code Examples 8 years 5 months ago #6875

  • bitfogav
  • bitfogav's Avatar
  • Offline
  • Fresh Boarder
  • Posts: 9
  • Thanks received: 53
Make sure that the Internal Check Box is selected

Graham also points out this in the tutorial, but like me and odessa we can't find what this Check box is?

Attachments:
The following user(s) said Thank You: odessa

Re: Timer0 Code Examples 8 years 5 months ago #6878

Ahh yes, I was using one calculator although I linked another.

There looks like a bug with the linked calculator; it's missing one cycle..
Timer Period = Cycle Time x Counts to Overflow
Timer Period = (1/(20000000/4)) x (256x32)
Timer Period = 200nS x 8192
Timer Period = 1.6384 mS
Where as the linked software appears to calculate like so:
Timer Period = Cycle Time x Counts to Overflow
Timer Period = (1/(20000000/4)) x (255x32)
Timer Period = 200nS x 8160
Timer Period = 1.632 mS
The following user(s) said Thank You: odessa

Re: Timer0 Code Examples 8 years 5 months ago #6877

  • bitfogav
  • bitfogav's Avatar
  • Offline
  • Fresh Boarder
  • Posts: 9
  • Thanks received: 53
Thank you Graham for clearing that up

Another question:
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?.
Interrupt TMR0_Interrupt()
    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
        EndIf
    EndIf                  
    Restore                  // Backup system variables
End Interrupt
Time to create page: 0.244 seconds