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? > > Thanks for any help. > > Jeff
Message
Re: Counter/Timer Question
2005-06-20 by arhodes19044
Attachments
- No local attachments were found for this message.