Clock
Dependencies: DS1307 Grove_Serial_LCD mbed
main.cpp@0:06a98b973b14, 2013-12-09 (annotated)
- Committer:
- yihui
- Date:
- Mon Dec 09 08:46:52 2013 +0000
- Revision:
- 0:06a98b973b14
initial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yihui | 0:06a98b973b14 | 1 | #include "mbed.h" |
yihui | 0:06a98b973b14 | 2 | #include "SerialLCD.h" |
yihui | 0:06a98b973b14 | 3 | #include "ds1307.h" |
yihui | 0:06a98b973b14 | 4 | |
yihui | 0:06a98b973b14 | 5 | SerialLCD lcd(P1_13, P1_14); // Grove Serial LCD is connected to UART Tx and Rx pins |
yihui | 0:06a98b973b14 | 6 | DS1307 rtc(P0_5, P0_4); // Grove RTC is connected to I2C SDA(P0_5) and SCL(P0_4) |
yihui | 0:06a98b973b14 | 7 | |
yihui | 0:06a98b973b14 | 8 | int main() { |
yihui | 0:06a98b973b14 | 9 | const char *week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; |
yihui | 0:06a98b973b14 | 10 | int sec, min, hour, day, date, month, year; |
yihui | 0:06a98b973b14 | 11 | char strBuffer[16]; |
yihui | 0:06a98b973b14 | 12 | |
yihui | 0:06a98b973b14 | 13 | lcd.begin(); |
yihui | 0:06a98b973b14 | 14 | lcd.print("Clock"); |
yihui | 0:06a98b973b14 | 15 | rtc.start_clock(); |
yihui | 0:06a98b973b14 | 16 | rtc.gettime(&sec, &min, &hour, &day, &date, &month, &year); |
yihui | 0:06a98b973b14 | 17 | if (0 == year) { |
yihui | 0:06a98b973b14 | 18 | rtc.settime(0, 0, 0, 4, 1, 1, 2014 % 100); // Jan 1st, 2014, Wed, 00:00:00 |
yihui | 0:06a98b973b14 | 19 | } |
yihui | 0:06a98b973b14 | 20 | |
yihui | 0:06a98b973b14 | 21 | while(1) { |
yihui | 0:06a98b973b14 | 22 | rtc.gettime(&sec, &min, &hour, &day, &date, &month, &year); |
yihui | 0:06a98b973b14 | 23 | snprintf(strBuffer, sizeof(strBuffer), "%d-%d-%d %s", 2000 + year, month, date, week[day]); |
yihui | 0:06a98b973b14 | 24 | lcd.setCursor(0, 0); |
yihui | 0:06a98b973b14 | 25 | lcd.print(strBuffer); |
yihui | 0:06a98b973b14 | 26 | snprintf(strBuffer, sizeof(strBuffer), "%d:%d:%d", hour, min, sec); |
yihui | 0:06a98b973b14 | 27 | lcd.setCursor(0, 1); |
yihui | 0:06a98b973b14 | 28 | lcd.print(strBuffer); |
yihui | 0:06a98b973b14 | 29 | wait(0.5); |
yihui | 0:06a98b973b14 | 30 | } |
yihui | 0:06a98b973b14 | 31 | } |
yihui | 0:06a98b973b14 | 32 | |
yihui | 0:06a98b973b14 | 33 |