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
212,212,106,126,159,169,190,119,119,126,159,142,159,0}; uchar code
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 } }

©2019-2020 Toolsou All rights reserved,
Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms (java code ) use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?