4.14.9 Real-Time Counter

16-bit Real-Time Counter (RTC) that can run from external crystal or internal oscillator

4.14.9.1 Introduction

The RTC peripheral offers two timing functions: the Real-Time Counter (RTC) and a Periodic Interrupt Timer (PIT), which can be enabled independently of the RTC functionality.

RTC Real Time Counter

The RTC counts (prescaled) clock cycles in a Counter register and compares the content of the Counter register to a Period register and a Compare register. It can generate both interrupts and events on compare match or overflow, which resets the counter value to zero. The RTC peripheral typically runs continuously, including in Low-Power Sleep modes, to keep track of time. It can wake up the device from sleep modes and/or interrupt the device at regular intervals.

PIT - Periodic Interrupt Timer

The PIT uses the same clock source (CLK_RTC) as the RTC function and can generate an interrupt request or a level event on every nth clock period. The n can be selected from {4, 8, 16,... 32768} for interrupts and from {64, 128, 256,... 8192} for events.

4.14.9.2 Supported Device Families

AVR® DxAVR® ExATtinyATmega

4.14.9.3 Required Header Files

#include "mcc_generated_files/timer/rtc.h"

4.14.9.4 How to Use the RTC PLIB Driver

The links below provide examples for the different use cases for the RTC, general instructions common for all code snippets: RTC use-case code snippet Instructions

Table 4-1087. Running the RTC Use-Cases
Videos Running MCC Melody use case code snippets for AVR:
Many of use cases build on the following components, so this is a good place to start if you are new to MCC Melody:

4.14.9.5 Module Documentation

4.14.9.5.1 RTC Use Cases

Using AVR RTC

RTC Use Case Code Snippet Instructions

The use cases show example uses of the RTC PLIB Driver, within a MCC Melody project:
  • Add RTC to the project

  • Configure:
    1. RTC as described in the example.

    2. Any other peripherals or pins needed for the use case.

  • Generate the code

  • Add the code snippet(s) to the application code

  • Program the board

RTC Use Case 1: 100 ms LED blink every 1s

This use-case configures the Real Time Counter (RTC) to generate an overflow interrupt at a period of 1s, which turns the LED on (assume active low). 100ms later the compare interrupt turns the LED off, resulting in a 100ms blink.

RTC Configuration:
  • RTC > Hardware Settings:
    1. Compare(s): 0.1 (100 ms).

    2. Period(s): 1(s).

  • RTC > Interrupt Settings:
    1. Compare Match Interrupt Enable: Yes.

    2. Overflow Interrupt Enable: Yes.

System Configuration
  • System > Interrupt Manager:
    1. Global Interrupt Enable: Yes.

  • System > Pins:
    1. Pin Grid View: Select LED pin as output (Check the schematic for your board).

    2. Pins: Rename Custom Name to "LED".

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

void RTC_CMP_Callback(void){
    LED_SetHigh();}

void RTC_OVF_Callback(void){
    LED_SetLow();
}
int main(void)
{
    SYSTEM_Initialize();
    
    RTC_SetCMPIsrCallback(RTC_CMP_Callback);
    RTC_SetOVFIsrCallback(RTC_OVF_Callback);

    while(1)
    {
    }
}

RTC Use Case 2: Event System Pulse on Compare, Interrupt on 1s Period

Using the Event System a pulse is generated on a RTC compare match of 200 ms. The RTC is used to toggle on the RTC overflow every second, using interrupts.

  • Components to add:
    1. RTC

    2. Event System (EVSYS)

RTC Configuration:
  • RTC > Hardware Settings:
    1. Compare(s): 0.2 (200 ms).

    2. Period(s): 1(s).

  • RTC > Interrupt Settings:
    1. Overflow Interrupt Enable: Yes.

Event System Configuration:
  • EVSYS > Software Settings:
    1. GENERATORS: CHANNEL0 RTC_CMP -> CHANNELS: CHANNEL0

    2. USERS: EVSYSEVOUTB -> CHANNELS: CHANNEL0

System Configuration
  • System > Interrupt Manager:
    1. Global Interrupt Enable: Yes.

  • System > Pins:
    1. Pin Grid View: Select LED pin as output (Check the schematic for your board).

    2. Pins: Rename Custom Name to "LED".

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

void RTC_OVF_Callback(void){
    LED_Toggle();
}
int main(void)
{
    SYSTEM_Initialize();
    
    RTC_SetOVFIsrCallback(RTC_OVF_Callback);

    while(1)
    {
    }
}

RTC Use Case 3: Using RTC's PIT to Wake from Sleep

