Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use stm32 universal timer

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use stm32 universal timer, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Stm32 timers are numerous and complex, but complexity means power. I am using stm32f103ZET6, and there are two basic timers TIM6 and TIM7 that can be used to trigger DAC,4 general timers TIM2,TIM3,TIM4,TIM5, two advanced timers TIM1 and TIM8, and two dedicated timers SysTick and RTC.

TIM2,3,4,5,6 is hung on APB1 bus, TIM1,8 is hung on APB2 bus, and the default clock of V3.5 firmware library is APB1=36MHz,APB2=72MHz. However, because the APB1 pre-division factor is 1 frequency * 2 for timers, the timer count clock TIMXCLK is 72MHz.

General timer function: 1, basic time base function timing. 2. Input capture (can be used to measure pulse period and duty cycle). 3. Output comparison (four channels can be used as four timers). 4. PWM function. 5. Orthogonal encoder.

Basic timer: first and foremost, of course, the timer clock is configured. If TIMXCLK is 72MHz, divide the clock according to your needs. Frequency division is to assign values to the two members of the structure.

Tim _ Prescaler / / this member is pre-divided, which corresponds to a 16-bit frequency division register, which means you can do 1-65536 frequency division. This frequency division can generally meet the demand. Tim _ ClockDivision / / this is the frequency division may have an impact on the filter when counting outside the timer, so just don't use it, directly assign it to TIM_CKD_ DIV1 frequency division, that is, no frequency division. Because the frequency division of .Tim _ Prescaler is enough.

The simple timing function is configured as follows:

Void tim2Config (void) / / simple timing function {TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE); / / enable clock TIM_TimeBaseStructInit (& TIM_TimeBaseInitStructure); TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up / / count up TIM_TimeBaseInitStructure.TIM_Prescaler = 71; / / 71pm 1 division, 1MHz cycle 1us TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; / / clock non-division TIM_TimeBaseInitStructure.TIM_Period = 1000-1; / / overflow period 1000 = 1ms TIM_TimeBaseInit (TIM2, & TIM_TimeBaseInitStructure) TIM_ClearFlag (TIM2, TIM_FLAG_Update); TIM_ITConfig (TIM2, TIM_IT_Update, ENABLE); / / interrupt TIM_Cmd (TIM2, ENABLE) occurs when overflow interrupt count is enabled to TIM_Period; / / enable TIM2 NVIC_PriorityGroupConfig (NVIC_PriorityGroup_4) / / configure interrupt vector NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init (& NVIC_InitStructure);}

About the interrupt vector, you can take a look at this blog: http://www.cnblogs.com/dyllove98/archive/2013/08/01/3230973.html

Interrupt function:

Void TIM2_IRQHandler (void) {if (TIM_GetITStatus (TIM2, TIM_IT_Update)! = RESET) / / whether it is an overflow interrupt {TIM_ClearITPendingBit (TIM2, TIM_FLAG_Update); / / clear interrupt suspend bit}}

Note here that the interrupt suspend bit must be cleared, otherwise it will be interrupted all the time.

2. PWM mode:

The simple way to understand PWM mode is that there are two registers, one is the cycle register and the other is the comparison register. The cycle register controls the cycle, and the comparison register controls the effective level (whether the high level or low level is determined by the value of .Tim _ OCPolarity_High) time. If the counter register counts from zero when counting up, the count remains at a valid level until it reaches the value in the comparison register, and then another level. When counting to the value in the cycle register, a cycle is completed. That makes it PWM.

1. Configure GPIO mode. 2. Configure the TIM_TimeBaseInitTypeDef structure. 3. Configure the TIM_OCInitTypeDef structure.

Static void TIM3_GPIO_Config (void) {GPIO_InitTypeDef GPIO_InitStructure; / * TIM3 clock enable * / / PCLK1 is equal to 72MHz RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM3, ENABLE) as the clock source of TIM3 after 2-octave; / * GPIOA and GPIOB clock enable * / RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE) / * GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull * / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; / / reuse push-pull output GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (GPIOA, & GPIO_InitStructure) / * GPIOB Configuration: TIM3 channel 3 and 4 as alternate function push-pull * / GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_Init (GPIOB, & GPIO_InitStructure) } / * * function name: TIM3_Mode_Config * description: configure the mode of PWM signal output by TIM3, such as period, polarity, duty cycle * input: none * output: none * call: internal call * / static void TIM3_Mode_Config (void) {TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure / * PWM signal level jump value * / U16 CCR1_Val = 500; U16 CCR2_Val = 375; U16 CCR3_Val = 250; U16 CCR4_Val = 125 / *-TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles: TIM3CLK = 72 MHz, Prescaler = 0x0 TIM3 counter clock = 72 MHz TIM3 ARR Register = 999 = > TIM3 Frequency = TIM3 counter clock/ (ARR + 1) TIM3 Frequency = 72 KHz. TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR) * 100 = 50% TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR) * 100 = 37.5% TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR) * 100 = 25% TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR) * 100 = 12.5%-- -- * / * Time base configuration * / TIM_TimeBaseStructure.TIM_Period = 999 / / when the timer counts from 0 to 999, that is, 1000 times, a timing period TIM_TimeBaseStructure.TIM_Prescaler = 71; / / set pre-division: no pre-division, that is, 72MHz TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; / / set clock division factor: no frequency division TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; / / upward counting mode TIM_TimeBaseInit (TIM3, & TIM_TimeBaseStructure) / * PWM1 Mode configuration: Channel1 * / TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; / / configure to PWM mode 1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; / / set the jump value, when the counter counts to this value, the level jumps TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High / / High level TIM_OC1Init (TIM3, & TIM_OCInitStructure) when the timer count is less than CCR1_Val; / / enable channel 1 TIM_OC1PreloadConfig (TIM3, TIM_OCPreload_Enable); / * PWM1 Mode configuration: Channel2 * / TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val / / set the level jump value of channel 2, and output another duty cycle PWM TIM_OC2Init (TIM3, & TIM_OCInitStructure); / / enable channel 2 TIM_OC2PreloadConfig (TIM3, TIM_OCPreload_Enable); / * PWM1 Mode configuration: Channel3 * / TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR3_Val / / set the level jump value of channel 3, and output another duty cycle PWM TIM_OC3Init (TIM3, & TIM_OCInitStructure); / / enable channel 3 TIM_OC3PreloadConfig (TIM3, TIM_OCPreload_Enable); / * PWM1 Mode configuration: Channel4 * / TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR4_Val / / set the level jump value of channel 4 and output another duty cycle PWM TIM_OC4Init (TIM3, & TIM_OCInitStructure); / / enable channel 4 TIM_OC4PreloadConfig (TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig (TIM3, ENABLE); / / enable TIM3 overload register ARR / * TIM3 enable counter * / TIM_Cmd (TIM3, ENABLE) / / enable timer 3} / * * function name: TIM3_PWM_Init * description: TIM3 output PWM signal initialization. As long as you call this function * four channels of TIM3, there will be PWM signal output * input: none * output: none * call: external call * / void TIM3_PWM_Init (void) {TIM3_GPIO_Config () TIM3_Mode_Config ();}

Changing the duty cycle of PWM means changing the value of the comparison register. Use this function

Void TIM_SetCompare1 (TIM_TypeDef* TIMx, uint16_t Compare1); void TIM_SetCompare2 (TIM_TypeDef* TIMx, uint16_t Compare2); void TIM_SetCompare3 (TIM_TypeDef* TIMx, uint16_t Compare3); void TIM_SetCompare4 (TIM_TypeDef* TIMx, uint16_t Compare4)

The method is simple, for example, to change the duty cycle of TIM3 channel 1, TIM_SetCompare1 (TIM3, 900); 900 is the value of the new comparison register.

The above is how to use stm32 universal timer. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report