stm8L输入捕获测频率使用库函数
时间: 2023-07-20 13:06:11 浏览: 91
stm8库函数
在STM8L中,可以使用输入捕获模块来测量外部信号的频率。下面是使用库函数的步骤:
1. 初始化输入捕获模块
```
TIM2_ICInit(TIM2_CHANNEL_1, TIM2_ICPOLARITY_RISING, TIM2_ICSELECTION_DIRECTTI,
TIM2_ICPSC_DIV1, 0x00);
```
2. 启动输入捕获模块
```
TIM2_Cmd(ENABLE);
```
3. 等待输入捕获事件发生
```
while (TIM2_GetFlagStatus(TIM2_FLAG_CC1) == RESET);
```
4. 获取捕获到的时间值
```
uint16_t capture = TIM2_GetCapture1();
```
5. 计算频率
```
uint32_t frequency = (uint32_t)(16000000 / capture);
```
完整示例代码:
```c
#include "stm8l15x.h"
void main(void)
{
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
TIM2_DeInit();
TIM2_ICInit(TIM2_CHANNEL_1, TIM2_ICPOLARITY_RISING, TIM2_ICSELECTION_DIRECTTI,
TIM2_ICPSC_DIV1, 0x00);
TIM2_Cmd(ENABLE);
while (1)
{
while (TIM2_GetFlagStatus(TIM2_FLAG_CC1) == RESET);
uint16_t capture = TIM2_GetCapture1();
uint32_t frequency = (uint32_t)(16000000 / capture);
}
}
```
阅读全文