Community Contributors / USBSerial-RTC-HelloWorld

Dependencies:   USBDevice mbed-src

Fork of USBSerial-RTC-HelloWorld by Community Contributors

Committer:
star297
Date:
Sun Jun 14 18:11:07 2015 +0000
Revision:
10:5a7c14815199
Parent:
9:d88699a0905a
Child:
11:7bf81089fce6
V1

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 10:5a7c14815199 5
samux 7:5e693654d5b4 6 #include "mbed.h"
samux 7:5e693654d5b4 7 #include "USBSerial.h"
samux 7:5e693654d5b4 8
samux 7:5e693654d5b4 9 //Virtual serial port over USB
samux 7:5e693654d5b4 10 USBSerial serial;
star297 10:5a7c14815199 11
star297 10:5a7c14815199 12 DigitalOut led(LED1);
star297 10:5a7c14815199 13
star297 10:5a7c14815199 14 struct tm t;
star297 10:5a7c14815199 15 char timebuff[60];
star297 10:5a7c14815199 16
star297 10:5a7c14815199 17 int minute =35; // 0-59
star297 10:5a7c14815199 18 int hour =18; // 2-23
star297 10:5a7c14815199 19 int dayofmonth =14; // 1-31
star297 10:5a7c14815199 20 int month =6; // 1-12
star297 10:5a7c14815199 21 int year =15; // last 2 digits
star297 10:5a7c14815199 22
star297 10:5a7c14815199 23 void setRTC()
star297 10:5a7c14815199 24 {
star297 10:5a7c14815199 25 t.tm_sec = (0); // 0-59
star297 10:5a7c14815199 26 t.tm_min = (minute); // 0-59
star297 10:5a7c14815199 27 t.tm_hour = (hour); // 0-23
star297 10:5a7c14815199 28 t.tm_mday = (dayofmonth); // 1-31
star297 10:5a7c14815199 29 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
star297 10:5a7c14815199 30 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
star297 10:5a7c14815199 31 set_time(mktime(&t)); // set RTC clock
star297 10:5a7c14815199 32 }
samux 7:5e693654d5b4 33
samux 7:5e693654d5b4 34 int main(void) {
samux 9:d88699a0905a 35
star297 10:5a7c14815199 36 setRTC(); // If back up cell fitted, comment this out after the RTC has been set then re-program.
star297 10:5a7c14815199 37
samux 7:5e693654d5b4 38 while(1)
star297 10:5a7c14815199 39 {
star297 10:5a7c14815199 40 time_t seconds = time(NULL);
star297 10:5a7c14815199 41 strftime(timebuff,60,"Teensy RTC\r\nTime: %H:%M:%S\r\nDate: %A %d %B %Y\r\n\n", localtime(&seconds));
star297 10:5a7c14815199 42 serial.printf(timebuff);
star297 10:5a7c14815199 43 led=0;
samux 9:d88699a0905a 44 wait(1);
star297 10:5a7c14815199 45 led=1;
samux 7:5e693654d5b4 46 }
star297 10:5a7c14815199 47 }
star297 10:5a7c14815199 48