3. Button control

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.

  1. Import the ioc file from the LED project and name it BEEP.

image-20220309122850686

  1. 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.

image-20220308180058144

image-20220308180450165

image-20220308182008836

  1. 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.

image-20220308180924554

image-20220308180842219

image-20220308181859196

Save and generate code.

3.3. Analysis of the experimental flow chart

image-20220519094137132

 

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.

  1. 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

image-20220309141245124

image-20220309141714873

  1. 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.

image-20220309142715170

image-20220309142452345

  1. 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.

image-20220309143541320

  1. 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.

image-20220309144033676

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

image-20220309144531602

  1. Call Bsp_Init() in the main function.

image-20220309145032773

  1. Call Bsp_Loop() in while(1).

image-20220309145224144

3.5. core code explanation

  1. Create new buzzer driver library bsp_beep.h and bsp_beep.c files in BSP. Add the following to bsp_beep.h:

image-20220309150419133

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).

  1. Create new buzzer driver library bsp_key.h and bsp_key.c files in BSP. Add the following to bsp_key.h:

image-20220309151335531

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.

  1. 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.

image-20220309153015735

image-20220309153310583

3.6. hardware connection

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

image-20220326172515831

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.