yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
Self-balancing Robot Car📖
00.Get started quickly
  • 01.Balance car manual
  • 02.Balancing car assembly
  • 03.Get started quickly
  • 04.Balance car interface description
  • 05.Common Problem Analysis
  • 06.Balance Car V2 Library Function Development Manual
01.Environment development
  • 01.MDK-ARM installation
  • 02.STM32CubeIDE installation
  • 03.Common driver installation
  • 04.Download the program
  • 05.MDK-ARM project usage
  • 06.Program simulation
  • 07. VSCode Installation
02.STM32 getting started course
  • 01.Project creation
  • 02.LED Control(GPIO)
  • 03.Buzzer control(GPIO)
  • 04.Button control(EXTI)
  • 05.Serial communication(USART)
  • 06.Voltage detection(ADC)
  • 07.Basic timer(TIM)
  • 08.Universal timer(TIM)
  • 09.Advanced Timer(TIM)
  • 10.I2C Communication(I2C)-OLED
  • 11.SPI communication(SPI)-2.4G handle
  • 12.Independent Watchdog (IWDG)
  • 13.Window Watchdog (WWDG)
  • 14.DMA-USART
  • 15.DMA-I2C
03.STM32 expansion course
  • 01.Battery voltage detection (ADC)
  • 02.Ultrasonic module-measuring distance (TIM)
  • 03.Motor drive+encoder (TIM)
  • 04.OLED Data-Display(I2C)
  • 05.MPU6050-Data Read (I2C)
  • 06.Bluetooth module-Data reading (USART)
  • 07.2.4G handle-Data reading (SPI)
  • 08.Tracking module-Read status (GPIO)
  • 09.CCD module-Reading data (ADC)
  • 10.Electromagnetic module-Reading data (ADC)
  • 11.K210 module-Serial communication (USART)
  • 12.Tmini-Plus lidar-Reading data (USART)
04.Robot car PID control course
  • 01.PID basic concept
  • 02.PID example analysis
  • 03.P, PI, PD controller theory
  • 04.Position PID
  • 05.Incremental PID
  • 06.Cascade PID
  • 07.Balance principle of car
  • 08.Car upright control (PD)
  • 09.Car speed ​​control (PI)
  • 10.Car steering control (PD)
  • 11.Got angle and angular velocity (DMP algorithm)
  • 12.Got angle and angular velocity (Kalman filter algorithm)
  • 13.Got angle and angular velocity(Complementary filter algorithm)
05.Robot car basic course
  • 01.Car parameter adjustment
  • 02.Ultrasonic obstacle avoidance
  • 03.Ultrasonic follow
  • 04.Bluetooth remote control
  • 05.Load balance
06.Robot car advanced course
  • 01.4-channel tracking
  • 02.4-channel tracking avoid
  • 03.2.4G handle control
  • 04.CCD tracking
  • 05.CCD tracking avoid
  • 06.Electromagnetic tracking
  • 07.K210-QR code recognition
  • 08.K210-Color line tracking
  • 09.K210-Color follow
  • 10.K210-Self learning
  • 11.K210-Number recognition
  • 12.Lidar avoid
  • 13.Lidar guard
  • 14.Lidar follow
  • 15.Lidar patrol
  • 16.Lidar wall tracking-straight line
  • 17.Lidar wall tracking-multiple walls
  • 18. DIY Automatic driving car
07.Robot car LQR control
  • Robot car LQR control
08.Circuit design
  • 01.Overall circuit design
  • 02.STM32 minimum system
  • 03.Serial port circuit design
  • 04.Power circuit design
  • 05.MPU6050 Circuit Design
  • 06.Motor drive circuit design
09.Mathematical model
  • Mathematical model
10.Expand course
  • Autonomous Driving
Download
  • APP(Android)
  • Codes
  • Hardware-Info
  • SCH
  • Software Tools
  • STM32_Chip_DataSheet
  • 3D_Model_File
  • Video_PPT
Download ZIP

Welcome to Self-balancing Robot Car repository

04.Button control(EXTI)

Button control

Button controlHardware connectionControl principleSoftware configurationPin definitionSoftware codeControl functionExperimental phenomenon

The tutorial demonstrates that the button controls the buzzer to sound, and the LED keeps flashing.

The tutorial only introduces the standard library project code

Hardware connection

image-20240825204524168

PeripheralsDevelopment boardDescription
KEY1PA8One end of KEY is connected to the PA8 pin, and the other end is connected to GND

Control principle

Trigger an external interrupt through a button, and switch the buzzer sound state according to the triggered button.

Control pinRead levelLED effect
PA8Read low levelSwitch buzzer sound state
PA8Read high levelDo not switch buzzer sound state
  • NVIC (Nested Vector Interrupt Controller)

NVIC controls the interrupt-related functions of the entire chip and provides a reliable interrupt processing mechanism for the system.

  • Interrupt priority

Preemption priority: Preemption priority is used to determine the relative priority between interrupts. When an interrupt with a high preemption priority occurs, it can interrupt the interrupt with a low preemption priority being executed;

Response priority: When multiple interrupt sources have the same preemption priority, the interrupt with a high response priority will be processed first, and will not be interrupted by the interrupt with the same preemption priority but a low response priority.

  • EXTI (External Interrupt)

The external interrupt/event controller consists of 19 edge detectors that generate event/interrupt requests.

Each input line can be independently configured with input type (pulse or hang-up) and corresponding trigger event (rising edge or falling edge or both edges), and each input line can be independently masked.

External interrupt/event line image

image-20231028234649354

xxxxxxxxxx
EXTI0~EXIT4 have independent interrupt channels
EXTI5~EXIT9 share the same interrupt channel: EXTI9_5_IRQ
EXTI10~EXIT15 share the same interrupt channel: EXTI15_10_IRQ

Key corresponding external interrupt trigger mode: falling edge trigger

Software configuration

Pin definition

Main control chipPinMain function (after reset)Default multiplexing functionRedefine function
STM32F103RCT6PA8PA8USART1_CK/TIM1_CH1/MCO 

Software code

The default function of the PA8 pin is a normal IO pin function.

xxxxxxxxxx
Product supporting data source code path: Attachment → Source code summary → 1.Base_Course → 4.KEY

Control function

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

Key1_GPIO_Init

xxxxxxxxxx
void Key1_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_APB2PeriphClockCmd(KEY1_GPIO_CLK, ENABLE);
    GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_Init(KEY1_GPIO_PORT, &GPIO_InitStructure);
    
    EXTI_InitStructure.EXTI_Line=EXTI_Line8;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; 
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    
    NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure); 
}

EXTI9_5_IRQHandler

xxxxxxxxxx
void EXTI9_5_IRQHandler(void)
{
    if(KEY_INT==0)
    {
        EXTI->PR=1<<8; // Clear interrupt flag
        BEEP_BEEP = !BEEP_BEEP;
    }
}

Experimental phenomenon

The KEY.hex file generated by the project compilation is located in the OBJ folder of the KEY project. Find the KEY.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 switches on and off every 500ms; each time the KEY1 button is pressed, the sound state will be switched.

Buzzer control(GPIO)
Serial communication(USART)

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
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文