library to control, set and read time and date from Hotboards rtcc board, wich contains the Microchip MCP7941x real time clock
Dependents: Hotboards_rtcc_manual_timedate Hotboards_rtcc_timeSpan Hotboards_rtcc_alarm Hotboards_rtcc_compiler_timedate ... more
Hotboards_rtcc.cpp@1:0790bcaf8b8f, 2016-02-09 (annotated)
- Committer:
- Hotboards
- Date:
- Tue Feb 09 19:28:24 2016 +0000
- Revision:
- 1:0790bcaf8b8f
- Parent:
- 0:3a2ad459941a
corrected a trouble with the device adress:; ; #define RTC_ADDR (uint8_t)(0xDE >> 1); #define EEPROM_ADDR (uint8_t)(0xAE >> 1); ; ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Hotboards | 0:3a2ad459941a | 1 | /* |
Hotboards | 0:3a2ad459941a | 2 | Hotboards_rtcc.cpp - Library to read, write and control the real time clock MCP7941x included in rtc board. |
Hotboards | 0:3a2ad459941a | 3 | http://hotboards.org |
Hotboards | 0:3a2ad459941a | 4 | adapted and taken from https://github.com/adafruit/RTClib |
Hotboards | 0:3a2ad459941a | 5 | Released into the public domain. |
Hotboards | 0:3a2ad459941a | 6 | */ |
Hotboards | 0:3a2ad459941a | 7 | |
Hotboards | 0:3a2ad459941a | 8 | #include "Hotboards_rtcc.h" |
Hotboards | 0:3a2ad459941a | 9 | |
Hotboards | 0:3a2ad459941a | 10 | |
Hotboards | 1:0790bcaf8b8f | 11 | #define RTC_ADDR (uint8_t)(0xDE) |
Hotboards | 1:0790bcaf8b8f | 12 | #define EEPROM_ADDR (uint8_t)(0xAE) |
Hotboards | 0:3a2ad459941a | 13 | #define RTC_STARTADDR (uint8_t)0x00 |
Hotboards | 0:3a2ad459941a | 14 | #define ALARM_STARTADDR (uint8_t)0x0A |
Hotboards | 0:3a2ad459941a | 15 | #define CTRL_STARTADDR (uint8_t)0x07 |
Hotboards | 0:3a2ad459941a | 16 | #define SRAM_SARTADDR (uint8_t)0x20 |
Hotboards | 0:3a2ad459941a | 17 | #define EEPROM_SARTADDR (uint8_t)0x00 |
Hotboards | 0:3a2ad459941a | 18 | #define PEEPROM_SARTADDR (uint8_t)0xF0 |
Hotboards | 0:3a2ad459941a | 19 | |
Hotboards | 0:3a2ad459941a | 20 | #define SECONDS_FROM_1970_TO_2000 946684800 |
Hotboards | 0:3a2ad459941a | 21 | |
Hotboards | 0:3a2ad459941a | 22 | const uint8_t daysInMonth[] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; |
Hotboards | 0:3a2ad459941a | 23 | |
Hotboards | 0:3a2ad459941a | 24 | // number of days since 2000/01/01, valid for 2001..2099 |
Hotboards | 0:3a2ad459941a | 25 | static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) |
Hotboards | 0:3a2ad459941a | 26 | { |
Hotboards | 0:3a2ad459941a | 27 | if (y >= 2000) |
Hotboards | 0:3a2ad459941a | 28 | y -= 2000; |
Hotboards | 0:3a2ad459941a | 29 | uint16_t days = d; |
Hotboards | 0:3a2ad459941a | 30 | for (uint8_t i = 1; i < m; ++i) |
Hotboards | 0:3a2ad459941a | 31 | days += daysInMonth[i - 1]; |
Hotboards | 0:3a2ad459941a | 32 | if (m > 2 && y % 4 == 0) |
Hotboards | 0:3a2ad459941a | 33 | ++days; |
Hotboards | 0:3a2ad459941a | 34 | return days + 365 * y + (y + 3) / 4 - 1; |
Hotboards | 0:3a2ad459941a | 35 | } |
Hotboards | 0:3a2ad459941a | 36 | |
Hotboards | 0:3a2ad459941a | 37 | static long time2long(uint16_t days, uint8_t h, uint8_t m, uint8_t s) |
Hotboards | 0:3a2ad459941a | 38 | { |
Hotboards | 0:3a2ad459941a | 39 | return ((days * 24L + h) * 60 + m) * 60 + s; |
Hotboards | 0:3a2ad459941a | 40 | } |
Hotboards | 0:3a2ad459941a | 41 | |
Hotboards | 0:3a2ad459941a | 42 | static uint8_t conv2d(const char* p) |
Hotboards | 0:3a2ad459941a | 43 | { |
Hotboards | 0:3a2ad459941a | 44 | uint8_t v = 0; |
Hotboards | 0:3a2ad459941a | 45 | if ('0' <= *p && *p <= '9') |
Hotboards | 0:3a2ad459941a | 46 | v = *p - '0'; |
Hotboards | 0:3a2ad459941a | 47 | return 10 * v + *++p - '0'; |
Hotboards | 0:3a2ad459941a | 48 | } |
Hotboards | 0:3a2ad459941a | 49 | |
Hotboards | 0:3a2ad459941a | 50 | /* |
Hotboards | 0:3a2ad459941a | 51 | * Constructor that use time in a 32 bit variable |
Hotboards | 0:3a2ad459941a | 52 | */ |
Hotboards | 0:3a2ad459941a | 53 | DateTime::DateTime( uint32_t t ) |
Hotboards | 0:3a2ad459941a | 54 | { |
Hotboards | 0:3a2ad459941a | 55 | t -= SECONDS_FROM_1970_TO_2000; // bring to 2000 timestamp from 1970 |
Hotboards | 0:3a2ad459941a | 56 | ss = t % 60; |
Hotboards | 0:3a2ad459941a | 57 | t /= 60; |
Hotboards | 0:3a2ad459941a | 58 | mm = t % 60; |
Hotboards | 0:3a2ad459941a | 59 | t /= 60; |
Hotboards | 0:3a2ad459941a | 60 | hh = t % 24; |
Hotboards | 0:3a2ad459941a | 61 | uint16_t days = t / 24; |
Hotboards | 0:3a2ad459941a | 62 | uint8_t leap; |
Hotboards | 0:3a2ad459941a | 63 | for( yOff = 0 ; ; ++yOff ) |
Hotboards | 0:3a2ad459941a | 64 | { |
Hotboards | 0:3a2ad459941a | 65 | leap = yOff % 4 == 0; |
Hotboards | 0:3a2ad459941a | 66 | if (days < 365 + leap) |
Hotboards | 0:3a2ad459941a | 67 | { |
Hotboards | 0:3a2ad459941a | 68 | break; |
Hotboards | 0:3a2ad459941a | 69 | } |
Hotboards | 0:3a2ad459941a | 70 | days -= 365 + leap; |
Hotboards | 0:3a2ad459941a | 71 | } |
Hotboards | 0:3a2ad459941a | 72 | for (m = 1; ; ++m) |
Hotboards | 0:3a2ad459941a | 73 | { |
Hotboards | 0:3a2ad459941a | 74 | uint8_t daysPerMonth = daysInMonth[m - 1]; |
Hotboards | 0:3a2ad459941a | 75 | if (leap && m == 2) |
Hotboards | 0:3a2ad459941a | 76 | { |
Hotboards | 0:3a2ad459941a | 77 | ++daysPerMonth; |
Hotboards | 0:3a2ad459941a | 78 | } |
Hotboards | 0:3a2ad459941a | 79 | if (days < daysPerMonth) |
Hotboards | 0:3a2ad459941a | 80 | { |
Hotboards | 0:3a2ad459941a | 81 | break; |
Hotboards | 0:3a2ad459941a | 82 | } |
Hotboards | 0:3a2ad459941a | 83 | days -= daysPerMonth; |
Hotboards | 0:3a2ad459941a | 84 | } |
Hotboards | 0:3a2ad459941a | 85 | d = days + 1; |
Hotboards | 0:3a2ad459941a | 86 | } |
Hotboards | 0:3a2ad459941a | 87 | |
Hotboards | 0:3a2ad459941a | 88 | /* |
Hotboards | 0:3a2ad459941a | 89 | * Constructor that use time variables for each element in decimal |
Hotboards | 0:3a2ad459941a | 90 | */ |
Hotboards | 0:3a2ad459941a | 91 | DateTime::DateTime( uint16_t year, uint8_t month, uint8_t day, |
Hotboards | 0:3a2ad459941a | 92 | uint8_t hour, uint8_t min, uint8_t sec, uint8_t dweek ) |
Hotboards | 0:3a2ad459941a | 93 | { |
Hotboards | 0:3a2ad459941a | 94 | if( year >= 2000 ) |
Hotboards | 0:3a2ad459941a | 95 | { |
Hotboards | 0:3a2ad459941a | 96 | year -= 2000; |
Hotboards | 0:3a2ad459941a | 97 | } |
Hotboards | 0:3a2ad459941a | 98 | yOff = year; |
Hotboards | 0:3a2ad459941a | 99 | m = month; |
Hotboards | 0:3a2ad459941a | 100 | d = day; |
Hotboards | 0:3a2ad459941a | 101 | hh = hour; |
Hotboards | 0:3a2ad459941a | 102 | mm = min; |
Hotboards | 0:3a2ad459941a | 103 | ss = sec; |
Hotboards | 0:3a2ad459941a | 104 | dw = dweek; |
Hotboards | 0:3a2ad459941a | 105 | } |
Hotboards | 0:3a2ad459941a | 106 | |
Hotboards | 0:3a2ad459941a | 107 | /* |
Hotboards | 0:3a2ad459941a | 108 | * Constructor tcreate a copy of DateTime object |
Hotboards | 0:3a2ad459941a | 109 | */ |
Hotboards | 0:3a2ad459941a | 110 | DateTime::DateTime (const DateTime& copy): |
Hotboards | 0:3a2ad459941a | 111 | yOff(copy.yOff), |
Hotboards | 0:3a2ad459941a | 112 | m(copy.m), |
Hotboards | 0:3a2ad459941a | 113 | d(copy.d), |
Hotboards | 0:3a2ad459941a | 114 | hh(copy.hh), |
Hotboards | 0:3a2ad459941a | 115 | mm(copy.mm), |
Hotboards | 0:3a2ad459941a | 116 | ss(copy.ss) |
Hotboards | 0:3a2ad459941a | 117 | {} |
Hotboards | 0:3a2ad459941a | 118 | |
Hotboards | 0:3a2ad459941a | 119 | |
Hotboards | 0:3a2ad459941a | 120 | /* |
Hotboards | 0:3a2ad459941a | 121 | * A convenient constructor for using "the compiler's time": |
Hotboards | 0:3a2ad459941a | 122 | DateTime now (__DATE__, __TIME__); |
Hotboards | 0:3a2ad459941a | 123 | */ |
Hotboards | 0:3a2ad459941a | 124 | DateTime::DateTime( const char* date, const char* time ) |
Hotboards | 0:3a2ad459941a | 125 | { |
Hotboards | 0:3a2ad459941a | 126 | // sample input: date = "Dec 26 2009", time = "12:34:56" |
Hotboards | 0:3a2ad459941a | 127 | yOff = conv2d(date + 9); |
Hotboards | 0:3a2ad459941a | 128 | // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec |
Hotboards | 0:3a2ad459941a | 129 | switch( date[0] ) |
Hotboards | 0:3a2ad459941a | 130 | { |
Hotboards | 0:3a2ad459941a | 131 | case 'J': m = date[1] == 'a' ? 1 : m = date[2] == 'n' ? 6 : 7; break; |
Hotboards | 0:3a2ad459941a | 132 | case 'F': m = 2; break; |
Hotboards | 0:3a2ad459941a | 133 | case 'A': m = date[2] == 'r' ? 4 : 8; break; |
Hotboards | 0:3a2ad459941a | 134 | case 'M': m = date[2] == 'r' ? 3 : 5; break; |
Hotboards | 0:3a2ad459941a | 135 | case 'S': m = 9; break; |
Hotboards | 0:3a2ad459941a | 136 | case 'O': m = 10; break; |
Hotboards | 0:3a2ad459941a | 137 | case 'N': m = 11; break; |
Hotboards | 0:3a2ad459941a | 138 | case 'D': m = 12; break; |
Hotboards | 0:3a2ad459941a | 139 | } |
Hotboards | 0:3a2ad459941a | 140 | d = conv2d( date + 4 ); |
Hotboards | 0:3a2ad459941a | 141 | hh = conv2d( time ); |
Hotboards | 0:3a2ad459941a | 142 | mm = conv2d( time + 3 ); |
Hotboards | 0:3a2ad459941a | 143 | ss = conv2d( time + 6 ); |
Hotboards | 0:3a2ad459941a | 144 | } |
Hotboards | 0:3a2ad459941a | 145 | |
Hotboards | 0:3a2ad459941a | 146 | uint8_t DateTime::dayOfTheWeek( void ) const |
Hotboards | 0:3a2ad459941a | 147 | { |
Hotboards | 0:3a2ad459941a | 148 | uint16_t day = date2days( yOff, m, d ); |
Hotboards | 0:3a2ad459941a | 149 | |
Hotboards | 0:3a2ad459941a | 150 | return ( day + 6 ) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6 |
Hotboards | 0:3a2ad459941a | 151 | } |
Hotboards | 0:3a2ad459941a | 152 | |
Hotboards | 0:3a2ad459941a | 153 | uint32_t DateTime::unixtime( void ) const |
Hotboards | 0:3a2ad459941a | 154 | { |
Hotboards | 0:3a2ad459941a | 155 | uint32_t t; |
Hotboards | 0:3a2ad459941a | 156 | uint16_t days = date2days( yOff, m, d ); |
Hotboards | 0:3a2ad459941a | 157 | |
Hotboards | 0:3a2ad459941a | 158 | t = time2long(days, hh, mm, ss); |
Hotboards | 0:3a2ad459941a | 159 | t += SECONDS_FROM_1970_TO_2000; // seconds from 1970 to 2000 |
Hotboards | 0:3a2ad459941a | 160 | |
Hotboards | 0:3a2ad459941a | 161 | return t; |
Hotboards | 0:3a2ad459941a | 162 | } |
Hotboards | 0:3a2ad459941a | 163 | |
Hotboards | 0:3a2ad459941a | 164 | uint32_t DateTime::secondstime( void ) const |
Hotboards | 0:3a2ad459941a | 165 | { |
Hotboards | 0:3a2ad459941a | 166 | uint32_t t; |
Hotboards | 0:3a2ad459941a | 167 | uint16_t days = date2days( yOff, m, d ); |
Hotboards | 0:3a2ad459941a | 168 | |
Hotboards | 0:3a2ad459941a | 169 | t = time2long( days, hh, mm, ss ); |
Hotboards | 0:3a2ad459941a | 170 | return t; |
Hotboards | 0:3a2ad459941a | 171 | } |
Hotboards | 0:3a2ad459941a | 172 | |
Hotboards | 0:3a2ad459941a | 173 | |
Hotboards | 0:3a2ad459941a | 174 | |
Hotboards | 0:3a2ad459941a | 175 | |
Hotboards | 0:3a2ad459941a | 176 | DateTime DateTime::operator+(const TimeSpan& span) |
Hotboards | 0:3a2ad459941a | 177 | { |
Hotboards | 0:3a2ad459941a | 178 | return DateTime(unixtime()+span.totalseconds()); |
Hotboards | 0:3a2ad459941a | 179 | } |
Hotboards | 0:3a2ad459941a | 180 | |
Hotboards | 0:3a2ad459941a | 181 | DateTime DateTime::operator-(const TimeSpan& span) |
Hotboards | 0:3a2ad459941a | 182 | { |
Hotboards | 0:3a2ad459941a | 183 | return DateTime(unixtime()-span.totalseconds()); |
Hotboards | 0:3a2ad459941a | 184 | } |
Hotboards | 0:3a2ad459941a | 185 | |
Hotboards | 0:3a2ad459941a | 186 | TimeSpan DateTime::operator-(const DateTime& right) |
Hotboards | 0:3a2ad459941a | 187 | { |
Hotboards | 0:3a2ad459941a | 188 | return TimeSpan(unixtime()-right.unixtime()); |
Hotboards | 0:3a2ad459941a | 189 | } |
Hotboards | 0:3a2ad459941a | 190 | |
Hotboards | 0:3a2ad459941a | 191 | |
Hotboards | 0:3a2ad459941a | 192 | |
Hotboards | 0:3a2ad459941a | 193 | TimeSpan::TimeSpan (int32_t seconds): |
Hotboards | 0:3a2ad459941a | 194 | _seconds(seconds) |
Hotboards | 0:3a2ad459941a | 195 | {} |
Hotboards | 0:3a2ad459941a | 196 | |
Hotboards | 0:3a2ad459941a | 197 | TimeSpan::TimeSpan (int16_t days, int8_t hours, int8_t minutes, int8_t seconds): |
Hotboards | 0:3a2ad459941a | 198 | _seconds((int32_t)days*86400L + (int32_t)hours*3600 + (int32_t)minutes*60 + seconds) |
Hotboards | 0:3a2ad459941a | 199 | {} |
Hotboards | 0:3a2ad459941a | 200 | |
Hotboards | 0:3a2ad459941a | 201 | TimeSpan::TimeSpan (const TimeSpan& copy): |
Hotboards | 0:3a2ad459941a | 202 | _seconds(copy._seconds) |
Hotboards | 0:3a2ad459941a | 203 | {} |
Hotboards | 0:3a2ad459941a | 204 | |
Hotboards | 0:3a2ad459941a | 205 | TimeSpan TimeSpan::operator+(const TimeSpan& right) |
Hotboards | 0:3a2ad459941a | 206 | { |
Hotboards | 0:3a2ad459941a | 207 | return TimeSpan(_seconds+right._seconds); |
Hotboards | 0:3a2ad459941a | 208 | } |
Hotboards | 0:3a2ad459941a | 209 | |
Hotboards | 0:3a2ad459941a | 210 | TimeSpan TimeSpan::operator-(const TimeSpan& right) |
Hotboards | 0:3a2ad459941a | 211 | { |
Hotboards | 0:3a2ad459941a | 212 | return TimeSpan(_seconds-right._seconds); |
Hotboards | 0:3a2ad459941a | 213 | } |
Hotboards | 0:3a2ad459941a | 214 | |
Hotboards | 0:3a2ad459941a | 215 | |
Hotboards | 0:3a2ad459941a | 216 | |
Hotboards | 0:3a2ad459941a | 217 | Hotboards_rtcc::Hotboards_rtcc( I2C &i2c ) |
Hotboards | 0:3a2ad459941a | 218 | : _i2c(i2c) |
Hotboards | 0:3a2ad459941a | 219 | { |
Hotboards | 0:3a2ad459941a | 220 | on_off = 0; |
Hotboards | 0:3a2ad459941a | 221 | } |
Hotboards | 0:3a2ad459941a | 222 | /* |
Hotboards | 0:3a2ad459941a | 223 | * enable internal oscilator if this is disable (start the clock) |
Hotboards | 0:3a2ad459941a | 224 | */ |
Hotboards | 0:3a2ad459941a | 225 | void Hotboards_rtcc::begin( void ) |
Hotboards | 0:3a2ad459941a | 226 | { |
Hotboards | 0:3a2ad459941a | 227 | if( isrunning( ) == 0 ) |
Hotboards | 0:3a2ad459941a | 228 | { |
Hotboards | 0:3a2ad459941a | 229 | writeReg( RTC_STARTADDR, 0x80 ); |
Hotboards | 0:3a2ad459941a | 230 | } |
Hotboards | 0:3a2ad459941a | 231 | } |
Hotboards | 0:3a2ad459941a | 232 | |
Hotboards | 0:3a2ad459941a | 233 | /* |
Hotboards | 0:3a2ad459941a | 234 | * set a new time and date |
Hotboards | 0:3a2ad459941a | 235 | */ |
Hotboards | 0:3a2ad459941a | 236 | void Hotboards_rtcc::adjust( const DateTime &dt ) |
Hotboards | 0:3a2ad459941a | 237 | { |
Hotboards | 0:3a2ad459941a | 238 | char buffer[8]; |
Hotboards | 0:3a2ad459941a | 239 | |
Hotboards | 0:3a2ad459941a | 240 | buffer[0] = RTC_STARTADDR; |
Hotboards | 0:3a2ad459941a | 241 | buffer[1] = bin2bcd(dt.second()) | 0x80; |
Hotboards | 0:3a2ad459941a | 242 | buffer[2] = bin2bcd(dt.minute()); |
Hotboards | 0:3a2ad459941a | 243 | buffer[3] = bin2bcd(dt.hour()); |
Hotboards | 0:3a2ad459941a | 244 | buffer[4] = bin2bcd(dt.dayOfTheWeek()) | on_off; |
Hotboards | 0:3a2ad459941a | 245 | buffer[5] = bin2bcd(dt.day()); |
Hotboards | 0:3a2ad459941a | 246 | buffer[6] = bin2bcd(dt.month()); |
Hotboards | 0:3a2ad459941a | 247 | buffer[7] = bin2bcd(dt.year() - 2000); |
Hotboards | 0:3a2ad459941a | 248 | |
Hotboards | 0:3a2ad459941a | 249 | stop(); |
Hotboards | 0:3a2ad459941a | 250 | _i2c.write( RTC_ADDR, buffer, 8 ); |
Hotboards | 0:3a2ad459941a | 251 | } |
Hotboards | 0:3a2ad459941a | 252 | |
Hotboards | 0:3a2ad459941a | 253 | /* |
Hotboards | 0:3a2ad459941a | 254 | * return an DateTime object with the actual time and date |
Hotboards | 0:3a2ad459941a | 255 | */ |
Hotboards | 0:3a2ad459941a | 256 | DateTime Hotboards_rtcc::now( void ) |
Hotboards | 0:3a2ad459941a | 257 | { |
Hotboards | 0:3a2ad459941a | 258 | char buffer[7]; |
Hotboards | 0:3a2ad459941a | 259 | |
Hotboards | 0:3a2ad459941a | 260 | buffer[0] = RTC_STARTADDR; |
Hotboards | 0:3a2ad459941a | 261 | _i2c.write(RTC_ADDR, buffer, 1, true); |
Hotboards | 0:3a2ad459941a | 262 | _i2c.read(RTC_ADDR, buffer, 7, false); |
Hotboards | 0:3a2ad459941a | 263 | |
Hotboards | 0:3a2ad459941a | 264 | uint8_t ss = bcd2bin( buffer[0] & 0x7F ); |
Hotboards | 0:3a2ad459941a | 265 | uint8_t mm = bcd2bin( buffer[1] ); |
Hotboards | 0:3a2ad459941a | 266 | uint8_t hh = bcd2bin( buffer[2] ); |
Hotboards | 0:3a2ad459941a | 267 | uint8_t dw = buffer[3] & 0x07; |
Hotboards | 0:3a2ad459941a | 268 | uint8_t d = bcd2bin( buffer[4] ); |
Hotboards | 0:3a2ad459941a | 269 | uint8_t m = bcd2bin( buffer[5] & 0xDF ); |
Hotboards | 0:3a2ad459941a | 270 | uint16_t y = bcd2bin( buffer[6] ) + 2000; |
Hotboards | 0:3a2ad459941a | 271 | |
Hotboards | 0:3a2ad459941a | 272 | return DateTime( y, m, d, hh, mm, ss, dw ); |
Hotboards | 0:3a2ad459941a | 273 | } |
Hotboards | 0:3a2ad459941a | 274 | |
Hotboards | 0:3a2ad459941a | 275 | /* |
Hotboards | 0:3a2ad459941a | 276 | * return a true if the rtcc is running |
Hotboards | 0:3a2ad459941a | 277 | */ |
Hotboards | 0:3a2ad459941a | 278 | uint8_t Hotboards_rtcc::isrunning( void ) |
Hotboards | 0:3a2ad459941a | 279 | { |
Hotboards | 0:3a2ad459941a | 280 | uint8_t running = readReg( RTC_STARTADDR + 3 ) & 0x20; |
Hotboards | 0:3a2ad459941a | 281 | return running >> 5; |
Hotboards | 0:3a2ad459941a | 282 | } |
Hotboards | 0:3a2ad459941a | 283 | |
Hotboards | 0:3a2ad459941a | 284 | /* |
Hotboards | 0:3a2ad459941a | 285 | * stop the internal rtcc clock |
Hotboards | 0:3a2ad459941a | 286 | */ |
Hotboards | 0:3a2ad459941a | 287 | void Hotboards_rtcc::stop( void ) |
Hotboards | 0:3a2ad459941a | 288 | { |
Hotboards | 0:3a2ad459941a | 289 | writeReg( RTC_STARTADDR, 0x00 ); |
Hotboards | 0:3a2ad459941a | 290 | while( isrunning( ) == 1 ); |
Hotboards | 0:3a2ad459941a | 291 | } |
Hotboards | 0:3a2ad459941a | 292 | |
Hotboards | 0:3a2ad459941a | 293 | void Hotboards_rtcc::setVBAT( uint8_t OnOff ) |
Hotboards | 0:3a2ad459941a | 294 | { |
Hotboards | 0:3a2ad459941a | 295 | on_off = ( OnOff & 0x01 ) << 3; |
Hotboards | 0:3a2ad459941a | 296 | } |
Hotboards | 0:3a2ad459941a | 297 | |
Hotboards | 0:3a2ad459941a | 298 | void Hotboards_rtcc::setAlarm( const DateTime &dt, uint8_t alarm ) |
Hotboards | 0:3a2ad459941a | 299 | { |
Hotboards | 0:3a2ad459941a | 300 | char buffer[8]; |
Hotboards | 0:3a2ad459941a | 301 | |
Hotboards | 0:3a2ad459941a | 302 | buffer[0] = ALARM_STARTADDR; |
Hotboards | 0:3a2ad459941a | 303 | buffer[1] = bin2bcd(dt.second()) | 0x80; |
Hotboards | 0:3a2ad459941a | 304 | buffer[2] = bin2bcd(dt.minute()); |
Hotboards | 0:3a2ad459941a | 305 | buffer[3] = bin2bcd(dt.hour()); |
Hotboards | 0:3a2ad459941a | 306 | buffer[4] = bin2bcd(dt.dayOfTheWeek()) | on_off; |
Hotboards | 0:3a2ad459941a | 307 | buffer[5] = bin2bcd(dt.day()); |
Hotboards | 0:3a2ad459941a | 308 | buffer[6] = bin2bcd(dt.month()); |
Hotboards | 0:3a2ad459941a | 309 | buffer[7] = bin2bcd(dt.year() - 2000); |
Hotboards | 0:3a2ad459941a | 310 | |
Hotboards | 0:3a2ad459941a | 311 | _i2c.write( RTC_ADDR, buffer, 8 ); |
Hotboards | 0:3a2ad459941a | 312 | } |
Hotboards | 0:3a2ad459941a | 313 | |
Hotboards | 0:3a2ad459941a | 314 | uint8_t Hotboards_rtcc::getAlarmStatus( uint8_t alarm ) |
Hotboards | 0:3a2ad459941a | 315 | { |
Hotboards | 0:3a2ad459941a | 316 | uint8_t status = readReg( ALARM_STARTADDR + 3 ); |
Hotboards | 0:3a2ad459941a | 317 | return ( status >> 3 ) & 0x01; |
Hotboards | 0:3a2ad459941a | 318 | } |
Hotboards | 0:3a2ad459941a | 319 | |
Hotboards | 0:3a2ad459941a | 320 | void Hotboards_rtcc::clearAlarm( uint8_t alarm ) |
Hotboards | 0:3a2ad459941a | 321 | { |
Hotboards | 0:3a2ad459941a | 322 | uint8_t status = readReg( ALARM_STARTADDR + 3 ); |
Hotboards | 0:3a2ad459941a | 323 | writeReg( ALARM_STARTADDR + 3, (status & 0xF7) ); |
Hotboards | 0:3a2ad459941a | 324 | } |
Hotboards | 0:3a2ad459941a | 325 | |
Hotboards | 0:3a2ad459941a | 326 | void Hotboards_rtcc::turnOnAlarm( uint8_t alarm ) |
Hotboards | 0:3a2ad459941a | 327 | { |
Hotboards | 0:3a2ad459941a | 328 | uint8_t ctrl = readReg( CTRL_STARTADDR ); |
Hotboards | 0:3a2ad459941a | 329 | writeReg( CTRL_STARTADDR, ctrl | 0x10 ); |
Hotboards | 0:3a2ad459941a | 330 | } |
Hotboards | 0:3a2ad459941a | 331 | |
Hotboards | 0:3a2ad459941a | 332 | void Hotboards_rtcc::turnOffAlarm( uint8_t alarm ) |
Hotboards | 0:3a2ad459941a | 333 | { |
Hotboards | 0:3a2ad459941a | 334 | uint8_t ctrl = readReg( CTRL_STARTADDR ); |
Hotboards | 0:3a2ad459941a | 335 | writeReg( CTRL_STARTADDR, ctrl & 0xEF ); |
Hotboards | 0:3a2ad459941a | 336 | } |
Hotboards | 0:3a2ad459941a | 337 | |
Hotboards | 0:3a2ad459941a | 338 | uint8_t Hotboards_rtcc::readReg( uint8_t address ) |
Hotboards | 0:3a2ad459941a | 339 | { |
Hotboards | 0:3a2ad459941a | 340 | char buffer[1]; |
Hotboards | 0:3a2ad459941a | 341 | |
Hotboards | 0:3a2ad459941a | 342 | buffer[0] = address; |
Hotboards | 0:3a2ad459941a | 343 | _i2c.write(RTC_ADDR, buffer, 1, true); |
Hotboards | 0:3a2ad459941a | 344 | _i2c.read(RTC_ADDR, buffer, 1, false); |
Hotboards | 0:3a2ad459941a | 345 | |
Hotboards | 0:3a2ad459941a | 346 | return buffer[0]; |
Hotboards | 0:3a2ad459941a | 347 | } |
Hotboards | 0:3a2ad459941a | 348 | |
Hotboards | 0:3a2ad459941a | 349 | void Hotboards_rtcc::writeReg( uint8_t address, uint8_t val ) |
Hotboards | 0:3a2ad459941a | 350 | { |
Hotboards | 0:3a2ad459941a | 351 | char buffer[2]; |
Hotboards | 0:3a2ad459941a | 352 | |
Hotboards | 0:3a2ad459941a | 353 | buffer[0] = address; |
Hotboards | 0:3a2ad459941a | 354 | buffer[1] = val; |
Hotboards | 0:3a2ad459941a | 355 | |
Hotboards | 0:3a2ad459941a | 356 | _i2c.write( RTC_ADDR, buffer, 2 ); |
Hotboards | 0:3a2ad459941a | 357 | } |
Hotboards | 0:3a2ad459941a | 358 | |
Hotboards | 0:3a2ad459941a | 359 | uint8_t Hotboards_rtcc::bcd2bin( uint8_t val ) |
Hotboards | 0:3a2ad459941a | 360 | { |
Hotboards | 0:3a2ad459941a | 361 | return val - 6 * ( val >> 4 ); |
Hotboards | 0:3a2ad459941a | 362 | } |
Hotboards | 0:3a2ad459941a | 363 | |
Hotboards | 0:3a2ad459941a | 364 | uint8_t Hotboards_rtcc::bin2bcd( uint8_t val ) |
Hotboards | 0:3a2ad459941a | 365 | { |
Hotboards | 0:3a2ad459941a | 366 | return val + 6 * ( val / 10 ); |
Hotboards | 0:3a2ad459941a | 367 | |
Hotboards | 0:3a2ad459941a | 368 | } |