STM32 notes - timer input capture function (measuring the frequency and duty cycle of PWM)

Table of contents

I. Overview

2. Input capture function

2.1 Introduction to input capture

2.2 Input capture channel 

3. Input capture function measures the frequency and duty cycle of the PWM wave

3.1 Method of measuring frequency

3.2 Measurement process


I. Overview

        This article mainly introduces the input capture function of the general timer. The input signal enters the input capture channel from the GPIO port, and then the input part samples the corresponding TIx input signal and generates a filtered signal TIxF. An edge detector with polarity selection then generates a signal (TIxFPx) which can be used as a trigger input to the slave mode controller or as a capture control.

2. Input capture function

2.1 Introduction to input capture

  • IC (Input Capture) input capture
  • In input capture mode, when a specified level transition occurs on the channel input pin, the current CNT value will be latched into the CCR, which can be used to measure the frequency, duty cycle, pulse interval, level duration, etc. of the PWM waveform. parameter
  • Each advanced timer and general timer has 4 input capture channels
  • Configurable to PWMI mode to measure frequency and duty cycle simultaneously
  • Can be used with master-slave trigger mode to achieve fully automatic measurement of hardware

2.2 Input capture channel 

Take input capture channel one as an example:

  • The TI1 signal is filtered by the filter to obtain the TI1F signal, and then passes through the edge detector with polarity selection to obtain the TI1F_Rising or TI1F_Falling signal. After selection by the selector, the TI1FP1 signal is obtained.
  • TI1FP1, the TI2FP1 signal from channel 2 and the TRC signal from slave mode are passed through the selector to obtain the input compare 1 signal IC1, and then the frequency divider is selected to divide the frequency to obtain the input capture/compare register signal IC1PS.

3. Input capture function measures the frequency and duty cycle of the PWM wave

3.1 Method of measuring frequency

  • Frequency measurement method: According to the definition of frequency: the number of times a substance completes periodic changes per unit time is called frequency. That is to say, how many times the high and low levels change within 1 second. f=N/T.
  • Cycle measurement method: within one PWM cycle, calculate the number of changes in the standard frequency, divide the standard frequency by the number of counts N, and obtain the frequency of the PWM wave.

3.2 Measurement process

  1. Initialize GPIO: ① Enable GPIO clock; ② Initialize structure parameters.
  2. Initialization of the input capture part: ① Enable TIM clock; ② Initialize input capture structure parameters: input capture channel selection, filtering, edge detection polarity selection, frequency divider and channel crossover selection.
  3. Configure it in PWMI mode, measure frequency and duty cycle at the same time, and let TI1FP2 enter channel two.
  4. Time base unit initialization.
  5. Select from pattern trigger source.
  6. Select from mode.
  7. Enable TIM.

The module program is as follows:

#include "stm32f10x.h"  

void IC_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	TIM_ICInitTypeDef TIM_ICInitStruct;
	TIM_ICInitStruct.TIM_Channel=TIM_Channel_1;
	TIM_ICInitStruct.TIM_ICFilter=0xF;
	TIM_ICInitStruct.TIM_ICPolarity=TIM_ICPolarity_Rising;
	TIM_ICInitStruct.TIM_ICPrescaler=TIM_ICPSC_DIV1 ;
	TIM_ICInitStruct.TIM_ICSelection=TIM_ICSelection_DirectTI;
	TIM_PWMIConfig(TIM3,&TIM_ICInitStruct);
	
	TIM_InternalClockConfig(TIM3);
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period=65536-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler=72-1;
	TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
	
	TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);
	TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);
	
	TIM_Cmd(TIM3,ENABLE);
}
uint32_t IC_GetFreq(void)
{
	return 1000000/TIM_GetCapture1(TIM3);
}
uint32_t IC_GetDuty(void)
{
	return TIM_GetCapture2(TIM3)*100/TIM_GetCapture1(TIM3);
}

Guess you like

Origin blog.csdn.net/ssssshhbh/article/details/129468440