在线时间101 小时
UID3617445
注册时间2020-2-7
NXP金币217

TA的每日心情 | 开心 2025-7-10 13:07 |
---|
签到天数: 43 天 连续签到: 1 天 [LV.5]常住居民I
版主
  
- 积分
- 1528

- 最后登录
- 2025-7-23
|
LPC55S69之输入捕获
官方没有提供输入捕获的例子,这里介绍使用CTimer做为输入捕获。
一、引脚。
二、代码。
- <font size="3" face="微软雅黑">#include "fsl_debug_console.h"
- #include "board.h"
- #include "fsl_ctimer.h"
- #include "pin_mux.h"
- #include <stdbool.h>
-
- #define CTIMER CTIMER2 /* Timer 2 */
- void ctimer2_callback(uint32_t flags);
- static ctimer_callback_t ctimer_callback[] = {ctimer2_callback};
- uint8_t gCtimer100msFlag = 0 , gOverFlow = 0;
- uint32_t gDiffValue;
- uint32_t gCaptureTime;
-
- void ctimer2_callback(uint32_t flags) {
- static uint8_t flag = 0;
- static uint32_t first = 0 , second = 0;
- if(flag == 0) {
- CTIMER_StartTimer(CTIMER);
- first = CTIMER_GetTimerCountValue(CTIMER);
- flag = 1;
- } else {
- CTIMER_StopTimer(CTIMER);
- second = CTIMER_GetTimerCountValue(CTIMER);
- if(second >= first) {
- gDiffValue = second - first;
- gOverFlow = 0;
- } else {
- gDiffValue = first - second;
- gOverFlow = 1; // overflow
- }
- CTIMER_Reset(CTIMER);
- gCaptureTime = gDiffValue / (CLOCK_GetFroHfFreq() / 1000);
- flag = 0;
- gCtimer100msFlag = 1;
- }
- }
-
- int main(void) {
- ctimer_config_t config;
-
- CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
- CLOCK_AttachClk(kFRO_HF_to_CTIMER2); // 96MHz
-
- BOARD_InitPins();
- BOARD_BootClockPLL150M();
- BOARD_InitDebugConsole();
-
- CTIMER_GetDefaultConfig(&config);
- config.input = kCTIMER_Capture_0;
- CTIMER_Init(CTIMER, &config);
- CTIMER_RegisterCallBack(CTIMER, &ctimer_callback[0], kCTIMER_SingleCallback);
- CTIMER_SetupCapture(CTIMER, kCTIMER_Capture_0, kCTIMER_Capture_BothEdge, true);
-
- PRINTF("CTimer Caputre\r\n");
-
- while (1) {
- if(gCtimer100msFlag == 1) {
- PRINTF("Caputure Once = %u\r\n",gDiffValue);
- PRINTF("Caputure Time = %u ms\r\n",gCaptureTime);
- gCtimer100msFlag = 0;
- }
- }
- }
- </font>
复制代码 三、说明。
按下S3,也就是开发板上的USER键,可以捕获按下的时长。
设置示波器为单次触发方式,可以看到按键按下的时长。
我这里使用默认新建的工程,不能计算浮点数,所以这里用了uint32_t来计算,能达到ms级精度。
改为浮点数,可达到us级精度。
新建工程时勾选红框所示,也不能使用%f来打印浮点数。
将所有代码移入官方提供的例子(lpcxpresso55s69_lpadc_temperature_measurement)中。
在SDK管理器中增加CTimer组件,重新编译、调试。
可以打印出浮点数了。
文章出处:点击
|
|