[Learning Record] Basic principles of STM32 universal timer + timer interrupt experiment-TIMER

Basic principles of universal timers

Differences between three STM32 timers

Description of general timer function features

         Located on the low-speed APB1 bus (APB1)
        16-bit up, down, up/down (center-aligned) counting mode, autoload counter (TIMx_CNT).
        16-bit programmable (can be modified in real time) prescaler (TIMx_PSC), the division coefficient of the counter clock frequency is any value between 1 and 65535.
        4 independent channels (TIMx_CH1~4), these channels can be used as: 
① Input capture 
② Output comparison
③ PWM generation (edge ​​or center alignment mode) 
④ Single pulse mode output 
        Synchronization circuits for timers and timer interconnections (one timer can be used to control another timer) can be controlled using an external signal (TIMx_ETR).
        Interrupt/DMA generation (6 independent IRQ/DMA request generators) when the following events occur: 
①Update: Counter overflows/underflows, counter initialization (via software or internal/external trigger) 
②Trigger event (counter start, stop, initialization or internal/external trigger counting) 
③Input capture 
④Output comparison 
⑤Support incremental (orthogonal) encoder and Hall sensor circuits for positioning 
⑥Trigger input as an external clock or cycle-by-cycle current management
        The general-purpose timer of STM32 can be used to: measure the pulse length of the input signal (input capture) or generate the output waveform (output comparison and PWM), etc.   
         Using the timer prescaler and RCC clock controller prescaler, the pulse length and waveform period can be adjusted from a few microseconds to a few milliseconds. Each general-purpose timer of STM32 is completely independent and does not share any resources with each other.

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

Counter current value register CNT
 Prescaler register TIMx_PSC

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);

Status flag acquisition and clearing

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

①Can timer clock.
RCC_APB1PeriphClockCmd();
②Initialize timer, configure ARR, PSC
TIM_TimeBaseInit ();
③Enable timer interrupt and configure NVIC .
v oid TIM_ITConfig ();
NVIC_Init ();
  Enable the timer.
TIM_Cmd ();
⑤Write    interrupt service function.
TIMx_IRQHandler ();

combat training

Program requirements: Configure the timer interrupt to interrupt every 500ms, and then control the LED in the interrupt service function to reverse the LED1 status (flash).
Tout (overflow time) = (ARR+1)(PSC+1)/Tclk
Tclk/(PSC+1): The length of time of one cycle of the timer
Example: System initialization function to initialize APB1, frequency division coefficient is 2, Tclk = 72M
The PSC can be 7199 and the ARR can be 499, which meets the program requirements.
Core code:
timer.c:

 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.

 

Guess you like

Origin blog.csdn.net/weixin_47723114/article/details/127658181