The Periodic Interrupt Timer (PIT) function of the RTC to wake up the CPU. Every second a PIT interrupt wakes the AVR from Power Down mode, then in the PIT callback a LED is toggled, before we put the CPU to sleep again.

NOTE: The EVSYSEVOUTx pin (Event System event out), will pulse on a RTC_CMP or RTC_OVF event, but will toggle on a PIT event.

  • Components to add:
    1. RTC.

    2. System > SLPCTRL.

RTC Configuration:
  • RTC > Hardware Settings:
    1. RTC Clock Source Selection: 1.024 kHz from OSC32K

    2. Enable RTC: No.

  • RTC > Periodic Interrupt Timer:
    1. PIT Enable: Yes.

    2. Period Selection: RTC Clock Cycles 1024.

  • RTC: Interrupt Settings:
    1. Overflow Interrupt Enable: No.

    2. Periodic Interrupt Enable: Yes.

System Configuration
  • System >SLPCTRL:
    1. Device Resources > System > Add SLPCTRL

    2. Sleep Enable

    3. Sleep mode: PDOWN

  • System > Interrupt Manager:
    1. Global Interrupt Enable: Yes.

  • System > Pins:
    1. Pin Grid View: Select LED pin as output (Check the schematic for your board).

    2. Pins: Rename Custom Name to "LED".

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

/* Required header files for RTC Use-case 2 */
#include "mcc_generated_files/system/system.h"
#include <avr/sleep.h>
int main(void)
{
    SYSTEM_Initialize();

    //RTC PIT ISR callback function
    RTC_SetPITIsrCallback(RTC_PIT_CallBack);
    while(1)
    {
        //Enter power down mode
        sleep_cpu();
    }    
}

RTC Use Case 3.5: Using the RTC's PIT with the Event System

We refactor the application, demonstrating how the Periodic Interrupt Timer works with the Event System. The same functionality, of a LED toggling every second is kept, without any code running.

NOTE: The EVSYSEVOUTx pin (Event System event out), will pulse on a RTC_CMP or RTC_OVF event, but will toggle on a PIT event.

Components to add:
  1. Timer > RTC.

  2. EVSYS (Event System)

  3. System > SLPCTRL.

Event System Configuration:
  • EVSYS > Software Settings:
    1. GENERATORS: RTC_PIT_DIV1024 -> CHANNELS: CHANNEL0

    2. USERS: EVSYSEVOUTB -> CHANNELS: CHANNEL0

  • RTC: Interrupt Settings:
    1. Overflow Interrupt Enable: No.

    2. Periodic Interrupt Enable: No.

  • System > Interrupt Manager:
    1. Global Interrupt Enable: No.

/* Required header files for RTC Use-case 2 */
#include "mcc_generated_files/system/system.h"
#include <avr/sleep.h>
int main(void)
{
    SYSTEM_Initialize();

    while(1)
    {
        //Enter power down mode
        sleep_cpu();
    }
}

RTC Use Case 4: Sleep 3s then wake for 1s, toggling LED every 50ms while awake.

Set up the PIT to wake the MCU every 4s and set up the RTC Overflow interrupt to go back to sleep a second later. While awake use a delay to toggle the LED every 50ms.

Components to add:
  1. Timer > RTC.

  2. System > SLPCTRL.

RTC Configuration:
  • RTC: Hardware Settings:
    1. Enable RTC: Yes

    2. RTC Clock Source Selection: 1.024kHz from OSC32K

    3. Period (s): 1

  • RTC: PIT
    1. PIT Enable: Yes.

    2. Period Selection: RTC Clock Cycles 4096.

  • RTC: Interrupt Settings
    1. Overflow Interrupt Enable: Yes.

    2. Periodic Interrupt Enable: Yes.

System Configuration
  • System >SLPCTRL:
    1. Device Resources > System > Add SLPCTRL

    2. Sleep Enable

    3. Sleep mode: PDOWN

  • System > Interrupt Manager:
    1. Global Interrupt Enable: Yes.

  • System > Pins:
    1. Pin Grid View: Select LED pin as output (Check the schematic for your board).

    2. Pins: Rename Custom Name to "LED".

After configuring the components as described above, click 'Generate' to generate the code. Then add the following code snippets to your application:

#include "mcc_generated_files/system/system.h"
#include <avr/sleep.h>
#include <util/delay.h>
volatile bool GOTOSLEEP = false;

void RTC_PIT_Callback(void){
    GOTOSLEEP = false;
}

void RTC_OVF_Callback(void)
{
    GOTOSLEEP = true;
}
int main(void)
{
    SYSTEM_Initialize();
    
    RTC_SetOVFIsrCallback(RTC_OVF_Callback);
    RTC_SetPITIsrCallback(RTC_PIT_Callback);

    while(1)
    {
        if (GOTOSLEEP) {
            LED_SetHigh();
            sleep_cpu();
            
            } else {
            LED_Toggle();
            _delay_ms(50);
        }
    }
}

