seeed grove RTC
Overview¶
The Seeed Grove RTC module is just one of over 50 prototyping modules in the Grove series. You can visit the Grove Wiki page to see all the modules offered:
You may wonder why you would want to use a RTC module when the mbed already has a built in RTC. The primary reason is that the mbed consumes 27-30uA from the RTC battery whereas the DS1307 consumes only 300-500nA while on battery.
Grove RTC module¶
Features¶
- Real-Time Clock (RTC) Counts Seconds,Minutes, Hours, Date of the Month, Month,Day of the week, and Year with Leap-Year Compensation Valid Up to 2100
- 56-Byte, Battery-Backed, Nonvolatile (NV)RAM for Data Storage
- I2C Serial Interface
- 5V DC supply
- Programmable Square-Wave Output Signal
- Automatic Power-Fail Detect and Switch Circuitry
- Consumes Less than 500nA in Battery-Backup Mode with Oscillator Running
- It uses a 3V Lithium cell battery (CR1225)
Additional Resources¶
Maxim webpage on the DS1307 RTC
Collection of application notes
Control Register¶
Code¶
Fortunately Philip Smith had already created a library for the DS1307 that works fine with the Grove RTC module. You can find his library here
Here is example code from Phillip's library page that I tested the Grove RTC module with:
// show how the DS1307 class works #include "ds1307.h" #include "mbed.h" Serial pc(USBTX, USBRX); // tx, rx for debug and usb pc comunications DS1307 my1307(p9,p10); // start DS1307 class and give it pins for connections of the DS1307 device int sec = 0; int min = 0; int hours = 0; int day = 0; int date = 0; int month = 0; int year = 0; void test_rw(int test) { if (test == 0) pc.printf("Last R/W operaion passed!\n\r"); else pc.printf("Last R/W operation failed!\n\r"); } int main() { int junk = 0; sec = 24; // 24 seconds min = 13; // 13 min hours = 13; // 1 pm day = 4; // wednesday date = 20; // June 20 month = 6; year = 12; // 2012 // set time to these values on the ds1307 connected device test_rw(my1307.settime( sec, min, hours, day, date, month, year)); pc.printf("seconds set are %.2D \n\r",sec); pc.printf("min set are %.2D \n\r",min); pc.printf("hour set are %.2D \n\r",hours); pc.printf("day set are %.2D \n\r",day); pc.printf("date set are %.2D \n\r",date); pc.printf("month set are %.2D \n\r",month); pc.printf("year set are %.2D \n\r",year); wait(3); // now read the time of the DS1307 device and see what time it is // note that because of the 3 second wait this time should be 3 seconds past what it was set to earlier test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year)); pc.printf("seconds read are %.2D \n\r",sec); pc.printf("min read are %.2D \n\r",min); pc.printf("hour read are %.2D \n\r",hours); pc.printf("day read are %.2D \n\r",day); pc.printf("date read are %.2D \n\r",date); pc.printf("month read are %.2D \n\r",month); pc.printf("year read are %.2D \n\r",year); junk = 0x39; // just a junk value do read and write test to DS1307 ram test_rw(my1307.write( 0x20, junk)); // this should write the value of junk to register 0x20 (a ram location) in the ds1307. pc.printf("Value written to register 0x20 %.2X \n\r",junk); junk = 0; // clear junk to show that when the register is read from the correct value is obtained test_rw(my1307.read( 0x20, &junk)); // this should read register 0x20 pc.printf("Value read from register 0x20 %.2X \n\r",junk); }
Modified the code above to display the time, date and day on a 40x2 LCD display.
#include "ds1307.h" #include "TextLCD.h" #include "mbed.h" Serial pc(USBTX, USBRX); // tx, rx for debug and usb pc comunications TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD40x2); // rs, e, d4-d7 DS1307 my1307(p9,p10); // start DS1307 class and give it pins for connections of the DS1307 device int sec = 0; // Values to set time with int min = 49; int hours = 03; int day = 5; int date = 12; int month = 7; int year = 12; int loop = 10; // Non zero value for the while loop void test_rw(int test) { if (test == 0) pc.printf("Last R/W operation passed!\n\r"); else pc.printf("Last R/W operation failed!\n\r"); } int main() { test_rw(my1307.settime( sec, min, hours, day, date, month, year)); // Set the time on the DS1307 test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year)); // Read the time and display on screen pc.printf("seconds read are %.2D \n\r",sec); pc.printf("min read are %.2D \n\r",min); pc.printf("hour read are %.2D \n\r",hours); pc.printf("day read are %.2D \n\r",day); pc.printf("date read are %.2D \n\r",date); pc.printf("month read are %.2D \n\r",month); pc.printf("year read are %.2D \n\r",year); lcd.printf(" Time Date Day "); // Print a header on line 1 of the 40 x 2 LCD display while (loop>0){ // Loop continously test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year)); lcd.locate(0,1); // Print and refresh data on line 2 of the LCD display lcd.printf("%.2D",hours); lcd.printf(":%.2D",min); lcd.printf(":%.2D",sec); lcd.printf(" %.2D",month); lcd.printf("/%.2D",date); lcd.printf("/%.2D",year); if (day == 1) lcd.printf(" Sunday \n"); if (day == 2) lcd.printf(" Monday \n"); if (day == 3) lcd.printf(" Tuesday \n"); if (day == 4) lcd.printf(" Wednesday \n"); if (day == 5) lcd.printf(" Thursday \n"); if (day == 6) lcd.printf(" Friday \n"); if (day == 7) lcd.printf(" Saturday \n"); } }