Basic knowledge

*
Change the frequency of the output waveform of the MCU pin , You can adjust the tone of the control buzzer , Produce different timbres , Tonal sound .

*
Change the high and low level duty cycle of the output level ,（ Duty cycle refers to the time occupied by high level in a cycle ） You can control the sound level of the buzzer .

*
The single chip microcomputer adopts a passive buzzer , It needs to generate a certain pulse to make the buzzer sound , This is the main principle of buzzer sound .（ The schematic diagram is as follows ）

*
p15 Port is the input port of buzzer voltage , The schematic diagram is different according to different single chip computers , This port is also different , Be sure to see clearly .

Program code

1. Simple drive
#include"reg52.h" typedef unsigned char u8; typedef unsigned int u16; sbit
beep=P1^5; void delay(u16 i) { while(i--); } void main() { while(1) {
beep=~beep; //p1.5 Reverse port voltage , Therefore, a pulse is generated delay(10); // Changing the delay time can change the frequency } }
2. Happy birthday song
#include <reg51.h> #define uint unsigned int #define uchar unsigned char sbit
beep = P1^5; uchar code
SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159,
212,212,106,126,159,169,190,119,119,126,159,142,159,0}; uchar code
SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24,
9,3,12,12,12,12,12,9,3,12,12,12,24,0}; // Delay Functions void DelayMS(uint x) { uchar t;
while(x--) for(t=0;t<120;t++); } // Control audio , Beat function void PlayMusic() { uint i=0,j,k;
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0) { for(j=0;j<SONG_LONG[i]*20;j++)
// Play each note ,SONG_LONG Is the beat length { beep=~beep;
for(k=0;k<SONG_TONE[i]/3;k++);//SONG_TONE The delay table determines the frequency of each note } DelayMS(10); i++; } }
void main() { beep=0; while(1) { PlayMusic(); // Play happy birthday DelayMS(500);
// Pause for a period of time after playing } }

Technology
Daily Recommendation