Microchip® Advanced Software Framework

Quick Start Guide for RTC (CAL) - Basic

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:

The following generic clock settings are needed for SAMD21:

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:

{
/* Initialize RTC in calendar mode. */
struct rtc_calendar_config config_rtc_calendar;
rtc_calendar_get_config_defaults(&config_rtc_calendar);
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;
config_rtc_calendar.alarm[0].mask = RTC_CALENDAR_ALARM_MASK_YEAR;
rtc_calendar_init(&rtc_instance, RTC, &config_rtc_calendar);
}

Add to Main

Add the following to main().

struct rtc_calendar_time time;
time.year = 2012;
time.month = 12;
time.day = 31;
time.hour = 23;
time.minute = 59;
time.second = 59;
/* Set current time. */

Workflow

  1. Make configuration structure.
    struct rtc_calendar_config config_rtc_calendar;
  2. Fill the configuration structure with the default driver configuration.
    rtc_calendar_get_config_defaults(&config_rtc_calendar);
    Note
    This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
  3. 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;
  4. Change configurations as desired.
    config_rtc_calendar.clock_24h = true;
    config_rtc_calendar.alarm[0].time = alarm;
    config_rtc_calendar.alarm[0].mask = RTC_CALENDAR_ALARM_MASK_YEAR;
  5. Initialize module.
    rtc_calendar_init(&rtc_instance, RTC, &config_rtc_calendar);
  6. Enable module.

Implementation

Add the following to main().

Workflow

  1. Start an infinite loop, to continuously poll for a RTC alarm match.
    while (true) {
  2. Check to see if a RTC alarm match has occurred.
  3. Once an alarm match occurs, perform the desired user action.
    /* Do something on RTC alarm match here */
  4. Clear the alarm match, so that future alarms may occur.