From dd2c35a95615250a0b0fade42383c102c8fbb7fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Wed, 23 Mar 2016 08:09:52 +0100 Subject: [PATCH] add rtc_read_time to read time at once --- lib/rtc_ds1307.c | 31 ++++++++++++++++++++++++++++--- lib/rtc_ds1307.h | 8 ++++++-- main.c | 4 +++- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/lib/rtc_ds1307.c b/lib/rtc_ds1307.c index 83ac4b4..3672f65 100644 --- a/lib/rtc_ds1307.c +++ b/lib/rtc_ds1307.c @@ -182,11 +182,36 @@ uint8_t rtc_read_month(void) return to_return; } -uint8_t rtc_read_year(void) +uint16_t rtc_read_year(void) { - uint8_t to_return = 0; // seconds to return + uint16_t to_return = 2000; // year to return uint8_t data[1] = {0}; // to read data from I2C rtc_read_memory(6, data, LENGTH(data)); // read a single byte to test - to_return = ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year + to_return += ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year return to_return; } + +uint16_t* rtc_read_time(void) +{ + static uint16_t time[7] = {0}; // store time {seconds, minutes, hours, day, date, month, year} + uint8_t data[7] = {0}; // to read data from I2C + rtc_read_memory(0, data, LENGTH(data)); // read all time bytes + time[0] = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert seconds from BCD + time[1] = (data[1]>>4)*10+(data[1]&0x0f); // convert minutes from BCD + time[2] = 0; // re-initialize hours + if (data[2]&0x40) { // 12 hour mode + if (data[2]&0x02) { // PM + time[2] += 12; // add the 12 hours + } + time[2] += ((data[2]&0x10)>>4)*10; // convert BCD coding into hours (first digit) + } else { + time[2] = ((data[2]&0x30)>>4)*10; // convert BCD coding into hours (first digit) + } + time[2] += (data[2]&0x0f); // convert BCD coding into hours (second digit) + time[3] = (data[3]&0x07); // convert BCD coding into days + time[4] = ((data[4]&0x30)>>4)*10+(data[4]&0x0f); // convert BCD coding into date + time[5] = ((data[5]&0x10)>>4)*10+(data[5]&0x0f); // convert BCD coding into month + time[6] = 2000+((data[6]&0xf0)>>4)*10+(data[6]&0x0f); // convert BCD coding into year + return time; +} + diff --git a/lib/rtc_ds1307.h b/lib/rtc_ds1307.h index 7a39b35..003dfb1 100644 --- a/lib/rtc_ds1307.h +++ b/lib/rtc_ds1307.h @@ -49,7 +49,11 @@ uint8_t rtc_read_date(void); * @return the month of the current time */ uint8_t rtc_read_month(void); -/** @brief read year (00-99) from RTC IC +/** @brief read year (2000-2099) from RTC IC * @return the year of the current time */ -uint8_t rtc_read_year(void); +uint16_t rtc_read_year(void); +/** @brief read time from RTC IC + * @return array of {seconds, minutes, hours, day, date, month, year} as defined above + */ +uint16_t* rtc_read_time(void); diff --git a/main.c b/main.c index 2c141fc..c384055 100644 --- a/main.c +++ b/main.c @@ -277,7 +277,9 @@ int main(void) } printf("it is now %02lu:%02lu:%02lu\n", time/ticks_hour, (time%ticks_hour)/ticks_minute, (time%ticks_minute)/ticks_second); - printf("RTC time 20%02d-%02d-%02d %02d:%02d:%02d\n", rtc_read_year(), rtc_read_month(), rtc_read_date(), rtc_read_hours(), rtc_read_minutes(), rtc_read_seconds()); // print time + printf("RTC time %04d-%02d-%02d %02d:%02d:%02d\n", rtc_read_year(), rtc_read_month(), rtc_read_date(), rtc_read_hours(), rtc_read_minutes(), rtc_read_seconds()); // print time + uint16_t* rtc_time = rtc_read_time(); + printf("RTC time %04d-%02d-%02d %02d:%02d:%02d\n", rtc_time[6], rtc_time[5], rtc_time[4], rtc_time[2], rtc_time[1], rtc_time[0]); printf("input commands\n"); bool action = false; // if an action has been performed don't go to sleep