yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
STM32-BoardCar📖
0. Get started quickly
  • 0.ReadMe
  • 1. Installation Instructions
  • 2. Version configuration
  • 3. Smart car installation video
  • 4. Precautions for use
1. Introduction to STM32 development board
  • 1. Learn about STM32
  • 2. Introduction to STM32 development board
  • 3. Development board pin assignment
  • 4. Introduction to HAL library and LL library
2. Setting up and using the development environment
  • 1. Common development tools
  • 2. STM32CubeMX Install - Use
  • 3. STM32CubeIDE Install - Use
  • 4. Common Driver Installation
  • 5. Program download and simulation
  • 6. Engineering experience and migration
3. Development board basic tutorial
  • 1. RGB searchlight (GPIO)
  • 2. Buzzer control (GPIO)
  • 3. Button control (EXTI)
  • 4. Serial communication (UASRT)
  • 5. Voltage detection (ADC)
  • 6. Internal temperature sensor(ADC)
  • 7. Independent watchdog(IWDG)
  • 8. Window watchdog(WWDG)
  • 9. Basic timer (TIM6)
  • 10. Universal Timer (TIM3)
  • 11. Advanced timers (TIM1)
  • 12. I2C Communication (IIC)
  • 13. Off-chip Flash (SPI)
  • 14. On-chip Flash (Flash)
  • 15. DMA:USART
  • 16. DMA:SPI
  • 17. DMA:I2C
  • 18. USB communication
  • 19. USB analog U disk
  • 20. FreeRTOS
4. Car expansion tutorial
  • 1. Servo control
  • 2. Basic use of ultrasonic steering gear gimbal
  • 3. Motor control
  • 4. OLED display light sensitivity value
  • 5. Infrared sensor
  • 6. Four-way tracking module
  • 7. Infrared remote control
  • 8. Ultrasonic ranging
  • 9. PS2 controller test
  • 10.K230 Vision Module
  • 11.AI voice interaction integration module
5. Robot car tutorials
  • 1. Car line patrol
  • 2. Ultrasonic follow
  • 3. Ultrasonic obstacle avoidance
  • 4. Ultrasonic obstacle avoidance (servo gimbal version)
  • 5. Infrared sensor obstacle avoidance
  • 6. Infrared remote control
  • 7. PS2 handle remote control
  • 8.K230 face tracking on PTZ
  • 9.K230 color tracking
  • 10.K230 Color Line Patrol
  • 11.K230 QR code command
  • 12.K230 machine code tracking
  • 13.K230 road sign indication action (voice)
  • 14.K230 color response (voice)
  • 15.K230 gesture control (voice)
  • 16.K230 Rock-Paper-Scissors Game (Voice)
6. Operation video
  • 1-How to install STM32CubeIDE software
  • 2-How to create Demo project
  • 3-How to download code into STM32 robot car
  • 4-How to open and compile a code file for STM32
  • 5-About IR control and Charge battery
Download
  • Code
  • SCH
  • Software File
  • STM32 Chip DataSheet
  • Installation manual
Download ZIP

Welcome to STM32-BoardCar repository

9. Basic timer (TIM6)

Basic timers

Basic timers1、software-hardware2、Brief principle2.1、Hardware schematic diagram2.2、Physical connection diagram2.3、Principle of control3、Engineering configuration3.1、Tips3.2、Pin configuration4、Main functions4.1、User function4.2、HAL library function parsing5、Experimental phenomenon

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

1、software-hardware

  • STM32F103CubeIDE

  • SSTM32 Robot Development Board

    TIM: Chip internal peripherals

    LED light: onboard

  • Type-C cable or ST-Link

    Download or simulate the program of the development board

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.

  • GPIO output

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
  • Basic timers

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.

xxxxxxxxxx
Please refer to [2. Development environment construction and use: STM32CubeIDE installation and use] to understand how to configure the omitted parts of the project.

3.2、Pin configuration

  • Configure the specified pin function

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

  • GPIO

image-20231030154122729

  • Timers

image-20231011101451859

  • NVIC

image-20231011101919947

  • Advanced Settings

image-20231028215554626

  • Generating code

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
xxxxxxxxxx
/*
If the HAL library is used, the HAL_TIM_IRQHandler function is invoked to automatically handle timer interrupts.
If the HAL library is not used, the timer update interrupt is checked to see if the interrupt bit is set.
*/
void TIM6_IRQHandler(void)
{
#if USE_HAL_IRQ
  HAL_TIM_IRQHandler(&htim6);//Using HAL_TIM_PeriodElapsedCallback is automatically invoked
#else
  if (__HAL_TIM_GET_FLAG(&htim6, TIM_FLAG_UPDATE) != RESET)//Check if the TIM update interrupt occurred
    {
      if (__HAL_TIM_GET_IT_SOURCE(&htim6, TIM_IT_UPDATE) != RESET) //Check if interrupts for TIM6 are enabled
      {
        __HAL_TIM_CLEAR_IT(&htim6, TIM_IT_UPDATE);//Clearing interrupts
        g_time++;
        if(g_time % 100 == 0)//1s = 100*10ms
         {
             g_time = 1;
             HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
         }
        else if(g_time % 50 == 0)//500ms = 50*10ms
         {
             HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
         }
      }
    }
#endif
}

HAL_TIM_PeriodElapsedCallback:The 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
xxxxxxxxxx
/*
HAL library internal timer update interrupt callback function
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
     if(htim->Instance==TIM6)
      {
         g_time++;
      }
     if(g_time % 100 == 0)//1s = 100*10ms
     {
         g_time = 1;
         HAL_GPIO_TogglePin(LED1_GPIO_Port,LED1_Pin);
     }
     if(g_time % 50 == 0)//500ms = 50*10ms
     {
         HAL_GPIO_TogglePin(LED2_GPIO_Port,LED2_Pin);
     }
}

4.2、HAL library function parsing

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

xxxxxxxxxx
If you want to find the HAL library and LL library function analysis involved in the entire tutorial, you can view the documents in the folder [8. STM32 Manual: STM32F1_HAL Library and LL Library_User Manual]

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

xxxxxxxxxx
For program download, please refer to [2. Development environment construction and use: program download and simulation]

现象:

LED1:The bright-off time interval is 1 second

LED2:The bright-off time interval is 0.5 seconds

Experimental phenomena can be found in [Basic Timer_Experimental Phenomenon.mp4]
Window watchdog(WWDG)
Universal Timer (TIM3)

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文