bad rtc

Dependencies:   DS1307

Committer:
davidyu
Date:
Sat Nov 09 23:14:07 2019 +0000
Revision:
0:fd9b81168745
bad rtc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
davidyu 0:fd9b81168745 1 // show how the DS1307 class works
davidyu 0:fd9b81168745 2 #include "ds1307.h"
davidyu 0:fd9b81168745 3 #include "mbed.h"
davidyu 0:fd9b81168745 4
davidyu 0:fd9b81168745 5 Serial pc(USBTX, USBRX); // tx, rx for debug and usb pc comunications
davidyu 0:fd9b81168745 6
davidyu 0:fd9b81168745 7 DS1307 my1307(D14, D15); // start DS1307 class and give it pins for connections of the DS1307 device
davidyu 0:fd9b81168745 8
davidyu 0:fd9b81168745 9 int sec = 0;
davidyu 0:fd9b81168745 10 int minute = 0;
davidyu 0:fd9b81168745 11 int hours = 0;
davidyu 0:fd9b81168745 12 int day = 0;
davidyu 0:fd9b81168745 13 int date = 0;
davidyu 0:fd9b81168745 14 int month = 0;
davidyu 0:fd9b81168745 15 int year = 0;
davidyu 0:fd9b81168745 16
davidyu 0:fd9b81168745 17 void test_rw(int test) {
davidyu 0:fd9b81168745 18 if (test == 0) pc.printf("Last R/W operaion passed!\n\r");
davidyu 0:fd9b81168745 19 else pc.printf("Last R/W operation failed!\n\r");
davidyu 0:fd9b81168745 20 }
davidyu 0:fd9b81168745 21
davidyu 0:fd9b81168745 22 int main() {
davidyu 0:fd9b81168745 23 int junk = 0;
davidyu 0:fd9b81168745 24
davidyu 0:fd9b81168745 25 sec = 24; // 24 seconds
davidyu 0:fd9b81168745 26 minute = 13; // 13 min
davidyu 0:fd9b81168745 27 hours = 13; // 1 pm
davidyu 0:fd9b81168745 28 day = 4; // wednesday
davidyu 0:fd9b81168745 29 date = 20; // June 20
davidyu 0:fd9b81168745 30 month = 6;
davidyu 0:fd9b81168745 31 year = 12; // 2012
davidyu 0:fd9b81168745 32 // set time to these values on the ds1307 connected device
davidyu 0:fd9b81168745 33
davidyu 0:fd9b81168745 34 //test_rw(my1307.settime( sec, minute, hours, day, date, month, year));
davidyu 0:fd9b81168745 35 // my1307.start_clock();
davidyu 0:fd9b81168745 36 // pc.printf("seconds set are %.2D \n\r",sec++);
davidyu 0:fd9b81168745 37 // pc.printf("min set are %.2D \n\r",minute);
davidyu 0:fd9b81168745 38 // pc.printf("hour set are %.2D \n\r",hours);
davidyu 0:fd9b81168745 39 // pc.printf("day set are %.2D \n\r",day);
davidyu 0:fd9b81168745 40 // pc.printf("date set are %.2D \n\r",date);
davidyu 0:fd9b81168745 41 // pc.printf("month set are %.2D \n\r",month);
davidyu 0:fd9b81168745 42 // pc.printf("year set are %.2D \n\r",year);
davidyu 0:fd9b81168745 43 // wait(3);
davidyu 0:fd9b81168745 44 // now read the time of the DS1307 device and see what time it is
davidyu 0:fd9b81168745 45 // note that because of the 3 second wait this time should be 3 seconds past what it was set to earlier
davidyu 0:fd9b81168745 46 while(1){
davidyu 0:fd9b81168745 47 pc.printf("--------------------------");
davidyu 0:fd9b81168745 48 test_rw(my1307.gettime( &sec, &minute, &hours, &day, &date, &month, &year));
davidyu 0:fd9b81168745 49 pc.printf("seconds read are %.2D \n\r",sec);
davidyu 0:fd9b81168745 50 pc.printf("min read are %.2D \n\r",minute);
davidyu 0:fd9b81168745 51 pc.printf("hour read are %.2D \n\r",hours);
davidyu 0:fd9b81168745 52 pc.printf("day read are %.2D \n\r",day);
davidyu 0:fd9b81168745 53 pc.printf("date read are %.2D \n\r",date);
davidyu 0:fd9b81168745 54 pc.printf("month read are %.2D \n\r",month);
davidyu 0:fd9b81168745 55 pc.printf("year read are %.2D \n\r",year);
davidyu 0:fd9b81168745 56 pc.printf("--------------------------");
davidyu 0:fd9b81168745 57 wait(1);
davidyu 0:fd9b81168745 58 // junk = 0x39; // just a junk value do read and write test to DS1307 ram
davidyu 0:fd9b81168745 59 // test_rw(my1307.write( 0x20, junk)); // this should write the value of junk to register 0x20 (a ram location) in the ds1307.
davidyu 0:fd9b81168745 60 // pc.printf("Value written to register 0x20 %.2X \n\r",junk);
davidyu 0:fd9b81168745 61 // junk = 0; // clear junk to show that when the register is read from the correct value is obtained
davidyu 0:fd9b81168745 62 // test_rw(my1307.read( 0x20, &junk)); // this should read register 0x20
davidyu 0:fd9b81168745 63 // pc.printf("Value read from register 0x20 %.2X \n\r",junk);
davidyu 0:fd9b81168745 64 }
davidyu 0:fd9b81168745 65 }