Basic timer

The tutorial demonstrates how to use the basic timer (TIM6) to control the onboard LED on the development board to flash.

Hardware connection

image-20240825204024431

PeripheralsDevelopment boardDescription
LEDPB3The anode of the LED is connected to the development board PB3, and the cathode is connected to the development board GND

Control principle

Use the timing function of TIM6 on the STM32F103RCT6 development board

Use the GPIO output function to control the LED and implement the timing function through the basic timer.

Use the timing function of TIM6 on the STM32F103ZET6 development board

Timer typeBasic timer
Timer nameTIM6, TIM7
Counter bit number16
Counting modeIncrementing
Prescaler coefficient1-65536
Generate DMA requestYes
Capture/compare channel0
Complementary outputNone
Clock frequency72MHz (maximum)
Mount busAPB1

Time base unit

RegisterFunction
Counter register (TIMx_CNT)Current count value of the counter
Prescaler register (TIMx_PSC)Set the scaler coefficient (1-65536)
Auto-reload register (TIMx_ARR)Counter count boundary and reload value

Timing formula

T(s)=(ARR+1)(PSC+1)TIM_CLK(Hz)
ParameterMeaning
T(s)Timing time, in seconds
ARRAuto-reload value
PSCPrescaler coefficient
TIM_CLKTimer clock, in Hz

Timing time for this project: 10ms

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

Software configuration

Software code

Configure the timing function of the basic timer TIM6, without configuring specific pins.

Control function

The tutorial only briefly introduces the code, you can open the project source code to read it.

TIM6_Init

TIM6_IRQHandler

cotrol_led

Experimental phenomenon

The Base_Time.hex file generated by the project compilation is located in the OBJ folder of the Base_Time project. Find the Base_Time.hex file corresponding to the project and use the FlyMcu software to download the program into the development board.

After the program is successfully downloaded: the LED flashes three times every 3 seconds.