Teensy3.1 USB Serial and RTC test. Use this program to check the operation of the RTC after the 32KHz crystal has been fitted. If a back up cell has also been fitted the RTC will remain running after the main power has been removed. Reconnect the USB to the PC and the RTC time will be displayed when using a PC terminal program.

Dependencies:   USBDevice mbed-src

Fork of USBSerial-RTC-HelloWorld by Community Contributors

main.cpp

Committer:
star297
Date:
2015-06-15
Revision:
11:7bf81089fce6
Parent:
10:5a7c14815199
Child:
12:f4e5544977c2

File content as of revision 11:7bf81089fce6:

// Teensy3.1 USB Serial, RTC test

// Requires 32KHz crystal fitted.
// Unplug then plug back in the Teensy after programing to reactivate the Teensy USB serial port.

#include "mbed.h"
#include "USBSerial.h"
 
//Virtual serial port over USB
USBSerial serial;

DigitalOut led(LED1);

struct tm t;
char timebuff[60];

int minute      =35;    // 0-59
int hour        =18;    // 0-23
int dayofmonth  =14;    // 1-31
int month       =6;     // 1-12
int year        =15;    // last 2 digits
 
void setRTC()
{
            t.tm_sec = (0);             // 0-59
            t.tm_min = (minute);        // 0-59
            t.tm_hour = (hour);         // 0-23
            t.tm_mday = (dayofmonth);   // 1-31
            t.tm_mon = (month-1);       // 0-11  "0" = Jan, -1 added for Mbed RCT clock format
            t.tm_year = ((year)+100);   // year since 1900,  current year + 100 + 1900 = correct year
            set_time(mktime(&t));       // set RTC clock                
}  
 
int main(void) {
 
    setRTC(); // If back up cell fitted, comment this out after the RTC has been set then re-program.  
    
    while(1)
    {   
        time_t seconds = time(NULL);
        strftime(timebuff,60,"Teensy RTC\r\nTime:  %H:%M:%S\r\nDate:  %A %d %B %Y\r\n\n", localtime(&seconds));       
        serial.printf(timebuff);
        led=0;        
        wait(1);
        led=1;         
    }
}