STM32用于PWM占空比测量

STM32的TIM输入信号结构
在这里插入图片描述

The input stage samples the corresponding TIx input to generate a filtered signal TIxF. Then, an edge detector with polarity selection generates a signal (TIxFPx) which can be used as trigger input by the slave mode controller or as the capture command. It is prescaled before the capture register (ICxPS).

从引脚接收一个信号TIx,经过数字滤波得到TIxF信号,经过边沿检测选择得到TIxFPx信号,这个信号可以用于从机模式,也可以直接使用这个信号用于编程。


输入捕获模式

In Input capture mode, the Capture/Compare Registers (TIMx_CCRx) are used to latch(锁存) the value of the counter after a transition detected by the corresponding ICx signal. When a capture occurs, the corresponding CCXIF (输入捕获中断标志)flag (TIMx_SR register) is set and an interrupt or a DMA request can be sent if they are enabled. If a capture occurs while the CCxIF flag was already high, then the over-capture flag CCxOF (TIMx_SR register) is set. CCxIF can be cleared by software by writing it to 0 or by reading the captured data stored in the TIMx_CCRx register.(通过读取CCRx中的数据可以清除CCxIF捕获中断标志) CCxOF is cleared when you write it to 0.

接着“STM32的TIM输入信号结构”的内容,上面最后说到用于编程的TIxFPx信号是检测到边沿跳变产生的,此时就产生ICx信号,一旦有ICx信号产生,CCRx寄存器就锁存此时计数器中的值,同时,中断标志CCxIF就会置位,如果CCxIF处于置位的时候又发生了捕获事件,那么过捕获标志CCxOF就置位,特别地,程序员读取CCRx中的数据后硬件清除CCxIF中断标志。

Input capture configuration code example 
/* (1) Select the active input TI1 (CC1S = 01),
 program the input filter for 8 clock cycles (IC1F = 0011),
 select the rising edge on CC1 (CC1P = 0, reset value)
 and prescaler at each valid transition (IC1PS = 00, reset value) */
/* (2) Enable capture by setting CC1E */
/* (3) Enable interrupt on Capture/Compare */
/* (4) Enable counter */
TIMx->CCMR1 |= TIM_CCMR1_CC1S_0 \
 | TIM_CCMR1_IC1F_0 | TIM_CCMR1_IC1F_1; /* (1 */
TIMx->CCER |= TIM_CCER_CC1E; /* (2) */
TIMx->DIER |= TIM_DIER_CC1IE; /* (3) */
TIMx->CR1 |= TIM_CR1_CEN; /* (4) */
//上面配置TIM的模式寄存器TIMx capture/compare mode register TIM->CCMR1
//CC1S用于选择信号来自于TI1还是TI2,这里的理解要回到第一张图,可以看到CCMR1这个配置寄存器的作用。
//这段代码就是第一张图的配置,程序员可以根据的需要去配置CCMR1寄存器。

PWM输入模式

This mode is a particular case of input capture mode. The procedure is the same except:

1)Two ICx signals are mapped(映射) on the same TIx input.

2)These 2 ICx signals are active on edges with opposite(相反的) polarity(极性).

3)One of the two TIxFP signals is selected as trigger input and the slave mode controller

扫描二维码关注公众号,回复: 12835487 查看本文章

is configured in reset mode.

这是输入捕获模式的特例,stm32TIM上某一个引脚的TIx信号可以产生两个IC信号IC1和IC2(这里以IC1和IC2举例,读者可以使用IC3和IC4,没有IC2和IC3,这是规定),这两个IC信号极性相反,这样刚好可以构成一个PWM,这两个信号中的一个用于触发,另一个处于从模式,被配置成复位的模式。

捕获PWM的步骤,用于测量占空比

\1. Select the active input for TIMx_CCR1: write the CC1S bits to 01 in the TIMx_CCMR1

register (TI1 selected).

\2. Select the active polarity for TI1FP1 (used both for capture in TIMx_CCR1 and counter

clear): write the CC1P to ‘0’ and the CC1NP bit to ‘0’ (active on rising edge).

\3. Select the active input for TIMx_CCR2: write the CC2S bits to 10 in the TIMx_CCMR1

register (TI1 selected).

\4. Select the active polarity for TI1FP2 (used for capture in TIMx_CCR2): write the CC2P

bit to ‘1’ and the CC2NP bit to ’0’(active on falling edge). (注意:极性跟IC1信号的相反)

\5. Select the valid trigger input: write the TS bits to 101 in the TIMx_SMCR register

(TI1FP1 selected). (选择IC1触发,则IC2沦为从模式)

\6. Configure the slave mode controller in reset mode: write the SMS bits to 100 in the

TIMx_SMCR register.

\7. Enable the captures: write the CC1E and CC2E bits to ‘1 in the TIMx_CCER register.

这是PWM的一个例子,看回PWM输入模式的介绍,这个例子首先选择信号来至于STM32的哪个TIM引脚,TI1,TI2或者TI3,这里又用到配置寄存器CCMR1了,配置IC1和IC2极性相反,一个作为触发源,另一个配置成reset的从模式,此例IC1作为触发,则IC1捕获触发时,CCR1锁存了此时计数器的值,下一次触发则是轮到与IC1相反极性的IC2触发,CCR2锁存计数器的值,并且复位计数器,因此CCR1和CCR2可以用于计算占空比。
在这里插入图片描述

这张图反映了IC1设置成reset的从模式,IC2作为触发源。

猜你喜欢

转载自blog.csdn.net/weixin_43810563/article/details/110940936