RTC drivers fix (#259)

This commit is contained in:
Ruslan V. Uss 2016-11-01 15:40:19 +06:00 committed by sheinz
parent 8ef476c71f
commit 98de5e573a
3 changed files with 17 additions and 16 deletions

View file

@ -14,32 +14,33 @@
#define SCL_PIN 5
#define SDA_PIN 4
void user_init (void)
void user_init(void)
{
uart_set_baud (0, 115200);
printf ("SDK version:%s\n", sdk_system_get_sdk_version ());
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
i2c_init (SCL_PIN, SDA_PIN);
ds1307_start (true);
i2c_init(SCL_PIN, SDA_PIN);
ds1307_start(true);
// setup datetime: 2016-10-09 13:50:10
struct tm time = {
.tm_year = 2016,
.tm_mon = 10,
.tm_mon = 9, // 0-based
.tm_mday = 9,
.tm_hour = 13,
.tm_min = 50,
.tm_sec = 10
.tm_min = 50,
.tm_sec = 10
};
ds1307_set_time (&time);
ds1307_set_time(&time);
while (true)
{
ds1307_get_time (&time);
ds1307_get_time(&time);
printf ("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
printf("%04d-%02d-%02d %02d:%02d:%02d\n", time.tm_year, time.tm_mon + 1,
time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
for (uint32_t i = 0; i < 1000; i ++)
sdk_os_delay_us (500);
for (uint32_t i = 0; i < 1000; i++)
sdk_os_delay_us(500);
}
}

View file

@ -78,14 +78,14 @@ void ds1307_get_time(struct tm *time)
if (buf[2] & HOUR12_BIT)
{
// RTC in 12-hour mode
time->tm_hour = bcd2dec(buf[2] & HOUR12_MASK);
time->tm_hour = bcd2dec(buf[2] & HOUR12_MASK) - 1;
if (buf[2] & PM_BIT)
time->tm_hour += 12;
}
else time->tm_hour = bcd2dec(buf[2] & HOUR24_MASK);
time->tm_wday = bcd2dec(buf[3]) - 1;
time->tm_mday = bcd2dec(buf[4]);
time->tm_mon = bcd2dec(buf[5]);
time->tm_mon = bcd2dec(buf[5]) - 1;
time->tm_year = bcd2dec(buf[6]) + 2000;
}

View file

@ -270,7 +270,7 @@ bool ds3231_getTime(struct tm *time)
time->tm_min = bcdToDec(data[1]);
if (data[2] & DS3231_12HOUR_FLAG) {
/* 12H */
time->tm_hour = bcdToDec(data[2] & DS3231_12HOUR_MASK);
time->tm_hour = bcdToDec(data[2] & DS3231_12HOUR_MASK) - 1;
/* AM/PM? */
if (data[2] & DS3231_PM_FLAG) time->tm_hour += 12;
} else {