STM32F4 - Timer principle and application interrupt, input capture, PWM output

Publisher:火星叔叔Latest update time:2016-05-25 Source: eefocusKeywords:STM32F4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Introduction:

    Different STM32 series have different numbers of timers. The STM32F40x series we are studying now has a total of 14 timers. These 14 timers can be divided into three categories: advanced timers, general timers and basic timers. The three timers are similar. The following will introduce the principles and applications of timers for general timers. General timers can be divided into three categories according to the number of bits and counting methods. The relevant classification chart is as follows:

STM32F4 - Timer principle and application interrupt, input capture, PWM output

2. Application:

    1. Update: overflow or underflow of counter. 2. Event trigger. 3. Input capture. 4. Output comparison. 5. Support incremental encoding and Hall sensor circuits for positioning. 5. Trigger input as external clock or cycle power management. The following will introduce and analyze the code for some applications.

3. Block Diagram:

    The following figure is a block diagram of the general timers TIM2~TIM5. The block diagrams of other timers are similar to this diagram, except that they are cut and modified slightly based on this diagram.

STM32F4 - Timer principle and application interrupt, input capture, PWM output

The following is a breakdown of the timer block diagram and an analysis of its various parts.

4. Clock selection and frequency division:

    The functionality of this part is generated by the following part of the block diagram:

STM32F4 - Timer principle and application interrupt, input capture, PWM output

    From the figure, we can find that the clock sources are: 1. Internal clock (CK_INT). 2. External clock mode 1; external pin TIx, generated by the input capture part. 3. External clock mode 2; external trigger input ETR. 4. Internal trigger input ITRx, this clock is generated by another timer output, corresponding to TRGO in the block diagram. After the corresponding selection, the clock CK_PSC is generated.

    This part will also set the timer's counting mode, reset, enable and other related operations in the relevant registers.

5. Time base unit

    The block diagram of the time base unit is as follows:

STM32F4 - Timer principle and application interrupt, input capture, PWM output

    These include: 1. PSC pre-divider: responsible for dividing the selected clock CK_PSC to generate the clock CK_CNT used by the final counter. 2. CNT counter: responsible for counting, as the core unit of the timer. 3. Auto-reload register: responsible for loading the value in the register into the timer after the relevant event is triggered.

6. Input Capture

    The basic description of the input capture function is: by detecting the edge signal on TIM_CHx, when the signal jumps, the current counter value is stored in the corresponding capture/compare register. This functional part is shown in the block diagram as follows:

STM32F4 - Timer principle and application interrupt, input capture, PWM output

    In fact, these are the 4 related channels of the timer. Take one of them for relevant analysis. The block diagram of one of the channels is as follows:

 

    TI1 is the channel input signal, which generates TI1F after filtering, and then passes through edge detection (rising edge or falling edge). The signal generated after relevant selection is divided by the divider to generate the final signal to be captured.

    The input capture related library functions are introduced as follows;

 

    void TIM_ICInit(TIM_TypeDef* TIMx,TIM_ICInitTypeDef* TIM_ICInitStruct); //Set channel related parameters.
    void TIM_OCxPolarityConfig(TIM_TpeDef* TIMx,uint16_t TIM_OCPolarity); //Channel polarity setting.
    uint32_t TIM_GetCapturex(TIM_TypeDef* TIMx); //Get the channel capture value.

 

 

 

7. Output Comparison

    Description of the output comparison function: Set the corresponding value in the CCRx register, compare the value in the counter with the value, and determine the high and low level state of the output voltage based on the comparison result and the corresponding polarity and validity settings. Same as input capture, take out one of the channels for analysis, the block diagram of one of the channels is as follows:

 

    The channel signal passes through the output mode controller, and the required output signal is obtained after the mode, polarity, and switch settings are made. CCRx: Capture compare register, used to set the comparison value. CCMRx: Set the PWM mode. CCER: CC1P bit sets the polarity validity. CCER: CC1E bit output enable setting.

    The output comparison related library functions are introduced as follows:

 

    void TIM_OCxInit(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct) //Set the parameters of related channels.
    void TIM_SetComparex(TIM_TypeDef* TIMx, uint32_t Comparex); //Set the comparison value.
    TIM_OCxPreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload); //Enable output compare preload.

 

 

 

    TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, ENABLE); //Enable automatic reloading of preload registers

 

8. Related initialization examples - input capture

 

//Timer 5 channel 1 input capture configuration
//arr: automatic reload value (TIM2, TIM5 are 32 bits!!)
//psc: clock pre-division number
void TIM5_CH1_Cap_Init(u32 arr,u16 psc)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;

	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //TIM5 clock enable    
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //Enable PORTA clock	
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA0
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Multiplexing function
	GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_100MHz; //Speed ​​100MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push-pull multiplexing output
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //Pull down
	GPIO_Init(GPIOA,&GPIO_InitStructure); //Initialize PA0

	GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5); //PA0 multiplex bit timer 5
  
	  
	TIM_TimeBaseStructure.TIM_Prescaler=psc; //Timer frequency division
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //Upward counting mode
	TIM_TimeBaseStructure.TIM_Period=arr; //Automatically reload value
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	
	TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);
	

	//Initialize TIM5 input capture parameters
	TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 Select input IC1 to map to TI1
        TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //Rising edge capture
        TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //Map to TI1
        TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //Configure input frequency division, no frequency division
        TIM5_ICInitStructure.TIM_ICFilter = 0x00; //IC1F=0000 configure input filter to not filter
        TIM_ICInit(TIM5, &TIM5_ICInitStructure);
		
	TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE); //Enable update interrupt, enable CC1IE to capture interrupt	
	
        TIM_Cmd(TIM5,ENABLE ); //Enable timer 5

 
        NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; //Preemption priority 3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //Subpriority 3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel enable
	NVIC_Init(&NVIC_InitStructure); //Initialize VIC registers according to the specified parameters,	
}

 

 

 

IX. Related initialization examples - PWM output

 

//TIM14 PWM initialization
//PWM output initialization
//arr: automatically reload value
//psc: clock pre-division number
void TIM14_PWM_Init(u32 arr,u32 psc)
{		 					 
	//This part requires manual modification of IO port settings
	
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14 clock enable    
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //Enable PORTF clock	
	
	GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GPIOF9 is multiplexed as timer 14
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOF9
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Multiplexing function
	GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_100MHz; //Speed ​​100MHz
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push-pull multiplexing output
	GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //Pull-up
	GPIO_Init(GPIOF,&GPIO_InitStructure); //Initialize PF9
	  
	TIM_TimeBaseStructure.TIM_Prescaler=psc; //Timer frequency division
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //Upward counting mode
	TIM_TimeBaseStructure.TIM_Period=arr; //Automatically reload value
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	
	TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure); //Initialize timer 14
	
	//Initialize TIM14 Channel1 PWM mode	 
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Select timer mode: TIM pulse width modulation mode 2
 	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Comparison output enable
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Output polarity: TIM output comparison polarity is low
	TIM_OC1Init(TIM14, &TIM_OCInitStructure); //Initialize peripheral TIM1 4OC1 according to the parameters specified by T

	TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //Enable TIM14 preload register on CCR1
 
        TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPE enable
	
	TIM_Cmd(TIM14, ENABLE); //Enable TIM14										  
}  
Keywords:STM32F4 Reference address:STM32F4 - Timer principle and application interrupt, input capture, PWM output

Previous article:stm32cube general purpose timer input capture
Next article:stm32 library function learning chapter general timer input capture function

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号