利用STM32的PWM输入捕获功能,可以测方波的占空比和(或)频率
使用时将相应的输入配置为对应定时器对应的复用功能,外部待测量波形从该引脚输入
再配置定时器输入捕获功能相应参数,选择主从模式,最后打开中断或者DMA读取测量数据
1. Enable TIM clock
2. Configure the TIM pins by configuring the corresponding GPIO pins
3.
Fill the TIM_ICInitStruct
5. Call TIM_ICInit(TIMx, &TIM_ICInitStruct) ;. Call TIM_PWMIConfig(TIMx, &TIM_ICInitStruct) ;
6. Enable the NVIC or the DMA to read the measured frequency.
7. Enable the corresponding interrupt (or DMA request) to read the Captured value
8. Configure the slave mode controller in reset mode
9. Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
10. Use TIM_GetCapturex(TIMx); to read the captured value.
代码:
#ifndef __PWM_INPUT_H
#define __PWM_INPUT_H
#include "main.h"
#define PWM_INPUT_TIM TIM3
#define PWM_INPUT_TIM_CLK RCC_APB1Periph_TIM3
#define PWM_INPUT_CHANNEL TIM_