[C51 basic experiment buzzer sound]

Buzzer sound experiment

Preface:
After understanding the content of the previous chapter, this content continues to write the second basic experiment of C51, the sound or chirping of the buzzer. Control experiment of onboard passive buzzer based on development board.

1. Basic introduction to buzzer

Baidu Encyclopedia: The buzzer is an electronic buzzer with an integrated structure, powered by DC voltage, and is widely used in computers, printers, copiers, alarms, electronic toys, automotive electronic equipment, telephones, timers and other electronic products As a sound-producing device .
Buzzer classification :
(1), Piezoelectric buzzer (passive)

The piezoelectric buzzer is mainly composed of a multivibrator, a piezoelectric buzzer, an impedance matcher, a resonance box, and a shell.
The multivibrator is composed of transistors or integrated circuits. When the power is turned on (1.5~15V DC operating voltage), the multivibrator starts to oscillate and outputs an audio signal of 1.5~5kHZ. The impedance matcher drives the piezoelectric buzzer to produce sound. .

(2), electromagnetic buzzer (active)

The electromagnetic buzzer consists of an oscillator, electromagnetic coil, magnet, vibrating diaphragm and shell.
After the power is turned on, the audio signal current generated by the oscillator passes through the electromagnetic coil, causing the electromagnetic coil to generate a magnetic field. The vibrating diaphragm periodically vibrates and sounds under the interaction of the electromagnetic coil and the magnet.

Summary :
To summarize the differences between them, if you want the piezoelectric buzzer to sound, you need to provide a pulse signal of a certain frequency; if you want the electromagnetic buzzer to sound, you only need to provide power.

2. Hardware circuit design

The sounding principle of the buzzer consists of a vibration device and a resonant device, and the buzzer is divided into a passive separately excited type and an active self-excited type.
Actual picture of the onboard buzzer :
Insert image description here

Buzzer working and sounding principle diagram:
The working and sounding principle of the passive separately excited buzzer is : the square wave signal is input into the resonant device and converted into a sound signal output.
The working and sounding principle diagram of the passive separately excited buzzer is shown in Figure 1: The
Insert image description here
working and sounding principle of the active self-excited buzzer is : the DC power input passes through the amplification and sampling circuit of the oscillation system to generate a sound signal under the action of the resonant device.
The working sound schematic diagram of the active self-excited buzzer is shown in Figure 2:
Insert image description here
The circuit schematic diagram of the buzzer module on the development board is shown below :
Insert image description here
The buzzer control pin is directly connected to the P2.5 pin of the 51 microcontroller Above, it can be seen from the picture that the transistor is not used for current amplification, but the ULN2003 chip is used for driving. The development board uses a passive buzzer, which requires a certain frequency of pulses (high and low levels) to sound. Therefore, the P25 pin needs to continuously output high and low level signals at a certain frequency to control the buzzer to sound.

3. Software design

The function achieved is to
make the buzzer sound repeatedly and then turn it off after a period of time, that is, let the P2.5 pin output a pulse signal of a certain frequency (here through high and low level pulses) to control the passive buzzer.
Principle analysis :
If you change the high and low level duty cycle of the output level, you can change the sound size of the buzzer .
In order to facilitate understanding, here we quote the idea of ​​PWM and analyze the characteristics of the passive buzzer:

PWM waveform (Pulse width modulationwave), PWM is pulse width modulation, which is a pulse waveform with a variable duty cycle.
Pulse width modulation is a method of digitally encoding analog signal levels.

Insert image description here

Explanation :
According to the above figure, it mainly depends on the first two cycles (2T). They are all high level at the beginning. Then you can see that the length of the high level duration of the first cycle of A, B, and C is different. The length here is It refers to the duty cycle, which can be converted into GPIO and understood as the delay time for setting the high level to the corresponding port.
A simple understanding, that is to say, the longer the delay time for the high level, the greater the duty cycle. This is the PWM idea.
You can refer to the following code to understand:

while(1)
	{
    
    
	//假定 :T = delay_us(20000);
		LED = 1;
		delay_10us(10000);delay_10us(15000);
		//50%             //75%             
		LED = 0;
		delay_10us(10000);delay_10us(5000);
		//50%             //25%
	}

It can be seen that by controlling the delay time ratio, a certain duty cycle effect is achieved, thereby achieving a certain pulse frequency .
Then to implement the onboard buzzer, this code is also very simple. If the value of the variable ten_us is modified, the buzzer sound time can be changed. We know the idea of ​​PWM above. If you want to change the tone, you can modify the delay time, but be careful that the frequency cannot be too large or too small. You can try to debug it specifically. To change the volume, you can modify the BEEP output high level time, for example:

beep = 1;
delay_10us(800);//80%
beep = 0;
delay_10us(200);//20%

Finally, we understand the idea of ​​PWM to realize high and low level pulse signals to drive a passive buzzer. This knowledge can initially implement the basic function to make it buzz. The code is as follows:

#include "at89x52.h"

sbit beep = P2^5;

void delay_10us(unsigned int ten_us)
{
    
    
	while(ten_us--);
}

void main()
{
    
    
	unsigned int i = 2000;
	while(1)
	{
    
    
		while(i--)
		{
    
    
			beep =! beep;
			delay_10us(200);
		}
		beep = 0;
		i = 0;
	}
}

Explanation :
The code in the main.c file is very small and simple. First, include the header file of the 51 microcontroller, and then use the sbit keyword to define the P2.5 pin. After the definition, you can use BEEP to replace P2.5 mouth operation. The function of the main function is very simple. It directly enters the while loop, and a while loop is applied again within the loop. However, this is not an infinite loop, but uses the value of the variable i to determine when to exit. The i value is initialized to 5, that is, the loop will Execute 5 times, **In this loop, BEEP is continuously inverted, and then delayed for a certain period of time, that is, P2.5 outputs high and low levels at a certain interval. This will generate a pulse signal to control the buzzer to sound. When the i value decreases When it reaches 0, exit the while loop, then clear the i value to zero, and output 0 to BEEP. **Among them, it is worth mentioning that inside the while(i–) loop, the situation of equal proportional duty cycle is realized. By repeatedly inverting and delaying for equal lengths of time, an equal proportional pulse signal is simply realized, thus Just drive the buzzer to sound.

4. Compilation results

At this point, the entire program has been written. Let's compile it, as shown in the figure below :
Insert image description here
As can be seen from the compilation information in the figure above, our code occupies FLASH size: code = 54 bytes, and the SRAM size used is: data = 9 Bytes (9.0), xdata refers to the size of the extended external storage XSRAM, and XSRAM is not used so it is 0.

The results of the hardware experiment are shown in the figure :

C51 Basic Experiment 3 --- Attachment

5. Conclusion

I write this article as a record of my own study. Please let me know if there are any mistakes. Thank you for reading.

Guess you like

Origin blog.csdn.net/m0_69455439/article/details/132866162