Real-Time Clock (RTC)

This library allows users to get a clock after setting the year, month, date, hour, minutes, and seconds. The specified function can be called by setting the alarm feature. To use this library, describe #include <RTC.h>

rtc_init

Description
Initialize RTC.
Syntax
int rtc_init()
Parameters
None
Returns
0: False, 1: Success, 2: Already initialized

rtc_deinit

Description
Stop RTC.
Syntax
int rtc_deinit()
Parameters
None
Returns
0: False, 1: Success

rtc_set_time

Description
Set a start time.
Syntax
int rtc_set_time(RTC_TIMETYPE *time)
Parameters
time: Specify the RTC_TIMETYPE structure for setting a time (Note).
Returns
0: False, 1: Success
Note
Refer to the sample program for setting a time.

rtc_get_time

Description
Get a current time.
Syntax
int rtc_get_time(RTC_TIMETYPE *time)
Parameters
time: Specify the RTC_TIMETYPE structure for storing a time.
Returns
0: False, 1: Success

rtc_attach_alarm_handler

Description
Register a function when an alarm occurs.
Syntax
rtc_attach_alarm_handler(void(*)(void) function)
Parameters
function: Function to be called when an alarm occurs.
Returns
None

rtc_set_alarm_time

Description
Set an alarm time.
Syntax
int rtc_set_alarm_time (int hour, int min, int week_flag)
Parameters
hour: Hour
min: Minute
week_flag: Week (specify multiple flags by using OR)
Returns
0: False, 1: Success

rtc_alarm_on

Description
Set alarm on.
Syntax
rtc_alarm_on()
Parameters
None
Returns
None

rtc_alarm_off

Description
Set alarm off
Syntax
rtc_alarm_off()
Parameters
None
Returns
None

Macro definition

Week for setting time
RTC_WEEK_SUNDAY: 0x00
RTC_WEEK_MONDAY: 0x01
RTC_WEEK_TUESDAY: 0x02
RTC_WEEK_WEDNESDAY: 0x03
RTC_WEEK_THURSDAY: 0x04
RTC_WEEK_FRIDAY: 0x05
RTC_WEEK_SATURDAY: 0x06
Week for setting alarm
RTC_ALARM_SUNDAY: 0x01
RTC_ALARM_MONDAY: 0x02
RTC_ALARM_TUESDAY: 0x04
RTC_ALARM_WEDNESDAY: 0x08
RTC_ALARM_THURSDAY: 0x10
RTC_ALARM_FRIDAY: 0x20
RTC_ALARM_SATURDAY: 0x40
RTC_ALARM_EVERYDAY: All of the above

rtc_attach_constant_period_interrupt_handler

Description
Register a function that operates periodically.
Syntax
rtc_attach_constant_period_interrupt_handler(function)
Parameters
function: A function to be called periodically.
Returns
None

rtc_set_constant_period_interrupt_time

Description
Specify the periodic interval.
Syntax
int rtc_set_constant_period_interrupt_time(period);
Parameters
period:
  RTC_CONSTANT_PERIOD_TIME_NONE: No period
  RTC_CONSTANT_PERIOD_TIME_HALFSECOND: 0.5 sec
  RTC_CONSTANT_PERIOD_TIME_1SECOND: 1 sec (default)
  RTC_CONSTANT_PERIOD_TIME_1MINUTE: 1 min
  RTC_CONSTANT_PERIOD_TIME_1HOUR: 1 hour
  RTC_CONSTANT_PERIOD_TIME_1DAY: 1 day
  RTC_CONSTANT_PERIOD_TIME_1MONTH: 1 week
Returns
None

rtc_constant_period_interrupt_on

Description
Set a periodic function on.
Syntax
rtc_constant_period_interrupt_on()
Parameters
None
Returns
None

rtc_constant_period_interrupt_off

Description
Set a periodic function off.
Syntax
rtc_constant_period_interrupt_off()
Parameters
None
Returns
None

Sample Program

This program is for setting a time and alarm. Time and alarm notification is output via serial communication.


#include <Arduino.h>
#include <RTC.h>
 
void alarm_handler();
RTC_TIMETYPE t;
 
void setup()
{
  Serial.begin(9600);
  pinMode(PIN_LED0, OUTPUT); //blue led for alarm
  digitalWrite(PIN_LED0, HIGH); //turn off
  int err = rtc_init();
 
  t.year    = 2016;
  t.mon     = 3;
  t.day     = 22;
  t.weekday = RTC_WEEK_TUESDAY;
  t.hour    = 9;
  t.min     = 20;
  t.second  = 50;
 
  err = rtc_set_time(&t);
  rtc_attach_alarm_handler(alarm_handler);
  err = rtc_set_alarm_time(t.hour, t.min + 1);
 
}
 
void loop()
{
  int err;
  err = rtc_get_time(&t);
 
  Serial.print(t.year, DEC);Serial.print("/");
  Serial.print(t.mon, DEC); Serial.print("/");
  Serial.print(t.day, DEC); Serial.print(" ");
  Serial.print(t.hour, DEC); Serial.print(":");
  Serial.print(t.min, DEC); Serial.print(":");
  Serial.println(t.second, DEC);
 
  delay(500);
 
}
 
void alarm_handler()
{
  digitalWrite(PIN_LED0, LOW);//blue led for alarm
 
}