- Use a 32.768kHz crystal which divides down perfectly to give you seconds etc
- Use any frequency oscillator and manually set the value of the timer register on each interrupt. I.e. to skip some number of cycles
- 32.768kHz is ridiculously slow compared to the 20-40MHz the PICs can run at
- Manually setting the timer register takes some number of clock cycles and this will affect the timing. Working out how many clock cycles exactly sounds like an error prone and rather boring task
Its a brilliantly simple solution. Here is my attempt to explain it. Perhaps having two different explanations helps.
Imagine you have an oscillator that ticks every 3 units but a clock event that needs to happen every 5 units of time. The key to understanding this is that even though 3 does not go into 5 you can get close without accumulating error.
Consider the first 5 oscillator ticks
- 3 - less that first time period (5), do nothing
- 6 - greater that first time period (5), do clock
- 9 - less that second time period (10), do nothing
- 12 - greater that second time period (10), do clock
- 15 - equal to third time period (15), do clock
With a micro running at many megahertz you can adjust the timer prescale to make this periodic discrepancy as small or large as you like.
In my timer I was happy to be out by a few seconds at any time. Of course you could of course use a smaller prescale value and keep it down to milliseconds if you really need to
No comments:
Post a Comment