Diff: RTC8564.cpp
- Revision:
- 0:ed1f619f1794
diff -r 000000000000 -r ed1f619f1794 RTC8564.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RTC8564.cpp Wed Jun 20 04:09:39 2012 +0000 @@ -0,0 +1,61 @@ +#include "mbed.h" +#include "RTC8564.h" + +void RTC8564::write(char address, char value) +{ + i2c.start(); + i2c.write(RTC8564NB_ADR); + i2c.write(address); + i2c.write(value); + i2c.stop(); +} + +char RTC8564::read(char address) +{ + char value; + i2c.start(); + i2c.write(RTC8564NB_ADR); + i2c.write(address); + i2c.start(); + i2c.write(RTC8564NB_ADR | _READ); + value = i2c.read(0); + i2c.stop(); + + return value; +} + +void RTC8564::time_just() +{ + char _min, _hour; + _min = read(MINUTES); + if (_min >= 0x30) { + _hour = read(HOURS); + if (_hour == 0x23) + _hour = 0x00; + else if ((_hour & 0x0F) == 0x09) + _hour = (_hour & 0xF0) + 0x10; + else + _hour = _hour + 0x01; + write(HOURS, _hour); + } + write(MINUTES, 0x00); + write(SECONDS, 0x00); +} + + +void RTC8564::setdaytime(char y[3], char m[3], char d[3], char h[3], char min[3], char s[3], char week_val[2]) +{ + write(CONTROL1, 0x20); //stop + write(CONTROL2, 0x00); + write(YEARS, ((y[0]-0x30)<<4)+(y[1]-0x30)); + write(MONTHS, ((m[0]-0x30)<<4)+(m[1]-0x30)); + write(DAYS, ((d[0]-0x30)<<4)+(d[1]-0x30)); + write(HOURS, ((h[0]-0x30)<<4)+(h[1]-0x30)); + write(MINUTES, ((min[0]-0x30)<<4)+(min[1]-0x30)); + write(SECONDS, ((s[0]-0x30)<<4)+(s[1]-0x30)); + write(WEEKDAYS, week_val[0] - 0x30); //Sunday = "0", Monday = "1" ... + write(CLOCKOUT_FREQ, 0x00); // 0x83 = TE on & 1Hz + write(TIMER_CONTROL, 0x00); + write(CONTROL1, 0x00); //start + return; +} \ No newline at end of file