If the output performance of the I/O cannot be guaranteed, you can add pull-up or pull-down resistors according to the situation. Let's get to the point: In the program, the driver of this buzzer is a high-low level driver. When the high-level transistor is turned on, the buzzer sounds, and when the low-level transistor is turned off, the buzzer does not sound. This is indeed very simple. I wrote the program like this at the beginning:
Here is a brief explanation: 1) Function function: buzzer sound drive Incoming parameters: the number of times the buzzer sounds 2) The incoming number cnt needs to be doubled in the function. This is because the parameter passed in is to make the buzzer sound cnt continuously. But the buzzer has a time when it does not sound. That is to say, the buzzer needs to be turned off every time it sounds. If it is not turned off, it will not sound several times but continuously. This is also easy to reason. 3) After the while loop is completed, a buzzer closing operation needs to be added. Here, if the parameter passed in is 2, the purpose is to make the buzzer sound twice. According to the execution steps of the program: cnt2 becomes 4. First while(4) buzzer is on, cnt decrements to 3 Second while(3) buzzer is off, cnt decrements to 2 Third while(2) buzzer is on, cnt decrements to 1 Fourth while(1) buzzer is off, cnt decrements to 0 Fifth while(0) jumps out of while It can be seen that the buzzer is already off after the while, but to be on the safe side, make sure the buzzer is off after the function call. For example, the I/O jump of the first function needs to be guaranteed, because the code can only show the jump, but not the state after the jump. So far, a simple buzzer circuit and driver program have been reviewed. Next, let's get to the practical stuff: When writing a program, we often pay attention to the efficiency of the program. For example, the efficiency of this buzzer driver will be reduced during the driving process. A skilled person can quickly see that it is the delay problem. But as mentioned above, it won't work without delay. So for the sake of efficiency, I tried to change the method to drive the buzzer. The code is as follows:
It is also very simple to implement. Here is the principle: 1) First, the I/O configuration of the buzzer driver is provided. 2) The second is the configuration of the timer 3) Finally, the timer interrupt function is implemented The timer I chose is the simplest timer in the project microcontroller, which is configured as a 1ms interrupt and can provide overflow interrupts. In fact, I often use this timer to count the system running time Systick_ms. However, this project does not use this system time, so I will use this timer to make an article. Implementation method: 1. The interface of the same function is the same when calling the buzzer driver, and the parameter passed in is still the number of buzzer sounds. 2. The function body has changed. It has been changed to the assignment of two variables. The first BELL_CNT is the same as cnt2 in the ordinary method. I will not repeat it here. The second is FLAG_BELL, which is used to save the state variable of whether the buzzer needs to be driven. So since it is calling the driver function, this variable must be true. 3. A static variable NOW is added to the timer interrupt function. Its function is to generate a 50ms time slice with Systic_ms. What is it used for? It must be used for the delay between the buzzer switch. It simulates software delay. Then let's analyze this code: 1) First of all, NOW and Systic_ms need to be assigned values unconditionally to ensure a 50ms time slice. The corresponding code is NOW=Systick_ms+50; 2) Determine whether the buzzer drive state variable is true. If it is not true, turn off the buzzer. This is also unconditional. 3) If the state variable is true: the buzzer first jumps Bell_Tog(); Of course, if there is no jump function, the above method of judging cnt can also be used. I won't write more. They are all the same. At the same time, the number of times is reduced by BELL_CNT--; At the same time, it is determined whether it has been reduced to 0. If it has been reduced to 0, it means that the ringing has ended, then the state variable is assigned to false. No matter whether the buzzer is on or off, the closing operation will be executed again. This is the same as the insurance mentioned above. 4)Finally, these two variables are global variables, which are presented here in the form of structures, because in many cases these two functions are not in the same C. If you insist on writing them in the same C, you can ignore this item.