Basic principles of universal timers
Differences between three STM32 timers
Description of general timer function features
Counter mode
The general timer can count up, count down, and count up and down in both directions.
①Up counting mode : The counter counts from 0 to the automatic load value (TIMx_ARR), then starts counting again from 0 and generates a counter overflow event.
②Down counting mode : The counter starts counting down to 0 from the automatically loaded value (TIMx_ARR), then restarts from the automatically loaded value, and generates a counter overflow event.
③ Center-aligned mode (counting up / down) : The counter starts counting from 0 to the automatically loaded value -1, generating a counter overflow event, then counts down to 1 and generates a counter overflow event; then starts from 0 again Recount.
Universal timer working process
Timer interrupt experiment-TIMER
clock selection
The counter clock can be provided by the following clock sources:
① Internal clock (CK_INT)
②External clock mode 1: External input pin (TIx)
③External clock mode 2: External trigger input (ETR)
④ Internal trigger input (ITRx): Use one timer as a prescaler for another timer. For example, you can configure a timer Timer1 as a prescaler for another timer Timer2.
Internal Clock Selection
Clock calculation method:
When the SystemInit function is called by default:
SYSCLK=72M
AHB clock=72M
APB1 clock=36M
So the frequency division coefficient of APB1=AHB/APB1 clock=2
Therefore, the general timer clock CK_INT=2*36M=72M
Up counting mode ( clock division factor = 1) (the same applies to downward and center alignment)
Timer interrupt experiment related registers
Auto-reload register (TIMx_ARR) control register 1 (TIMx_CR1 DMA interrupt enable register (TIMx_DIER)
Commonly used library functions
Timer parameter initialization:
void TIM_TimeBaseInit(TIM_TypeDef* TIMx,
TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);
Timer enable function:
void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
Timer interrupt enable function:
void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState);
FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);
void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);
ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT);
void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT);
Timer interrupt implementation steps
combat training
main.c:
After downloading the program, the onboard LED lights: the red light flashes once every 200ms, and the green light flashes once every 500ms.
Summarize
I haven’t summarized it for a long time. It was today that I discovered that when I used the previous NVIC, I forgot the path to the channel entry parameter of NVIC_IRQChannel, so I went back and looked at the previous one and remembered it. I also forgot the path to the interrupt service function. Marked again today. But the other basics have not been forgotten, after all, go to definition is a good thing hahaha.