Welcome, Guest
Username: Password: Secret Key Remember me

TOPIC: Swordfish Code Snippet - TMR0

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

  • be80be
  • be80be's Avatar
  • Offline
  • Senior Boarder
  • Posts: 79
  • Thanks received: 90
Timer0 can only count 255 and then it rolls over if you google you'll see there a lot of people saying you get the wrong value if you figure it at 256
a) On a TMR0 overflow (255 → 0) the T0IF flag is set briefly (INTCON<2>).

But when you get down to the end of Microchips data sheet you read this LOL
Time to increment counter variable = Internal instruction cycle x 28
(we must count the zero) = 500nS x 256 = 128μS
So it is 256 when doing the math funny there a lot of people that believe it's 255

This is where zero counts LOL
The following user(s) said Thank You: bitfogav

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

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?.
At first glance it may appear to be a very inaccurate method.. Keep in mind the difference is carried forward - which ensures no timing data is lost.

The oscillator/hardware could be configured to accommodate more-natural timing.. I think when I wrote that short guide I'd considered just that, although kept it at 20Mhz as most PIC new comers would probably be using that hardware by default.

So yes; after the first second, the mS counter (with a scale of 1000) would equal 11466. One second is deducted from the counter, leaving 1466. The next interrupt would add 1638, resulting with 3104, and so on (difference carried forward).

The overall result; at any one-second interval the program would be accurate to within 1.638mS. At large scale (and in the perfect world); the interrupt that occurred after 100 hours would be accurate to within 1.638mS (6000 perfect seconds, plus a possible 1.638mS lag).

This has been a good discussion; I've merged the topic with the comments for the TMR0 article.
The following user(s) said Thank You: bitfogav, odessa

Re: Swordfish Code Snippet - TMR0 8 years 5 months ago #6884

  • odessa
  • odessa's Avatar
  • Offline
  • Fresh Boarder
Thanks to all for explaining this, and thanks for posting my question Gav

I wasn't taking into account the -10000 and carry over but it makes perfect sense now

Re: Swordfish Code Snippet - TMR0 8 years 5 months ago #6886

  • be80be
  • be80be's Avatar
  • Offline
  • Senior Boarder
  • Posts: 79
  • Thanks received: 90
Gram the Pic multc-cal give the same numbers
Attachments:

Re: Swordfish Code Snippet - TMR0 8 years 5 months ago #6887

Not sure I'm on the same page Burt - It is showing us a different result as well.. The Multi Calc has a setting for "Re-load cycles" which is a minimum of 1 cycle. You've also defined a pre-load setting (1). The overall result is 1.6332mS.

Reloading the timer is a common approach, however, I'm using the timer's overflow flag (and not re-loading the timer).

Re: Swordfish Code Snippet - TMR0 8 years 5 months ago #6888

  • be80be
  • be80be's Avatar
  • Offline
  • Senior Boarder
  • Posts: 79
  • Thanks received: 90
I posted the wrong picture I guess This calculator I think works this out right


I was seeing what happen when you loaded a 1 LOL you get the same just reloading the timer flag too 1.638
Attachments:
Time to create page: 0.245 seconds