Music box design based on 51 microcontroller (proteus simulation + program + schematic + PCB + report + explanation video)


Based on the 51 microcontroller music box simulation design (proteus simulation + program + schematic + PCB + report + explanation video)

Simulation diagram proteus 7.8 and above

Program compiler: keil 4/keil 5

Programming language: C language

Design number: S0030

The main function:

This course is designed to use microcontroller principles and control theory to design the hardware circuit of a music performance controller, and use C language for programming. By controlling the timer inside the microcontroller to generate square waves of different frequencies, the speaker is driven to emit music of different tones, and the delay is used to control the length of the pronunciation time. By converting the musical score into the corresponding timing constants, you can play pleasant music from the sound-producing device.

2 Design instructions

2.1 Design requirements

(1) Use the I/O port to generate a square wave of a certain frequency, drive the buzzer, and emit different tones to play music.

(2) It can realize the sequential playback of songs or single loop;

(3) Store more than three songs

2.2 Additional functions

(1) Use the digital tube to display the currently playing track;

(2) LED pattern lights change with the playing notes;

(3) Switch to the next song by pressing the button;

The following is a display diagram of this design information:

simulation

img

Start simulation

After starting the simulation, use a digital tube to display the currently playing track, the LED pattern lights change with the playing notes, and the next song can be switched by pressing the button. Use the toggle switch to control single cycle or full cycle. Built-in music "Thousand Years of Love", "Lonely Sandbank", "Song of Seven Sons" and "Grateful Heart"

Schematic PCB diagram

img

programming:

img

sbit key1=P3^2;  /*暂停键 */
sbit key2=P3^3;  /* 切换歌曲键*/
sbit fm=P1^1;  /*蜂鸣器口*/
sbit P1_7=P1^7;
uchar count2=1; /*歌曲标志*/
uchar timeh,timel,i;

uchar code DSY_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
						0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x70};  
uint code tone_delay_table[]={ 64021,64103,64260,64400,64524,64580,64684,
							64777,64820,64898,64968,65030,65058,65110,65157,65178};/*每个音符对应的延迟*/
uchar code huayang[]={0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x0f,0xf0,0x00};

void delay_ms(uchar x) /*延迟函数*/
{
	uchar i;
	while(x--)
	for(i=0;i<120;i++);
}


