
Display the date and time on text LCD using RTC DS1307.
Dependencies: DS1307 TextLCD mbed
Diff: main.cpp
- Revision:
- 1:1f723a262eeb
- Parent:
- 0:bad75bd13618
--- a/main.cpp Fri Feb 21 10:00:46 2014 +0000 +++ b/main.cpp Fri Dec 26 11:22:36 2014 +0000 @@ -1,26 +1,77 @@ #include "mbed.h" - -DigitalOut myled(LED1); +#include "ds1307.h" +#include "TextLCD.h" -int main() { - - printf("RTC example\n"); - set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC - printf("Date and time are set.\n"); +TextLCD lcd(D8, D9, D4, D5, D6, D7); // rs, e, d4-d7 +DS1307 my_tm(D14, D15); //sda, scl +DigitalOut myled(LED1); // blinking LED +InterruptIn mybutton(USER_BUTTON); // user button (blue) - while(1) { +int year, month, day, hour, min, sec, dw; // dw - day of week +float delay=1.0; // delay time +float showtime=0.0; - time_t seconds = time(NULL); - - //printf("Time as seconds since January 1, 1970 = %d\n", seconds); - - printf("Time as a basic string = %s", ctime(&seconds)); +void lcdPrintDate(int row) +{ + lcd.locate(0, row); + lcd.printf("Data :"); + lcd.locate(6, row); + lcd.printf("%02d", day); + lcd.printf("-"); + lcd.printf("%02d", month); + lcd.printf("-"); + lcd.printf("%04d", 2000+year); +} - //char buffer[32]; - //strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds)); - //printf("Time as a custom formatted string = %s", buffer); +void lcdPrintTime(int row) +{ + lcd.locate(0, row); + lcd.printf("Czas :"); + lcd.locate(6, row); + lcd.printf("%02d", hour); + lcd.printf(":"); + lcd.printf("%02d", min); + lcd.printf(":"); + lcd.printf("%02d", sec); +} - myled = !myled; - wait(1); +void pressed() +{ + if (delay == 1.0) + { + delay = 0.5; // 500 ms + } + else + { + delay = 1.0; // 1 sec + lcd.cls(); } } + + + +int main() +{ + lcd.cls(); + my_tm.twentyfour_hour(); + mybutton.fall(&pressed); + while (1) + { + myled = !myled; + if (delay == 0.5) + { + my_tm.gettime(&sec, &min, &hour, &dw, &day, &month, &year); + lcdPrintDate(0); + lcdPrintTime(1); + showtime = showtime + delay; + if (showtime > 10) // after 10 s sleep + { + showtime = 0.0; + delay = 1.0; + lcd.cls(); + } + } + wait(delay); + } + +}