Basic timers

This tutorial demonstrates controlling the onboard LED1 and LED2 blinkers on the dev board using a basic timer (TIM6).

1、software-hardware

2、Brief principle

2.1、Hardware schematic diagram

image-20231030230259805

2.2、Physical connection diagram

image-20231014153242365

2.3、Principle of control

The GPIO output function is used to control the LED, and the timing function is realized through the basic timer.

By controlling the high and low level of the LED lamp pin to control the color of the LED lamp display.

LED:High level light, low level off

LED(Schematic name)Control pinFunctions
LED1PG14Control LED1 to turn on and off
LED2PG15Control LED2 to turn on and off

The timing function of TIM6 on the STM32F103ZET6 development board was used

Timer typesBasic timer
The name of timerTIM6、TIM7
Number of counter bits16
Counting modeincrementally
Predivision coefficient1-65536
Generating DMA requestsYes
Capture/compare channels0
Complementary outputNo
Clock frequency72MHz (Max)
Mount busAPB1

Time base unit

registerFunciton
The counter register(TIMx_CNT)The current value of the counter
Predivision register(TIMx_PSC)Set frequency division coefficient (1-65536)
Automatically reload registers(TIMx_ARR)The counter counts the boundary and the overloaded value

Timing formula

T(s)=(ARR+1)(PSC+1)TIM_CLK(Hz)
parametersMeaning
T(s)Timing time in seconds
ARRAutomatically reload the value
PSCPredivision coefficient
TIM_CLKThe timer ticks in Hz

Timing time for the project: 10ms

T(s)=(ARR+1)(PSC+1)TIM_CLK(Hz)=(99+1)(7199+1)72000000(Hz)=0.01s

3、Engineering configuration

Project Configuration: Prompts for configuration options in the STM32CubeIDE project configuration process

3.1、Tips

Omitted project configuration: New project, chip selection, project configuration, SYS for pin configuration, RCC configuration, clock configuration, and project configuration content

The project configuration part, which is not omitted, is the key point to configure in this tutorial.

3.2、Pin configuration

You can directly select the corresponding pin number in the pin view, and the corresponding option will appear when the mouse is left clicked

d3d723bc194dac4ec7b14d02d903fcf1

image-20231030154122729

image-20231011101451859

image-20231011101919947

image-20231028215554626

image-20231011102854602

4、Main functions

This section mainly introduces the functional code written by users. Detailed code can be opened by yourself in the project file we provide, and enter the Bsp folder to view the source code.

4.1、User function

HAL_TIM_IRQHandler:The timer interrupt service function

Function prototypesvoid HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
Functional DescriptionThe timer interrupt service function
Input parametershtim:Timer handle address
Return valueNone
Notes1. Internally, the function needs to determine the interrupt type and clear the corresponding interrupt flag, and finally call the callback function
Define the USE_HAL_IRQ macro to switch between different interrupt handling functions

HAL_TIM_PeriodElapsedCallbackThe timer update interrupt callback function

Function prototypesvoid HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
Functional DescriptionThe timer update interrupt callback function
Input parametershtim:Timer handle address
Return valueNone
NotesThis function is called by HAL_TIM_IRQHandler, which allows you to write specific tasks

4.2、HAL library function parsing

The HAL library functions that were covered in the previous tutorial will not be covered

function:HAL_TIM_Base_Init

Function prototypesHAL_StatusTypeDef HAL_TIM_Base_Init(TIM_HandleTypeDef *htim)
Functional DescriptionInitialize the timer base unit
Input parametershtim:Timer handle address
Return valueHAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT
NotesThis calls the MCU low-level initialization function HAL_TIM_Base_MspInit to set the pins, clocks, and interrupts

function:HAL_TIM_Base_MspInit

Function prototypevoid HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim);
Functional descriptionInitialize the peripheral clock, GPIO, and NVIC for the timer
Input parametershtim:Timer handle address
Return valueNone

function:HAL_TIM_Base_MspDeInit

Function prototypevoid HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim)
Functional descriptionUninitialize the timer peripheral clock, GPIO, and NVIC
Input parametershtim:Timer handle address
Return valueNone

function:HAL_TIM_Base_Start_IT

Function prototypeHAL_StatusTypeDef HAL_TIM_Base_Start_IT(TIM_HandleTypeDef *htim)
Functional descriptionStarts the timer and enables the interrupt function of the timer能
Input parametershtim:Timer handle address
Return valueHAL status value:HAL_OK、HAL_ERROR、HAL_BUSY、HAL_TIMEOUT
NotesThis function needs to be called by the user to enable and start the update interrupt of the timer

5、Experimental phenomenon

After downloading the program successfully, press the RESET button of the development board to observe the phenomenon of the development board

现象:

LED1:The bright-off time interval is 1 second

LED2:The bright-off time interval is 0.5 seconds