Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

Counter/Timer Question

Counter/Timer Question

2005-06-20 by jeff_smith_8992

When I use the AVR's internal oscillator or an external crystal
oscillator, like 1.0 MHz, is the frequency 1,000,000 Hz (give or take
a few Hz for accuracy, temp, etc)?  And not a power of 2, like 2 to
the 20th = 1,048,576?

The reason I'm asking, is because the prescaler divisors are all
powers of 2 (i.e., 2, 4, 8, 16, 32, etc), and this makes it difficult
to use the prescaler to get whole number frequencies.  For instance,
if I use a 1 MHz clock and the 1024 prescaler, it gives an interval of
0.001024 seconds instead of a nice, even number like 0.001 seconds.

I guess my real question is, "Given a particular input clock, how can
I make a timer that counts in, say, one millisecond intervals"?  And
how can I keep it as accurate as possible over a long period of time?

Thanks for any help.

Jeff

Re: [AVR-Chat] Counter/Timer Question

2005-06-20 by Ralph Hilton

On Mon, 20 Jun 2005 16:38:09 -0000 you wrote:

>
>When I use the AVR's internal oscillator or an external crystal
>oscillator, like 1.0 MHz, is the frequency 1,000,000 Hz (give or take
>a few Hz for accuracy, temp, etc)?  And not a power of 2, like 2 to
>the 20th = 1,048,576?
>
>The reason I'm asking, is because the prescaler divisors are all
>powers of 2 (i.e., 2, 4, 8, 16, 32, etc), and this makes it difficult
>to use the prescaler to get whole number frequencies.  For instance,
>if I use a 1 MHz clock and the 1024 prescaler, it gives an interval of
>0.001024 seconds instead of a nice, even number like 0.001 seconds.
>
>I guess my real question is, "Given a particular input clock, how can
>I make a timer that counts in, say, one millisecond intervals"?  And
>how can I keep it as accurate as possible over a long period of time?

For accuracy the internal RC oscillator is useless.

Xtals are available with frequencies that are a power of 2 e.g. 2097152 and
4194304 are available from my local supplier.

For long time better accuracy a TCXO can be used such as the 32768 DS32KHZ from
Maxim.


--
Ralph Hilton
http://www.ralphhilton.org
C-Meter: http://www.cmeter.org
FZAOINT http://www.fzaoint.net

Re: [AVR-Chat] Counter/Timer Question

2005-06-20 by Dave VanHorn

At 11:38 AM 6/20/2005, jeff_smith_8992 wrote:

>When I use the AVR's internal oscillator or an external crystal
>oscillator, like 1.0 MHz, is the frequency 1,000,000

You would be looking at something more like 1-ish MHz.

Re: [AVR-Chat] Counter/Timer Question

2005-06-20 by David Kelly

On Mon, Jun 20, 2005 at 04:38:09PM -0000, jeff_smith_8992 wrote:
> 
> When I use the AVR's internal oscillator or an external crystal
> oscillator, like 1.0 MHz, is the frequency 1,000,000 Hz (give or take
> a few Hz for accuracy, temp, etc)?  And not a power of 2, like 2 to
> the 20th = 1,048,576?

Yes, both. If you purchase a 1,000,000 Hz crystal or oscillator it will
not be a power of two. But you could choose 1,048,576 if that suited the
application better.

On top of that crystals are graded for accuracy on a "ppm" scale. Parts
Per Million. Common cheap crystals are 80 ppm. 10 ppm is getting
expensive. 2 ppm is getting into the range of an oscillator with built
in oven. In general crystal accuracy and stability is more important
to radio applications than to microprocessors.

-- 
David Kelly N4HHE, dkelly@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

Re: [AVR-Chat] Counter/Timer Question

2005-06-20 by James Hatley

Hello Mr. Jeff Smith,

Well as the others have mentioned the internal oscillator isn't too
accurate. 2% would be the best estimate of how fine you can tune it. But if
that is OK, it is easy to get pretty much any even number you want.

Study the Atmel manual carefully on your timer. (the following is somewhat
simplified but) Using mode 4 or the CTC, clear counter on top, you can set
most anything you want by loading the appropriate OCR register and setting
all the appropriate bits. For instance, at 1Mhz the counter is counting 1us
per tick so if you want 1000uS or 1ms you just put 1000 into the counter (
assuming you are using a 16bit counter), if not and it is an 8 bit counter,
set to divide by 4, now you have 4 uS per tick so put in 250 and you get an
interrupt every 1ms.

But the KEY here is to thoroughly understand the timer hardware and you will
find that you can do many timing tasks quite well.

