Skip to content

Instantly share code, notes, and snippets.

@kongmunist
Created February 5, 2024 20:54
Show Gist options
  • Save kongmunist/fb0f0ba41522a056364a2c41e3f1b07e to your computer and use it in GitHub Desktop.
Save kongmunist/fb0f0ba41522a056364a2c41e3f1b07e to your computer and use it in GitHub Desktop.
Code for interfacing the ATtiny 25/45/85 with the DS3231, as described in https://andykong.org/blog/attinyrtc/
#include <TinyWireM.h>
#include "SoftwareSerial.h"
byte ss=0, mi=0, hh=0, wd=6, dd=1, mo=1, yy=0;
const int Rx = 3; // this is physical pin 2
const int Tx = 4; // this is physical pin 3
SoftwareSerial mySerial(Rx, Tx);
void setup() {
TinyWireM.begin();
mySerial.begin(9600); // send serial data at 9600 bits/sec
}
void loop() {
// read the time from the RTC, if we can
boolean gotTheTime = grabTime();
if (gotTheTime) {
// if we are here, then the time has been successfully read
// and stored in global variables (ss, mi, hh, wd, dd, mo, yy)
mySerial.print("Got the time: ");
printTime();
}
else {
// if we are here, then we tried to read the time but couldn't
mySerial.println("Unable to read time from RTC");
}
delay(500);
}
boolean grabTime() {
// get time from the RTC and put it in global variables
// send request to receive data starting at register 0
TinyWireM.beginTransmission(0x68); // 0x68 is DS3231 device address
TinyWireM.write((byte)0); // start at register 0
TinyWireM.endTransmission();
TinyWireM.requestFrom(0x68, 7); // request seven bytes (ss, mi, hh, wd, dd, mo, yy)
// check for a reply from the RTC, and use it if we can
if (TinyWireM.available() >= 7) {
// if we're here, we got a reply and it is long enough
// so now we read the time
ss = bcd2bin(TinyWireM.read()); // get seconds
mi = bcd2bin(TinyWireM.read()); // get minutes
hh = bcd2bin(TinyWireM.read()); // get hours
wd = bcd2bin(TinyWireM.read()); // get day of week
dd = bcd2bin(TinyWireM.read()); // get day of month
mo = bcd2bin(TinyWireM.read()); // get month
yy = bcd2bin(TinyWireM.read()); // get year (two digits)
// indicate that we successfully got the time
return true;
}
else {
// indicate that we were unable to read the time
return false;
}
}
byte bcd2bin(byte x) {
// converts from binary-coded decimal to a "regular" binary number
return ((((x >> 4) & 0xF) * 10) + (x & 0xF)) ;
}
void printTime() {
// just like it says on the tin
mySerial.print ("\'");
if (yy<10) mySerial.print("0"); mySerial.print(yy,DEC); mySerial.print("-");
if (mo<10) mySerial.print("0"); mySerial.print(mo,DEC); mySerial.print("-");
if (dd<10) mySerial.print("0"); mySerial.print(dd,DEC); mySerial.print("(");
switch (wd) {
case 1: mySerial.print("Mon"); break;
case 2: mySerial.print("Tue"); break;
case 3: mySerial.print("Wed"); break;
case 4: mySerial.print("Thu"); break;
case 5: mySerial.print("Fri"); break;
case 6: mySerial.print("Sat"); break;
case 7: mySerial.print("Sun"); break;
default: mySerial.print("Bad");
}
mySerial.print(") ");
if (hh<10) mySerial.print("0"); mySerial.print(hh,DEC); mySerial.print(":");
if (mi<10) mySerial.print("0"); mySerial.print(mi,DEC); mySerial.print(":");
if (ss<10) mySerial.print("0"); mySerial.print(ss,DEC); mySerial.println("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment