pour olivier

Dependencies:   DS1307 mbed

Fork of RTC_test_ds1307 by Cellular building monitoring

Committer:
mathieusab
Date:
Thu Dec 01 16:31:04 2016 +0000
Revision:
2:fd30a7165b68
Parent:
1:b9b861f14c76
pour olivier

Who changed what in which revision?

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