4.14.9.6 File Documentation

4.14.9.6.1 source/rtc.c File Reference

This file contains the driver code for RTC module.

#include "../rtc.h"

Functions

Detailed Description

This file contains the driver code for RTC module.

RTC Generated Driver File

version RTC Driver Version 2.0.3

Function Documentation

ISR()[1/2]

ISR (RTC_CNT_vect )

ISR()[2/2]

ISR (RTC_PIT_vect )

RTC_ClearOVFInterruptFlag()

void RTC_ClearOVFInterruptFlag (void )[inline]

Clearing the Overflow interrupt flag after the overflow flag set.

Parameters:
none
Returns:

none

RTC_DisableCMPInterrupt()

void RTC_DisableCMPInterrupt (void )[inline]

Disable the CMP interrupt for RTC interface.

Parameters:
none
Returns:

none

RTC_DisableOVFInterrupt()

void RTC_DisableOVFInterrupt (void )[inline]

Disable the Overflow interrupt for RTC interface.

Parameters:
none
Returns:

none

RTC_DisablePITInterrupt()

void RTC_DisablePITInterrupt (void )[inline]

Disable the PIT interrupt for RTC interface.

Parameters:
none
Returns:

none

RTC_EnableCMPInterrupt()

void RTC_EnableCMPInterrupt (void )[inline]

Enable the CMP interrupt to set the flag, if match value between counter register and compare register.

Parameters:
none
Returns:

none

RTC_EnableOVFInterrupt()

void RTC_EnableOVFInterrupt (void )[inline]

Enable the overflow interrupt set the OVF flag, if the counter reached value from the period register and wrapped to zero.

Parameters:
none
Returns:

none

RTC_EnablePITInterrupt()

void RTC_EnablePITInterrupt (void )[inline]

Enable the Period Interrupt Timer to set the flag, if a time period has passed as configured in period bit field.

Parameters:
none
Returns:

none

RTC_Initialize()

int8_t RTC_Initialize (void )

Initialize RTC interface.

Parameters:
none
Return values:
0

- the RTC init was successful

1

- the RTC init was not successful

RTC_IsOVFInterruptEnabled()

bool RTC_IsOVFInterruptEnabled (void )[inline]

Enable the Overflow interrupt to set overflow flag, when overflow occur.

Parameters:
none
Returns:

none

RTC_ReadCounter()

uint16_t RTC_ReadCounter (void )[inline]

API to read the counter clock cycle value from counter register.

Parameters:
none
Returns:

uint16_t - Counter values returns from the RTC interface.

RTC_ReadPeriod()

uint16_t RTC_ReadPeriod (void )[inline]

API to read the overflow value in period register.

Parameters:
none
Returns:

uint16_t - Period values returns from the RTC interface.

RTC_SetCMPIsrCallback()

void RTC_SetCMPIsrCallback (RTC_cb_t cb)

Isr callback function to be called if Compare match interrupt flag is set.

Parameters:
RTC_cb_t

cb - call back value for compare.

Returns:

none

RTC_SetOVFIsrCallback()

void RTC_SetOVFIsrCallback (RTC_cb_t cb)

Isr callback function to be called if overflow interrupt flag is set.

Parameters:
RTC_cb_t

cb - call back value for overflow.

Returns:

none

RTC_SetPITIsrCallback()

void RTC_SetPITIsrCallback (RTC_cb_t cb)

Isr callback function to be called if PIT interrupt flag is set.

Parameters:
RTC_cb_t

cb - call back value for PIT.

Returns:

none

RTC_Start()

void RTC_Start (void )

API to start the counter register for RTC interface.

Parameters:
none
Returns:

none

RTC_Stop()

void RTC_Stop (void )

API to stop the counter register for RTC interface.

Parameters:
none
Returns:

none

RTC_WriteCounter()

void RTC_WriteCounter (uint16_t timerVal)[inline]

API to write the counter value for RTC.

Parameters:
uint16_t

timerVal - Loading the counter value to write for RTC.

Returns:

none

RTC_WritePeriod()

void RTC_WritePeriod (uint16_t timerVal)[inline]

API to write the counter value to load for RTC.

Parameters:
uint16_t

timerVal - Loading the write period to determine overflow period in RTC.

Returns:

none

Variable Documentation

RTC_CMP_isr_cb

void(* RTC_CMP_isr_cb) (void) = NULL

RTC_OVF_isr_cb

void(* RTC_OVF_isr_cb) (void) = NULL

