3. Key control buzzer sounding3.1. Purpose of Experiment3.2 Configuring Pin Information3.3. Experimental flow chart analysis3.4 Adding File Structure3.5 The core code explanation3.6. Hardware Connection3.7. Experimental Effect
Detect the status of KEY1 on the expansion board and control the buzzer to sound. Every time you press a key, the buzzer sounds once.
As we need to configure the information every time we create a new project, it is quite troublesome, good thing STM32CubeIDE provides the function of importing .ioc file, which can help us save time.
Save and generate the code.
The pin information has been configured graphically, and the generated code already contains the system initialization content, so there is no need to initialize the system configuration additionally.
These two files are mainly responsible for linking the main.c part of the function, you can avoid duplication of code.
Where Beep_Timeout_Close_Handle() function needs to be called every 10 milliseconds, so as to ensure that the Beep_On_Time() function sets the time after the normal effect in accordance with the Beep_On_Time() function. time in Beep_On_Time(time) represents the time when the buzzer is turned on, if time=0 then the buzzer is turned off If time=0, the buzzer will be turned off, if time=1, the buzzer will be on all the time, if time>=10, the buzzer will be turned off automatically after time milliseconds (time should be a multiple of 10).
The function of Key1_State(mode) is to detect whether the key is pressed or not, and it needs to be called once every 10 milliseconds. mode can be entered as 0 or 1, mode=0 means that pressing KEY1 always returns KEY_PRESS, and releasing it only returns KEY_RELEASE, and mode=1 means that no matter how long KEY1 has been pressed, it will return KEY_PRESS once, and in all other cases, it will return KEY_RELEASE. mode=1 means that no matter how long KEY1 is pressed, it will only return KEY_PRESS once, and in all other cases, it will return KEY_RELEASE.
The key KEY1 and buzzer are on-board components and do not need to be connected manually.
After burning the program, the buzzer will first sound for 50 milliseconds when powering on, the LED will flash every 200 milliseconds, and the buzzer will sound for 50 milliseconds every time a key is pressed.