Now if you really need "accuracy" ... you will have to install a crystal
like 2Mhz, 4Mhz, 8Mhz or 16Mhz or whatever fits your needs and power
desires. (Low Mhz lower power.)

Jim

----- Original Message ----- 
Show quoted textHide quoted text
From: "jeff_smith_8992" <jeff_smith_8992@yahoo.com>
To: <AVR-Chat@yahoogroups.com>
Sent: Monday, June 20, 2005 9:38 AM
Subject: [AVR-Chat] Counter/Timer Question


>
> When I use the AVR's internal oscillator or an external crystal
> oscillator, like 1.0 MHz, is the frequency 1,000,000 Hz (give or take
> a few Hz for accuracy, temp, etc)?  And not a power of 2, like 2 to
> the 20th = 1,048,576?
>
> The reason I'm asking, is because the prescaler divisors are all
> powers of 2 (i.e., 2, 4, 8, 16, 32, etc), and this makes it difficult
> to use the prescaler to get whole number frequencies.  For instance,
> if I use a 1 MHz clock and the 1024 prescaler, it gives an interval of
> 0.001024 seconds instead of a nice, even number like 0.001 seconds.
>
> I guess my real question is, "Given a particular input clock, how can
> I make a timer that counts in, say, one millisecond intervals"?  And
> how can I keep it as accurate as possible over a long period of time?
>
> Thanks for any help.
>
> Jeff
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>
>

Re: Counter/Timer Question

2005-06-20 by arhodes19044

I agree that it is a major pain in the neck having powers of 2 as 
the prescaler, but NOT powers of 2 as the usual crystal freq.

In my case I needed a 1 millisecond clock.  So, I used a 16 bit 
clock for my 16MHz crystal.  (8 bits would not do).

I uses CTC mode with a prescaler of zero, and triggered the 
interrupt after 16,000 clock cycles.  (16e6/16e3 = 1e3).  This 
worked out fine.  

The crystal's accuracy is rated for +/- 30 ppm.  I found the 
accuracy was 7ppm.  I also let the clock run for 2 days and then 
calculated a correction factor.  A "leap millisecond" is added or 
subtracted every so often.  It is very accurate now.  I need 
something that is better than a second per day (1 sec per day is 
11.7ppm).  I am sure I can get a better calibration if I were to run 
the clock for a week, but this is sufficient for my current needs.

I initially found that the error was MUCH higher, at 62ppm, but then 
I realized that this was about 1:16000.  I then realized that I had 
the counters interrupt trigger at the count of 16,000,  but this 
would occur on the 16,001th clock cycle.  So once I changed the 
timer to trigger on 15,999 and this was great.

For your 1mhz crystal, 16 bit timer, you would set the timer to 
trigger at 999 with a prescaler of zero.  You could also use a 
different prescaler and a smaller comparison value.  You have to 
check all the possibilities, but 1 mhz clock, prescale of 8 and a 
count of 125 works, on an 8 bit timer, and gives a millisecond 
resolution timer.

So, I have found that it is quite possible to get a reasonable 
millisecond resolution on the AVR timers.  I have also found that 
extremely good accuracy is possible with a simple calibration.

-Tony





--- In AVR-Chat@yahoogroups.com, "jeff_smith_8992" 
<jeff_smith_8992@y...> wrote:
> 
> When I use the AVR's internal oscillator or an external crystal
> oscillator, like 1.0 MHz, is the frequency 1,000,000 Hz (give or 
take
> a few Hz for accuracy, temp, etc)?  And not a power of 2, like 2 to
> the 20th = 1,048,576?
> 
> The reason I'm asking, is because the prescaler divisors are all
> powers of 2 (i.e., 2, 4, 8, 16, 32, etc), and this makes it 
difficult
> to use the prescaler to get whole number frequencies.  For 
instance,
> if I use a 1 MHz clock and the 1024 prescaler, it gives an 
interval of
> 0.001024 seconds instead of a nice, even number like 0.001 seconds.
> 
> I guess my real question is, "Given a particular input clock, how 
can
> I make a timer that counts in, say, one millisecond intervals"?  
And
> how can I keep it as accurate as possible over a long period of 
time?
Show quoted textHide quoted text
> 
> Thanks for any help.
> 
> Jeff

Re: [AVR-Chat] Re: Counter/Timer Question

2005-06-21 by Roy E. Burrage

You can also set up the prescaler and count registers for something slightly less than what you require, then waste a few cycles in the ISR to get your required time.

System clock = 1 MHz

Using a prescaler value of 32 and a count register value of 31 would give 992 timer overflows per second, or 1 overflow every 992 uSec. Then if you either waste or do something else for 7 cycles you'll have 999.

Application note AVR130 explains this pretty well.


REB


