3. Button control3.1. Purpose of the experiment 3.2. configure pin information 3.3. Analysis of the experimental flow chart 3.4. add file structure 3.5. core code explanation 3.6. hardware connection 3.7. Experimental effect
Detect the state of KEY1 on the expansion board and control the buzzer to sound. Each time the button is pressed, the buzzer will sound once.
Since each new project needs configuration information, it is more troublesome. Fortunately, STM32CubeIDE provides the function of importing .ioc files, which can help us save time.
Save and generate code.
The pin information is configured graphically, and the generated code already contains the system initialization content, so there is no need to additionally initialize the system configuration.
These two files are mainly responsible for linking some functions in main.c, which can avoid repetitive code writing.
The Beep_Timeout_Close_Handle() function needs to be called every 10 milliseconds, so as to ensure that the Beep_On_Time() function will follow the normal effect after setting the time. The time in Beep_On_Time(time) represents the time when the buzzer is turned on. If time=0, the buzzer will be turned off. If time=1, the buzzer will keep ringing. If time>=10, the buzzer will sound after time milliseconds. Auto-off (time should be a multiple of 10).
The function of the Key1_State(mode) function is to detect whether the key is pressed, and it needs to be called every 10 milliseconds. You can enter 0 or 1 for mode, mode=0 means that pressing KEY1 will always return to KEY_PRESS, and releasing it will return to KEY_RELEASE, mode=1 means that no matter how long KEY1 is pressed, only return KEY_PRESS once, otherwise return KEY_RELEASE.
The buttons KEY1 and the buzzer are all onboard components and do not need to be connected manually.
After the program is programmed, the buzzer will ring for 50 milliseconds first, the LED light will flash every 200 milliseconds, and the buzzer will ring for 50 milliseconds each time the button is pressed.