esp-open-rtos/examples/ds1307/main.c

52 lines
1.2 KiB
C
Raw Normal View History

2016-10-09 09:14:17 +00:00
/*
* Example of using DS1307 RTC driver
*
* Part of esp-open-rtos
* Copyright (C) 2016 Ruslan V. Uss <unclerus@gmail.com>
* BSD Licensed as described in the file LICENSE
*/
#include <esp/uart.h>
#include <espressif/esp_common.h>
#include <i2c/i2c.h>
#include <ds1307/ds1307.h>
#include <stdio.h>
2017-09-01 09:29:32 +00:00
#define I2C_BUS 0
2016-10-09 09:14:17 +00:00
#define SCL_PIN 5
#define SDA_PIN 4
2016-11-01 09:40:19 +00:00
void user_init(void)
2016-10-09 09:14:17 +00:00
{
2016-11-01 09:40:19 +00:00
uart_set_baud(0, 115200);
printf("SDK version:%s\n", sdk_system_get_sdk_version());
2016-10-09 09:14:17 +00:00
2017-09-01 09:29:32 +00:00
i2c_init(I2C_BUS, SCL_PIN, SDA_PIN, I2C_FREQ_400K);
i2c_dev_t dev = {
.addr = DS1307_ADDR,
.bus = I2C_BUS,
};
ds1307_start(&dev, true);
2016-10-09 09:14:17 +00:00
// setup datetime: 2016-10-09 13:50:10
struct tm time = {
.tm_year = 2016,
2016-11-01 09:40:19 +00:00
.tm_mon = 9, // 0-based
2016-10-09 09:14:17 +00:00
.tm_mday = 9,
.tm_hour = 13,
2016-11-01 09:40:19 +00:00
.tm_min = 50,
.tm_sec = 10
2016-10-09 09:14:17 +00:00
};
2017-09-01 09:29:32 +00:00
ds1307_set_time(&dev, &time);
2016-10-09 09:14:17 +00:00
while (true)
{
2017-09-01 09:29:32 +00:00
ds1307_get_time(&dev, &time);
2016-10-09 09:14:17 +00:00
2016-11-01 09:40:19 +00:00
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);
2016-10-09 09:14:17 +00:00
2016-11-01 09:40:19 +00:00
for (uint32_t i = 0; i < 1000; i++)
sdk_os_delay_us(500);
2016-10-09 09:14:17 +00:00
}
}