]> git.piffa.net Git - sketchbook_andrea_bak/blob - hardware/real_time_clock__DS3231/real_time_clock__DS3231.ino
Initial Commit
[sketchbook_andrea_bak] / hardware / real_time_clock__DS3231 / real_time_clock__DS3231.ino
1 // See tutotial: http://tronixlabs.com/news/tutorial-using-ds1307-and-ds3231-realtime-clock-modules-with-arduino/
2
3 #include "Wire.h"
4 #define DS3231_I2C_ADDRESS 0x68
5 // Convert normal decimal numbers to binary coded decimal
6 byte decToBcd(byte val)
7 {
8   return( (val/10*16) + (val%10) );
9 }
10 // Convert binary coded decimal to normal decimal numbers
11 byte bcdToDec(byte val)
12 {
13   return( (val/16*10) + (val%16) );
14 }
15 void setup()
16 {
17   Wire.begin();
18   Serial.begin(9600);
19   // set the initial time here:
20   // DS3231 seconds, minutes, hours, day, date, month, year
21   //setDS3231time(30,27,19,2,10,2,15); // Comment again after setting up
22    // or you will rset the clock at each upload
23 }
24 void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
25 dayOfMonth, byte month, byte year)
26 {
27   // sets time and date data to DS3231
28   Wire.beginTransmission(DS3231_I2C_ADDRESS);
29   Wire.write(0); // set next input to start at the seconds register
30   Wire.write(decToBcd(second)); // set seconds
31   Wire.write(decToBcd(minute)); // set minutes
32   Wire.write(decToBcd(hour)); // set hours
33   Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
34   Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
35   Wire.write(decToBcd(month)); // set month
36   Wire.write(decToBcd(year)); // set year (0 to 99)
37   Wire.endTransmission();
38 }
39 void readDS3231time(byte *second,
40 byte *minute,
41 byte *hour,
42 byte *dayOfWeek,
43 byte *dayOfMonth,
44 byte *month,
45 byte *year)
46 {
47   Wire.beginTransmission(DS3231_I2C_ADDRESS);
48   Wire.write(0); // set DS3231 register pointer to 00h
49   Wire.endTransmission();
50   Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
51   // request seven bytes of data from DS3231 starting from register 00h
52   *second = bcdToDec(Wire.read() & 0x7f);
53   *minute = bcdToDec(Wire.read());
54   *hour = bcdToDec(Wire.read() & 0x3f);
55   *dayOfWeek = bcdToDec(Wire.read());
56   *dayOfMonth = bcdToDec(Wire.read());
57   *month = bcdToDec(Wire.read());
58   *year = bcdToDec(Wire.read());
59 }
60 void displayTime()
61 {
62   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
63   // retrieve data from DS3231
64   readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
65   &year);
66   // send it to the serial monitor
67   Serial.print(hour, DEC);
68   // convert the byte variable to a decimal number when displayed
69   Serial.print(":");
70   if (minute<10)
71   {
72     Serial.print("0");
73   }
74   Serial.print(minute, DEC);
75   Serial.print(":");
76   if (second<10)
77   {
78     Serial.print("0");
79   }
80   Serial.print(second, DEC);
81   Serial.print(" ");
82   Serial.print(dayOfMonth, DEC);
83   Serial.print("/");
84   Serial.print(month, DEC);
85   Serial.print("/");
86   Serial.print(year, DEC);
87   Serial.print(" Day of week: ");
88   switch(dayOfWeek){
89   case 1:
90     Serial.println("Sunday");
91     break;
92   case 2:
93     Serial.println("Monday");
94     break;
95   case 3:
96     Serial.println("Tuesday");
97     break;
98   case 4:
99     Serial.println("Wednesday");
100     break;
101   case 5:
102     Serial.println("Thursday");
103     break;
104   case 6:
105     Serial.println("Friday");
106     break;
107   case 7:
108     Serial.println("Saturday");
109     break;
110   }
111 }
112 void loop()
113 {
114   displayTime(); // display the real-time clock data on the Serial Monitor,
115   delay(1000); // every second
116 }
117
118