code init

This commit is contained in:
Lurkars
2019-02-13 19:27:22 +01:00
parent 9650b482ee
commit 001591a54d
25 changed files with 2751 additions and 0 deletions
+406
View File
@@ -0,0 +1,406 @@
#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);
}
+133
View File
@@ -0,0 +1,133 @@
#ifndef DS3231_h
#define DS3231_h
#include <Arduino.h>
/**
TODO: alarms
*/
class DS3231
{
public:
// begin function
void begin();
// get seconds
uint8_t getSeconds();
// set seconds
void setSeconds(uint8_t seconds);
// increment seconds
void incrementSeconds();
// decrement seconds
void decrementSeconds();
// get minutes
uint8_t getMinutes();
// set minutes
void setMinutes(uint8_t minutes);
// increment minutes
void incrementMinutes();
// decrement minutes
void decrementMinutes();
// get hours
uint8_t getHours();
// set hours
void setHours(uint8_t hours);
// increment hours
void incrementHours();
// decrement hours
void decrementHours();
// get day
uint8_t getDay();
// set day
void setDay(uint8_t day);
// get date
uint8_t getDate();
// set date
void setDate(uint8_t date);
// increment date
void incrementDate();
// decrement date
void decrementDate();
// get month
uint8_t getMonth();
// set month
void setMonth(uint8_t month);
// increment month
void incrementMonth();
// decrement month
void decrementMonth();
// get year
int16_t getYear();
// set year
void setYear(int16_t year);
// increment year
void incrementYear();
// decrement year
void decrementYear();
// get control register
uint8_t getControlRegister();
// set control register
void setControlRegister(uint8_t value);
// set status register
uint8_t getStatusRegister();
// set status register
void setStatusRegister(uint8_t value);
// get temperature
uint8_t getTemperature();
// helpers
protected:
// get register
uint8_t getRegister(uint8_t address);
// set register
void setRegister(uint8_t address, uint8_t value);
// decimal to binary coded decimal
uint8_t dectobcd(uint8_t value);
// binary coded decimal to decimal
uint8_t bcdtodec(uint8_t value);
// get century value
uint8_t getCentury();
// set century value
void setCentury(uint8_t century);
};
// I2C address
#define DS3231_I2C_ADDRESS 0x68
// I2C registers
#define DS3231_SECONDS 0x00
#define DS3231_MINUTES 0x01
#define DS3231_HOURS 0x02
#define DS3231_DAY 0x03
#define DS3231_DATE 0x04
#define DS3231_MONTH 0x05
#define DS3231_YEAR 0x06
#define DS3231_ALARM1_SECONDS 0x07
#define DS3231_ALARM1_MINUTES 0x08
#define DS3231_ALARM1_HOURS 0x09
#define DS3231_ALARM1_DATE 0x0A
#define DS3231_ALARM2_MINUTES 0x0B
#define DS3231_ALARM2_HOURS 0x0C
#define DS3231_ALARM1_DATE 0x0D
#define DS3231_CONTROL 0x0E
#define DS3231_STATUS 0x0F
#define DS3231_AGING_OFFSET 0x10
#define DS3231_MSB_TEMP 0x11
#define DS3231_LSB_TEMP 0x12
// control registers
#define DS3231_CONTROL_A1IE 0x01
#define DS3231_CONTROL_A2IE 0x02
#define DS3231_CONTROL_INTCN 0x04
#define DS3231_CONTROL_RS1 0x08
#define DS3231_CONTROL_RS2 0x10
#define DS3231_CONTROL_CONV 0x20
#define DS3231_CONTROL_BBSQW 0x40
#define DS3231_CONTROL_EOSC 0x80
// status registers
#define DS3231_STATUSL_A1F 0x01
#define DS3231_STATUSL_A2F 0x02
#define DS3231_STATUSL_BSY 0x04
#define DS3231_STATUSL_EN32KHZ 0x08
#define DS3231_STATUSL_OSF 0x80
#endif