2 Commits

  1. 95
      libraries/DS3231/DS3231.cpp
  2. 36
      libraries/DS3231/DS3231.h

95
libraries/DS3231/DS3231.cpp

@ -12,7 +12,7 @@ void DS3231::begin()
uint8_t DS3231::getSeconds()
{
uint8_t seconds;
// read minutes
// read seconds
seconds = getRegister(DS3231_SECONDS);
return bcdtodec(seconds);
}
@ -305,24 +305,109 @@ void DS3231::decrementYear()
setYear(year);
}
uint8_t DS3231::getAlarm1Seconds()
{
return bcdtodec(getRegister(DS3231_ALARM1_SECONDS));
}
void DS3231::setAlarm1Seconds(uint8_t seonds)
{
setRegister(DS3231_ALARM1_SECONDS, dectobcd(seconds % 60));
}
uint8_t DS3231::getAlarm1Minutes()
{
return bcdtodec(getRegister(DS3231_ALARM1_MINUTES));
}
void DS3231::setAlarm1Minutes(uint8_t minutes)
{
setRegister(DS3231_ALARM1_MINUTES, dectobcd(minutes % 60));
}
uint8_t DS3231::getAlarm1Hours()
{
return bcdtodec(getRegister(DS3231_ALARM1_HOURS));
}
void DS3231::setAlarm1Hours(uint8_t hours)
{
setRegister(DS3231_ALARM1_HOURS, dectobcd(hours % 24));
}
uint8_t DS3231::getAlarm1Date()
{
return bcdtodec(getRegister(DS3231_ALARM1_DATE));
}
void DS3231::setAlarm1Date(uint8_t date)
{
setRegister(DS3231_ALARM1_DATE, dectobcd(data % 31));
}
void DS3231::setAlarm1Mask(uint8_t mask)
{
setRegister(DS3231_ALARM1_SECONDS, dectobcd(getAlarm1Seconds()) | ((mask & 0b00000001) << 7));
setRegister(DS3231_ALARM1_MINUTES, dectobcd(getAlarm1Minutes()) | ((mask & 0b00000010) << 6));
setRegister(DS3231_ALARM1_HOURS, dectobcd(getAlarm1Hours()) | ((mask & 0b00000100) << 5));
setRegister(DS3231_ALARM1_DATE, dectobcd(getAlarm1Date()) | ((mask & 0b00001000) << 4));
}
uint8_t DS3231::getAlarm2Minutes()
{
return bcdtodec(getRegister(DS3231_ALARM2_MINUTES));
}
void DS3231::setAlarm2Minutes(uint8_t minutes)
{
setRegister(DS3231_ALARM2_MINUTES, dectobcd(minutes % 60));
}
uint8_t DS3231::getAlarm2Hours()
{
return bcdtodec(getRegister(DS3231_ALARM2_HOURS));
}
void DS3231::setAlarm2Hours(uint8_t hours)
{
setRegister(DS3231_ALARM2_HOURS, dectobcd(hours % 24));
}
uint8_t DS3231::getAlarm2Date()
{
return bcdtodec(getRegister(DS3231_ALARM2_DATE));
}
void DS3231::setAlarm2Date(uint8_t date)
{
setRegister(DS3231_ALARM2_DATE, dectobcd(date % 31));
}
void DS3231::setAlarm2Mask(uint8_t mask)
{
setRegister(DS3231_ALARM2_MINUTES, dectobcd(getAlarm2Minutes()) | ((mask & 0b00000001) << 7));
setRegister(DS3231_ALARM2_HOURS, dectobcd(getAlarm2Hours()) | ((mask & 0b00000010) << 6));
setRegister(DS3231_ALARM2_DATE, dectobcd(getAlarm2Date()) | ((mask & 0b00000100) << 5));
}
uint8_t DS3231::getControlRegister()
{
return bcdtodec(getRegister(DS3231_CONTROL));
return getRegister(DS3231_CONTROL);
}
void DS3231::setControlRegister(uint8_t value)
{
setRegister(DS3231_CONTROL, dectobcd(value));
setRegister(DS3231_CONTROL, value);
}
uint8_t DS3231::getStatusRegister()
{
return bcdtodec(getRegister(DS3231_STATUS));
return getRegister(DS3231_STATUS);
}
void DS3231::setStatusRegister(uint8_t value)
{
setRegister(DS3231_STATUS, dectobcd(value));
setRegister(DS3231_STATUS, value);
}
uint8_t DS3231::getTemperature()

36
libraries/DS3231/DS3231.h

@ -63,6 +63,38 @@ public:
void incrementYear();
// decrement year
void decrementYear();
// get alarm1 seconds
uint8_t getAlarm1Seconds();
// set alarm1 seconds
void setAlarm1Seconds(uint8_t seonds);
// get alarm1 minutes
uint8_t getAlarm1Minutes();
// set alarm1 Minutes
void setAlarm1Minutes(uint8_t minutes);
// get alarm1 hours
uint8_t getAlarm1Hours();
// set alarm1 hours
void setAlarm1Hours(uint8_t hours);
// get alarm1 date
uint8_t getAlarm1Date();
// set alarm1 data
void setAlarm1Date(uint8_t date);
// set alarm1 mask
void setAlarm1Mask(uint8_t mask);
// get alarm2 minutes
uint8_t getAlarm2Minutes();
// set alarm2 Minutes
void setAlarm2Minutes(uint8_t minutes);
// get alarm2 hours
uint8_t getAlarm2Hours();
// set alarm2 hours
void setAlarm2Hours(uint8_t hours);
// get alarm2 date
uint8_t getAlarm2Date();
// set alarm2 data
void setAlarm2Date(uint8_t date);
// set alarm2 mask
void setAlarm2Mask(uint8_t mask);
// get control register
uint8_t getControlRegister();
// set control register
@ -73,8 +105,6 @@ public:
void setStatusRegister(uint8_t value);
// get temperature
uint8_t getTemperature();
// helpers
protected:
// get register
uint8_t getRegister(uint8_t address);
// set register
@ -106,7 +136,7 @@ protected:
#define DS3231_ALARM1_DATE 0x0A
#define DS3231_ALARM2_MINUTES 0x0B
#define DS3231_ALARM2_HOURS 0x0C
#define DS3231_ALARM1_DATE 0x0D
#define DS3231_ALARM2_DATE 0x0D
#define DS3231_CONTROL 0x0E
#define DS3231_STATUS 0x0F
#define DS3231_AGING_OFFSET 0x10

Loading…
Cancel
Save