RTC_PIT_isr_cb

void(* RTC_PIT_isr_cb) (void) = NULL

4.14.9.6.2 source/RTC.dox File Reference

4.14.9.6.3 source/rtc.h File Reference

This header file provides APIs for the RTC driver.

#include "../system/utils/compiler.h"
#include <stdint.h>
#include <stdbool.h>

Functions

Typedefs

  • typedef void(* RTC_cb_t) (void)

    Function pointer to callback function called by RTC. NULL=default value: No callback function is to be used.

Detailed Description

This header file provides APIs for the RTC driver.

RTC Generated Driver API Header File

defgroup rtc RTC

Version: RTC Driver Version 2.0.2

Function Documentation

RTC_ClearOVFInterruptFlag()

void RTC_ClearOVFInterruptFlag (void )[inline]

Clearing the Overflow interrupt flag after the overflow flag set.

Parameters:
none
Returns:

none

RTC_DisableCMPInterrupt()

void RTC_DisableCMPInterrupt (void )[inline]

Disable the CMP interrupt for RTC interface.

Parameters:
none
Returns:

none

RTC_DisableOVFInterrupt()

void RTC_DisableOVFInterrupt (void )[inline]

Disable the Overflow interrupt for RTC interface.

Parameters:
none
Returns:

none

RTC_DisablePITInterrupt()

void RTC_DisablePITInterrupt (void )[inline]

Disable the PIT interrupt for RTC interface.

Parameters:
none
Returns:

none

RTC_EnableCMPInterrupt()

void RTC_EnableCMPInterrupt (void )[inline]

Enable the CMP interrupt to set the flag, if match value between counter register and compare register.

Parameters:
none
Returns:

none

RTC_EnableOVFInterrupt()

void RTC_EnableOVFInterrupt (void )[inline]

Enable the overflow interrupt set the OVF flag, if the counter reached value from the period register and wrapped to zero.

Parameters:
none
Returns:

none

RTC_EnablePITInterrupt()

void RTC_EnablePITInterrupt (void )[inline]

Enable the Period Interrupt Timer to set the flag, if a time period has passed as configured in period bit field.

Parameters:
none
Returns:

none

RTC_Initialize()

int8_t RTC_Initialize (void )

Initialize RTC interface.

Parameters:
none
Return values:
0

- the RTC init was successful

1

- the RTC init was not successful

RTC_IsOVFInterruptEnabled()

bool RTC_IsOVFInterruptEnabled (void )[inline]

Enable the Overflow interrupt to set overflow flag, when overflow occur.

Parameters:
none
Returns:

none

RTC_ReadCounter()

uint16_t RTC_ReadCounter (void )[inline]

API to read the counter clock cycle value from counter register.

Parameters:
none
Returns:

uint16_t - Counter values returns from the RTC interface.

RTC_ReadPeriod()

uint16_t RTC_ReadPeriod (void )[inline]

API to read the overflow value in period register.

Parameters:
none
Returns:

uint16_t - Period values returns from the RTC interface.

RTC_SetCMPIsrCallback()

void RTC_SetCMPIsrCallback (RTC_cb_t cb)

Isr callback function to be called if Compare match interrupt flag is set.

Parameters:
RTC_cb_t

cb - call back value for compare.

Returns:

none

RTC_SetOVFIsrCallback()

void RTC_SetOVFIsrCallback (RTC_cb_t cb)

Isr callback function to be called if overflow interrupt flag is set.

Parameters:
RTC_cb_t

cb - call back value for overflow.

Returns:

none

RTC_SetPITIsrCallback()

void RTC_SetPITIsrCallback (RTC_cb_t cb)

Isr callback function to be called if PIT interrupt flag is set.

Parameters:
RTC_cb_t

cb - call back value for PIT.

Returns:

none

RTC_Start()

void RTC_Start (void )

API to start the counter register for RTC interface.

Parameters:
none
Returns:

none

RTC_Stop()

void RTC_Stop (void )

API to stop the counter register for RTC interface.

Parameters:
none
Returns:

none

RTC_WriteCounter()

void RTC_WriteCounter (uint16_t timerVal)[inline]

API to write the counter value for RTC.

Parameters:
uint16_t

timerVal - Loading the counter value to write for RTC.

Returns:

none

RTC_WritePeriod()

void RTC_WritePeriod (uint16_t timerVal)[inline]

API to write the counter value to load for RTC.

Parameters:
uint16_t

timerVal - Loading the write period to determine overflow period in RTC.

Returns:

none

Typedef Documentation

RTC_cb_t

void RTC_cb_t

Function pointer to callback function called by RTC. NULL=default value: No callback function is to be used.