Skip to content

Instantly share code, notes, and snippets.

@jhoolmans
Created March 26, 2018 21:09
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 jhoolmans/a3e816285f88799f431ad3e06cd24539 to your computer and use it in GitHub Desktop.
Save jhoolmans/a3e816285f88799f431ad3e06cd24539 to your computer and use it in GitHub Desktop.
ZS-042 DS3231 Clock and SSD1306 OLED (128x64) both on i2c with the Adafruit Trinket Pro.
// Pro Trinket - OLED Display Clock
//
// OLED (SSD1306)
// CLOCK (ZS-042)
//
#include "Wire.h"
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
#define ROTATION 2
// CLOCK
#define DS3231_I2C_ADDRESS 0x68
// OLED
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
//
// Conversion functions
//
// 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) );
}
const char compile_date[] = __DATE__ " " __TIME__;
void setup() {
// I2C start
Wire.begin();
// Setup display
display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
display.display();
delay(1000);
// initial time
//DS3231 seconds, minutes, hours, day, date, month, year
/*
setDS3231time(0, 54, 19, 1, 25, 3, 18);
display.setCursor(0,0);
display.println("Updated Time!");
display.display();
delay(5000);
*/
}
const char* daysOfTheWeek[7] = {
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
};
void loop() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
char buffer[9];
sprintf(buffer, "%02d:%02d:%02d", int(hour), int(minute), int(second));
//display.println(sprintf("%02d:%02d:%02d", int(hour), int(minute), int(second)));
display.println(daysOfTheWeek[int(dayOfWeek) - 1]);
display.println(buffer);
display.display();
delay(1000);
}
//
// Clock Functions
//
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());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment