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:
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.
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:
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:
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:
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 }
Previous article:stm32cube general purpose timer input capture
Next article:stm32 library function learning chapter general timer input capture function
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- How haptic technology can enhance driving safety
- Let’s talk about the “Three Musketeers” of radar in autonomous driving
- Why software-defined vehicles transform cars from tools into living spaces
- How Lucid is overtaking Tesla with smaller motors
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Wi-Fi 8 specification is on the way: 2.4/5/6GHz triple-band operation
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Vietnam's chip packaging and testing business is growing, and supply-side fragmentation is splitting the market
- Three steps to govern hybrid multicloud environments
- Three steps to govern hybrid multicloud environments
- TMS320F28377S LaunchPad XDS100 V2 driver cannot be installed
- Question about the delay circuit composed of transistors and capacitors
- Building a Raspberry Pi Pico development environment under Windows
- On-policy, its process is the same as the above strategic gradient. Practical analysis
- Answer the questions and win prizes | TDK special reports are waiting for you (Issue 2)
- Today is Chinese Valentine's Day, Alipay has launched an electronic marriage certificate, have you gone to pick up your partner?
- TI MSP430 series microcontroller serial communication baud rate calculation method
- What are the differences in the working principles of military radios and mobile phones? Are their power the same?
- 【Qinheng Trial】Four overall system designs (1/2)——Function integration of ch549 modules
- Share: Solution to BQ40Z80's constant hiccups