uchar code qnzl[]={   /*千年之恋*/
0x12,0x22,0x34,0x84,0x74,0x54,0x38,0x42,0x32,0x22,0x42,0x34,0x84,0x72,0x82,0x94,0xA8,0x08,
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
0x32,0x31,0x21,0x32,0x82,0x71,0x81,0x71,0x51,0x32,0x22,
0x32,0x31,0x21,0x32,0x52,0x32,0x31,0x21,0x32,0x62,
0x32,0x31,0x21,0x32,0x83,0x82,0x71,0x72,0x02,
0x63,0xA1,0xA2,0x62,0x92,0x82,0x52,
0x31,0x51,0x63,0x51,0x63,0x51,0x63,0x51,0x62,0x82,0x7C,0x02,
0x61,0x71,0x82,0x71,0x62,0xA2,0x71,0x76,
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
0x42,0x63,0x83,0x83,0x91,0x91,
0x61,0x71,0x82,0x71,0x62,0x0A2,0x71,0x76,
0x61,0x71,0x82,0x71,0x62,0x52,0x31,0x36,
0x61,0x71,0x82,0x71,0x62,0xA3,0x73,0x62,0x53,
0x42,0x82,0x88,0x02,0x74,0x93,0x89,0xff	  /*结束标志*/
};
uchar code jmszl[]={ /*寂寞沙洲冷*/
0x12,0x12,0x22,0x32,0x31,0x22,0x21,0x22,
0x21,0x31,0x51,0x52,0x31,0x52,0x61,0x15,0x14,
0x51,0x52,0x31,0x52,0x62,0x13,0x11,0x13,0x32,0x28,0x08,0x28,
0x31,0x32,0x31,0x32,0x11,0x21,0x51,0x52,0x51,0x52,
0x51,0x51,0x31,0x32,0x31,0x32,0x81,0x72,0x63,
0x62,0x71,0x81,0x72,0x61,0x61,0x52,0x31,0x21,0x32,0x51,0x54,
0x22,0x12,0x11,0x12,0x11,0x12,0x12,0x14,0x26,0x32,0x26,
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,
0x02,0x32,0x81,0x81,0x81,0x81,0x62,0x52,0x34,
0x31,0x81,0x81,0x81,0x61,0x91,0x82,
0x51,0x51,0x51,0x51,0x31,0x61,0x53,
0x21,0x11,0x21,0x11,0x22,0x11,0x21,0x26,
0x32,0x61,0x51,0x51,0x31,0x31,0x21,0x31,0x51,0x61,0x51,0x31,0x51,0x52,
0x31,0x31,0x81,0x81,0x81,0x61,0x91,0x81,0x61,0x31,0x56,
0x32,0x32,0x81,0x81,0x81,0x81,0x91,0x81,0x61,0x81,0x61,0x51,0x31,0x51,0x34,
0x21,0x31,0x51,0x31,0x21,0x11,0x61,0x21,0x16,0xff  /*结束标志*/
};
uchar code qizige[]={  /*七子之歌*/
0x54,0x32,0x52,0x32,0x54,0x62,0x52,0x32,0x62,0x54,
0x14,0x12,0x22,0x34,0x52,0x32,0x02,0x32,0x58,
0x52,0x52,0x62,0x52,0x32,0x54,0x52,0x62,0x52,0x82,0x62,0x58,
0x14,0x52,0x32,0x22,0x12,0x24,0x32,0x54,0x2,0x22,0x34,0x1f,0x18,
0x04,0x54,0xa4,0x92,0x81,0x62,0x52,0x54,0x62,0x64,0x52,0x62,0x81,0xa2,0x82,0x9c,
0x02,0x52,0xa4,0x92,0x81,0x62,0x52,0x54,0x62,0x64,0x52,0x64,0xa2,0x92,0x9f,
0xa4,0x92,0x81,0x62,0x52,0x54,0x84,0x62,0x52,0x32,0x22,0x14,
0x04,0x22,0x32,0x58,0x58,0x04,0x52,0x62,0x8f,
0xff /*结束标志*/
};
uchar code ganen[]={ /*感恩的心*/
0x12,0x14,0x22,0x32,0x54,0x32,0x84,0x72,0x62,0x54,
0x02,0x62,0x62,0x52,0x54,0x12,0x22,0x32,0x28,
0x12,0x14,0x22,0x32,0x54,0x32,0x84,0x92,0x82,0x54,
0x32,0x24,0x21,0x61,0x54,0x22,0x32,0x12,0x18,
0x22,0x24,0x12,0x24,0x11,0x21,0x34,0x32,0x21,0x31,0x34,
0x12,0x22,0x22,0x22,0x11,0x21,0x22,0x64,0x52,0x52,0x32,0x38,
0x31,0x52,0x51,0x52,0x31,0x51,0x58,0x31,0x82,0x81,0x82,0x31,0x61,0x68,
0x62,0x62,0x62,0x51,0x61,0x64,0x61,0x82,0x61,0x9c,
0x52,0xa4,0x92,0xa4,0x32,0x84,0x72,0x64,0x62,0x94,0x82,0x94,
0x61,0x81,0x92,0x91,0x91,0x92,0x81,0xa1,0xa2,0x92,0x94,
0x52,0xa4,0x92,0xa4,0x32,0x84,0x72,0x64,
0x52,0x62,0x81,0x81,0x82,0x92,0xa2,0x94,0x82,0x72,0x82,0x88,
0xff  /*结束标志*/
};
/*简谱音调对应的定时器初值*/
/*适合11.0592M的晶振*/
uchar code cuzhi[]={
					0xff,0xff,  /*占位*/
					0xFC,0x8E,  /*中央C调1~7*/
					0xFC,0xED,
					0xFD,0x43,
					0xFD,0x6A,
					0xFD,0xB3,
					0xFD,0xF3,
					0xFE,0x2D,
					0xFE,0x47,  /*高八度1~7*/
					0xFE,0x76,
					0xFE,0xA1,
					0xFE,0xC7,
					0xFE,0xD9,
					0xFE,0xF9,
					0xFF,0x16
					};
