Buzzers are divided into piezoelectric buzzers and electromagnetic buzzers based on their structure. Piezoelectric buzzers use piezoelectric ceramics to make sounds, and the current is relatively small. Electromagnetic buzzers use coils to vibrate and make sounds, and their size is relatively small.
According to the driving method, they are divided into active buzzers and passive buzzers. The active and passive here do not refer to power supplies, but to oscillation sources. Active buzzers have oscillation sources inside. As shown in Figure 9-8, when a low level is given to the BUZZ pin, the buzzer will ring directly. Passive buzzers do not have oscillation sources inside. To make them ring, they must be driven by a pulse frequency signal between 500Hz and 4.5KHz. Active buzzers are often more expensive than passive buzzers because they have an oscillation circuit inside and are simple to drive and produce sound. They can be driven by electrical level, while passive buzzers are relatively cheap. In addition, the sound frequency of passive buzzers can be controlled, and there is a definite correspondence between the scale and the frequency. Therefore, the effect of "do re mi fa sol la si" can be produced. It can be used to make simple music tracks, such as the birthday song, two tigers, etc.
Figure 9-8 Buzzer circuit schematic
Let's take a look at the circuit in Figure 9-8. The buzzer current is still relatively large, so it needs to be driven by a transistor, and a 100-ohm resistor is added as a current-limiting resistor. In addition, a D4 diode is added, which is called a freewheeling diode. Our buzzer is an inductive device. When the transistor is turned on to power the buzzer, a conduction current will flow through the buzzer. As we know, one of the characteristics of an inductor is that the current cannot change suddenly. When it is turned on, the current gradually increases, which is no problem. But when it is turned off, the circuit of "power supply-transistor-buzzer-ground" is cut off, and no current can pass. So where does the stored current go? It is consumed through this D4 and the buzzer's own loop, thereby avoiding the reverse impact caused by the inductive current when it is turned off. The current when it is turned off continues, which is the origin of the name of the freewheeling diode.
Buzzers are often used as prompt sounds on devices such as computers, printers, and multimeters. The prompt sounds are generally very simple, just making a sound. We used a program to make a sound at a frequency of 4KHZ and 1KHZ. Students can study the program by themselves and compare the actual effects.
#include
sbit BUZZ = P1^6; //Buzzer control pin
unsigned char T0RH = 0; //T0 high byte of reload value
unsigned char T0RL = 0; //Low byte of T0 reload value
void OpenBuzz(unsigned int frequ);
void StopBuzz();
void main(){
unsigned int i;
TMOD = 0x01; //Configure T0 to work in mode 1, but do not start it yet
EA = 1;
while (1){ //Enable global interrupt
OpenBuzz(4000); //Start the buzzer at 4KHz frequency
for (i=0; i<40000; i++);
StopBuzz(); //Stop the buzzer
for (i=0; i<40000; i++);
OpenBuzz(1000); //Start the buzzer at 1KHz frequency
for (i=0; i<40000; i++);
StopBuzz(); //Stop the buzzer
for (i=0; i<40000; i++);
}
}
/* Buzzer start function, frequ-working frequency*/
void OpenBuzz(unsigned int frequ){
unsigned int reload; //Calculate the required timer reload value
reload = 65536 - (11059200/12)/(frequ*2); //Calculate the timer reload value based on the given frequency
T0RH = (unsigned char)(reload >> 8); //16-bit reload value is decomposed into high and low bytes
T0RL = (unsigned char)reload;
TH0 = 0xFF; //Set an initial value close to overflow so that the timer can start working immediately
TL0 = 0xFE;
ET0 = 1; // Enable T0 interrupt
TR0 = 1; //Start T0
}
/* Buzzer stop function*/
void StopBuzz(){
ET0 = 0; //Disable T0 interrupt
TR0 = 0; //Stop T0
}
/* T0 interrupt service function, used to control the buzzer sound*/
void InterruptTimer0() interrupt 1{
TH0 = T0RH; //Reload the reload value
TL0 = T0RL;
BUZZ = ~BUZZ; //Invert buzzer control level
}
In addition, using a buzzer to output music is just for fun, and has few applications. It contains relevant content about scales and music scores, and the program is a bit complicated, so I won't explain it in detail. I only provide a program that can play "Two Tigers". You can download it to the board to play and satisfy your curiosity.
Plain text copy
#include
sbit BUZZ = P1^6; //Buzzer control pin
unsigned int code NoteFrequ[] = { // List of corresponding frequencies for middle notes 1-7 and treble notes 1-7
523, 587, 659, 698, 784, 880, 988, // Alto 1-7
1047, 1175, 1319, 1397, 1568, 1760, 1976 // Treble 1-7
};
unsigned int code NoteReload[] = { //Timer reload values corresponding to middle notes 1-7 and treble notes 1-7
65536 - (11059200/12) / (523*2), // Middle tone 1
65536 - (11059200/12) / (587*2), //2
65536 - (11059200/12) / (659*2), //3
65536 - (11059200/12) / (698*2), //4
65536 - (11059200/12) / (784*2), //5
65536 - (11059200/12) / (880*2), //6
65536 - (11059200/12) / (988*2), //7
65536 - (11059200/12) / (1047*2), //treble 1
65536 - (11059200/12) / (1175*2), //2
65536 - (11059200/12) / (1319*2), //3
65536 - (11059200/12) / (1397*2), //4
65536 - (11059200/12) / (1568*2), //5
65536 - (11059200/12) / (1760*2), //6
65536 - (11059200/12) / (1976*2), //7
};
bit enable = 1; //Buzzer sound enable flag
bit tmrflag = 0; //Timer interrupt completion flag
unsigned char T0RH = 0xFF; //T0 high byte of reload value
unsigned char T0RL = 0x00; //Low byte of T0 reload value
void PlayTwoTiger();
void main(){
unsigned int i;
EA = 1; // Enable global interrupt
TMOD = 0x01; //Configure T0 to work in mode 1
TH0 = T0RH;
TL0 = T0RL;
ET0 = 1; // Enable T0 interrupt
TR0 = 1; //Start T0
while (1){
PlayTwoTiger(); //Play the song - Two Tigers
for (i=0; i<40000; i++); //Stop for a while
}
}
/* Two tiger music playing functions*/
void PlayTwoTiger(){
unsigned char beat; //Current beat index
unsigned char note; //The note corresponding to the current beat
unsigned int time = 0; //Current beat timing
unsigned int beatTime = 0; //Current beat total time
unsigned int soundTime = 0; //The current beat needs to sound time
//Two tigers musical note table
unsigned char code TwoTigerNote[] = {
1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5,
5,6, 5,4, 3, 1, 5,6, 5,4, 3, 1, 1, 5, 1, 1, 5, 1,
};
//Two Tigers beat chart, 4 represents one beat, 1 represents 1/4 beat, 8 represents 2 beats
unsigned char code TwoTigerBeat[] = {
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8,
3,1, 3,1, 4, 4, 3,1, 3,1, 4, 4, 4, 4, 8, 4, 4, 8,
};
//Use the beat index as a loop variable
for (beat=0; beat while (!tmrflag); //After each timer interrupt is completed, detect and process the beat tmrflag = 0; if (time == 0){ //Start a new beat after the current beat is finished note = TwoTigerNote[beat] - 1; T0RH = NoteReload[note] >> 8; T0RL = NoteReload[note]; //Calculate the total beat time. Shifting right by 2 bits is equivalent to dividing by 4. Shifting instead of dividing can speed up execution. beatTime = (TwoTigerBeat[beat] * NoteFrequ[note]) >> 2; //Calculate the sound time, which is 0.75 of the total time. The shift principle is the same as above soundTime = beatTime - (beatTime >> 2); enable = 1; //instruct the buzzer to start sounding time++; }else{ //If the current beat is not finished, process the current beat //When the current duration reaches the total beat time, it returns to zero. // and increment the beat index to prepare to start a new beat if (time >= beatTime){ time = 0; beat++; }else{ //When the current duration does not reach the total time, time++; //Accumulate time count //When the sounding time is reached, the buzzer is turned off. //Insert a silence interval of 0.25*total time, if (time == soundTime){ enable = 0; //Used to distinguish two consecutive beats } } } } } /* T0 interrupt service function, used to control the buzzer sound*/ void InterruptTimer0() interrupt 1{ TH0 = T0RH; //Reload the reload value TL0 = T0RL; tmrflag = 1; if (enable){ //Reverse buzzer control level when enabled BUZZ = ~BUZZ; }else{ //Turn off the buzzer when not enabled BUZZ = 1; } }
Previous article:Practical Motor Control Program
Next article:Single chip digital stopwatch program
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Ended | Live broadcast [Cloud-based secure authentication | Microchip embedded security solutions]
- Please tell me, how to make a dual power automatic switching circuit?
- Decimal to hexadecimal C language function
- MSP432E401Y SimpleLink Ethernet Microcontroller
- [AB32VG1 Development Board Review] Getting to Know the Development Board
- Simple driver for 64Mb PSRAM
- How to tell how many layers a PCB has
- [Sipeed LicheeRV 86 Panel Review] II. GPIO pins are brought out, RGB tri-color LED flashes
- Showing off the LTC3588 Energy Harvesting Development Board
- [LSM6DSOX finite state machine routine learning 2]--How to use finite state machine programming