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

Committer:
star297
Date:
Wed Sep 30 22:24:41 2015 +0000
Revision:
12:f4e5544977c2
Parent:
11:7bf81089fce6
Added clock rate display output

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 10:5a7c14815199 1 // Teensy3.1 USB Serial, RTC test
star297 10:5a7c14815199 2
star297 10:5a7c14815199 3 // Requires 32KHz crystal fitted.
star297 10:5a7c14815199 4 // Unplug then plug back in the Teensy after programing to reactivate the Teensy USB serial port.
star297 12:f4e5544977c2 5 // if your terminal program can't see the Teensy
star297 10:5a7c14815199 6
samux 7:5e693654d5b4 7 #include "mbed.h"
samux 7:5e693654d5b4 8 #include "USBSerial.h"
star297 12:f4e5544977c2 9
star297 12:f4e5544977c2 10 // include to check/display clock rates
star297 12:f4e5544977c2 11 #include "clk_freqs.h"
samux 7:5e693654d5b4 12
samux 7:5e693654d5b4 13 //Virtual serial port over USB
samux 7:5e693654d5b4 14 USBSerial serial;
star297 10:5a7c14815199 15
star297 10:5a7c14815199 16 DigitalOut led(LED1);
star297 10:5a7c14815199 17
star297 10:5a7c14815199 18 struct tm t;
star297 12:f4e5544977c2 19 char timebuff[80];
star297 10:5a7c14815199 20
star297 12:f4e5544977c2 21 int minute =20; // 0-59
star297 12:f4e5544977c2 22 int hour =23; // 0-23
star297 12:f4e5544977c2 23 int dayofmonth =30; // 1-31
star297 12:f4e5544977c2 24 int month =9; // 1-12
star297 10:5a7c14815199 25 int year =15; // last 2 digits
star297 10:5a7c14815199 26
star297 10:5a7c14815199 27 void setRTC()
star297 10:5a7c14815199 28 {
star297 10:5a7c14815199 29 t.tm_sec = (0); // 0-59
star297 10:5a7c14815199 30 t.tm_min = (minute); // 0-59
star297 10:5a7c14815199 31 t.tm_hour = (hour); // 0-23
star297 10:5a7c14815199 32 t.tm_mday = (dayofmonth); // 1-31
star297 10:5a7c14815199 33 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
star297 11:7bf81089fce6 34 t.tm_year = ((year)+100); // year since 1900, current year + 100 + 1900 = correct year
star297 10:5a7c14815199 35 set_time(mktime(&t)); // set RTC clock
star297 10:5a7c14815199 36 }
samux 7:5e693654d5b4 37
samux 7:5e693654d5b4 38 int main(void) {
samux 9:d88699a0905a 39
star297 12:f4e5544977c2 40 led=1;
star297 12:f4e5544977c2 41
star297 12:f4e5544977c2 42 //setRTC(); // If back up cell fitted, comment this out after the RTC has been set then re-program.
star297 10:5a7c14815199 43
samux 7:5e693654d5b4 44 while(1)
star297 10:5a7c14815199 45 {
star297 10:5a7c14815199 46 time_t seconds = time(NULL);
star297 12:f4e5544977c2 47 strftime(timebuff,80,"\n\n Teensy RTC\r\nTime: %H:%M:%S\r\nDate: %A %d %B %Y\n\n", localtime(&seconds));
star297 12:f4e5544977c2 48 serial.printf(timebuff);
star297 12:f4e5544977c2 49
star297 12:f4e5544977c2 50 serial.printf("\n core %d",SystemCoreClock);
star297 12:f4e5544977c2 51
star297 12:f4e5544977c2 52 serial.printf("\n Bus %d",bus_frequency());
star297 12:f4e5544977c2 53
star297 12:f4e5544977c2 54 serial.printf("\n Osc %d",extosc_frequency());
star297 12:f4e5544977c2 55
star297 10:5a7c14815199 56 led=0;
samux 9:d88699a0905a 57 wait(1);
star297 10:5a7c14815199 58 led=1;
samux 7:5e693654d5b4 59 }
star297 10:5a7c14815199 60 }