51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)
单片机蜂鸣器播放音乐
#include <reg52.h>#define uchar unsigned char#define uint unsigned intsbit speaker = P1^7;uint j;uchar m=1;uchar flag;uchar line;uchar code * data song;// 休止符低6 低7 中1 中2 中3 中4 中5 中 6 中7 高 1 低 3 低 5 低 4 高3uchar code yin[30]={0xFF,0xFF,0xFB,0x90,0xFC,0x0C,0xFC,0x44,0xFC,0xAC,0xFD,0x09,0xFD,0x34,0xF D,0x82,0xFD,0xC8,0xFE,0x06,0xFE,0x22,0xFA,0X15,0XFB,0x04,0xFA,0x67,0xFE,0x85};uchar code song1[97]={0x34,0x32,0x32,0x34,0x42,0x51, //<<干杯,朋友>>0x62,0x52,0x42,0x32,0x34,0x04,0x74,0x74,0x62,0x62,0x64,0x3c,0x04,0x64,0x62,0x52,0x42,0x32,0x34,0x33,0x31,0x32,0x72,0x76,0x72,0x83,0x81,0x82,0x82,0x82,0x74,0x72,0x7c,0x04,0x63,0x61,0x62,0x62,0x64,0x72,0x82,0x72,0x74,0x72,0x62,0x52,0x42,0x32,0x42,0x44,0x42,0x42,0x52,0x62,0x52,0x5c,0x04,0x64,0x62,0x62,0x64,0x72,0x82,0x72,0x74,0x72,0x62,0x52,0x42,0x32,0x42,0x46,0x53,0x41,0x42,0x32,0x3c,0x04,0x44,0x48,0x02,0x32,0x3f,0x44,0x48,0x02,0x32,0x3f,0x34,0x0c,0xFF};uchar code song2[46]={0x12,0x52,0x52,0x52,0x56,0x42, //<<兰花草>>0x32,0x42,0x32,0x22,0x18,0x82,0x82,0x82,0x82,0x86,0x72,0xB2,0x72,0x72,0x62,0x58,0x52,0x82,0x82,0x72,0x56,0x42,0x32,0x42,0x32,0x22,0x16,0xB2,0xB2,0x32,0x32,0x22,0x16,0x52,0x42,0x32,0x22,0xC2,0x88,0xFF};uchar code song3[131]={0x52,0x42, //<<两只蝴蝶>>0x58,0x02,0x42,0x52,0x42,0x38,0x04,0x12,0x32,0x44,0x42,0x52,0x42,0x32,0x12,0x12,0xC8,0x04,0x52,0x42,0x58,0x02,0x42,0x52,0x42,0x38,0x04,0x12,0x32,0x44,0x42,0x52,0x42,0x32,0x12,0x32,0x48,0x04,0x52,0x42,0x58,0x02,0x42,0x52,0x42,0x38,0x04,0x12,0x32,0x44,0x42,0x52,0x42,0x32,0x12,0x11,0x31,0xC8,0x04,0x52,0x72,0x78,0x02,0x72,0x82,0x72,0x58,0x04,0x42,0x42,0x44,0x42,0x52,0x42,0x32,0x12,0x12,0x32,0x32,0x3C,0x09,0x72,0x72,0x82,0xA2,0x92,0x92,0x82,0x52,0x42,0x42,0x42,0x58,0x02,0x52,0x52,0x72,0x84,0x84,0x02,0x12,0x52,0x42,0x48,0x04,0x52,0x72,0x72,0x52,0x74,0x02,0xA4,0x92,0x82,0x92,0x54,0x02,0x82,0x82,0x92,0x82,0x72,0x52,0x42,0x02,0xC4,0x12,0x3C,0xFF};uchar code song4[37]={0xC4,0x12,0x32,0x44,0x42,0x52, //<<祈祷>>0x52,0x44,0x32,0x32,0x12,0x14,0x12,0x12,0x32,0x42,0x54,0x44,0x4F,0xC4,0x12,0x32,0x44,0x42,0x72,0x58,0x42,0x32,0x34,0x14,0x12,0x32,0x34,0x32,0x12,0x1F,0xFF};uchar code song5[144]={0x52,0x62, //<<阳光总在风雨后>>0x74,0x74,0x74,0x34,0x42,0x34,0x52,0x56,0x42,0x34,0x34,0x14,0x32,0x12,0x12,0xC2,0xC4,0x04,0x52,0x62,0x74,0x74,0x74,0x34,0x42,0x34,0x52,0x56,0x42,0x34,0x32,0x12,0x12,0x34,0x42,0x4C,0x52,0x42,0x34,0x32,0x42,0x32,0x12,0x12,0xC2,0xC2,0x54,0x52,0x54,0x52,0x52,0x42,0x32,0x32,0x12,0x32,0x42,0x32,0x52,0x5C,0x52,0x42,0x34,0x32,0x42,0x32,0x12,0x12,0xC2,0xC2,0x54,0x52,0x54,0x02,0x52,0x52,0x42,0x32,0x12,0x32,0x84,0x72,0x7F,0x0F,0x74,0x72,0x52,0x54,0x74,0x82,0x52,0x52,0x72,0x78,0x34,0x32,0x32,0x42,0x54,0x72,0x7F,0x86,0x82,0x72,0x52,0x54,0x72,0x84,0x32,0x36,0x52,0x42,0x52,0x42,0x32,0x34,0x12,0x32,0x32,0x42,0x4C,0x86,0x82,0x72,0x52,0x54,0x72,0x84,0x52,0x56,0x52,0x42,0x52,0x72,0x82,0x52,0x34,0x12,0x12,0x32,0x3C,0xFF};uchar code song6[85]={0x04,0x24,0x14,0xC4, //<<心心相印>>0x54,0x54,0x48,0x04,0x54,0x44,0x34,0x22,0x14,0xB2,0xC8,0x04,0x12,0xC2,0x16,0x12,0x76,0x52,0x46,0x12,0x3F,0x04,0xB4,0xD4,0xC4,0x14,0x28,0xC4,0x14,0x12,0x32,0x16,0xC2,0x54,0x78,0x34,0x4C,0x42,0x52,0x86,0x72,0x76,0x52,0x74,0x28,0x42,0x52,0x46,0x12,0x26,0x12,0xCF,0x04,0x24,0x14,0xC4,0x56,0x52,0x48,0x04,0x54,0x44,0x34,0x22,0x14,0xB2,0xC8,0x04,0x34,0x14,0xC4,0x12,0x32,0x18,0x42,0x42,0x74,0x58,0x44,0x3F,0xFF};uchar code song7[138]={0xC2,0xC2,0x12, //<<最浪漫的事>>0x34,0x32,0x32,0x32,0x12,0x12,0x52,0x54,0x02,0xC2,0x12,0x32,0x32,0x32,0x32,0x34,0x52,0x72,0x7C,0x02,0x72,0x82,0x72,0x82,0x72,0x84,0x72,0x41,0x51,0x52,0x41,0x31,0x34,0x02,0x32,0x32,0x12,0x34,0x32,0x12,0x32,0x54,0x42,0x32,0x32,0x32,0x32,0x34,0x52,0x72,0x7C,0x72,0x72,0x82,0x72,0x82,0x72,0x84,0x82,0x72,0x42,0x52,0x42,0x34,0x12,0x12,0x32,0xC4,0x52,0x42,0x34,0x12,0x12,0x31,0x41,0x32,0x3C,0x0A,0x72,0x82,0xA2,0x84,0x82,0x72,0x82,0x72,0x52,0x72,0x78,0x02,0x72,0x82,0xA2,0x84,0x82,0x72,0x82,0x72,0x82,0x32,0x38,0x02,0x32,0x42,0x52,0x64,0x62,0x72,0x82,0x82,0x72,0x82,0x82,0xA2,0x82,0x82,0x82,0xA2,0x82,0x72,0x78,0x72,0x32,0x82,0x72,0x78,0x72,0x82,0xA3,0x51,0x42,0x51,0x41,0x3C,0xFF};uchar code song8[77]={0xC2,0xC2,0x34,0x42,0x42, //<<一生有你>>0x52,0x41,0x51,0x5C,0x04,0xC2,0xC2,0x34,0x42,0x42,0x52,0x31,0x41,0x4C,0x04,0xC2,0xC2,0x34,0x42,0x42,0x52,0x41,0x51,0x58,0x32,0x52,0x4C,0x42,0x32,0x34,0x32,0x42,0x48,0x04,0xC2,0xC2,0x34,0x42,0x42,0x72,0x41,0x51,0x5C,0x04,0xC2,0xC2,0x32,0x32,0x42,0x42,0x52,0x42,0x4C,0x04,0xC2,0xC2,0x34,0x42,0x42,0x52,0x41,0x51,0x58,0x32,0x52,0x4C,0x42,0x32,0x32,0x42,0x4C,0xFF};uchar code song9[]={0X54,0XC4,0X12,0X32,0X34, //<<宁夏>>0X52,0X52,0X52,0X52,0X42,0X32,0X34,0XC1,0XC2,0X52,0XC2,0X12,0X32,0X34,0X11,0X12,0X31,0X12,0XC2,0X78,0X72,0X52,0X52,0X42,0X32,0X42,0X52,0X11,0XC1,0XC4,0XC1,0XC1,0XC1,0XC1,0X12,0X32,0X32,0X12,0X42,0X42,0X44,0XC2,0X12,0X32,0X42,0X52,0X72,0X72,0X82,0X41,0X51,0X42,0X48,0X42,0X12,0X3C,0X72,0X62,0X3C,0X72,0X62,0X3C,0X72,0X62,0x21,0x31,0x41,0x51,0x61,0x71,0x81,0x91,0x61,0x71,0x81,0x91,0X3F,0X3F,0X54,0XC2,0XC2,0X42,0XC2,0XC4,0X34,0X32,0X42,0X52,0XC2,0XC4,0XD2,0XC2,0X12,0X22,0X38,0X12,0X22,0X32,0X42,0X48,0xFF};uchar code song10[]={0x32,0x31,0x41, //<<老鼠爱大米>>0x52,0x52,0x42,0x31,0x41,0x44,0x31,0x41,0x52,0x52,0x42,0x32,0x34,0x71,0x11,0x74,0x71,0x71,0x11,0x31,0x34,0x32,0x32,0x32,0x32,0x31,0x41,0x44,0x32,0x41,0x51,0x52,0x52,0x72,0x81,0x41,0x44,0x52,0x41,0x31,0x32,0x32,0x42,0x51,0x71,0x74,0x71,0x71,0x12,0x31,0x31,0x31,0x52,0x42,0x32,0x32,0x41,0x31,0x34,0x52,0x51,0x71,0x72,0x71,0x71,0x72,0x71,0x81,0x84,0x52,0x42,0x32,0x32,0x32,0x41,0x51,0x54,0x51,0x71,0x72,0x72,0x72,0x81,0xA1,0xA2,0x82,0x72,0x52,0x42,0x32,0x32,0x12,0x44,0x51,0x71,0x72,0x72,0x71,0x82,0x71,0x84,0x52,0x42,0x32,0x32,0x32,0x41,0x51,0x54,0x51,0x71,0x72,0x72,0x72,0x81,0xA1,0xA2,0x82,0x72,0x51,0x41,0x42,0x42,0x44,0x32,0x51,0x61,0x72,0x42,0x44,0x52,0x41,0x31,0x34,0x52,0x41,0x31,0x34,0x52,0x42,0x52,0x32,0x52,0x81,0x71,0x74,0x51,0x71,0x82,0x81,0x71,0x72,0x81,0x71,0x74,0x51,0x41,0x31,0x42,0x41,0x51,0x42,0x31,0x41,0x44,0x52,0x41,0x31,0x34,0x52,0x41,0x31,0x34,0x52,0x42,0x52,0x32,0x52,0x81,0x71,0x74,0x51,0x41,0x82,0x82,0xA2,0x71,0x81,0x72,0x71,0x51,0x41,0x31,0x44,0x41,0x31,0x41,0x51,0x44,0x52,0x52,0x42,0x31,0x34,0x52,0x41,0x31,0x42,0x31,0x34,0xFF};uchar code song11[]={0XC2,0XB2, //<<泰坦尼克号>>0X36,0X52,0X42,0X32,0X11,0XC1,0XB2,0XC6,0X12,0XC4,0X12,0XC2,0X76,0XA2,0X82,0X72,0X51,0X41,0X32,0X4C,0X42,0X32,0X74,0X82,0X74,0X82,0X72,0X52,0X41,0X38,0X32,0X42,0X16,0XC2,0X72,0X52,0X42,0X42,0X3C,0XC2,0XB2,0X36,0X52,0X42,0X32,0X11,0XC1,0XB2,0XC6,0X12,0XC4,0X12,0XC2,0X76,0XA2,0X82,0X72,0X51,0X41,0X32,0X4C,0X42,0X32,0X76,0X82,0X74,0X82,0X72,0X53,0X41,0X38,0X32,0X42,0X16,0XC2,0X72,0X52,0X41,0X51,0X12,0XCC,0XC2,0XB2,0X16,0XC2,0X72,0X52,0X42,0X42,0X3C,0X32,0X32,0X26,0X22,0X24,0X42,0X32,0x22,0x12,0x18,0x02,0x52,0X46,0X12,0X44,0X72,0X82,0X5C,0X52,0X52,0X46,0X42,0X74,0X42,0X32,0X36,0X42,0X58,0X82,0X72,0X62,0X52,0X46,0X32,0X3C,0X12,0XB2,0X36,0X52,0X42,0X32,0X11,0XC1,0XB2,0XC6,0X12,0XC4,0X12,0XC2,0X76,0XA2,0X82,0X72,0X51,0X41,0X32,0X48,0X02,0X42,0X52,0X62,0X76,0X82,0X74,0X82,0X72,0X56,0X41,0X38,0X32,0X22,0X16,0XC2,0X72,0X52,0X42,0X42,0X3C,0X04,0X04,0X04,0X04,0xFF};uchar code song12[]={0x34,0x32, //<<喜欢你>>0x34,0x34,0x24,0x34,0x32,0x32,0x24,0x38,0x44,0x58,0x48, 0x34,0x32,0x34,0x34,0x24,0x38,0x34,0x7F,0x34,0x32,0x34,0x32,0x24,0x38,0x34,0x24,0x38,0x44,0x58,0x48,0x34,0x32,0x34,0x34,0x24,0x38,0x34,0x7F,0x16,0x4C,0x74,0x78,0x64,0x54,0x48,0x54,0x64,0x58,0x44,0x34,0x24,0x38,0x24,0x14,0x12,0x21,0x11,0x78,0x68,0x3F,0x4C,0x74,0x78,0x64,0x52,0x42,0x48,0x54,0x64,0x58,0x44,0x34,0x24,0x38,0x24,0x24,0x38,0x44,0x58,0x48,0x3C,0xFF};//**********************************************************************//// 延时程序////**********************************************************************//void delay(uint d) //延时d微秒,最大延时65536微妙{while(d--);}/*延时1/4拍时间即187ms*/void delay187ms(void){uchar t;for(t=0;t<130;t++) delay(100);}void main(){uchar i;uchar k;delay(100);TMOD=0x01;IT0=1;EX0=1;TR0=1;ET0=1;EA=1;song=song1;while(1){switch(m){case 1 :song=song1;break;case 2 :song=song2;break;case 3 :song=song3;break;case 4 :song=song4;break;case 5 :song=song5;break;case 6 :song=song6;break;case 7 :song=song7;break;case 8 :song=song8;break;case 9 :song=song9;break;case 10 :song=song10;break;case 11 :song=song11;break;case 12 :song=song12;break;default :m=1;song=song1;break;}j=0;flag=1;while(*(song+j)!=0xFF&&flag){k=*(song+j)&0x0F; //取出节拍line=*(song+j)>>4; //取出音TH0=yin[2*line];TL0=yin[2*line+1];TR0=1;if ((yin[2*line]==0xFF)&&(yin[2*line+1]==0xFF)){TR0=0;}for(i=k;i>0;--i){delay187ms();}TR0=0;j++;}for(i=0;i<20;i++)delay187ms();m++;}}/* 每个音符的发声*/void timer1() interrupt 1{TH0=yin[2*line];TL0=yin[2*line+1];speaker = !speaker;}void int0() interrupt 0{flag=0;TR0=0;}。
51单片机蜂鸣器播放音乐代码
/*生日快乐歌曲*/#include <>#define uint unsigned int#define uchar unsigned charsbit 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};//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:#include <> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为可以修改成其它 IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0 xB9},{0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0 x5B},{0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0 xAD},{0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0 x56},{0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8}, {21,4},{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4}, {23,4},{21,4},{19,4},{16,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{ 0,4},{23,4},{23,4},{26,4},{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{2 3,8},{0,4},{28,4},{28,4},{30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4 },{26,16},{0xFF,0xFF}};void delay(uchar p){uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}void T0_int() interrupt 1{beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main()uchar i=0;TMOD=0x01; EA=1; ET0=1;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include ""unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20 ,0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20 ,0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20 ,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20 ,0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10 ,0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20 ,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20 ,0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20 ,0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10 ,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80 ,0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20 ,0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x200x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10 ,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1{TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/void Delay_xMs(unsigned int x){unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){_Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*-------------------------------------------------功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。
51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)精编版
/*生日快乐歌曲*/#include <reg51.h>#define uint unsigned int#define uchar unsigned charsbit 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};//延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:#include <reg51.h> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为P1.5 可以修改成其它IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03 },{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x8 1},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0 },{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{ 0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16 ,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{23,4},{23,4},{26,4} ,{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{0,4},{28,4},{28,4},{ 30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16},{0xFF,0xFF}};void delay(uchar p){uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}void T0_int() interrupt 1beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main(){uchar i=0;TMOD=0x01; EA=1; ET0=1;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include "reg52.h"unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10, 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1{TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/ void Delay_xMs(unsigned int x){unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){_Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*------------------------------------------------- 功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。
单片机蜂鸣器发声代码
单片机蜂鸣器发声代码蜂鸣器是一种常见的电子元件,可以发出短促的蜂鸣声用于提醒或警示。
在单片机应用中,蜂鸣器可以用来作为声音提示的一种方式。
下面介绍一段单片机蜂鸣器的发声代码。
1. 引脚连接在使用单片机控制蜂鸣器发声之前,需要将蜂鸣器与单片机的引脚连接。
通常情况下,蜂鸣器有两个引脚,其中一个为正极,一个为负极。
将蜂鸣器的正极连接到单片机的一个IO口上,将负极连接到单片机的GND上。
2. 代码实现单片机发出频率为f的声音,需要将IO口以1/f的频率高低电平切换。
以下是一个基本的单片机蜂鸣器发声代码:#include 'reg51.h' //单片机头文件#define BEEP P1_5 //定义蜂鸣器连接的IO口void Delay(unsigned int Time) //延时函数{unsigned int i,j;for(i=Time;i>0;i--)for(j=110;j>0;j--);}void Beep(unsigned int Time) //发出一次蜂鸣声{BEEP = 0; //使IO口输出低电平Delay(Time); //延时BEEP = 1; //使IO口输出高电平Delay(Time); //延时}void main(void){while(1){Beep(100); //发出100ms的蜂鸣声}}在上述代码中,我们首先定义了蜂鸣器连接的IO口为P1_5,然后定义了一个延时函数Delay和一个发出蜂鸣声函数Beep。
Delay函数用于延时,Beep函数用于发出一次蜂鸣声。
在main函数中,我们使用一个while循环不停地发出蜂鸣声。
在Beep函数中,我们首先使IO口输出低电平,然后延时一段时间,再使IO口输出高电平,再延时一段时间,从而在IO口上形成一个频率为1/(2*Time)的方波信号,从而发出蜂鸣声。
3. 修改参数如果我们想要修改蜂鸣器发出的声音的频率和时长,只需要在Beep函数中修改Time的值即可。
#51单片机实现的音乐闹钟程序代码
功能描述:产品可以显示时间和日期,时间格式为 hh mm ss 日期格式为 yy.mm.dd时间和日期轮流显示。
时间显示5S 日期显示3S。
可以设置5个闹铃,闹铃音乐可以设置两种:毛驴和童年。
三个按键对时间和闹铃进行设置,六个LED进行显示。
计时采用DS1307。
继电时间不丢失,设置过的闹铃也不丢失。
闹铃音乐由单片机的两个定时器去产生频率实现。
部分程序如下://*************************************************//************************************************//***********************************************//程序名:DS1307 时钟程序//功能描述:用六个八段LED 轮流显示时间// 和日期。
有6个闹钟。
上电时从DS1307中读出// 当前时间、日期、闹钟。
//////////#include <reg52.h>#include <intrins.h>#define uchar unsigned char#define uint unsigned int#define LED P2#define LEDBit P0#define nop _nop_(>#define LightCount 40#define LightMax 80sbit SCL=P3^1。
sbit SDA=P3^0。
sbit ModeKey=P1^0。
sbit UpKey=P1^1。
sbit DownKey=P1^2。
sbit Speak=P3^6。
code ucharLCD_NUM[10]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09}。
//0x25,//uchar Clock[]={0x88,0x88,0x88}。
51单片机蜂鸣器代码理解
51单片机蜂鸣器代码理解1.引言1.1 概述概述:蜂鸣器是一种广泛应用于电子设备中的声音输出装置,它通过控制某个频率的电信号使蜂鸣器发出特定的声音。
而51单片机,则是一种常见的单片机芯片,具有广泛的应用领域。
本文将主要探讨51单片机蜂鸣器的代码理解和应用。
通过对其基本原理的概述以及相关代码的解析,希望读者能够深入理解51单片机蜂鸣器的工作原理和实现方式。
在第二部分中,我们将介绍单片机蜂鸣器的基本原理。
包括如何通过单片机控制蜂鸣器的电信号频率和时长,从而实现不同的声音效果。
接着,在第二点中,我们将详细解析51单片机蜂鸣器的代码。
通过对代码的分析,读者可以了解到如何使用51单片机的引脚功能和定时器功能来控制蜂鸣器。
最后,在结论部分,我们将对所述内容进行总结,并展望51单片机蜂鸣器在未来的应用前景。
蜂鸣器作为一种重要的声音输出装置,具有广泛的应用前景,可以应用于报警系统、提醒装置等领域。
通过本文的阅读,读者将能够全面了解51单片机蜂鸣器的工作原理和代码实现方式,为相关领域的应用开发提供参考和指导。
让我们开始探索吧!1.2 文章结构文章结构的部分主要介绍了本文的组织和分类方式,以帮助读者更好地理解文章的内容和思路。
本文按照以下结构进行组织:1. 引言部分:介绍了文章的概述、结构和目的。
通过引言部分,读者可以初步了解到本文的内容和主题,并对文章的结构和目的有一个整体的认识。
2. 正文部分:主要分为两个小节,分别是"单片机蜂鸣器的基本原理"和"51单片机蜂鸣器代码解析"。
2.1 单片机蜂鸣器的基本原理:该部分将详细介绍单片机蜂鸣器的基本工作原理,包括蜂鸣器的构成和工作原理,以及单片机如何控制蜂鸣器发出指定的声音。
2.2 51单片机蜂鸣器代码解析:该部分将对51单片机蜂鸣器的代码进行解析,包括如何初始化引脚、设置定时器和中断等相关代码。
通过对代码的逐行解析和说明,读者可以更加深入地理解代码的功能和实现原理。
51单片机蜂鸣器播放音乐代码(生日快乐 两只蝴蝶 祝你平安)
精心整理/*生日快乐歌曲*/#include<reg51.h>#defineuintunsignedint#defineucharunsignedcharsbitbeep=P1^5;ucharcodeSONG_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};ucharcodeSONG_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};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t<120;t++);}voidPlayMusic(){uinti=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){//播放各个音符,SONG_LONG为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}voidmain(){beep=0;while(1){PlayMusic();//播放生日快乐DelayMS(500);//播放完后暂停一段时间}}两只蝴蝶:#include<reg51.h>//这是单片机音乐代码生成器生成的代码#defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharm,n;ucharcodeT[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03},{0xFB,0x4A },{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x81},{0xFD,0x A5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0},{0xFE,0x02 },{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{0xFF,0x69}, {0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};ucharcodemusic[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16,4},{19,4 },{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16,4},{19,4 },{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16,4},{19,4 },{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16,4},{16,2},{19,2 },{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{23,4},{23,4},{26,4},{28,8},{28,12}, {16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{0,4},{28,4},{28,4},{30,4},{28,4},{2 6,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16},{0xFF,0xFF}};voiddelay(ucharp){uchari,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}voidpause(){uchari,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}voidT0_int()interrupt1{beepIO=!beepIO;TH0=T[m][0];TL0=T[m][1];}voidmain(){uchari=0;TMOD=0x01;EA=1;ET0=1;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}elseif(m==0xFF){TR0=0;delay(30);i=0;}elseif(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include"reg52.h"unsignedcharCount;sbit_Speak=P1^5;unsignedcharcodeSONG[]={//祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10, 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};voidTime0_Init(){TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振,10ms}voidTime0_Int()interrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/voidDelay_xMs(unsignedintx){unsignedinti,j;for(i=0;i<x;i++){for(j=0;j<3;j++);}}voidPlay_Song(unsignedchari){unsignedcharTemp1,Temp2; unsignedintAddr;Count=0;//中断计数器清0Addr=i*217;while(1){Temp1=SONG[Addr++];if(Temp1==0xFF)//休止符{TR0=0;Delay_xMs(100);}elseif(Temp1==0x00)//歌曲结束符{return;}else{Temp2=SONG[Addr++];TR0=1;while(1){_Speak=~_Speak;Delay_xMs(Temp1);if(Temp2==Count){Count=0;break;}}}}}/*------------------------------------------------- 功能:主程序-------------------------------------------------*/ voidmain(){Time0_Init();//定时器0中断初始化while(1){Play_Song(0);//播放}}。
51单片机蜂鸣器播放音乐代码
/*生日快乐歌曲*/#include <>#define uint unsigned int#define uchar unsigned charsbit 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};//延时!void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}void PlayMusic(){uint i=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LONG 为拍子长度%for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE 延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}~void main(){beep=0;while(1){PlayMusic(); //播放生日快乐DelayMS(500); //播放完后暂停一段时间}}两只蝴蝶:%#include <> // 这是单片机音乐代码生成器生成的代码#define uchar unsigned charsbit beepIO=P1^5; // 输出为可以修改成其它IO 口uchar m,n;uchar code T[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{0xFB,0x03 },{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{0xFD,0x8 1},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{0xFE,0xC0 },{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{0xFF,0x60},{ 0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};~uchar code music[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4},{23,4},{21,4},{19 ,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4},{21,4},{19,4},{16 ,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{23,4},{23,4},{26,4} ,{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{0,4},{28,4},{28,4},{ 30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16},{0xFF,0xFF}};void delay(uchar p){.uchar i,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}void pause(){uchar i,j;for(i=150;i>0;i--)for(j=150;j>0;j--);/}void T0_int() interrupt 1{beepIO=!beepIO;TH0=T[m][0]; TL0=T[m][1];}void main(){uchar i=0;TMOD=0x01; EA=1; ET0=1;、while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}else if(m==0xFF){TR0=0;delay(30);i=0;}else if(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else.{TR0=1;delay(n);i++;}}}祝你平安:#include ""unsigned char Count;sbit _Speak =P1^5 ;unsigned char code SONG[] ={ //祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, %0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,¥0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00}; void Time0_Init(){TMOD = 0x01;IE = 0x82;TH0 = 0xD8;TL0 = 0xEF; //12MZ晶振,10ms}void Time0_Int() interrupt 1({TH0 = 0xD8;TL0 = 0xEF;Count++; //长度加1}/*-------------------------------------------------功能:1MS延时子程序-------------------------------------------------*/void Delay_xMs(unsigned int x){)unsigned int i,j;for( i =0;i < x;i++ ){for( j =0;j<3;j++ );}}void Play_Song(unsigned char i){unsigned char Temp1,Temp2;unsigned int Addr;!Count = 0; //中断计数器清0Addr = i * 217;while(1){Temp1 = SONG[Addr++];if ( Temp1 == 0xFF ) //休止符{TR0 = 0;Delay_xMs(100);}:else if ( Temp1 == 0x00 ) //歌曲结束符{return;}else{Temp2 = SONG[Addr++];TR0 = 1;while(1){._Speak = ~_Speak;Delay_xMs(Temp1);if ( Temp2 == Count ){Count = 0;break;}}}}}/*-------------------------------------------------功能:主程序-------------------------------------------------*/ void main(){Time0_Init(); //定时器0中断初始化while(1){Play_Song(0); //播放}}。
蜂鸣器唱歌程序
蜂鸣器唱歌----世上只有妈妈好#include<reg52.h>#define uchar unsigned char#define uint unsigned intsbit sound=P2^0;uint counter=0;uchar code FREQH[3][8]= //音符定时器初值,三个8度音的高八位{//注意每行起始都为0xff,因为音调中没有0 这个音,所以随便填了个数字,后面不不会引用它// 低音:// 1 2 3 4 5 6 7{0xff,0xf9, 0xfa, 0xfa, 0xfb, 0xfb, 0xfc, 0xfc},//中音://1 2 3 4 5 6 7{0xff,0xfc, 0xfd, 0xfd, 0xfd, 0xfd, 0xfe, 0xfe},//高音://1 2 3 4 5 6 7{0xff,0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xff, 0x00}//0x00};uchar code FREQL[3][8]= //音符定时器初值,三个8度音的低八位{// 低音:// 1 2 3 4 5 6 7{0xff,0x5b, 0x15, 0xb9, 0x04, 0x90, 0x0c, 0x79},//中音:// 1 2 3 4 5 6 7{0xff,0xac, 0x09, 0x5c, 0x82, 0xc8, 0x05, 0x3c},//高音://1 2 3 4 5 6 7{0xff,0x55, 0x84, 0xad, 0xc0, 0xe3, 0x02, 0x00}//0x00};uchar code MUSIC[]={//生日快乐//歌曲代码,每三个表示一个音符及节拍,第一个表示音符1234567,第二个表示音高,0表示低音,1表示中音,2表示高音,第三个表示节拍长度,1表示半拍,2表示一拍,以此6,1,3, 5,1,1, 3,1,2, 5,1,2, 1,2,2, 6,1,1, 5,1,1, 6,1,4, 3,1,1, 5,1,1, 6,1,1, 5,1,2, 3,1,2,1,1,1, 6,0,1, 5,1,1, 3,1,1, 2,1,4, 2,1,3, 3,1,1, 5,1,2, 5,1,1, 6,1,1,3,1,2, 2,1,2, 1,1,4, 5,1,4, 3,1,1, 2,1,1, 1,1,1, 6,0,1, 1,1,1, 5,0,8, 0};void init(){//定时器初始化TMOD=0x11;TH0=0xd8;TL0=0xef;EA=1;ET0=1;}void timer0(void) interrupt 1 using 3{//timer0中断服务程序,中断一次延时10mscounter++;//节拍次数计数TH0=0xd8; //定义单位节拍的延时大小TL0=0xef;}void delay(uchar n){ //延时n毫秒uchar i;while(n--)for(i=0;i<125;i++);}void sound_delay(uchar sound_signalhigh,uchar sound_signallow)//音调延时{TH1=sound_signalhigh;TL1=sound_signallow;TR1=1;while(TF1==0);TR1=0;TF1=0;}void main(){uint i,j,k=0;uchar sound_signalhigh,sound_signallow;//定义音符大小uchar sound_pace;//定义节拍大小while(1){k=0;while(MUSIC[k]!=0)//因为歌曲的结尾处人为的加入了标示符0{j=MUSIC[k++];//从表中取得音符大小i=MUSIC[k++];sound_signalhigh=FREQH[i][j];sound_signallow=FREQL[i][j];//从表中取得节拍大小sound_pace=30*MUSIC[k++];//600为一拍的时长,在此修改一拍的时长。
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安/*生日快乐歌曲*/#include<reg51.h>#defineuintunsignedint#defineucharunsignedcharsbitbeep=P1^5;ucharcodeSONG_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};ucharcodeSONG_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};//延时voidDelayMS(uintx){uchart;while(x--)for(t=0;t<120;t++);}voidPlayMusic(){uinti=0,j,k;while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){//播放各个音符,SONG_LONG为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){beep=~beep;//SONG_TONE延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);}DelayMS(10);i++;}}voidmain(){beep=0;while(1){PlayMusic();//播放生日快乐DelayMS(500);//播放完后暂停一段时间}}两只蝴蝶:#include<reg51.h>//这是单片机音乐代码生成器生成的代码#defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharcodeT[49][2]={{0,0},{0xF8,0x8B},{0xF8,0xF2},{0xF9,0x5B},{0xF9,0xB7},{0xFA,0x14},{0xFA,0x66},{0xFA,0xB9},{ 0xFB,0x03},{0xFB,0x4A},{0xFB,0x8F},{0xFB,0xCF},{0xFC,0x0B},{0xFC,0x43},{0xFC,0x78},{0xFC,0xAB},{0xFC,0xDB},{0xFD,0x08},{0xFD,0x33},{0xFD,0x5B},{ 0xFD,0x81},{0xFD,0xA5},{0xFD,0xC7},{0xFD,0xE7},{0xFE,0x05},{0xFE,0x21},{0xFE,0x3C},{0xFE,0x55},{0xFE,0x6D},{0xFE,0x84},{0xFE,0x99},{0xFE,0xAD},{ 0xFE,0xC0},{0xFE,0x02},{0xFE,0xE3},{0xFE,0xF3},{0xFF,0x02},{0xFF,0x10},{0xFF,0x1D},{0xFF,0x2A},{0xFF,0x36},{0xFF,0x42},{0xFF,0x4C},{0xFF,0x56},{ 0xFF,0x60},{0xFF,0x69},{0xFF,0x71},{0xFF,0x79},{0xFF,0x81}};ucharcodemusic[][2]={{0,4},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,24},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{21,24},{23,4},{21,4},{23,16},{23,4},{21,4},{23,4},{21,4},{19,16},{16,4},{19,4},{21,8},{21,4} ,{23,4},{21,4},{19,4},{16,4},{19,4},{14,24},{23,4},{26,4},{26,16},{26,4},{28,4},{26,4},{23,24},{21,4},{23,4},{21,8},{21,4},{23,4} ,{21,4},{19,4},{16,4},{16,2},{19,2},{19,24},{0,20},{26,4},{26,4},{28,4},{31,4},{30,4},{30,4},{28,4},{23,4},{21,4},{21,4},{23,16},{0,4},{ 23,4},{23,4},{26,4},{28,8},{28,12},{16,4},{23,4},{21,4},{21,24},{23,4},{26,4},{26,4},{23,4},{26,8},{0,4},{31,8},{30,4},{28,4},{30,4},{23,8},{ 0,4},{28,4},{28,4},{30,4},{28,4},{26,4},{23,4},{21,8},{23,4},{21,4},{23,4},{26,16}, {0xFF,0xFF}};voiddelay(ucharp){uchari,j;for(;p>0;p--)for(i=181;i>0;i--)for(j=181;j>0;j--);}voidpause(){uchari,j;for(i=150;i>0;i--)for(j=150;j>0;j--);}voidT0_int()interrupt1{beepIO=!beepIO;TH0=T[m][0];TL0=T[m][1];}voidmain(){uchari=0;while(1){m=music[i][0];n=music[i][1];if(m==0x00){TR0=0;delay(n);i++;}elseif(m==0xFF){TR0=0;delay(30);i=0;}elseif(m==music[i+1][0]){TR0=1;delay(n);TR0=0;pause();i++;}else{TR0=1;delay(n);i++;}}}祝你平安:#include"reg52.h"unsignedcharCount;sbit_Speak=P1^5;unsignedcharcodeSONG[]={//祝你平安0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20, 0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20, 0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20, 0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10, 0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20, 0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20, 0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20, 0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10, 0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80, 0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20, 0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20, 0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10, 0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00};voidTime0_Init(){TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振,10ms}voidTime0_Int()interrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}功能:1MS延时子程序-------------------------------------------------*/ voidDelay_xMs(unsignedintx){unsignedinti,j;for(i=0;i<x;i++){for(j=0;j<3;j++);}}voidPlay_Song(unsignedchari){unsignedcharTemp1,Temp2;unsignedintAddr;Count=0;//中断计数器清0Addr=i*217;while(1){Temp1=SONG[Addr++];if(Temp1==0xFF)//休止符{TR0=0;Delay_xMs(100);}elseif(Temp1==0x00)//歌曲结束符{return;}else{Temp2=SONG[Addr++];TR0=1;while(1){_Speak=~_Speak;Delay_xMs(Temp1);if(Temp2==Count){Count=0;break;}}}}}功能:主程序-------------------------------------------------*/ voidmain(){Time0_Init();//定时器0中断初始化while(1){Play_Song(0);//播放}}。
基于51单片机蜂鸣器发声的-C语言程序
本例使用延时函数实精品文档按键发音/* 名称:按键发音说明:按下不同的按键会是SOUNDE 发出不同频率的声音。
现不同频率的声音输出,以后也可使用定时器*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3A 7;sbit K 仁 P1A4;sbit K2=P1A5;sbit K3=P1A6;sbit K4=P1A7;// 延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}// 按周期 t 发音void Play(uchar t){uchar i;for(i=0;i<100;i++){BEEP=~BEEP;DelayMS(t);}BEEP=0;}void main(){P1=0xff;BEEP=0;while(1){if(K1==0) Play(1);if(K2==0) Play(2);if(K3==0) Play(3);if(K4==0) Play(4);}}精品文档播放音乐/* 名称:播放音乐说明:程序运行时播放生日快乐歌, 未使用定时器中断,所有频率完全用延时 实现*/#include<reg51.h>#define uchar unsigned char#define uint unsigned intsbit BEEP=P3A7;// 生日快乐歌的音符频率表,不同频率由不同的延时来决定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};// 延时void DelayMS(uint x){uchar t;while(x--) for(t=0;t<120;t++);}// 播放函数void PlayMusic(){ uint i=0,j,k; while(SONG_LONG[i]!=0||SONG_TONE[i]!=0){ //播放各个音符,SONG_LON为拍子长度for(j=0;j<SONG_LONG[i]*20;j++){BEEP=~BEEP;〃SONG_TON延时表决定了每个音符的频率for(k=0;k<SONG_TONE[i]/3;k++);} DelayMS(10);i++;}}void main(){BEEP=0;while(1){PlayMusic(); // 播放生日快乐DelayMS(500); // 播放完后暂停一段时间}精品文档。
蜂鸣器演奏祝你平安——51单片机
蜂鸣器演奏祝你平安——51单片机的Proteus实验实验原理51单片机的一个I/O口控制speaker发声,演唱祝你平安歌曲。
主要器件以及电路图单片机——AT89C51,蜂鸣器——speaker。
汇编程序源码晶振12M,已在Proteus下测试,好像有点失真。
程序是从网上照的,感觉不是很对劲,删除了一部分之后还是没有变化。
部分注释是我后加上去的。
;------------------------------------; 蜂鸣器演奏--祝你平安; 功能:蜂鸣器-蜂鸣器奏乐-祝你平安;------------------------------------SPK bit P2.7 ;以后SPK就是speaker了ORG 0000HLJMP STARTORG 000BHINC 20H ;T0中断服务,中断计数器加1MOV TH0,#0D8HMOV TL0,#0EFH ;12M晶振,形成10毫秒中断RETISTART:MOV TH0,#0D8HMOV TL0,#0EFHMOV TMOD,#01HMOV IE,#82H ;10000010B——开T0中断MUSIC0:NOPMOV DPTR,#DAT ;表头地址送DPTRMOV 20H,#00H ;中断计数器清0MUSIC1:NOPCLR AMOVC A,@A+DPTR ;查表取代码JZ END0 ;是00H,则结束CJNE A,#0FFH,MUSIC5 ;只要A不等于FF,一直执行MUSIC5LJMP MUSIC3 ;当A到FF的时候,执行MUSIC3 MUSIC5:NOPMOV R6,A ;代码送R6INC DPTRMOVC A,@A+DPTR ;取节拍代码送R7MOV R7,ASETB TR0 ;启动计数MUSIC2:NOPCPL SPKMOV A,R6 ;按R6(代码)延时MOV R3,A ;LCALL DEL ;利用代码(R6)来调用延时CJNE A,20H,MUSIC2 ;中断计数器(20H)=R7否?不等,则继续循环 MOV 20H,#00H ;等于,则取下一代码INC DPTRLJMP MUSIC1MUSIC3:NOPCLR TR0 ;休止100毫秒MOV R2,#0DHMUSIC4:NOPMOV R3,#0FFHLCALL DELDJNZ R2,MUSIC4INC DPTRLJMP MUSIC1END0:NOPMOV R2,#64H ;歌曲结束,延时1秒后继续MUSIC6:MOV R3,#00HLCALL DELDJNZ R2,MUSIC6LJMP MUSIC0DEL: ;DEL子程序利用R3来延时一定时间 NOPDEL3:MOV R4,#02HDEL4:NOPDJNZ R4,DEL4NOPDJNZ R3,DEL3RETNOPDAT: ;祝你平安db 26h,20h,20h,20h,20h,20h,26h,10h,20h,10h,20h,80h,26h,20h,30h,20h db 30h,20h,39h,10h,30h,10h,30h,80h,26h,20h,20h,20h,20h,20h,1ch,20h db 20h,80h,2bh,20h,26h,20h,20h,20h,2bh,10h,26h,10h,2bh,80h,26h,20h db 30h,20h,30h,20h,39h,10h,26h,10h,26h,60h,40h,10h,39h,10h,26h,20h db 30h,20h,30h,20h,39h,10h,26h,10h,26h,80h,26h,20h,2bh,10h,2bh,10h db 2bh,20h,30h,10h,39h,10h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,20h db 20h,10h,20h,10h,2bh,10h,26h,30h,30h,80h,18h,20h,18h,20h,26h,20h db 20h,20h,20h,40h,26h,20h,2bh,20h,30h,20h,30h,20h,1ch,20h,20h,20h db 20h,80h,1ch,20h,1ch,20h,1ch,20h,30h,20h,30h,60h,39h,10h,30h,10h db 20h,20h,2bh,10h,26h,10h,2bh,10h,26h,10h,26h,10h,2bh,10h,2bh,80h db 18h,20h,18h,20h,26h,20h,20h,20h,20h,60h,26h,10h,2bh,20h,30h,20h db 30h,20h,1ch,20h,20h,20h,20h,80h,26h,20h,30h,10h,30h,10h,30h,20h db 39h,20h,26h,10h,2bh,10h,2bh,20h,2bh,40h,40h,10h,40h,10h,20h,10h db 20h,10h,2bh,10h,26h,30h,30h,80h,00HEND ;程序结束。
51单片机c语言编写电子琴+音乐播放器
此程序是51单片机c语言编写的电子琴+音乐播放器,当用杜邦线将P1^0管脚接地时,单片机就会放出《世上只有妈妈好》,将P1^1 管脚接地时单片机就播放《祝你生日快乐》,两者为高电平时进入电子钢琴模式,利用16个矩阵键盘发出相应的哆、瑞、米、发、索。
此文档的乐谱是借鉴别人的,因为本人是音乐白痴!这是我第一次发表,我希望能帮到有需要的人啦........#include <reg52.h>#define uchar unsigned char#define uint unsigned intsbit beep=P2^3; //定义蜂鸣器sbit music=P1^0; //music为低电平播放《世上只有妈妈好》(工作模式为音乐播放器),music1为低电平时播放祝你生日快乐,music、music1都为高电平时(工作模式为电子钢琴)sbit music1=P1^1;uchar timer0h,timer0l,time,key,flag; //flag==0时为电子钢琴模式,flag==1时为音乐播放器uchar code yuepu[]={ 6,2,3,5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1, 6,2,4,3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1, 6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2, 5,2,1,6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1, 2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0 }; //世上只有妈妈好乐谱uchar code yuepu1[]={5,1,1, 5,1,1, 6,1,2, 5,1,2, 1,2,2, 7,1,4,5,1,1, 5,1,1, 6,1,2, 5,1,2, 2,2,2, 1,2,4,5,1,1, 5,1,1, 5,2,2, 3,2,2, 1,2,2, 7,1,2, 6,1,2,4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4};//祝你生日快乐乐谱uchar code TH_0[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC,0xFC,0xFD,0xFD,0xFD,0xFD,0xFE, // 28个音阶高八位频率表装进定时器高八位0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,} ;uchar code TL_0[]={0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, // 28个音阶低八位频率表装进定时器低八位0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,};void delay(uchar t){uchar t1;unsigned long t2;for(t1=0;t1<t;t1++) //延时半拍函数,控制音乐节奏{for(t2=0;t2<8000;t2++){ ; }}}void song(){TH0=timer0h;TL0=timer0l; // 唱歌程序TR0=1;delay(time); //延时,让此音节播放的长短TR0=0; //关定时器,为下个音节响做准备}void delayms(uint x){uint i,j; //去抖延时for(i=x;i>0;i--)for(j=110;j>0;j--);}void keyscan() //检测十六个发出音乐按键函数{ uchar temp;P3=0xfe;temp=P3;temp=0xf0&temp;if(temp!=0x0f){ delayms(10); //检测第一行按键temp=P3 ;temp=temp&0xf0;if(temp!=0xf0){temp=P3;switch(temp){case 0xee: key=0;break;case 0xde: key=1;break;case 0xbe: key=2;break;case 0x7e: key=3;break;}TH0=TH_0[key]; //将按下的键对应发出此频率声音的初值装入TL0=TL_0[key];TR0=1; //打开定时器0while(temp!=0xf0) //一直按下就一直发声{temp=P3;temp=temp&0xf0;}TR0=0; //松手时关闭定时器0,不能发声beep=1;//使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开}}P3=0xfd;temp=P3;temp=0xf0&temp;if(temp!=0x0f) // 检测第二行按键{ delayms(10);temp=P3 ;temp=temp&0xf0;if(temp!=0xf0){temp=P3;switch(temp){case 0xed: key=4;break;case 0xdd: key=5;break;case 0xbd: key=6;break;case 0x7d: key=7;break;}TH0=TH_0[key]; //将按下的键对应发出此频率声音的初值装入TL0=TL_0[key];定时器0while(temp!=0xf0) //一直按下就一直发声{temp=P3;temp=temp&0xf0;}TR0=0; //松手时关闭定时器0,不能发声beep=1; //使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开}}P3=0xfb;temp=P3;temp=0xf0&temp;if(temp!=0x0f){ delayms(10);temp=P3 ;temp=temp&0xf0;if(temp!=0xf0){temp=P3;switch(temp){case 0xeb: key=8;break;case 0xdb: key=9;break;case 0xbb: key=10;break;case 0x7b: key=11;break;}TH0=TH_0[key]; //将按下的键对应发出此频率声音的初值装入TL0=TL_0[key];TR0=1; //打开定时器0while(temp!=0xf0) //一直按下就一直发声{temp=P3;temp=temp&0xf0;}手时关闭定时器0,不能发声beep=1; //使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开}}P3=0xf7;temp=P3;temp=0xf0&temp;if(temp!=0x0f){ delayms(10);temp=P3 ;temp=temp&0xf0;if(temp!=0xf0){temp=P3;switch(temp){case 0xe7: key=12;break;case 0xd7: key=13;break;case 0xb7: key=14;break;case 0x77: key=15;break;}TH0=TH_0[key]; //将按下的键对应发出此频率声音的初值装入TL0=TL_0[key];TR0=1; //打开定时器0while(temp!=0xf0) //一直按下就一直发声{temp=P3;temp=temp&0xf0;}TR0=0; //松手时关闭定时器0,不能发声beep=1; //使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开}}}void main(){uchar k,i;TMOD=1;EA=1;ET0=1;while(1){if(music&&music1) //当两个都为高电平时没有放音乐,扫描键盘{ flag=0;keyscan();}if(!music) //当P1^0管脚为低电平时,播放《世上只有妈妈好》{ flag=1;i=0;while(i<100) //音乐数组长度,唱完从头再来{if(music) //在播放音乐时P1^0为高电平时退出播放此歌曲{ beep=1; //使蜂鸣器置高关闭,预防在进入中断取反时将它置低打开break;}k=yuepu[i]+7*yuepu[i+1]-1;timer0h=TH_0[k]; // 把对应的音乐频率放进定时器0timer0l=TL_0[k];time=yuepu[i+2]; //音乐节拍i+=3;song();}}if(!music1) // music为低电平时,播放《祝你生日快乐》{ flag=1;i=0;while(i<75) //乐谱的长度{if(music1){beep=1;break;}k=yuepu1[i]+7*yuepu1[i+1]-1;timer0h=TH_0[k];timer0l=TL_0[k];time=yuepu1[i+2];i+=3;song();}}}}void time0() interrupt 1{if(flag==0){beep=!beep; //电子琴模式时进入此语句TH0=TH_0[key];TL0=TL_0[key];}else{ beep=!beep;TH0=timer0h; //音乐播放器模式进入此语句TL0=timer0l;}}。
(原创)51单片机C语言程序设计--速学教程实例(进阶篇)之蜂鸣器奏乐
/******************************************************************//*名称:蜂鸣器奏乐演示程序*//*作者:苏涛*//*时间:2011-01-18*//*单位:安徽建筑工业学院电子与信息工程学院*//*07级电子信息工程专业二班*//******************************************************************/#include<reg52.h>//头文件调用,写程序时都要加上#include"SoundPlay.h"/******************************************************************//*名称:延时函数*//*功能:C语言中常用的for循环延时方法*//*延时长短可以通过变量初值来改变*//******************************************************************/void Delay1ms(uint count){uint i,j;for(i=0;i<count;i++)for(j=0;j<120;j++);}/*****************************Music***************************************************/ //挥着翅膀的女孩unsigned char code Music_Girl[]={0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x03,0x16,0x03,0x17,0x03,0x17,0x03,0x17,0x03,0x18,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x02,0x18,0x03,0x17,0x03,0x15,0x02,0x18,0x03,0x17,0x03,0x18,0x02,0x10,0x03,0x15,0x03,0x16,0x02,0x15,0x03,0x16,0x03,0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x1A,0x03,0x1B,0x03,0x1F,0x03,0x1F,0x03,0x17,0x03,0x18,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x1F,0x03,0x1F,0x02,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x1A,0x03,0x1A,0x03,0x1A,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x19,0x03,0x19,0x02,0x17,0x03,0x15,0x17,0x15,0x03,0x16,0x03,0x17,0x03,0x18,0x03,0x17,0x04,0x18,0x0E,0x18,0x03,0x17,0x04,0x18,0x0E,0x18,0x66,0x17,0x03,0x18,0x03,0x17,0x03,0x18,0x03,0x20,0x03,0x20,0x02,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x04,0x1B,0x0E,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x15,0x03,0x15,0x03,0x17,0x03,0x16,0x66,0x17,0x04,0x18,0x04,0x18,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x20,0x03,0x21,0x03,0x20,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x66,0x1F,0x03,0x1B,0x03,0x19,0x03,0x19,0x03,0x15,0x03,0x1A,0x66,0x1A,0x03,0x19,0x03,0x19,0x03,0x1F,0x03,0x1B,0x03,0x1F,0x00,0x18,0x02,0x18,0x03,0x1A,0x03,0x19,0x0D,0x15,0x03,0x15,0x02,0x18,0x66,0x16,0x02,0x17,0x02,0x15,0x00,0x00,0x00}; //同一首歌unsigned char code Music_Same[]={0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x66,0x18,0x03,0x17,0x02,0x15,0x02,0x16,0x01,0x15,0x02,0x10,0x02,0x15,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x02,0x17,0x03,0x18,0x03,0x19,0x02,0x15,0x02,0x18,0x66,0x17,0x03,0x19,0x02,0x16,0x03,0x17,0x03,0x16,0x00,0x17,0x01,0x19,0x02,0x1B,0x02,0x1B,0x70,0x1A,0x03,0x1A,0x01,0x19,0x02,0x19,0x03,0x1A,0x03,0x1B,0x02,0x1A,0x0D,0x19,0x03,0x17,0x00,0x18,0x66,0x18,0x03,0x19,0x02,0x1A,0x02,0x19,0x0C,0x18,0x0D,0x17,0x03,0x16,0x01,0x11,0x02,0x11,0x03,0x10,0x03,0x0F,0x0C,0x10,0x02,0x15,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x1B,0x02,0x1B,0x03,0x1B,0x03,0x1B,0x0C,0x1A,0x0D,0x19,0x03,0x17,0x00,0x1F,0x01,0x1A,0x01,0x18,0x66,0x19,0x03,0x1A,0x01,0x10,0x02,0x10,0x03,0x10,0x03,0x1A,0x0C,0x18,0x0D,0x17,0x03,0x16,0x00,0x0F,0x01,0x15,0x02,0x16,0x02,0x17,0x70,0x18,0x03,0x17,0x02,0x15,0x03,0x15,0x03,0x16,0x66,0x16,0x03,0x16,0x02,0x16,0x03,0x15,0x03,0x10,0x02,0x10,0x01,0x11,0x01,0x11,0x66,0x10,0x03,0x0F,0x0C,0x1A,0x02,0x19,0x02,0x16,0x03,0x16,0x03,0x18,0x66,0x18,0x03,0x18,0x02,0x17,0x03,0x16,0x03,0x19,0x00,0x00,0x00};//两只蝴蝶unsigned char code Music_Two[]={0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x15,0x03,0x16,0x01,0x17,0x03,0x16,0x03,0x17,0x01,0x16,0x03,0x17,0x03,0x16,0x03,0x15,0x01,0x10,0x03,0x15,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0E,0x15,0x04,0x0F,0x01,0x17,0x03,0x19,0x03,0x19,0x01,0x19,0x03,0x1A,0x03,0x19,0x03,0x17,0x01,0x16,0x03,0x16,0x03,0x16,0x02,0x16,0x0D,0x17,0x03,0x16,0x03,0x15,0x03,0x10,0x03,0x10,0x0D,0x15,0x00,0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x0D,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x02,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x17,0x0D,0x16,0x03,0x17,0x03,0x19,0x01,0x19,0x03,0x19,0x03,0x1A,0x03,0x1F,0x03,0x1B,0x03,0x1B,0x03,0x1A,0x03,0x17,0x0D,0x16,0x03,0x16,0x03,0x16,0x03,0x17,0x01,0x17,0x03,0x17,0x03,0x19,0x03,0x1A,0x02,0x1A,0x02,0x10,0x03,0x17,0x0D,0x16,0x03,0x16,0x01,0x17,0x03,0x19,0x03,0x19,0x03,0x17,0x03,0x19,0x03,0x1F,0x02,0x1B,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x02,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x17,0x16,0x1A,0x03,0x1A,0x03,0x1A,0x0E,0x1B,0x04,0x1A,0x03,0x19,0x03,0x17,0x03,0x16,0x03,0x0F,0x02,0x10,0x03,0x15,0x00,0x00,0x00};/*************************************************************************************/ void main(){InitialSound();while(1){Play(Music_Girl,0,3,360);Delay1ms(500);Play(Music_Same,0,3,360);Delay1ms(500);Play(Music_Two,0,3,360);Delay1ms(500);}}/******************************************************************//*头文件:SOUNDPLAY.H*//*说明:此头文件来源于网络*//******************************************************************//*说明**************************************************************曲谱存贮格式unsigned char code MusicName{音高,音长,音高,音长....,0,0};末尾:0,0表示结束(Important)音高由三位数字组成:个位是表示1~7这七个音符十位是表示音符所在的音区:1-低音,2-中音,3-高音;百位表示这个音符是否要升半音:0-不升,1-升半音。
51单片机电子音乐设计
实验三电子音乐设计一、实验内容
1.调试《生日快乐歌》,在实验板上运行程序播放。
2.编写《兰花草》程序,在实验板上运行程序播放。
二.实验设计
1.硬件电路
图3.1 硬件电路原理图
2.程序设计
《生日快乐》程序,参考课件。
《兰花草》程序设计流程图,见图3.2, 3.2. 单片机P3.7输出一定频率信号,经过9012驱动蜂鸣器发声。
三、实验操作
1. 打开KEILuvison3软件,建立工程,设置调试环境,实验板上电;
2. 输入源程序,编译,连接,加载;
3. 运行《生日快乐》歌曲程序,聆听歌曲播放效果;
4.运行《兰花草》歌曲程序,聆听歌曲播放效果。
四、实验报告要求
1.实验名称
2.实验内容
3.实验设计,包括:
(1)硬件电路原理图
(2)程序流程图和源程序
4.实验操作,包括调试程序的方法、过程、出现的问题及解决
5.实验总结
图3.3 T0中断子程序流程图图3.2电子音乐主程序流程图。
51单片机演奏音乐“祝你生日快乐”
R7,A ;r7=30H 音符发音的时间 TR0 ;启动计数
;不等,则继续循环
MOV INC LJMP
20H,#00H DPTR MUSIC1
;等于,则取下一代码
MUSIC3: NOP CLR MOV TR0 ;休止 100 毫秒 R2,#0DH
MUSIC4: NOP MOV LCALL DJNZ INC LJMP END0: NOP R2,#64H ;歌曲结束,延时 1 秒后继续 R3,#0FFH DEL R2,MUSIC4 DPTR MUSIC1
MOV
MUSIC6: MOV LCALL DJNZ LJMP DEL: NOP DEL3: DEL4: MOV NOP DEL
R3,#00H
R2,MUSIC6 MUSIC0
R4,#02H
DJNZ NOP DJNZ RET MUSIC_DAT:
R4,DEL4
R3,DEL3
; 猪你生日快乐曲谱 DB 3FH,10H DB 0FFH DB 3FH, DB DB DB DB 0FFH DB DB DB DB 3FH, 10H 20H, 30H, 3FH, 40H 20H 10H 39H, 30H, 20H, 10H 20H, 33H, 3FH, 40H 20H
TH0,#0bbH ;d8 TL0,#0EFH ;12M 晶振,形成 10 毫秒中断
TH0,#0D8H TL0,#0EFH TMOD,#01H IE,#82H
MUSIC1: NOP
CLR MOVC JZ
A A,@A+DPTR E来自D0 ;查表取代码;是 00H,则结束
CPL P1.7 CPL P1.6 CJNE LJMP MUSIC5: NOP MOV INC MOV MOVC MOV SETB MUSIC2: NOP CPL MOV MOV LCALL MOV CJNE P2.3 ;音乐输出的管脚 A,R6 R3,A DEL A,R7 A,20H,MUSIC2 ;中断计数器(20H)=R7 否? R6,A DPTR A,B A,@A+DPTR ;取节拍代码送 R7 ;r6=18H 音符的频率 A,#0FFH,MUSIC5 MUSIC3 ;是休止符则继续执行