编写使用stm32的TIM1获取4如输入方波的频率
时间: 2024-05-17 19:16:46 浏览: 69
使用STM32的单个普通定时器产生4路不同频率的方波
5星 · 资源好评率100%
下面是使用STM32的TIM1获取4路输入方波的频率的代码示例:
```c
#include "stm32f10x.h"
#define FREQ_NUM 4
TIM_ICInitTypeDef TIM_ICInitStructure;
uint16_t IC1Value[FREQ_NUM], IC2Value[FREQ_NUM], IC3Value[FREQ_NUM], IC4Value[FREQ_NUM];
uint16_t CaptureNumber = 0, Capture = 0;
float Frequency[FREQ_NUM];
uint32_t TIM1Freq = 72000000;
void TIM1_Configuration(void)
{
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1 | TIM_Channel_2 | TIM_Channel_3 | TIM_Channel_4;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
TIM_Cmd(TIM1, ENABLE);
}
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM1_Configuration();
while (1)
{
if (CaptureNumber >= FREQ_NUM)
{
CaptureNumber = 0;
Frequency[0] = (float)TIM1Freq / (float)IC1Value[1];
Frequency[1] = (float)TIM1Freq / (float)IC2Value[1];
Frequency[2] = (float)TIM1Freq / (float)IC3Value[1];
Frequency[3] = (float)TIM1Freq / (float)IC4Value[1];
}
}
}
void TIM1_IRQHandler(void)
{
if (TIM_GetITStatus(TIM1, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1);
if (CaptureNumber == 0)
{
Capture = TIM_GetCapture1(TIM1);
TIM_SetCompare1(TIM1, Capture + 500);
IC1Value[CaptureNumber++] = Capture;
}
else
{
Capture = TIM_GetCapture1(TIM1);
TIM_SetCompare1(TIM1, Capture + 500);
IC1Value[CaptureNumber++] = Capture - IC1Value[CaptureNumber - 1];
}
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC2) == SET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC2);
if (CaptureNumber == 0)
{
Capture = TIM_GetCapture2(TIM1);
TIM_SetCompare2(TIM1, Capture + 500);
IC2Value[CaptureNumber++] = Capture;
}
else
{
Capture = TIM_GetCapture2(TIM1);
TIM_SetCompare2(TIM1, Capture + 500);
IC2Value[CaptureNumber++] = Capture - IC2Value[CaptureNumber - 1];
}
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC3) == SET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC3);
if (CaptureNumber == 0)
{
Capture = TIM_GetCapture3(TIM1);
TIM_SetCompare3(TIM1, Capture + 500);
IC3Value[CaptureNumber++] = Capture;
}
else
{
Capture = TIM_GetCapture3(TIM1);
TIM_SetCompare3(TIM1, Capture + 500);
IC3Value[CaptureNumber++] = Capture - IC3Value[CaptureNumber - 1];
}
}
if (TIM_GetITStatus(TIM1, TIM_IT_CC4) == SET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC4);
if (CaptureNumber == 0)
{
Capture = TIM_GetCapture4(TIM1);
TIM_SetCompare4(TIM1, Capture + 500);
IC4Value[CaptureNumber++] = Capture;
}
else
{
Capture = TIM_GetCapture4(TIM1);
TIM_SetCompare4(TIM1, Capture + 500);
IC4Value[CaptureNumber++] = Capture - IC4Value[CaptureNumber - 1];
}
}
}
```
其中,TIM1是定时器1,通过`TIM_ICInitStructure`结构体对其进行初始化配置。在`main`函数中,使用`CaptureNumber`变量记录当前已获取的频率数目。当`CaptureNumber`达到`FREQ_NUM`时,计算出4路输入方波的频率,存储在`Frequency`数组中。在`TIM1_IRQHandler`中,分别读取4路输入方波的计数值,计算出上升沿之间的时间差,存储在`IC1Value`、`IC2Value`、`IC3Value`、`IC4Value`数组中。在计算频率时,使用定时器1的时钟频率`TIM1Freq`除以计数值,即可得到频率。
阅读全文