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:

/* SYSTEM_CLOCK_SOURCE_OSC32K configuration - Internal 32KHz oscillator */
# 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:

/* Configure GCLK generator 2 (RTC) */
# 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:

{
/* 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.