void delay1(uint z); /*	 延时1MS*/
void delay(uint z);  /*延时165ms,既16分音符*/
void song();
void beep(); /*蜂鸣器叫一声*/
void main()
{
	P0=0xbf;
//	KK=0;

	EA=1;
	EX0=1;
	IT0=1;
	EX1=1;
	IT1=1;
	TMOD=0x01;
	TH0=0;
	TL0=0;
	ET0=1;
	while(1)
	{
	    if(P1_7==1)
		{
			count2++;//歌曲递增
		if(count2==5)
			count2=0;
		}
		if(count2!=0)
		{
			song();
			delay1(1000);
		}
	
	}
}

void int0() interrupt 0 
{
	EA=0;
	delay1(1); 
	if(key1==0)
	{
		count2=0;/*暂停音乐*/
		TR0=0;
	}
	EA=1;
} 

void int1() interrupt 2 
{
	TR0 = 0;	
	delay1(1);  /*去抖*/
	if(key2==0)
	{
		i=0;/*从头开始唱*/
		count2++;
		TR0=~TR0;
		if(count2==5)
		count2=0;
	}
}

void timer0() interrupt 1/*用于产生各种音调*/
{
/*	if(count2==0)	
	{
		TH0 = tone_delay_table[keynum]/256;
		TL0 = tone_delay_table[keynum]%256;
		fm = ~fm;
	}	*/

	
	if(count2!=0) //放音乐
	{
		TH0=timeh;
		TL0=timel;
		fm=~fm;
	}
}

void song()
{
	uint temp;
	uchar jp;
	i=0;
	while(1)
	{

		
		if(count2==0)
		{
			break;
		}
		if(count2==1)  //选曲
		temp=jmszl[i];
		if(count2==2)
		temp=qnzl[i];
		if(count2==3)
		temp=qizige[i];
		if(count2==4)
		temp=ganen[i];
		if(temp==0xff)
		break;
		jp=temp/16;
		P2 = huayang[jp];
		P0=DSY_table[count2];
		if(jp!=0)
		{
			timeh=cuzhi[jp*2];
			timel=cuzhi[jp*2+1];
		}
		else
		{
			TR0=0;
			fm=1;
		}
		delay(temp%16);
		TR0=0;
	
		fm=1;
		delay1(10);
		TR0=1;
		i++;
	}
	TR0=0;
	fm=1;
}

design report

Report

System block diagram

img

This design uses STC89C516RD+ as the main control chip (the simulation can be replaced by STC89C51, STC89C52, AT89C51, AT89C52D and other 51 microcontrollers). The buzzer is used as the output device to generate music. The playback mode can be controlled through the MOD key. The digital tube (LED) can Display the current song number in real time. Among them, P3.3 is connected to an external button, P0 port is used as a digital tube output data port, and P2 port is used as an LED output data port and P1.1 buzzer port.

programming

How to determine pitch, tempo, and coding

Generally speaking, the music played by a microcontroller is basically a single tone frequency. It does not contain harmonic frequencies of corresponding amplitude, which means that it cannot produce a variety of timbres like an electronic keyboard. Therefore, when playing music on a microcontroller, you only need to understand two concepts, that is, "pitch" and beat represent how long a note takes to be sung.

Determination of pitch

