Welcome to ROSMASTER X3 repository
3. Button control
3. Button control
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
3.1. Purpose of the experiment
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.
3.2. configure pin information
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.
- Import the ioc file from the LED project and name it BEEP.

- According to the schematic diagram, the control pin of the buzzer is connected to the PC5 pin of the STM32 chip. It is necessary to set PC5 to GPIO_Output mode, and modify the label (Label) to BEEP. Other configurations are shown in the following figure.



- The button KEY1 is connected to the PD2 pin. It is necessary to set PD2 to GPIO_Input mode, and modify the label (Label) to KEY1. Other configurations are shown in the following figure.



Save and generate code.
3.3. Analysis of the experimental flow chart

3.4. add file structure
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.
- For the convenience of management, we create a new BSP source code folder. Right-click on the mouse to move the project name->New->Source Folder


- Add the BSP to the environment. Click Project->Properties->C/C++ Build->settings->MCU GCC Compiler->include paths, and then click the Add button to fill in ../BSP and save it.


- Create a new bsp.h and a bsp.c file, right-click BSP->New->Header File/Source File, and then enter the corresponding name.
These two files are mainly responsible for linking some functions in main.c, which can avoid repetitive code writing.

- Add the following content in bsp.h: Make the control of the LED into a macro definition, which is simple and fast. The new Bsp_Init() function is mainly responsible for initialization, and Bsp_Loop() is mainly responsible for the main program content. The Bsp_Led_Show_State_Handle() function is mainly responsible for the blinking effect of the LED indicator, which is used to indicate that the system is running.

- Import the bsp.h header file in the main.c file.

- Call Bsp_Init() in the main function.

- Call Bsp_Loop() in while(1).

3.5. core code explanation
- Create new buzzer driver library bsp_beep.h and bsp_beep.c files in BSP. Add the following to bsp_beep.h:

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).
- Create new buzzer driver library bsp_key.h and bsp_key.c files in BSP. Add the following to bsp_key.h:

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.
- Add the start-up buzzer in Bsp_Init() to sound for 50 milliseconds, and detect whether the button is pressed in Bsp_Loop(). If it is pressed, it will automatically turn off after 50 milliseconds. At the bottom are the control handles for the LED lights and buzzer, which only need to be called every 10 milliseconds.


3.6. hardware connection
The buttons KEY1 and the buzzer are all onboard components and do not need to be connected manually.

3.7. Experimental effect
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.