You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
#include <Arduino.h>
#include <Wire.h>
#include <DS3231.h>
void DS3231::begin() { Wire.begin(); }
uint8_t DS3231::getSeconds() { uint8_t seconds; // read minutes
seconds = getRegister(DS3231_SECONDS); return bcdtodec(seconds); }
void DS3231::setSeconds(uint8_t seconds) { // write seconds
setRegister(DS3231_SECONDS, dectobcd(seconds)); }
void DS3231::incrementSeconds() { uint8_t seconds = getSeconds();
if (seconds == 58) { seconds = 0; } else { seconds += 2; }
setSeconds(seconds); }
void DS3231::decrementSeconds() { uint8_t seconds = getSeconds();
if (seconds == 1) { seconds = 59; } else { seconds -= 2; }
setSeconds(seconds); }
uint8_t DS3231::getMinutes() { uint8_t minutes; // read minutes
minutes = getRegister(DS3231_MINUTES); return bcdtodec(minutes); }
void DS3231::setMinutes(uint8_t minutes) { // write minutes
setRegister(DS3231_MINUTES, dectobcd(minutes)); }
void DS3231::incrementMinutes() { uint8_t minutes = getMinutes();
if (minutes == 59) { minutes = 0; } else { minutes++; }
setMinutes(minutes); }
void DS3231::decrementMinutes() { uint8_t minutes = getMinutes();
if (minutes == 0) { minutes = 59; } else { minutes--; }
setMinutes(minutes); }
uint8_t DS3231::getHours() { uint8_t hours; // read hours
hours = getRegister(DS3231_HOURS); return bcdtodec(hours); }
void DS3231::setHours(uint8_t hours) { // write hours
setRegister(DS3231_HOURS, dectobcd(hours)); }
void DS3231::incrementHours() { uint8_t hours = getHours();
if (hours == 23) { hours = 0; } else { hours++; }
setHours(hours); }
void DS3231::decrementHours() { uint8_t hours = getHours();
if (hours == 0) { hours = 23; } else { hours--; }
setHours(hours); }
uint8_t DS3231::getDay() { uint8_t day; // read day
day = getRegister(DS3231_DAY); return bcdtodec(day); }
void DS3231::setDay(uint8_t day) { // write day
setRegister(DS3231_DAY, dectobcd(day)); }
uint8_t DS3231::getDate() { uint8_t date; // read date
date = getRegister(DS3231_DATE); return bcdtodec(date); }
void DS3231::setDate(uint8_t date) { // write date
setRegister(DS3231_DATE, dectobcd(date)); }
void DS3231::incrementDate() { uint8_t date = getDate(); date++; if (date > 31) { date = 1; } setDate(date); }
void DS3231::decrementDate() { uint8_t date = getDate(); date--; if (date < 1) { date = 31; } setDate(date); }
uint8_t DS3231::getMonth() { uint8_t month; // read month
month = getRegister(DS3231_MONTH);
// read month, ingore century;
return bcdtodec(month & 0x1F); }
void DS3231::setMonth(uint8_t month) { uint8_t century = getCentury();
month = dectobcd(month);
if (century == 1) { month = month + 0x80; }
// write month
setRegister(DS3231_MONTH, month); }
void DS3231::incrementMonth() { uint8_t month = getMonth(); month++; if (month > 12) { month = 1; } setMonth(month); }
void DS3231::decrementMonth() { uint8_t month = getMonth(); month--; if (month < 1) { month = 12; } setMonth(month); }
int16_t DS3231::getYear() { uint8_t century = getCentury();
int16_t year;
// read year
year = getRegister(DS3231_YEAR);
year = bcdtodec(year);
if (century == 1) { year = 2000 + year; } else { year = 1900 + year; }
return year; }
void DS3231::setYear(int16_t year) { if (year > 1999) { year = year - 2000; setCentury(0x80); } else { year = year - 1900; setCentury(0); }
setRegister(DS3231_YEAR, dectobcd(year)); }
void DS3231::incrementYear() { int16_t year = getYear(); year++; if (year > 2099) { year = 1900; } setYear(year); }
void DS3231::decrementYear() { int16_t year = getYear(); year--; if (year < 1900) { year = 2099; } setYear(year); }
uint8_t DS3231::getControlRegister() { return bcdtodec(getRegister(DS3231_CONTROL)); }
void DS3231::setControlRegister(uint8_t value) { setRegister(DS3231_CONTROL, dectobcd(value)); }
uint8_t DS3231::getStatusRegister() { return bcdtodec(getRegister(DS3231_STATUS)); }
void DS3231::setStatusRegister(uint8_t value) { setRegister(DS3231_STATUS, dectobcd(value)); }
uint8_t DS3231::getTemperature() { int8_t msb_temp; uint8_t msb_value = getRegister(DS3231_MSB_TEMP); uint8_t lsb_temp = getRegister(DS3231_LSB_TEMP) >> 6; float result;
if ((msb_value & 0x80) != 0) { msb_temp = msb_value | ~((1 << 8) - 1); } else { msb_temp = msb_value; }
result = 0.25 * lsb_temp + msb_temp;
return result; }
uint8_t DS3231::getRegister(uint8_t address) { uint8_t value;
Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(address); Wire.endTransmission();
// request 1 byte
Wire.requestFrom(DS3231_I2C_ADDRESS, 1);
// read value
value = Wire.read();
return value; }
void DS3231::setRegister(uint8_t address, uint8_t value) { Wire.beginTransmission(DS3231_I2C_ADDRESS); // start at address
Wire.write(address);
// write value
Wire.write(value);
Wire.endTransmission(); }
uint8_t DS3231::dectobcd(uint8_t value) { return ((value / 10 * 16) + (value % 10)); }
uint8_t DS3231::bcdtodec(uint8_t value) { return ((value / 16 * 10) + (value % 16)); }
uint8_t DS3231::getCentury() { uint8_t century = 0;
uint8_t month = getRegister(DS3231_MONTH);
// read century from month
century = (month & 0x80) >> 7; return century; }
void DS3231::setCentury(uint8_t century) { uint8_t month = getMonth();
month = month + century;
setRegister(DS3231_MONTH, month); }
|