# TMR0 counting

#### AlucardSensei

Joined Aug 7, 2012
20
Hey, I have something that's not really a problem, but more of a clarification. I need to count exactly 1ms with the timer 0 (precision is really important here). Oscillator is on 8Mhz. I read something about timer 0 skipping the first 2 cycles when it is set manually, and if I set the prescaler to 1:8, I need to set the tmr0 to 4 every time to get exactly 250 cycles, right? Thanks in advance.

#### John P

Joined Oct 14, 2008
1,995
I think you should toggle an output pin and check it with an oscilloscope to get the timing right; otherwise you'll never be sure you have the correct settings. What will happen at the end of your measured millisecond, an interrupt? With the time taken to save registers, that alone will give you a time of response that's hard to predict.

#### atferrari

Joined Jan 6, 2004
4,684
The MPSIM in MPLAB provides the stopwatch function, simple and easy. You could start there.

As John says, a scope is good to confirm the actual timing.

For related calculations I use this calculator.

I recall using TMR0 in several 16F micros and the concept I retained is that once started it run continuously.

I do not recall the actual reason, one day, I moved to TMR2 and never looked back. Used it to generate the "tick" of a software clock.

#### Markd77

Joined Sep 7, 2009
2,806
It's possible, but takes some getting right. Using the prescaler makes it more complicated. The prescaler count gets reset when TMR0 is written to so you have to write to TMR0 two cycles before the point that TMR0 would increment.
Using the prescaler it takes 2 cycles before the prescaler starts counting, ie. if you set TMR0 to 0 and the prescaler is 4, after 2 cycles the prescaler starts counting from 0, after another 4 cycles TMR0 will increase to 1, after another 4 cycles TMR0 will increase to 2, etc.
For using it in an interrupt with prescaler value 4:
Count the number of cycles the instructions take from the interrupt being triggered to the point of changing tmr0 value (including interrupt latency).
Add nops to make that a multiple of 4 cycles (minus 2 for the sync cycles)
Write to TMR0 a value calculated from the above.
The stopwatch is going to be handy for checking.

#### THE_RB

Joined Feb 11, 2008
5,438
Use TMR2, set it to rollover at 250 counts by setting PR2 = (250-1).

Then 1mS will be exactly 8 TMR2 rolls, = 8 interrupts.

It is important to describe what you need the "exact" 1mS period for, as that will determine the best way to do it. If it's for a real time clock you don't need to have every 1mS with no jitter, but if it's for a gated period etc you need ot have no jitter.

#### I have no education!

Joined Sep 19, 2012
47
Surely there must be timers on the internet or in books that you can just copy and change the variables?

Remember only a very limited number of people design from scratch, the rest of just use what they have designed.

In this way most electronics engineers can be thought as sophisticated lego builders!!

#### ErnieM

Joined Apr 24, 2011
8,353
Surely there must be timers on the internet or in books that you can just copy and change the variables?

Remember only a very limited number of people design from scratch, the rest of just use what they have designed.

In this way most electronics engineers can be thought as sophisticated lego builders!!
This is a specific question concerning the operation of a hardware timer common to most PIC devices. Thus it is a proper question to this forum.

And stop calling me Shirley.

#### I have no education!

Joined Sep 19, 2012
47
Hahaha airplane is a quality film!!

im sure the timing requirements and operation is in the operating manual for these devices?

I did these in university and cant quite remember but im sure it was a pain in the neck to do!!

ahh!!

#### AlucardSensei

Joined Aug 7, 2012
20
Use TMR2, set it to rollover at 250 counts by setting PR2 = (250-1).

Then 1mS will be exactly 8 TMR2 rolls, = 8 interrupts.

It is important to describe what you need the "exact" 1mS period for, as that will determine the best way to do it. If it's for a real time clock you don't need to have every 1mS with no jitter, but if it's for a gated period etc you need ot have no jitter.
Yeah, it is for a real time clock, but one that needs to be operational for a very long time, so I can't have a large margin of error. I could've set it to interrupt every 2, 4 or 8ms, but the principle is the same, I still need the same amount of cycles, just with a different prescaler. Anyway, I've determined it works as intended, but I've come across a different problem, which I may or may not post here, seeing as how I'm already embarrassed at how much problems the people around here have solved for me.

#### ErnieM

Joined Apr 24, 2011
8,353
Yeah, it is for a real time clock, but one that needs to be operational for a very long time, so I can't have a large margin of error.
OK, then completely forget about that 1mS rep rate. Just let Timer0 roll over every 256 counts and set the interrupt on the rollover. You'll get an odd number for the time but just use that in your calculations.

Example: 8MHz clock, 1:32 prescale divider, 256 count roll over gets a tap on your shoulder 976.5625 times in one second, or once every 1.024 mS. EXACTLY EVERY TIME. Use that number against your counter to set the long interval.

#### John P

Joined Oct 14, 2008
1,995
If the device has to maintain an accurate clock over a long period, I don't think you should be using a high-speed crystal at all. Those just aren't very accurate for timekeeping purposes. Many PIC processors have the ability to operate a watch crystal at 32.768KHz on TMR1, and if you did that, I assume it would be as accurate as a digital watch. It also uses less power than running the processor in its regular mode, too.

#### Markd77

Joined Sep 7, 2009
2,806
Apart from power saving it doesn't make much difference, most frequencies of crystal 32.768kHz - 20MHz are commonly available in 10 or 20ppm accuracy.

#### John P

Joined Oct 14, 2008
1,995
Yes, you're right. Crystal accuracy is pretty standard. But now I'm worried by the original message saying "Oscillator is on 8Mhz". Does that mean the processor's internal 8MHz oscillator and not a crystal at all? If so, forget measuring 1msec or any other time interval accurately. I think they promise 1% accuracy under the best conditions.

#### AlucardSensei

Joined Aug 7, 2012
20
No, it's a crystal alright.

#### THE_RB

Joined Feb 11, 2008
5,438
If you want accurate seconds there is apage of code example here, to get accurate seconds from any PIC xtal;
http://www.romanblack.com/one_sec.htm

Here is code for exact seconds using a 8Mhz xtal;
Rich (BB code):
  unsigned long bres;   // the 32bit variable (goes in top of code)

// making exact seconds from an 8MHz PIC xtal
// TMR0 runs at 1:1 prescale, ie 2 MHz
// uses 1 variable; unsigned long bres
// gets here every TMR0 int (every 256 ticks)

bres += 256;          // add 256 ticks to bresenham total
if(bres >= 2000000)   // if reached 1 second!
{
bres -= 2000000;    // subtract 1 second, retain error
do_1sec_event();    // update clock, etc
}
Another advantage (besides being able to use any xtal) is that you can "tweak" that 2000000 value to fine tune your xtal to a very high accuracy.

Just be aware that won't compensate for temperature variations, only the average running speed of the xtal.