TIM2_CH1_CAPTURE_STA
时间: 2023-11-22 07:06:13 浏览: 149
STM8L_TIM2_CH1_PWM_pwm_STM8LPWM_STM8L051F3_STM8L_TIM2_CH1
5星 · 资源好评率100%
TIM2_CH1_CAPTURE_STA是一个用于记录TIM2通道1输入捕获状态的变量。在使用STM32的输入捕获功能时,我们需要在捕获到边沿信号时记录当前定时器的值,以便后续计算时间差或者频率等信息。TIM2_CH1_CAPTURE_STA通常是一个32位的变量,其中高16位用于记录捕获次数,低16位用于记录捕获状态。具体的定义和使用可以参考以下代码:
```c
#define TIM2_CH1_CAPTURE_STA_COUNT 0XFFFF //捕获计数器的最大值
#define TIM2_CH1_CAPTURE_STA_RISING 0X01 //上升沿捕获标志
#define TIM2_CH1_CAPTURE_STA_FALLING 0X02 //下降沿捕获标志
uint32_t TIM2_CH1_CAPTURE_STA = 0; //捕获状态变量
uint32_t TIM2_CH1_CAPTURE_VAL; //捕获值
void TIM2_IRQHandler(void)
{
if ((TIM2_CH1_CAPTURE_STA & TIM2_CH1_CAPTURE_STA_FALLING) == 0) //还未捕获到下降沿
{
if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) //捕获到上升沿
{
TIM2_CH1_CAPTURE_STA |= TIM2_CH1_CAPTURE_STA_RISING; //标记上升沿已经被捕获
TIM_SetCounter(TIM2, 0); //清空定时器计数器
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); //清除中断标志位
}
}
else //已经捕获到上升沿
{
TIM2_CH1_CAPTURE_VAL = TIM_GetCapture1(TIM2); //获取捕获值
TIM2_CH1_CAPTURE_STA |= TIM2_CH1_CAPTURE_STA_FALLING; //标记下降沿已经被捕获
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); //清除中断标志位
}
}
int main(void)
{
//初始化TIM2通道1输入捕获
TIM_ICInitTypeDef TIM2_ICInitStructure;
TIM2_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM2_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM2_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM2_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM2_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM2, &TIM2_ICInitStructure);
//使能TIM2通道1输入捕获中断
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
//启动TIM2
TIM_Cmd(TIM2, ENABLE);
while (1)
{
if ((TIM2_CH1_CAPTURE_STA & TIM2_CH1_CAPTURE_STA_FALLING) != 0) //已经捕获到下降沿
{
uint32_t capture_time = TIM2_CH1_CAPTURE_VAL + TIM2_CH1_CAPTURE_STA_COUNT * TIM_GetCounter(TIM2); //计算捕获时间
uint32_t capture_freq = SystemCoreClock / capture_time; //计算捕获频率
TIM2_CH1_CAPTURE_STA = 0; //清空捕获状态
}
}
}
```
阅读全文