1 #ifndef LIBPROPELLER_PCF8523_H_
2 #define LIBPROPELLER_PCF8523_H_
5 #include "libpropeller/i2c/i2c.h"
72 if (status_ ==
false) {
77 bus_->
Put(kDeviceAddress, kCONTROL_1, 0b10000000);
78 bus_->
Put(kDeviceAddress, kCONTROL_2, 0b00000000);
79 bus_->
Put(kDeviceAddress, kCONTROL_3, 0b00000000);
91 status_ = bus_->
Ping(kDeviceAddress);
113 bool SetClock(
const int year,
const int month,
const int day,
114 const int hour,
const int minute,
const int second,
const int weekday = 0) {
120 clock[0] = ConvertToBCD(second);
121 clock[1] = ConvertToBCD(minute);
122 clock[2] = ConvertToBCD(hour);
123 clock[3] = ConvertToBCD(day);
124 clock[4] = ConvertToBCD(weekday);
125 clock[5] = ConvertToBCD(month);
126 clock[6] = ConvertToBCD(year);
128 clock[0] &= 0b01111111;
130 bus_->
Put(kDeviceAddress, kSECONDS, clock, 7);
149 int & hour,
int & minute,
int & second,
int & weekday) {
155 bus_->
Get(kDeviceAddress, kSECONDS, clock, 7);
156 second = ConvertFromBCD(clock[0]);
157 minute = ConvertFromBCD(clock[1]);
158 hour = ConvertFromBCD(clock[2]);
159 day = ConvertFromBCD(clock[3]);
160 weekday = ConvertFromBCD(clock[4]);
161 month = ConvertFromBCD(clock[5]);
162 year = ConvertFromBCD(clock[6]);
180 int & hour,
int & minute,
int & second) {
182 return GetClock(year, month, day, hour, minute, second, temp);
191 const static unsigned char kDeviceAddress = 0b11010000;
193 const static unsigned char kCONTROL_1 = 0x00;
194 const static unsigned char kCONTROL_2 = 0x01;
195 const static unsigned char kCONTROL_3 = 0x02;
196 const static unsigned char kSECONDS = 0x03;
207 char ConvertToBCD(
const int number)
const {
208 int unit = number % 10;
209 int tens = (number % 100) / 10;
211 return (tens << 4) | unit;
222 int ConvertFromBCD(
const unsigned char bcd)
const {
223 return ((bcd >> 4) * 10) + (bcd & 0xF);
227 friend class ::UnityTests;
232 #endif // LIBPROPELLER_PCF8523_H_