3. Key control buzzer sounding

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

3.1. Purpose of Experiment

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.

3.2 Configuring Pin Information

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.

  1. Import the ioc file from the LED's 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. You need to set PC5 to GPIO_Output mode and modify the label to BEEP, and other configurations are shown in the figure below.

image-20220308180058144

 

image-20220308180450165

image-20220308182008836

  1. Key KEY1 is connected to the PD2 pin. You need to set PD2 to GPIO_Input mode and change the label to KEY1, other configurations are shown below.

image-20220308180924554

image-20220308180842219

image-20220308181859196

Save and generate the code.

3.3. Experimental flow chart analysis

image-20220309161121839

3.4 Adding File Structure

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.

  1. For management convenience, we create a new BSP source code folder. In the mouse over the name of the project where the right click -> 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 add the... /BSP in 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 can be.

These two files are mainly responsible for linking the main.c part of the function, you can avoid duplication of code.

image-20220309143541320

  1. in bsp.h add the following: the control of the LED into a macro definition of the way, simple and fast. New Bsp_Init () function is mainly responsible for the initialization, Bsp_Loop () is mainly responsible for the content of the main program. Bsp_Led_Show_State_Handle () function is mainly responsible for the LED indicator flashing effect, used to prompt the system is running.

image-20220309144033676

  1. In the main.c file import bsp.h header file.

image-20220309144531602

  1. Call Bsp_Init() in the main function.

image-20220309145032773

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

image-20220309145224144

3.5 The core code explanation

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

image-20220309150419133

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

  1. In the BSP in the new buzzer driver library bsp_key.h and bsp_key.c file. In bsp_key.h add the following content:

image-20220309151335531

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.

  1. In Bsp_Init(), add power-on buzzer for 50 ms, and in Bsp_Loop(), detect whether the key is pressed or not, if it is pressed, it will sound for 50 ms and then turn off automatically. At the bottom are the control handles for the LEDs and buzzer, which only need to be called every 10 milliseconds.

image-20220309153015735

image-20220309153310583

 

3.6. Hardware Connection

The key KEY1 and buzzer are on-board components and do not need to be connected manually.

image-20220326172515831

3.7. Experimental Effect

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.