51 microcontroller buzzer makes a pleasant sound

51 microcontroller buzzer makes a pleasant sound

1 Overview

This article introduces a small introductory experiment of using a microcontroller to control a buzzer. Through this experiment, we can master the principle of buzzer sound and control the sound to produce the music we want to hear.

2. Buzzer sounds

2.1.Hardware principle

1. The positive pole of the buzzer is connected to pin 20 of the microcontroller, VCC, and the negative pole is connected to pin 19, P1.7. 2. The
20MHZ crystal oscillator is connected to pins 4 and 5 of the microcontroller, XTAL2 and XTAL1 respectively
. 3. Each pin of the crystal oscillator is connected to a 30pf capacitor. Microcontroller pin 10 GND

Insert image description here

2.2. Buzzer sound code

Observe the changes in the buzzer mainby modifying the value of the delay function in the function, and understand through actual experience how changes in the current frequency change the sound of the buzzer.DELAY_MS()

/*************************************************************
* 程序名:控制蜂鸣器发声
* 编写人:bruce
* 日  期:2023-11-26
* 修改日志:开始编写
/*************************************************************/

#include<STC12C2052AD.H>

sbit BEEP = P1^7;

/*********************************************************************************************
函数名:毫秒级CPU延时函数
调  用:DELAY_MS (?);
参  数:1~65535(参数不可为0)
返回值:无
结  果:占用CPU方式延时与参数数值相同的毫秒时间
备  注:应用于1T单片机时i<600,应用于12T单片机时i<125
/*********************************************************************************************/
void DELAY_MS (unsigned int a){
    
    
	unsigned int i;
	while( --a != 0){
    
    
		for(i = 0; i < 600; i++);
	}
}

void main(){
    
    
	while(1){
    
    
		BEEP = 1;
		DELAY_MS(2);
		BEEP = 0;
		DELAY_MS(2);
	}
}

3.Electronic keyboard

The buzzer emits different sounds by controlling the width of the square wave of the digital signal in the current via the microcontroller.

3.1. Keyboard electronic keyboard

1.Hardware principle

Connect the hardware according to the circuit schematic diagram and physical diagram.
Insert image description here

Insert image description here

2. Eight-key electronic keyboard program
/*************************************************************
* 程序名:控制蜂鸣器发声
* 编写人:bruce
* 日  期:2023-11-26
* 修改日志:开始编写
/*************************************************************/

#include<STC12C2052AD.H>

sbit SPEAKER  = P3^7;
#define KEY  P1 
unsigned char MUSIC; 
unsigned char STH0;
unsigned char STL0;
// tab数组中的值是乐谱中音调对应HZ频率值
unsigned int code tab[]={
    
    
64021,64103,64260,64400,//低音3开始 
64524,64580,64684,64777, 
64820,64898,64968,65030, 
65058,65110,65157,65178
}; 

void main(void){
    
     
	TMOD=0x01; 
	ET0=1; 
	EA=1; 
	KEY = 0xff;
	while(1){
    
     	
		if(KEY != 0xff){
    
    
		switch (~KEY){
    
    //显示的列位置		
				case 0x01://			
					MUSIC = 7;	
					break;//		
				case 0x02://			
					MUSIC = 6;	
					break;//		
				case 0x04://			
					MUSIC = 5;	
					break;//		
				case 0x08://			
					MUSIC = 4;	
					break;//		
				case 0x10://			
					MUSIC = 3;	
					break;//		
				case 0x20://			
					MUSIC = 2;	
					break;//		
				case 0x40://			
					MUSIC = 1;	
					break;//		
				case 0x80://			
					MUSIC = 0;	
					break;//		
		}	
		// 用数组的值/256获取十六进制的值
    		STH0=tab[MUSIC]/256; 
    		STL0=tab[MUSIC]%256; 
    		TR0=1;
		}else{
    
     
    		SPEAKER = 1;
			TR0=0;
		} 
    } 
} 
// 定义计数器的起始值,来控制电流方形波的宽度,改变发声的音调。
void t0(void) interrupt 1 using 0{
    
     
  TH0=STH0; 
  TL0=STL0; 
  SPEAKER=~SPEAKER; 
}

4. Play music

Guess you like

Origin blog.csdn.net/m0_38039437/article/details/134624689
Recommended