arhodes19044 wrote:
Show quoted textHide quoted text
I agree that it is a major pain in the neck having powers of 2 as 
the prescaler, but NOT powers of 2 as the usual crystal freq.

In my case I needed a 1 millisecond clock.  So, I used a 16 bit 
clock for my 16MHz crystal.  (8 bits would not do).

I uses CTC mode with a prescaler of zero, and triggered the 
interrupt after 16,000 clock cycles.  (16e6/16e3 = 1e3).  This 
worked out fine.  
  

Re: Counter/Timer Question

2005-06-21 by arhodes19044

I do not think it is necessary to interrupt early, then dawdle for a 
few cycles, then execute the interrupt code.  In the simpler  
method, there is a slight "phase delay" of the interrupt code 
execution, but since it executes everytime with the same delay, then 
the next time it executes will be (for instance) exactly one 
millisecond after the last time.  So, there might be a slight delay 
compared to world time, but I doubt most people (and even most 
software) would ever notice this discrepancy.  It is more important 
that the interval between executions is the exact same.

-Tony

--- In AVR-Chat@yahoogroups.com, "Roy E. Burrage" <RBurrage@b...> 
wrote:
> You can also set up the prescaler and count registers for 
something 
> slightly less than what you require, then waste a few cycles in 
the ISR 
> to get your required time.
> 
>     System clock = 1 MHz
> 
> Using a prescaler value of 32 and a count register value of 31 
would 
> give 992 timer overflows per second, or 1 overflow every 992 
uSec.  Then 
> if you either waste or do something else for 7 cycles you'll have 
999.
Show quoted textHide quoted text
> 
> Application note AVR130 explains this pretty well.
> 
> 
> REB
> 
> 
> arhodes19044 wrote:
> 
> >I agree that it is a major pain in the neck having powers of 2 as 
> >the prescaler, but NOT powers of 2 as the usual crystal freq.
> >
> >In my case I needed a 1 millisecond clock.  So, I used a 16 bit 
> >clock for my 16MHz crystal.  (8 bits would not do).
> >
> >I uses CTC mode with a prescaler of zero, and triggered the 
> >interrupt after 16,000 clock cycles.  (16e6/16e3 = 1e3).  This 
> >worked out fine.  
> >
> >  
> >

Re: Counter/Timer Question

2005-06-21 by jeff_smith_8992

Thanks to everyone for all the Timer/Counter information and tips.

Jeff

--- In AVR-Chat@yahoogroups.com, "arhodes19044" <spamiam@c...> wrote:
Show quoted textHide quoted text
> I do not think it is necessary to interrupt early, then dawdle for a 
> few cycles, then execute the interrupt code.  In the simpler  
> method, there is a slight "phase delay" of the interrupt code 
> execution, but since it executes everytime with the same delay, then 
> the next time it executes will be (for instance) exactly one 
> millisecond after the last time.  So, there might be a slight delay 
> compared to world time, but I doubt most people (and even most 
> software) would ever notice this discrepancy.  It is more important 
> that the interval between executions is the exact same.
> 
> -Tony
> 
> --- In AVR-Chat@yahoogroups.com, "Roy E. Burrage" <RBurrage@b...> 
> wrote:
> > You can also set up the prescaler and count registers for 
> something 
> > slightly less than what you require, then waste a few cycles in 
> the ISR 
> > to get your required time.
> > 
> >     System clock = 1 MHz
> > 
> > Using a prescaler value of 32 and a count register value of 31 
> would 
> > give 992 timer overflows per second, or 1 overflow every 992 
> uSec.  Then 
> > if you either waste or do something else for 7 cycles you'll have 
> 999.
> > 
> > Application note AVR130 explains this pretty well.
> > 
> > 
> > REB
> > 
> > 
> > arhodes19044 wrote:
> > 
> > >I agree that it is a major pain in the neck having powers of 2 as 
> > >the prescaler, but NOT powers of 2 as the usual crystal freq.
> > >
> > >In my case I needed a 1 millisecond clock.  So, I used a 16 bit 
> > >clock for my 16MHz crystal.  (8 bits would not do).
> > >
> > >I uses CTC mode with a prescaler of zero, and triggered the 
> > >interrupt after 16,000 clock cycles.  (16e6/16e3 = 1e3).  This 
> > >worked out fine.  
> > >
> > >  
> > >

Re: Counter/Timer Question

2005-06-23 by arhodes19044

Now, let us know how it turned out!


--- In AVR-Chat@yahoogroups.com, "jeff_smith_8992" 
<jeff_smith_8992@y...> wrote:
Show quoted textHide quoted text
> Thanks to everyone for all the Timer/Counter information and tips.
> 
> Jeff

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.