add RTC write time functions
This commit is contained in:
parent
721b56e657
commit
6a64bc876c
128
lib/rtc_ds1307.c
128
lib/rtc_ds1307.c
|
@ -259,27 +259,139 @@ error:
|
|||
return to_return;
|
||||
}
|
||||
|
||||
void rtc_oscillator_disable(void)
|
||||
bool rtc_oscillator_disable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
|
||||
data[0] |= 0x80; // set CH to disable oscillator
|
||||
rtc_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
|
||||
return rtc_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
void rtc_oscillator_enable(void)
|
||||
bool rtc_oscillator_enable(void)
|
||||
{
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
|
||||
data[0] &= 0x7f; // clear CH to enable oscillator
|
||||
rtc_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
|
||||
return rtc_write_memory(0, data, LENGTH(data)); // write current seconds with CH value
|
||||
}
|
||||
|
||||
void rtc_write_seconds(uint8_t seconds)
|
||||
bool rtc_write_seconds(uint8_t seconds)
|
||||
{
|
||||
if (seconds>59) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to read CH value data from I2C
|
||||
rtc_read_memory(0, data, LENGTH(data)); // read seconds with CH value
|
||||
if (!rtc_read_memory(0, data, LENGTH(data))) { // read seconds with CH value
|
||||
return false;
|
||||
}
|
||||
data[0] &= 0x80; // only keep CH flag
|
||||
data[0] |= (((seconds/10)%10)<<4)+(seconds%10); // encode seconds in BCD format
|
||||
rtc_write_memory(0, data, LENGTH(data)); // write current seconds with previous CH value
|
||||
data[0] |= (((seconds/10)%6)<<4)+(seconds%10); // encode seconds in BCD format
|
||||
return rtc_write_memory(0, data, LENGTH(data)); // write current seconds with previous CH value
|
||||
}
|
||||
|
||||
bool rtc_write_minutes(uint8_t minutes)
|
||||
{
|
||||
if (minutes>59) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((minutes/10)%6)<<4)+(minutes%10); // encode minutes in BCD format
|
||||
return rtc_write_memory(1, data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_write_hours(uint8_t hours)
|
||||
{
|
||||
if (hours>24) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((hours/10)%3)<<4)+(hours%10); // encode hours in BCD 24h format
|
||||
return rtc_write_memory(2, data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_write_day(uint8_t day)
|
||||
{
|
||||
if (day<1 || day>7) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (day%8); // encode day in BCD format
|
||||
return rtc_write_memory(3, data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_write_date(uint8_t date)
|
||||
{
|
||||
if (date<1 || date>31) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((date/10)%4)<<4)+(date%10); // encode date in BCD format
|
||||
return rtc_write_memory(4, data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_write_month(uint8_t month)
|
||||
{
|
||||
if (month<1 || month>12) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
|
||||
return rtc_write_memory(5, data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_write_year(uint16_t year)
|
||||
{
|
||||
if (year<2000 || year>2099) {
|
||||
return false;
|
||||
}
|
||||
uint8_t data[1] = {0}; // to write time value
|
||||
data[0] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
|
||||
return rtc_write_memory(6, data, LENGTH(data)); // write time value on RTC
|
||||
}
|
||||
|
||||
bool rtc_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint16_t year)
|
||||
{
|
||||
uint8_t data[7] = {0}; // to write all time values
|
||||
// seconds
|
||||
if (seconds>59) {
|
||||
return false;
|
||||
}
|
||||
if (!rtc_read_memory(0, data, 1)) { // read seconds with CH value
|
||||
return false;
|
||||
}
|
||||
data[0] &= 0x80; // only keep CH flag
|
||||
data[0] |= (((seconds/10)%6)<<4)+(seconds%10); // encode seconds in BCD format
|
||||
// minutes
|
||||
if (minutes>59) {
|
||||
return false;
|
||||
}
|
||||
data[1] = (((minutes/10)%6)<<4)+(minutes%10); // encode minutes in BCD format
|
||||
// hours
|
||||
if (hours>24) {
|
||||
return false;
|
||||
}
|
||||
data[2] = (((hours/10)%3)<<4)+(hours%10); // encode hours in BCD 24h format
|
||||
// day
|
||||
if (day<1 || day>7) {
|
||||
return false;
|
||||
}
|
||||
data[3] = (day%8); // encode day in BCD format
|
||||
// date
|
||||
if (date<1 || date>31) {
|
||||
return false;
|
||||
}
|
||||
data[4] = (((date/10)%4)<<4)+(date%10); // encode date in BCD format
|
||||
// month
|
||||
if (month<1 || month>12) {
|
||||
return false;
|
||||
}
|
||||
data[5] = (((month/10)%2)<<4)+(month%10); // encode month in BCD format
|
||||
// year
|
||||
if (year<2000 || year>2099) {
|
||||
return false;
|
||||
}
|
||||
data[6] = (((year/10)%10)<<4)+(year%10); // encode year in BCD format
|
||||
|
||||
return rtc_write_memory(0, data, LENGTH(data)); // write time values on RTC
|
||||
}
|
||||
|
||||
|
|
|
@ -30,31 +30,31 @@ void rtc_setup(void);
|
|||
*/
|
||||
bool rtc_oscillator_disabled(void);
|
||||
/** @brief read seconds from RTC IC
|
||||
* @return the number of seconds (0-59) of the current time
|
||||
* @return number of seconds (0-59) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_seconds(void);
|
||||
/** @brief read minutes from RTC IC
|
||||
* @return the number of minutes (0-59) of the current time
|
||||
* @return number of minutes (0-59) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_minutes(void);
|
||||
/** @brief read hours from RTC IC
|
||||
* @return the number of hours (0-23) of the current time
|
||||
* @return number of hours (0-23) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_hours(void);
|
||||
/** @brief read day from RTC IC
|
||||
* @return the day (1-7) of the current time, 1 being Sunday
|
||||
* @return day (1-7) of the current time, 1 being Sunday
|
||||
*/
|
||||
uint8_t rtc_read_day(void);
|
||||
/** @brief read date from RTC IC
|
||||
* @return the date (day of the month, 1-31) of the current time
|
||||
* @return date (day of the month, 1-31) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_date(void);
|
||||
/** @brief read month from RTC IC
|
||||
* @return the month (1-12) of the current time
|
||||
* @return month (1-12) of the current time
|
||||
*/
|
||||
uint8_t rtc_read_month(void);
|
||||
/** @brief read year from RTC IC
|
||||
* @return the year (2000-2099) of the current time
|
||||
* @return year (2000-2099) of the current time
|
||||
*/
|
||||
uint16_t rtc_read_year(void);
|
||||
/** @brief read time from RTC IC
|
||||
|
@ -62,13 +62,56 @@ uint16_t rtc_read_year(void);
|
|||
*/
|
||||
uint16_t* rtc_read_time(void);
|
||||
/** @brief disable RTC IC oscillator
|
||||
* @return if disabling oscillator succeeded
|
||||
*/
|
||||
void rtc_oscillator_disable(void);
|
||||
bool rtc_oscillator_disable(void);
|
||||
/** @brief enable RTC IC oscillator
|
||||
* @return if enabling oscillator succeeded
|
||||
*/
|
||||
void rtc_oscillator_enable(void);
|
||||
bool rtc_oscillator_enable(void);
|
||||
/** @brief write seconds into RTC IC
|
||||
* @return the number of seconds (0-59)
|
||||
* @param[in] number of seconds (0-59)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
void rtc_write_seconds(uint8_t seconds);
|
||||
|
||||
bool rtc_write_seconds(uint8_t seconds);
|
||||
/** @brief write minutes into RTC IC
|
||||
* @param[in] number of minutes (0-59)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_write_minutes(uint8_t minutes);
|
||||
/** @brief write hours into RTC IC
|
||||
* @param[in] number of hours (0-23)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_write_hours(uint8_t hours);
|
||||
/** @brief write day into RTC IC
|
||||
* @param[in] day (1-7, 1 is Sunday)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_write_day(uint8_t day);
|
||||
/** @brief write date into RTC IC
|
||||
* @param[in] date (day of the month, 1-31)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_write_date(uint8_t date);
|
||||
/** @brief write month into RTC IC
|
||||
* @param[in] month (1-12)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_write_month(uint8_t month);
|
||||
/** @brief write year into RTC IC
|
||||
* @param[in] year (2000-2099)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_write_year(uint16_t year);
|
||||
/** @brief write time into RTC IC
|
||||
* @param[in] number of seconds (0-59)
|
||||
* @param[in] number of minutes (0-59)
|
||||
* @param[in] number of hours (0-23)
|
||||
* @param[in] day (1-7, 1 is Sunday)
|
||||
* @param[in] date (day of the month, 1-31)
|
||||
* @param[in] month (1-12)
|
||||
* @param[in] year (2000-2099)
|
||||
* @return if write succeeded
|
||||
*/
|
||||
bool rtc_write_time(uint8_t seconds, uint8_t minutes, uint8_t hours, uint8_t day, uint8_t date, uint8_t month, uint16_t year);
|
||||
|
|
7
main.c
7
main.c
|
@ -278,7 +278,6 @@ int main(void)
|
|||
printf("it is now %02lu:%02lu:%02lu\n", time/ticks_hour, (time%ticks_hour)/ticks_minute, (time%ticks_minute)/ticks_second);
|
||||
|
||||
// test RTC
|
||||
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 full 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("RTC oscillator: ");
|
||||
|
@ -287,10 +286,8 @@ int main(void)
|
|||
} else {
|
||||
printf("enabled\n");
|
||||
}
|
||||
uint8_t seconds = 23;
|
||||
printf("set RTC seconds to %02d\n", seconds);
|
||||
rtc_write_seconds(seconds);
|
||||
printf("RTC seconds: %02d\n", rtc_read_seconds());
|
||||
//rtc_write_time(0,52,9,4,23,3,2016);
|
||||
//rtc_oscillator_enable();
|
||||
|
||||
printf("input commands\n");
|
||||
bool action = false; // if an action has been performed don't go to sleep
|
||||
|
|
Loading…
Reference in New Issue