Comparator

This sample implements the timing function using the RTC Comparator.

When the RTC timer expires, the RTC comparator interrupt is triggered.

Users can change the RTC channel settings, timer duration, and other information in the sample through different macro configurations. For specific macro configurations, see Configuration.

Requirements

For requirements, please refer to the Requirements.

Configurations

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

    #define RTC_PRESCALER_VALUE     (3200-1)                /*< Set this macro to modify the RTC Prescale value. RTC_CLK = 32k / 3200 = 10Hz. */
    #define RTC_COMP_VALUE          (10)                    /*< Set this macro to modify the RTC Comparator value. Time = 10 / RTC_CLK. */
    
  2. The following macros can be configured to modify the RTC channel settings.

    #define RTC_COMP_INDEX          RTC_COMP1
    #define RTC_COMP_INDEX_INT      RTC_INT_COMP1
    

Building and Downloading

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

Experimental Verification

  1. When the comparator setting time (1s) arrives, it triggers the interrupt and prints the interrupt message with a count value of +10 each time.

    RTC_Handler: RTC_COMP_INDEX1
    RTC_Handler: RTC counter current value = 10
    RTC_Handler: RTC_COMP_INDEX1
    RTC_Handler: RTC counter current value = 20
    ...
    

Code Overview

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

Source Code Directory

The directory for project file and source code are as follows:

  • Project directory: sdk\samples\peripheral\rtc\comparator\proj

  • Source code directory: sdk\samples\peripheral\rtc\comparator\src

Initialization

The RTC initialization flow is shown in the diagram:

../../../../../../_images/rtc_comparator_init_flow.png

RTC Initialization Flow

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

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

  3. Call RTC_INTConfig() to enable the RTC comparator channel 1 count interrupt, and call RTC_NvCmd() to enable the RTC interrupt function. For NVIC related configuration, refer to Interrupt Configuration.

  4. Call RTC_ResetCounter() to reset the RTC counter, and call RTC_Cmd() to enable the RTC peripheral.

    void driver_rtc_init(void)
    {
        RTC_DeInit();
    
        RTC_SetPrescaler(RTC_PRESCALER_VALUE);
        RTC_SetCompValue(RTC_COMP_INDEX, RTC_COMP_VALUE);
    
        RTC_INTConfig(RTC_COMP_INDEX_INT, ENABLE);
    
        /* Config RTC interrupt */
        NVIC_InitTypeDef NVIC_InitStruct;
        NVIC_InitStruct.NVIC_IRQChannel = RTC_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelPriority = 3;
        NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
        NVIC_Init(&NVIC_InitStruct);
    
        RTC_NvCmd(ENABLE);
        /* Start RTC */
        RTC_ResetCounter();
        RTC_Cmd(ENABLE);
    }
    

Functional Implementation

  1. When the comparator setting time (0.1s * 10 = 1s) is reached, it triggers an RTC interrupt and enters the interrupt handling function RTC_Handler.

    1. Print the interrupt message and comparison value message.

    2. Reset the comparison value.

    3. Clear the RTC comparator channel 1 interrupt.

    DBG_DIRECT("RTC_Handler: RTC_COMP_INDEX%d", RTC_COMP_INDEX);
    DBG_DIRECT("RTC_Handler: RTC counter current value = %d", RTC_GetCounter());
    RTC_SetCompValue(RTC_COMP_INDEX, RTC_GetCounter() + RTC_COMP_VALUE);
    RTC_ClearCompINT(RTC_COMP_INDEX);