STM32 is used for PWM duty cycle measurement

STM32's TIM input signal structure
Insert picture description here

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).

Receive a signal TIx from the pin, get the TIxF signal through digital filtering, and select the TIxFPx signal through edge detection. This signal can be used in slave mode or directly used for programming.


Input capture mode

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.

Next to the content of "STM32 TIM input signal structure", the last mentioned above is that the TIxFPx signal used for programming is generated by detecting the edge transition, and the ICx signal is generated at this time. Once the ICx signal is generated, the CCRx register latches this At the same time, the interrupt flag CCxIF will be set. If a capture event occurs when CCxIF is set, then the over-capture flag CCxOF will be set. In particular, after the programmer reads the data in CCRx The hardware clears the CCxIF interrupt flag.

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 input mode

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

is configured in reset mode.

This is a special case of the input capture mode. The TIx signal of a pin on stm32TIM can generate two IC signals IC1 and IC2 (here IC1 and IC2 are used as examples, readers can use IC3 and IC4, without IC2 and IC3, this is a rule) , The polarity of these two IC signals is opposite, so that it can just form a PWM, one of these two signals is used for triggering, the other is in slave mode and configured as reset mode.

Steps to capture PWM to measure duty cycle

\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). (Note: the polarity is opposite to the IC1 signal)

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

(TI1FP1 selected). (Select IC1 to trigger, then IC2 will be reduced to slave mode)

\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.

This is an example of PWM. Looking back at the introduction of PWM input mode, this example first selects which TIM pin of the STM32, TI1, TI2 or TI3, the configuration register CCMR1 is used to configure the polarity of IC1 and IC2. On the contrary, one is used as the trigger source and the other is configured as the slave mode of reset. In this example, IC1 is used as a trigger. When IC1 captures the trigger, CCR1 latches the value of the counter at this time, and the next trigger is the turn of IC1 with the opposite polarity The IC2 is triggered, CCR2 latches the value of the counter, and resets the counter, so CCR1 and CCR2 can be used to calculate the duty cycle.
Insert picture description here

This picture reflects the slave mode with IC1 set to reset and IC2 as the trigger source.

Guess you like

Origin blog.csdn.net/weixin_43810563/article/details/110940936