Comparator

该示例通过使用 RTC Comparator实现定时功能。

环境需求

该示例支持以下开发套件:

开发套件

Hardware Platforms

Board Name

RTL87x2G HDK

RTL87x2G EVB

更多信息请参考 快速入门

编译和下载

该示例的工程路径如下:

Project file: samples\peripheral\rtc\comparator\proj\rtl87x2g\mdk

Project file: samples\peripheral\rtc\comparator\proj\rtl87x2g\gcc

请按照以下步骤操作构建并运行该示例:

  1. 打开工程文件。

  2. 按照 快速入门编译APP Image 给出的步骤构建目标文件。

  3. 编译成功后,在路径 mdk\bingcc\bin 下会生成 app bin app_MP_xxx.bin 文件。

  4. 按照 快速入门MPTool 给出的步骤将app bin烧录至EVB内。

  5. 按下复位按键,开始运行。

测试验证

  1. 比较器设定时间(1s)到,触发中断,打印中断信息,每次计数值+10。

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

代码介绍

该章节分为以下几个部分:

  1. 源码路径

  2. 初始化函数将在 初始化 章节介绍。

  3. 初始化后的功能实现将在 功能实现 章节介绍。

源码路径

  • 工程路径: sdk\samples\peripheral\rtc\comparator\proj

  • 源码路径: sdk\samples\peripheral\rtc\comparator\src

该工程的工程文件代码结构如下:

└── Project: comparator
    └── secure_only_app
        └── Device                   includes startup code
            ├── startup_rtl.c
            └── system_rtl.c
        ├── CMSIS                    includes CMSIS header files
        ├── CMSE Library             Non-secure callable lib
        ├── Lib                      includes all binary symbol files that user application is built on
            └── rtl87x2g_io.lib
        ├── Peripheral               includes all peripheral drivers and module code used by the application
            ├── rtl_rcc.c
            ├── rtl_pinmux.c
            ├── rtl_nvic.c
            └── rtl_rtc.c
        └── APP                      includes the ble_peripheral user application implementation
            ├── main_ns.c
            └── io_rtc.c

初始化

初始化流程包括了 driver_rtc_init


driver_rtc_init 包含了对RTC外设的初始化。

  1. 复位RTC外设。

  2. 设置RTC的分频系数为(3200-1),RTC时钟频率为10Hz(32K/3200)。

  3. 设置比较器通道1的比较值为10。

  4. 使能RTC比较器通道1计数中断 RTC_COMP_INDEX_INT

  5. 复位RTC计数值。

  6. 启动RTC外设。

RTC_DeInit();

RTC_SetPrescaler(RTC_PRESCALER_VALUE);
RTC_SetCompValue(RTC_COMP_INDEX, RTC_COMP_VALUE);
RTC_INTConfig(RTC_COMP_INDEX_INT, ENABLE);
...
RTC_NvCmd(ENABLE);
RTC_ResetCounter();
RTC_Cmd(ENABLE);

功能实现

  1. 比较器设定时间(0.1s*10 = 1s)到,触发中断,进入中断处理函数 RTC_Handler

    1. 打印中断信息和比较值信息。

    2. 重新设置比较值。

    3. 清除比较器通道1计数中断。

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);