Based on STM32CubeMX and keil, the basic timer interrupt of STM32F407 is used to realize LED flashing

Preface

There are three types of timers, basic timers, general timers, and advanced timers.
This blog takes the simplest basic timer as an example to achieve LED flashing.
The usage of the latter two timers will be written later.

实现功能:
TIM6 controls the LED to change state every 0.5s.
TIM7 controls LED1 constant and goes out after 2s.


1. Understanding circuit schematics

Because LEDs are used, it is similar to the previous article based on STM32CubeMX and keil using button external interrupts to control LEDs and buzzers .
Just copy and paste it here.
Insert image description here
Insert image description here

LED部分:
The 3.3V voltage is connected to the DS0 light-emitting diode through a resistor R12. If there is a voltage drop between VCC and the corresponding pin of LED0, DS0 will be turned on and emit light.
The 3.3V voltage is connected to the DS1 light-emitting diode through a resistor R14. If there is a voltage drop between VCC and the corresponding pin of LED1, DS1 will be turned on and emit light.

Therefore, if we want to make the LED light up first, we need to make the pins corresponding to the LED0 and LED1 labels output low level to ensure that LED0 and LED1 light up. However, since our default state is that the LED is on, we need to set it to high voltage first. flat.

2. Basic timer

In the STM32F407ZET6 chip
, TIM6 and TIM7 are basic timers
TIM2-5, TIM9~14 are general timers
TIM1, and TIM8 is an advanced timer.

2.1 STM32 timer interrupt process:

定时器配置: First, you need to configure the parameters of the timer, such as clock source, prescaler coefficient, and auto-reload value of the counter. These parameters determine the counting speed and timing interval of the timer.

中断配置: Next, you need to configure the timer interrupt. In STM32, each timer has an interrupt enable bit and an interrupt flag bit. You can enable or disable timer interrupts by setting the interrupt enable bit. The interrupt flag bit is used to indicate whether an interrupt event has occurred. You can clear the interrupt flag bit in the interrupt handler.

中断处理函数: When the timer reaches the set count value, the timer interrupt will be triggered and jump to the corresponding interrupt vector table entry to execute the interrupt processing function. You need to implement the interrupt handler function to perform the required operations. In the interrupt handling function, you can perform some timing-related tasks, such as updating variables, sending data, or triggering other events.

中断优先级: When multiple interrupts occur at the same time, the priority determines which interrupt is processed first. STM32 provides priority group settings, and you can configure different interrupt priorities as needed.

For detailed explanation of timer related parameters, please refer to:

2.2 Detailed explanation of some parameters

2.2.1 Clock source

The STM32 microcontroller family offers a variety of clock source options to meet different application needs. Here are some common clock source options:

HSI(High-Speed Internal)内部高速时钟:HSI is a high-frequency oscillator integrated inside STM32, usually 16MHz. It is the default system clock source and starts automatically after the chip is powered on. HSI is suitable for most application scenarios and provides relatively high accuracy and stability.
HSE(High-Speed External)外部高速时钟:HSE uses an external crystal oscillator or clock source to provide a stable clock signal. The frequency range of HSE can vary depending on the specific chip model, usually 4MHz to 25MHz. External clock sources provide higher accuracy and stability than internal clocks.
LSI(Low-Speed Internal)内部低速时钟:LSI is a low frequency oscillator, usually 32kHz. It is used in some low power applications such as RTC (Real Time Clock) or Independent Watchdog Timer (IWDG).
LSE(Low-Speed External)外部低速时钟:LSE uses an external crystal oscillator or clock source to provide a low-speed clock signal. It is usually 32.768kHz and is used for RTC and low power modes.
PLL(Phase-Locked Loop)锁相环:PLL is a circuit used to generate high-frequency clocks. It can generate a higher frequency clock by multiplying and dividing the input clock signal (such as HSI or HSE). PLL provides flexible clock frequency adjustment capabilities and is suitable for high-performance applications.

Please note that specific clock source options and configuration methods may vary by chip model and series. When using a specific model of STM32 chip, you should consult the chip's data sheet and reference manual for detailed clock source configuration information.

2.2.2 Prescaler coefficient

The prescaler coefficient is used to set the division ratio of the timer clock frequency. It determines the step size of the timer counter increment in each clock cycle, thus affecting the counting speed and timing interval of the timer.

Specific prescaler options and configuration methods may vary by chip model and series. Here are some common prescaler configurations:

APB frequency division coefficient (PCLKx): APB (Advanced Peripheral Bus) is the bus used to connect peripherals in STM32. By configuring the APB frequency division coefficient, the system clock (SYSCLK) can be divided to obtain a clock frequency suitable for peripheral operation. Common prescaler options include 2, 4, 8, and 16.

TIMx prescaler coefficient: The timer module (TIM) has its own prescaler that further divides the frequency of the clock source. Specific prescaler options and configuration methods vary by chip model and timer module.

PLL multiplication coefficient (PLLM, PLLN, PLLP): If a PLL phase-locked loop is used to generate a high-frequency clock, the frequency of the PLL output clock can be adjusted by setting different frequency multiplication coefficients. PLLM is the input divider coefficient, PLLN is the frequency multiplier coefficient, and PLLP is the output divider coefficient.

2.2.3 Automatic reload value

