measure RTC crystal freqeuncy against MCU crystal
This program uses the RTC 1-second interrupt to capture the Timer microsecond value to estimate the error in the RTC's 32KHz crystal. (the K64F MCU's crystal could be in error as well, though mine was within 20 ppm). This program measured the error at about 250 ppm, well out of the 20ppm spec for the RTC crystal???
main.cpp@0:b30b04f4a2aa, 2015-10-27 (annotated)
- Committer:
- manitou
- Date:
- Tue Oct 27 21:07:11 2015 +0000
- Revision:
- 0:b30b04f4a2aa
first cut
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
manitou | 0:b30b04f4a2aa | 1 | // k64f rtcisr |
manitou | 0:b30b04f4a2aa | 2 | // seconds interrupt from RTC compare micros() to check RTC frequency |
manitou | 0:b30b04f4a2aa | 3 | #include "mbed.h" |
manitou | 0:b30b04f4a2aa | 4 | |
manitou | 0:b30b04f4a2aa | 5 | #define micros tmr.read_us |
manitou | 0:b30b04f4a2aa | 6 | |
manitou | 0:b30b04f4a2aa | 7 | DigitalOut led(LED_RED); |
manitou | 0:b30b04f4a2aa | 8 | Timer tmr; |
manitou | 0:b30b04f4a2aa | 9 | |
manitou | 0:b30b04f4a2aa | 10 | volatile static unsigned long ticks,us; |
manitou | 0:b30b04f4a2aa | 11 | static unsigned long us0=0; |
manitou | 0:b30b04f4a2aa | 12 | |
manitou | 0:b30b04f4a2aa | 13 | extern "C" void RTC_Seconds_IRQHandler() { |
manitou | 0:b30b04f4a2aa | 14 | if (us0 == 0) us0 = micros(); |
manitou | 0:b30b04f4a2aa | 15 | else { |
manitou | 0:b30b04f4a2aa | 16 | ticks++; |
manitou | 0:b30b04f4a2aa | 17 | us = micros() - us0; |
manitou | 0:b30b04f4a2aa | 18 | } |
manitou | 0:b30b04f4a2aa | 19 | led = !led; // toggle led |
manitou | 0:b30b04f4a2aa | 20 | } |
manitou | 0:b30b04f4a2aa | 21 | |
manitou | 0:b30b04f4a2aa | 22 | int main(){ |
manitou | 0:b30b04f4a2aa | 23 | int ppm, tprev=0; |
manitou | 0:b30b04f4a2aa | 24 | |
manitou | 0:b30b04f4a2aa | 25 | tmr.start(); // for micros |
manitou | 0:b30b04f4a2aa | 26 | time(NULL); // start RTC |
manitou | 0:b30b04f4a2aa | 27 | RTC_IER = 0x10 ; //TSIE enable seconds interrupt |
manitou | 0:b30b04f4a2aa | 28 | NVIC_EnableIRQ(RTC_Seconds_IRQn); |
manitou | 0:b30b04f4a2aa | 29 | while (true) { |
manitou | 0:b30b04f4a2aa | 30 | ppm = 1000000*ticks - us; |
manitou | 0:b30b04f4a2aa | 31 | ppm = 1.e6 * ppm/ (float)us; |
manitou | 0:b30b04f4a2aa | 32 | printf("%d %d %f %d\n",ticks,ticks-tprev,us*1.e-6,ppm); |
manitou | 0:b30b04f4a2aa | 33 | wait(2.0); |
manitou | 0:b30b04f4a2aa | 34 | } |
manitou | 0:b30b04f4a2aa | 35 | } |