measure RTC crystal freqeuncy against MCU crystal

Dependencies:   mbed

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

Committer:
manitou
Date:
2015-10-27
Revision:
0:b30b04f4a2aa

File content as of revision 0:b30b04f4a2aa:

// k64f rtcisr
// seconds interrupt from RTC compare micros() to check RTC frequency
#include "mbed.h"

#define micros tmr.read_us

DigitalOut led(LED_RED);
Timer tmr;

volatile static unsigned long ticks,us;
static unsigned long us0=0;

extern "C" void RTC_Seconds_IRQHandler() {
    if (us0 == 0) us0 = micros();
      else {
        ticks++;
        us = micros() - us0;
    }
    led = !led; // toggle led
}

int main(){
    int ppm, tprev=0;

    tmr.start();   // for micros
    time(NULL);  // start RTC
    RTC_IER = 0x10 ;    //TSIE enable seconds interrupt
    NVIC_EnableIRQ(RTC_Seconds_IRQn);
    while (true) {
        ppm = 1000000*ticks - us;
        ppm = 1.e6 * ppm/ (float)us;
        printf("%d %d %f %d\n",ticks,ticks-tprev,us*1.e-6,ppm);
        wait(2.0);
    }
}