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
The following macros can be configured to modify the RTC prescaler value.
#define RTC_PRESCALER_VALUE 0
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
The following macros can be configured to modify the RTC comparator value.
#define RTC_COMP_VALUE (0xC000)
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.

RTC Comparator Function Initialization Flow Chart
Call
RTC_DeInit()
to reset the RTC peripheral.Call
RTC_SetPrescaler()
to set the RTC prescaler.Call
RTC_SetComp()
to set RTC comparator channel 1 value.Call
RTC_MaskINTConfig()
to unmask the RTC comparator channel 1 interrupt_RTC_INT::RTC_INT_CMP1
.Call
RTC_CompINTConfig()
to enable the RTC comparator channel 1 interrupt_RTC_INT::RTC_INT_CMP1
.Call
NVIC_Init()
to enable NVIC of RTC.Call
RTC_SystemWakeupConfig()
to enable the RTC system wakeup function.Call
RTC_RunCmd()
to enable the RTC.
Functional Implementation
Interrupt Handle
RTC interrupt handle flow is shown in the following figure.

RTC Interrupt Handle Flow Chart
When the comparator setting time (1536ms) is reached, it triggers an RTC interrupt:
Call
RTC_GetINTStatus()
to check the RTC comparator channel 1 interrupt status flag.Call
RTC_SetComp()
to set the RTC comparator channel 1 value.Call
RTC_ClearCompINT()
to clear the RTC comparator channel 1 interrupt.