The Auto-reload value is an important parameter used to control the overflow and reloading of the timer counter.

The timer counter is incremented every clock cycle and when the auto-reload value is reached, the counter is reloaded to its initial value and an interrupt is triggered (if enabled). This reloading operation allows the timer to periodically generate interrupts or trigger other events.

The size of the auto-reload value determines the timer interval. Typically, the setting of the auto-reload value can be calculated by the following formula:

自动重装载值 = (定时周期 / 时钟周期) - 1, minus one here because counting starts from 0, for example, 0~9 actually counts 10.

Among them, the timing period is the required timing interval, and the clock period is the clock frequency of the timer. The timer interval can be adjusted by changing the auto-reload value.

In STM32, the auto-reload value can be stored in the timer’s auto-reload register (ARR). Periodic operation of the timer is achieved by setting the auto-reload register to the desired value.

It is important to note that the auto-reload value should be chosen appropriately to ensure that overflow does not occur. The appropriate value should be chosen based on the required timing interval and the clock frequency of the timer to avoid overflow or too long timing intervals.


3. STM32CubeMX parameter configuration

3.1GPIO configuration

Insert image description here

Insert image description here

3.2 Clock configuration

Insert image description here

In order to facilitate our calculation, our final clock here is set to 100MHz, and the previous clock source and frequency division coefficient will be automatically set.
Insert image description hereThe clock given to the timer is actually obtained from APB1 and APB2. Because my blog is about basic timers, we need to find the clocks corresponding to the basic timers TIM6 and TIM7. Here is APB1.

Insert image description here

The buses where other timers of STM32F407ZET3 are located are listed here.

Timer type timer name The bus where the timer is located
basic timer TIM6、TIM7 APB1
Universal timer TIM2~5,TIM12~14 APB1
Universal timer TIM9~11 APB2
Advanced timer TEAM1, TEAM8 APB2

3.3 Configure timer related parameters

Insert image description here
Insert image description here

The frequency division coefficient here is 49999, which is 0~49999, and the length is 50000. The clock transmitted from our APB1 bus is 50MHz, so each clock cycle is 1ms.

APB 1 clock TIM 6 division coefficient = 50 ∗ 1 0 6 H z 5 ∗ 1 0 4 = 1 KH z = 1 T (time) = > T = 1 ms \frac{APB1 clock}{TIM6 division coefficient}= \frac{50*10^6Hz}{5*10^4}=1KHz=\frac{1}{T(time)}=>T=1msT I M 6 frequency division coefficientA PB 1 clock=510450106Hz=1 KHz=T ( time )1=>T=1 m s
For the LED controlled by TIM6, our goal is to switch the state of the LED once every 0.5s, so the count reload here is 500-1 because counting starts from 0.

When writing this, a simple understanding is that the clock is divided from the clock source to the bus, and the timer is divided again from the bus to be used as the division value of time. This statement may not be appropriate, but it means how many seconds a clock cycle is. Then use the count to overload the value*. The division value just now is the time we want. That is, how often and what to do.

The settings of TIM7 are the same, except that the count reload value is 1999, so the full picture will not be posted here.
Insert image description here


3.4 Debug configuration

Insert image description here

3.5 Interrupt configuration

In fact, after NVIC is turned on in the clock configuration, it is automatically checked and configured.
Insert image description herePriority preemption and response: When two interrupts occur and have different priorities, the higher-priority interrupt will preempt the executing low-priority interrupt. If multiple interrupts have the same priority, the order of interrupts is determined based on the settings of Preemption and Response. Let’s look at the preemptive priority first. The smaller the value in the front, the higher the priority. If it’s the same, look at the priority in the back. The smaller the value, the higher the priority.

In fact, this blog does not have very high priority requirements and will not involve preemption of priorities. So the default here is 0, 0 is fine.

Another thing is whether to generate the corresponding interrupt service function when the code is generated. They are all checked by default.
Insert image description here


3.6 Code generation

Insert image description here

Insert image description here


4. keil code writing

Open the stm32f4xx_it.c file in the generated project
Insert image description here
and add the following code directly at the bottom

Insert image description here

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim->Instance==TIM6)
	{
		HAL_GPIO_TogglePin(LED0_GPIO_Port,LED0_Pin);
	}
	if(htim->Instance==TIM7)
	{
		HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
	}
}

Explain why this function is rewritten. When the timer reaches the set count value, the timer interrupt will be triggered and jump to the corresponding interrupt vector table entry to execute the interrupt processing function. You need to implement the interrupt handler function to perform the required operations.

Insert image description here
Insert image description here
Insert image description here

If the count value exceeds 500, an interrupt will be triggered and the function callback function will be called. Here we can know it by looking at the function name. PeriodElapseCallback corresponds to the value we set.

Insert image description here


The last step is the burning program. This is the same as the first article of my column. Please refer to this blog for details.

Summarize

This blog records the simple use of timers in detail, starting from the goal to explaining the principle to configuration and code writing, and implementing the function step by step. The difficulty is not very high. The interrupt processing part has a lot in common with the previous article, and it is relatively fast to learn. It just takes more time to record. Part of the content of this blog was written using the chatGPT tool, and the effect was quite good, which indeed improved some efficiency.

Guess you like

Origin blog.csdn.net/Edwinwzy/article/details/131754055