I'm working on a clock made with the SparkFun e-paper display and a DS3231.
I have everything running just like it should. I also have it running off a LiPo battery. I figured to save battery power, I'd just update the clock display when then minutes change.
I thought I could set the DS3231 to alarm on the minute (when the seconds hit zero), and have my code update the display when the alarm goes off. I can't for the life of me figure out how to do it. I've spent 2 days on it and I'm no closer to figuring it out than I was 2 days ago! Very frustrating!
Can someone baby-step me through it? What library should I be using? For now I'm just talking to the DS3231 with the Wire library.
I pulled out all the code that I used to set the time and date to make it easier to read. Here's my code:
#include <Wire.h>
#include <ePaper.h>
int EIO1pin = 9; // Input/output pin for chip selection
int XCKpin = 10; // Clock input pin for taking display data
int LATCHpin = 11; // Latch pulse input pin for display data
int SLEEPBpin = 12; // Sleep Pin for the display
int DI0pin = 13; // Input pin for display data
int VCCpin = 8;
//setup display with pin definitions
ePaper epaper = ePaper(EIO1pin, XCKpin, LATCHpin, SLEEPBpin, DI0pin);
int seconds;
int minutes;
int hours;
int dayOfWeek;
int dayOfMonth;
int month;
int year;
char *space;
char *suffix;
char *dayOfWeekChar;
char *seperator;
char *bottomLineData;
void setup()
{
Wire.begin();
Serial.begin(9600);
pinMode (VCCpin, OUTPUT);
digitalWrite(VCCpin, HIGH);
//////////////////////////////// force time setting:
/*
seconds = 00;
minutes = 56;
hours = 19; //in 24 format
dayOfWeek = 7;
dayOfMonth = 25;
month = 05;
year = 25;
initChrono();
*/
///////////////////////////////
}
void loop() {
//poll the DS3231 for the date and time
get_time();
get_date();
// time
//display hours
if (hours > 11) {
suffix = " PM ";
}
else {
suffix = " AM ";
}
if (hours > 12) {
hours -= 12;
}
else if (hours == 0) {
hours = 12;
}
//figure out leading spaces
if (hours > 9) {
space = " "; //1 space
}
else {
space = " "; //2 spaces
}
if (minutes < 10) {
seperator = "-0";
}
else {
seperator = "-";
}
//construct the string to be displayed
char timeChr[10];
sprintf(timeChr, "%s%d%s%d%s", space, hours, seperator, minutes, suffix);
/////////////////////////////////////////////////////
//date
switch(dayOfWeek){
case 1:
dayOfWeekChar ="Sun";
break;
case 2:
dayOfWeekChar ="Mon";
break;
case 3:
dayOfWeekChar ="Tue";
break;
case 4:
dayOfWeekChar ="Wed";
break;
case 5:
dayOfWeekChar ="Thu";
break;
case 6:
dayOfWeekChar ="Fri";
break;
case 7:
dayOfWeekChar ="Sat";
break;
}
//construct the string to be displayed
char dateChr[10];
if ((month < 10) && (dayOfMonth < 10)) {
sprintf(dateChr, " %s %d/%d ", dayOfWeekChar, month, dayOfMonth); //add an extra space to center text when month and DOM are < 10; extra spaces are intentional so no weird char pop in at the end
}
else {
sprintf(dateChr, " %s %d/%d ", dayOfWeekChar, month, dayOfMonth); //extra spaces are intentional so no weird char pop in at the end
}
epaper.writeTop(timeChr);
epaper.writeBottom(dateChr);
epaper.writeDisplay();
} //end of loop
///////////////////////////////////////////////////////////////////////////////////////////
//DS3231 RTC interface
void initChrono()
{
set_time();
set_date();
}
void set_date()
{
Wire.beginTransmission(104);
Wire.write(3);
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
void get_date()
{
Wire.beginTransmission(104);
Wire.write(3);//set register to 3 (day)
Wire.endTransmission();
Wire.requestFrom(104, 4); //get 5 bytes(day,date,month,year,control);
dayOfWeek = bcdToDec(Wire.read());
dayOfMonth = bcdToDec(Wire.read());
month = bcdToDec(Wire.read());
year = bcdToDec(Wire.read());
}
void set_time()
{
Wire.beginTransmission(104);
Wire.write(0);
Wire.write(decToBcd(seconds));
Wire.write(decToBcd(minutes));
Wire.write(decToBcd(hours));
Wire.endTransmission();
}
void get_time()
{
Wire.beginTransmission(104);
Wire.write(0);//set register to 0
Wire.endTransmission();
Wire.requestFrom(104, 3);//get 3 bytes (seconds,minutes,hours);
seconds = bcdToDec(Wire.read() & 0x7f);
minutes = bcdToDec(Wire.read());
hours = bcdToDec(Wire.read() & 0x3f);
}
///////////////////////////////////////////////////////////////////////
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
Any help or guidance would be much appreciated