In this use case, the RTC is set up in calendar mode.
The time is set and also an alarm is set to show a general use of the RTC in calendar mode. Also the clock is swapped from 24h to 12h mode after initialization. The board LED will be toggled once the current time matches the set time.
Prerequisites
The Generic Clock Generator for the RTC should be configured and enabled; if you are using the System Clock driver, this may be done via conf_clocks.h
.
Clocks and Oscillators
The conf_clock.h
file needs to be changed with the different values to configure the clocks and oscillators for the module according to the used device.
For example, the following oscillator settings are needed for SAMD21:
# define CONF_CLOCK_OSC32K_ENABLE true
# define CONF_CLOCK_OSC32K_STARTUP_TIME SYSTEM_OSC32K_STARTUP_130
# define CONF_CLOCK_OSC32K_ENABLE_1KHZ_OUTPUT true
# define CONF_CLOCK_OSC32K_ENABLE_32KHZ_OUTPUT true
# define CONF_CLOCK_OSC32K_ON_DEMAND true
# define CONF_CLOCK_OSC32K_RUN_IN_STANDBY false
The following generic clock settings are needed for SAMD21:
# define CONF_CLOCK_GCLK_2_ENABLE true
# define CONF_CLOCK_GCLK_2_RUN_IN_STANDBY false
# define CONF_CLOCK_GCLK_2_CLOCK_SOURCE SYSTEM_CLOCK_SOURCE_OSC32K
# define CONF_CLOCK_GCLK_2_PRESCALER 32
# define CONF_CLOCK_GCLK_2_OUTPUT_ENABLE false
Setup
Initialization Code
Create an rtc_module struct and add to the main application source file, outside of any functions:
struct rtc_module rtc_instance;
Copy-paste the following setup code to your application:
void configure_rtc_calendar(void)
{
alarm.year = 2013;
alarm.month = 1;
alarm.day = 1;
alarm.hour = 0;
alarm.minute = 0;
alarm.second = 4;
config_rtc_calendar.clock_24h = true;
config_rtc_calendar.alarm[0].time = alarm;
}
Add to Main
Add the following to main()
.
time.month = 12;
time.day = 31;
time.hour = 23;
time.minute = 59;
time.second = 59;
configure_rtc_calendar();
Workflow
- Make configuration structure.
- Fill the configuration structure with the default driver configuration.
- Note
- This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
- Make time structure for alarm and set with default and desired values.
alarm.year = 2013;
alarm.month = 1;
alarm.day = 1;
alarm.hour = 0;
alarm.minute = 0;
alarm.second = 4;
- Change configurations as desired.
config_rtc_calendar.clock_24h = true;
config_rtc_calendar.alarm[0].time = alarm;
- Initialize module.
- Enable module.
Implementation
Add the following to main()
.
Workflow
- Start an infinite loop, to continuously poll for a RTC alarm match.
- Check to see if a RTC alarm match has occurred.
- Once an alarm match occurs, perform the desired user action.
- Clear the alarm match, so that future alarms may occur.