I started noticing some clock drift after leaving my clock going for a while and I believe I've found one of the reasons.
I realized I was not performing the most efficient method for reloading Timer2 upon overflow. Right now, I have a divide-by-1024 prescaler on Timer2, which requires me to reload 224 into Timer2 every time we overflow. The instructions required to implement this (and overhead from entering the ISR) introduce a delay to the timekeeping and cause time drift. We need to have a Timer2 that automatically reloads itself in hardware without requiring instructions that are delayed by the amount of time required to perform context saving and jump to the ISR block.
There are two ways to do this: 1. use the output compare registers + CTC function to auto-reload the Timer2 upon overflow or 2.) set a prescaler such that Timer2 requires exactly 256 ticks to overflow every 1 second. As I already have everything set up for an overflow interrupt and I am using a 32.768 kHz source clock, I decided to change the prescaler to 128 (128/32768)*256 = 1s) so that we do not need to reload Timer2 after each overflow.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.