I'm using a dsPIC 33FJ128GP804 and I'm trying to record data at 200 Hz as accurately as possible. My device also has a GPS and I'm noticing that my timer is drifting compared to the GPS by about 333 ms / hour. I think it should be possible to do better than that. My clock source is a 40 Mhz crystal with a tolerance of 30ppm and stability of 50ppm and aging of 5ppm/year.
I have the Oscillator of the PIC running at 72Mhz. I can't change this, it's required to run the UARTS at 3Mbit.
During my testing to figure out why there is so much drift, I did an experiment. I'm running two timers, one at 200Hz and one at 5Hz. In the interrupt handlers, the 200Hz timer increments a long int by 1 and the 5Hz timer increments a long int by 40. The two long ints should remain the same. However, after 45 minutes, the values differ by 6. (30ms)
Why am I seeing a difference between the two values? And is there anything I can do to increase my timer accuracy other than changing the crystal? Does having a longer period increase accuracy? If so, could I use a slower timer periodically reset my faster timer. Would changing the source of my timer to the external oscillator from the instruction clock make a difference?
I do use the GPS time to correct the timing in post processing, but GPS isn't always available so I don't want to have to rely on it.