add rtc_read_time to read time at once

This commit is contained in:
King Kévin 2016-03-23 08:09:52 +01:00
parent d00c20ffb0
commit dd2c35a956
3 changed files with 37 additions and 6 deletions

View File

@ -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;
}

View File

@ -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);

4
main.c
View File

@ -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