Musical tones of different pitches are represented by C, D, E, F, G, A, and B. These seven letters are the name of the music. They are usually sung in order as DO, RE, MI, FA, SO, LA, SI, that is, the numbers 1, 2, 3, 4, 5, 6, and 7 that are sung into simplified musical notation, are equivalent to the pronunciation of the Chinese character "Duolai Mifa Solasi". This is the pronunciation of the musical sound when singing, so it is called "tone" , namely Tone. Divide the distance between the tones C, D, E, F, G, A, and B into 12 equal parts, and each equal part is called a "semitone." The distance between two tones is two "semitones", which is called a "whole tone". On keyboard instruments such as pianos, there is a black key between the two notes C-D, D-E, F-G, G-A, and A-B, and the distance between them is the whole tone; E-F, B-C There is no black key between two notes, the distance between them is a semitone. The tones usually sung as 1, 2, 3, 4, 5, 6, and 7 are called natural tones, and those with a | or b sign in their upper left corner are called altered tones. ﹟ is called a sharp sign, which means that the sound is raised by a semitone on the original basis, and b is called a flat mark, which means that the sound is lowered by a semitone on the original basis. For example, the frequency of treble DO (1046Hz) is exactly twice the frequency of midrange DO (523Hz), and the frequency of midrange DO (523Hz) is exactly twice the frequency of bass DO (266 Hz); similarly, the frequency of treble RE (1175Hz) is exactly twice the frequency of the midrange RE (587Hz), and the frequency of the midrange RE (587Hz) is exactly twice the frequency of the bass RE (294 Hz).

To generate audio pulses, just find the period of a certain tone (1/frequency) and then divide this period by 2, which is the half-cycle time. Use a timer to count this half-cycle time. Whenever the timer expires, invert the I/O output pulse. Then repeat the timer for this half-cycle time and then invert the I/O phase. You can get the result on the I/O pin. pulses of this frequency. Use the internal timer of AT89C51 to work in counter mode MODE1 and change the count values ​​TH0 and TL0. In addition, the end symbol and rest symbol can be represented by codes 00H and FFH respectively. If the table lookup result is 00H, it means the song is over; if the table lookup result is FFH, a corresponding pause effect will be produced. For example, the frequency is 523Hz, and its period T=1/523=1912us, so as long as the counter counts 956us/1us=956, and the I/O is inverted every time 956 times, the midrange DO (523Hz) can be obtained.

Determination of beat

To compose music, it is not enough to have pitch alone. You also need a beat, so that the music has a melody (fixed rhythm), and the speed and fullness of each note can be adjusted. "Beat", that is, Beat, simply means beating the time, just like when we listen to music and clap our hands or stamp our feet involuntarily. If 1 beat is actually 0.5s, then 1/4 beat is 0.125s. As for how many seconds per beat, there is no strict rule. Just like the human heartbeat, most people's heartbeat is 72 beats per minute. Some people's heartbeat is faster, some people are slower, as long as it sounds pleasant to the ear. The real-time value of the duration of a sound, generally expressed in beats. Rests indicate a pause in pronunciation.

A piece of music is composed of many different notes, and each note corresponds to a different frequency. In this way, the combination of different frequencies can be used to add a delay corresponding to the number of beats to form music. Understanding some basic knowledge of music, we know that music can be produced by generating audio pulses of different frequencies. It is very convenient for a microcontroller to generate pulses of different frequencies. The timer/counter of the microcontroller is used to generate such a square wave frequency signal. Therefore, it is necessary to clarify the relationship between the notes in the music and the corresponding frequencies, as well as the timing count of the microcontroller.

note encoding

do re mi fa so la si are coded as 1~7 respectively, the accent do is coded as 8, the stress re is coded as 9, and the pause is coded as 0. The playback length is in sixteenth note units (165ms in this program). One beat is a quarter note, which is equal to 4 sixteenth notes, and is programmed as 4. The other playback times can be deduced in this way. The pitch is used as the high 4 bits of the code, and the playback time is used as the low 4 bits, so the pitch and beat constitute a code. Use 0xff as the end mark of the score. For example, the tone do has a pronunciation length of two beats, that is, a half note, and is encoded as 0x18.

Song playback design. First, encode the musical notation of the song and store it in an array with data type unsigned char. The program takes a number from the array, then separates the high 4 bits to get the tone, then finds the corresponding value and assigns it to timer 0, so that it operates the buzzer regularly to get the corresponding tone; then separates the low digit of the number 4 bits, get the delay time, and then call the software delay.

Main program flow chart

img

Physical map

img

Information list (provide all documents in the information list):

Simulation + code + schematic PCB + design report + explanation video
img

Data download link:

Guess you like

Origin blog.csdn.net/weixin_52733843/article/details/134588519