RTC Comparator Function

This sample code guide is designed to help users easily and comprehensively understand RTC sample. This sample demonstrates RTC comparator function.

Requirements

For hardware requirements, please refer to the Requirements.

Configurations

  1. The following macros can be configured to modify the RTC prescaler value.

    • #define RTC_PRESCALER_VALUE     0

  2. The following macros can be configured to modify the RTC comparator channel.

    • #define RTC_COMP_INDEX          1

    • #define RTC_INT_CMP_1           RTC_INT_CMP1

  3. The following macros can be configured to modify the RTC comparator value.

    • #define RTC_COMP_VALUE          (0xC000)

  4. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    rtc_demo();
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

Preparation Phase

Press the Reset button on the EVB, when the comparator setting time (1536ms) arrives, it triggers the RTC interrupt and prints log in Debug Analyzer.

rtc_handler

Code Overview

This section introduces the code and process description for initialization and corresponding function implementation in the sample.

Source Code Directory

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\src\sample\io_demo\rtc\comparator\rtc_demo.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

RTC comparator function initialization flow is shown in the following figure.

../../../_images/RTC_Comparator_Function_Initialization_Flow_Chart.png

RTC Comparator Function Initialization Flow Chart

  1. Call RTC_DeInit() to reset the RTC peripheral.

  2. Call RTC_SetPrescaler() to set the RTC prescaler.

  3. Call RTC_SetComp() to set RTC comparator channel 1 value.

  4. Call RTC_MaskINTConfig() to unmask the RTC comparator channel 1 interrupt _RTC_INT::RTC_INT_CMP1.

  5. Call RTC_CompINTConfig() to enable the RTC comparator channel 1 interrupt _RTC_INT::RTC_INT_CMP1.

  6. Call NVIC_Init() to enable NVIC of RTC.

  7. Call RTC_SystemWakeupConfig() to enable the RTC system wakeup function.

  8. Call RTC_RunCmd() to enable the RTC.

Functional Implementation

Interrupt Handle

RTC interrupt handle flow is shown in the following figure.

../../../_images/RTC_Interrupt_Handle_Flow_Chart_1.png

RTC Interrupt Handle Flow Chart

When the comparator setting time (1536ms) is reached, it triggers an RTC interrupt:

  1. Call RTC_GetINTStatus() to check the RTC comparator channel 1 interrupt status flag.

  2. Call RTC_SetComp() to set the RTC comparator channel 1 value.

  3. Call RTC_ClearCompINT() to clear the RTC comparator channel 1 interrupt.