Comparator
该示例通过使用 RTC 比较器实现定时功能。
当RTC定时时间到,进入RTC比较器中断。
环境需求
该示例支持以下开发套件:
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
请按照以下步骤操作构建并运行该示例:
打开工程文件。
按照 快速入门 中 编译APP Image 给出的步骤构建目标文件。
编译成功后,在路径
mdk\bin
或gcc\bin
下会生成 app binapp_MP_xxx.bin
文件。按下复位按键,开始运行。
测试验证
比较器设定时间(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 ...
代码介绍
该章节分为以下几个部分:
源码路径
工程路径:
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外设的初始化。
复位RTC外设。
设置RTC的分频系数为(3200-1),RTC时钟频率为10Hz(32K/3200)。
设置比较器通道1的比较值为10。
使能RTC比较器通道1计数中断
RTC_COMP_INDEX_INT
。复位RTC计数值。
启动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);
功能实现
比较器设定时间(0.1s*10 = 1s)到,触发RTC中断,进入中断处理函数
RTC_Handler
。打印中断信息和比较值信息。
重新设置比较值。
清除比较器通道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);