[136] | 1 | #include "Arduino.h"
|
---|
| 2 | #include <Wire.h>
|
---|
| 3 |
|
---|
| 4 | const uint8_t address = 0x68;
|
---|
| 5 |
|
---|
| 6 | uint8_t decToBcd(byte val)
|
---|
| 7 | {
|
---|
| 8 | return ( (val/10*16) + (val%10) );
|
---|
| 9 | }
|
---|
| 10 |
|
---|
| 11 | // Convert binary coded decimal to normal decimal numbers
|
---|
| 12 | uint8_t bcdToDec(byte val)
|
---|
| 13 | {
|
---|
| 14 | return ( (val/16*10) + (val%16) );
|
---|
| 15 | }
|
---|
| 16 |
|
---|
| 17 | void setup()
|
---|
| 18 | {
|
---|
| 19 | Serial5.begin(9600);
|
---|
| 20 | Serial5.println("Wire init");
|
---|
| 21 | Wire.begin();
|
---|
| 22 |
|
---|
| 23 | // Setting the date
|
---|
| 24 | /*
|
---|
| 25 | Wire.beginTransmission(address);
|
---|
| 26 | Wire.write((uint8_t)0x00); // Init register
|
---|
| 27 |
|
---|
| 28 | Wire.write(decToBcd(0)); // Second
|
---|
| 29 | Wire.write(decToBcd(39)); // Minute
|
---|
| 30 | Wire.write(decToBcd(11)); // Hours
|
---|
| 31 |
|
---|
| 32 | Wire.write(decToBcd(4)); // Day of week
|
---|
| 33 |
|
---|
| 34 | Wire.write(decToBcd(21)); // Day of Month
|
---|
| 35 | Wire.write(decToBcd(5)); // Month
|
---|
| 36 | Wire.write(decToBcd(14)); // Year
|
---|
| 37 | Wire.endTransmission();
|
---|
| 38 | */
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | void loop()
|
---|
| 42 | {
|
---|
| 43 |
|
---|
| 44 | Wire.beginTransmission(address);
|
---|
| 45 | Wire.write((uint8_t)0x3F);
|
---|
| 46 | Wire.endTransmission();
|
---|
| 47 |
|
---|
| 48 | delay(10);
|
---|
| 49 |
|
---|
| 50 | Wire.requestFrom(address, 7);
|
---|
| 51 | /* while(Wire.available())
|
---|
| 52 | {
|
---|
| 53 | Serial5.print(bcdToDec(Wire.read()));
|
---|
| 54 | Serial5.print(" ");
|
---|
| 55 | }
|
---|
| 56 | Serial5.println();*/
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | int second = bcdToDec(Wire.read() & 0x7f);
|
---|
| 60 | int minute = bcdToDec(Wire.read());
|
---|
| 61 | int hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
|
---|
| 62 | int dayOfWeek = bcdToDec(Wire.read());
|
---|
| 63 | int dayOfMonth = bcdToDec(Wire.read());
|
---|
| 64 | int month = bcdToDec(Wire.read());
|
---|
| 65 | int year = bcdToDec(Wire.read());
|
---|
| 66 |
|
---|
| 67 | Serial5.print(hour, DEC);
|
---|
| 68 | Serial5.print(":");
|
---|
| 69 | Serial5.print(minute, DEC);
|
---|
| 70 | Serial5.print(":");
|
---|
| 71 | Serial5.print(second, DEC);
|
---|
| 72 | Serial5.print(" ");
|
---|
| 73 | Serial5.print(month, DEC);
|
---|
| 74 | Serial5.print("/");
|
---|
| 75 | Serial5.print(dayOfMonth, DEC);
|
---|
| 76 | Serial5.print("/");
|
---|
| 77 | Serial5.print(year,DEC);
|
---|
| 78 | Serial5.print(" ");
|
---|
| 79 | Serial5.println();
|
---|
| 80 |
|
---|
| 81 | delay(990);
|
---|
| 82 | }
|
---|