RTC Calibration Sample

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial pc(USBTX, USBRX);
00004 DigitalOut myled(LED1);
00005 
00006 void resetClockTickCounter () {
00007     LPC_RTC->CCR |= (1<<1); // CTC Reset
00008     LPC_RTC->CCR &= ~(1<<1);
00009 }
00010 
00011 void calibConfig (int value, int dir) {
00012     if (dir) {
00013         LPC_RTC->CALIBRATION = (value & 0x1ffff) | ((dir == -1) ? (1<<17) : 0);
00014         LPC_RTC->CCR &= ~(1<<4); // Calibration counter disable.
00015     } else {
00016         LPC_RTC->CALIBRATION = 0;
00017         LPC_RTC->CCR |= (1<<4); // Calibration counter disable.
00018     }
00019 }
00020 extern "C"
00021 void RTC_IRQHandler () {
00022     time_t sec = time(NULL);
00023     struct tm *t = localtime(&sec);
00024 
00025     myled = !myled;
00026     pc.printf("%d  %04d-%02d-%02d %02d:%02d:%02d\r\n", sec,
00027         t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
00028     LPC_RTC->ILR |= (1<<0);
00029 }
00030 
00031 int main() {
00032 
00033     pc.baud(115200);
00034     pc.printf("RTC Calibration\r\n");
00035     set_time(1514732400); // 2018-01-01 00:00:00
00036     LPC_RTC->CIIR |= (1<<0);
00037     NVIC_EnableIRQ(RTC_IRQn);
00038 
00039     for (;;) {
00040         if (pc.readable()) {
00041             char c = pc.getc();
00042             switch (c) {
00043             case '0':
00044                 calibConfig(0, 0);
00045                 pc.printf("0\r\n");
00046                 break;
00047             case 'f': // Forward
00048                 calibConfig(4, 1);
00049                 pc.printf("F\r\n");
00050                 break;
00051             case 'b': // Backward
00052                 calibConfig(4, -1);
00053                 pc.printf("B\r\n");
00054                 break;
00055             case 'r': // Reset
00056                 resetClockTickCounter();
00057                 pc.printf("R\r\n");
00058                 break;
00059             }
00060         }
00061     }
00062 }