首页 > 其他分享 >蓝桥杯嵌入式——输入捕获的补充

蓝桥杯嵌入式——输入捕获的补充

时间:2023-03-18 09:22:05浏览次数:36  
标签:HAL text 捕获 ccrl htim 嵌入式 蓝桥 TIM CHANNEL

输入捕获除了可以测量频率,也可以测量占空比

配置

 首先是定时器2的配置,通道一直接捕获,测量上升沿,通道二间接捕获,测量下降沿

 

 

 

 定时器3同上

编程(中断部分)

这个程序与之前的区别:

——将整形变量改为浮点型变量,便于除法计算占空比

——新增判断语句if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1),我猜测是与后文占空比用ccrl_val1b除以ccrl_val1a与此有关(但将判断语句修改为HAL_TIM_ACTIVE_CHANNEL_2后,再用ccrl_val1a除以ccrl_val1b,大概在0.4左右,存在一定差异)

——时钟的开启两个都需要HAL_TIM_IC_Start(____, _____________);

 1 float ccrl_val1a=0,ccrl_val2a=0;
 2 float ccrl_val1b=0,ccrl_val2b=0;
 3 float frq1=0,frq2=0;
 4 float duty1=0,duty2=0;
 5 void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
 6 {
 7   if(htim->Instance == TIM2)
 8     {
 9       if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)
10         {
11           ccrl_val1a = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);
12           ccrl_val1b = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);
13           __HAL_TIM_SetCounter(htim,0);
14           frq1 = (80000000/80)/ccrl_val1a;
15           duty1 = ccrl_val1b/ccrl_val1a;
16           HAL_TIM_IC_Start(htim,TIM_CHANNEL_1);
17           HAL_TIM_IC_Start(htim,TIM_CHANNEL_2);
18         }
19     }
20   if(htim->Instance == TIM3)
21     {
22       if(htim->Channel==HAL_TIM_ACTIVE_CHANNEL_1)
23         {
24           ccrl_val2a = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1);
25           ccrl_val2b = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_2);
26           __HAL_TIM_SetCounter(htim,0);
27           frq2 = (80000000/80)/ccrl_val2a;
28           duty2 = ccrl_val2b/ccrl_val2a;
29           HAL_TIM_IC_Start(htim,TIM_CHANNEL_1);
30           HAL_TIM_IC_Start(htim,TIM_CHANNEL_2);
31         }
32     }
33 }

编程(main函数部分)

变量申明

1 extern float frq1,frq2;
2 extern float duty1,duty2;

再次强调记得初始化

    HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_2);
    HAL_TIM_IC_Start_IT(&htim3,TIM_CHANNEL_2);

然后将数据显示到屏幕上

            char text[30];
            sprintf(text,"    FRQ1=%.2f         ",frq1);
            LCD_DisplayStringLine(Line3,(uint8_t *)text);
            sprintf(text,"    DUTY1=%.2f         ",duty1);
            LCD_DisplayStringLine(Line4,(uint8_t *)text);
            sprintf(text,"    FRQ2=%.2f         ",frq2);
            LCD_DisplayStringLine(Line5,(uint8_t *)text);
            sprintf(text,"    DUTY2=%.2f         ",duty2);
            LCD_DisplayStringLine(Line6,(uint8_t *)text);

 

标签:HAL,text,捕获,ccrl,htim,嵌入式,蓝桥,TIM,CHANNEL
From: https://www.cnblogs.com/Euphonium611/p/17229360.html

相关文章