make year of the century

This commit is contained in:
King Kévin 2016-05-05 23:25:55 +02:00
parent bbdfe97cc6
commit 79bb509c83
2 changed files with 16 additions and 17 deletions

View File

@ -213,18 +213,17 @@ uint8_t rtc_ds1307_read_month(void)
return to_return; return to_return;
} }
uint16_t rtc_ds1307_read_year(void) uint8_t rtc_ds1307_read_year(void)
{ {
uint16_t to_return = 2000; // year to return
uint8_t data[1] = {0}; // to read data over I2C uint8_t data[1] = {0}; // to read data over I2C
rtc_ds1307_read_memory(6, data, LENGTH(data)); // read a single byte containing year value rtc_ds1307_read_memory(6, data, LENGTH(data)); // read a single byte containing year value
to_return += ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year uint8_t to_return = ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year
return to_return; return to_return;
} }
uint16_t* rtc_ds1307_read_time(void) uint8_t* rtc_ds1307_read_time(void)
{ {
static uint16_t time[7] = {0}; // store time {seconds, minutes, hours, day, date, month, year} static uint8_t time[7] = {0}; // store time {seconds, minutes, hours, day, date, month, year}
uint8_t data[7] = {0}; // to read data over I2C uint8_t data[7] = {0}; // to read data over I2C
rtc_ds1307_read_memory(0, data, LENGTH(data)); // read all time bytes rtc_ds1307_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[0] = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert seconds from BCD
@ -242,7 +241,7 @@ uint16_t* rtc_ds1307_read_time(void)
time[3] = (data[3]&0x07); // convert BCD coding into days 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[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[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 time[6] = ((data[6]&0xf0)>>4)*10+(data[6]&0x0f); // convert BCD coding into year
return time; return time;
} }
@ -388,9 +387,9 @@ bool rtc_ds1307_write_month(uint8_t month)
return rtc_ds1307_write_memory(5, data, LENGTH(data)); // write time value on RTC return rtc_ds1307_write_memory(5, data, LENGTH(data)); // write time value on RTC
} }
bool rtc_ds1307_write_year(uint16_t year) bool rtc_ds1307_write_year(uint8_t year)
{ {
if (year<2000 || year>2099) { if (year>99) {
return false; return false;
} }
uint8_t data[1] = {0}; // to write time value uint8_t data[1] = {0}; // to write time value
@ -398,7 +397,7 @@ bool rtc_ds1307_write_year(uint16_t year)
return rtc_ds1307_write_memory(6, data, LENGTH(data)); // write time value on RTC return rtc_ds1307_write_memory(6, data, LENGTH(data)); // write time value on RTC
} }
bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint16_t year) bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint8_t year)
{ {
uint8_t data[7] = {0}; // to write all time values uint8_t data[7] = {0}; // to write all time values
// seconds // seconds
@ -436,7 +435,7 @@ bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint
} }
data[5] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format data[5] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
// year // year
if (year<2000 || year>2099) { if (year>99) {
return false; return false;
} }
data[6] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format data[6] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format

View File

@ -94,13 +94,13 @@ uint8_t rtc_ds1307_read_date(void);
*/ */
uint8_t rtc_ds1307_read_month(void); uint8_t rtc_ds1307_read_month(void);
/** @brief read year from RTC IC /** @brief read year from RTC IC
* @return year (2000-2099) of the current time * @return year of the century (00-99) of the current time
*/ */
uint16_t rtc_ds1307_read_year(void); uint8_t rtc_ds1307_read_year(void);
/** @brief read time from RTC IC /** @brief read time from RTC IC
* @return array of {seconds, minutes, hours, day, date, month, year} as defined above * @return array of {seconds, minutes, hours, day, date, month, year} as defined above
*/ */
uint16_t* rtc_ds1307_read_time(void); uint8_t* rtc_ds1307_read_time(void);
/** @brief disable RTC IC oscillator /** @brief disable RTC IC oscillator
* @return if disabling oscillator succeeded * @return if disabling oscillator succeeded
*/ */
@ -145,10 +145,10 @@ bool rtc_ds1307_write_date(uint8_t date);
*/ */
bool rtc_ds1307_write_month(uint8_t month); bool rtc_ds1307_write_month(uint8_t month);
/** @brief write year into RTC IC /** @brief write year into RTC IC
* @param[in] year year (2000-2099) * @param[in] year year of the century (00-99)
* @return if write succeeded * @return if write succeeded
*/ */
bool rtc_ds1307_write_year(uint16_t year); bool rtc_ds1307_write_year(uint8_t year);
/** @brief write time into RTC IC /** @brief write time into RTC IC
* @param[in] seconds number of seconds (0-59) * @param[in] seconds number of seconds (0-59)
* @param[in] minutes number of minutes (0-59) * @param[in] minutes number of minutes (0-59)
@ -156,7 +156,7 @@ bool rtc_ds1307_write_year(uint16_t year);
* @param[in] day day of the week (1-7, 1 is Sunday) * @param[in] day day of the week (1-7, 1 is Sunday)
* @param[in] date day of the month (1-31) * @param[in] date day of the month (1-31)
* @param[in] month month of the year (1-12) * @param[in] month month of the year (1-12)
* @param[in] year year (2000-2099) * @param[in] year year of the century (00-99)
* @return if write succeeded * @return if write succeeded
*/ */
bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint16_t year); bool rtc_ds1307_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint8_t year);