Skip to content

Instantly share code, notes, and snippets.

@viewpointsa
Created April 16, 2019 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viewpointsa/b601970ecba6599ad723d2e24aa6259f to your computer and use it in GitHub Desktop.
Save viewpointsa/b601970ecba6599ad723d2e24aa6259f to your computer and use it in GitHub Desktop.
Dialog Arduino Mkrfox 1200 with RTC3231
/*
^ DS3231 ^Arduino ^
| SCL | 12 SCL |
| SDA | 11 SDA |
| VCC | VCC |
| GND | GND |
*/
#include "Wire.h"
#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val){
return( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val){
return( (val/16*10) + (val%16) );
}
void setup(){
Wire.begin();
Serial.begin(9600);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
//setDS3231time(30,42,16,5,13,10,19);
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year){
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year){
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void displayTime(){
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
&year);
Serial.print("20");
Serial.print(year, DEC);
Serial.print("-");
Serial.print(month, DEC);
Serial.print("-");
Serial.print(dayOfMonth, DEC);
Serial.print(" ");
// send it to the serial monitor
Serial.print(hour, DEC);
// convert the byte variable to a decimal number when displayed
Serial.print(":");
if (minute<10){
Serial.print("0");
}
Serial.print(minute, DEC);
Serial.print(":");
if (second<10){
Serial.print("0");
}
Serial.println(second, DEC);
}
void loop(){
if (Serial.available() > 0) {
byte cmd = Serial.read();
if ( cmd == 0x10 ) {
char data[7];
Serial.readBytes(data,7);
setDS3231time(data[0],data[1],data[2],data[3],data[4],data[5],data[6]);
// say what you got:
Serial.println("OK");
} else {
displayTime(); // display the real-time clock data on the Serial Monitor,
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment