Button control

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

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 controls the interrupt-related functions of the entire chip and provides a reliable interrupt processing mechanism for the system.

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.

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

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.

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

EXTI9_5_IRQHandler

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.