《基于51单片机的音乐盒设计.docx》由会员分享,可在线阅读,更多相关《基于51单片机的音乐盒设计.docx(9页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、基于51单片机的音乐盒设计 基于51单片机的音乐盒设计 题目要求: 1. 查阅资料,了解单片机控制单音喇叭发声原理; 2. 设计基于单片机的电子音乐盒; 3通过按钮可选择不同的音乐,切换3首音乐; 运行仿真图: 程序完整代码: #include sbit fx_buzzer=P37; /蜂鸣器音乐输出,可根据需要自行修改 sbit music_1 = P20; /第一首音乐 sbit music_2 = P21; /第二首音乐 sbit music_3 = P22; /第二首音乐 unsigned int tone; /简谱音调计数 unsigned char fx_timeh,fx_time
2、l,music,speed; /fx_timeh:TH0初值,fx_timel:TL0初值; music =1播放,=0停止 unsigned char code fx_tone0= /音调对应定时器初值 0xFC,0x8E, 0xFC,0xED, 0xFD,0x43, /中音 0xFD,0x6A, 0xFD,0xB3, 0xFD,0xF3, 0xFE,0x2D, 0xFE,0x47, 0xFE,0x76, 0xFE,0xA1, /高音 0xFE,0xC7, 0xFE,0xD9, 0xFE,0xF9, 0xFF,0x16 ; unsigned char code songbie=/送别 0x5
3、4,0x32,0x52,0x88,0x64,0x84,0x58,0x54,0x12,0x22,0x34,0x22,0x12,0x2c,0x04, 0x54,0x32,0x52,0x86,0x72,0x64,0x84,0x58,0x54,0x22,0x32,0x46,0x72,0x1c, 0x64,0x8c,0x74,0x62,0x72,0x88,0x62,0x72,0x82,0x62,0x52,0x32,0x12,0x2f, 0x54,0x32,0x52,0x86,0x72,0x64,0x84,0x58,0x54,0x22,0x32,0x46,0x72,0x1c, 0x64,0x8c,0x74
4、,0x62,0x72,0x88,0x62,0x72,0x82,0x62,0x52,0x32,0x12,0x2f, 0x54,0x32,0x52,0x86,0x72,0x64,0x84,0x58,0x54,0x22,0x32,0x46,0x72,0x1c, 0xff; unsigned char 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,0x3
5、2,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,0x
6、61,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; unsigned char
7、code laohu= /两只老虎 0x14,0x14,0x24,0x34,0x14,0x14,0x24,0x34,0x14, 0x34,0x44,0x58,0x34,0x44,0x58, 0x53,0x61,0x53,0x41,0x34,0x14, 0x53,0x61,0x53,0x41,0x34,0x14, 0x14,0x54,0x18, 0x14,0x54,0x18, 0xFF; unsigned char keyValue; /存储按下的键值 void init() /初始化函数 EA=1;/开总中断 TMOD=0x10;/定时器0工作在方式1 TH1=0; TL1=0; ET1=1;
8、 music=1; /默认播放 tone=0; speed=20; /播放速度 void fx_delay(unsigned char i) /音长延时函数 unsigned int j,k; for(i;i0;i-) for(k=speed;k0;k-) for(j=625;j0;j-); void play(unsigned char *temp) /音阶播放函数 if(speed60) speed=60; while(1) if(!music) break; if(music=2) tone=0;music=1;break; /配合按钮换歌 if(temptone=0xff)tone=0
9、;break; if(temptone/16!=0) /取高4位的音阶判断 fx_timeh=fx_tone0temptone/16*2-2; fx_timel=fx_tone0temptone/16*2-1; TR1=1; fx_delay(temptone%16); /取数的低4位 TR1=0; tone+; TR1=0; void fx_tone() interrupt 3 /用于产生各种音调 TH1=fx_timeh; TL1=fx_timel; fx_buzzer=fx_buzzer; void Delay10ms(unsigned int n) /延时函数,延时10ms unsig
10、ned char a, b; for (; n0; n-) for (b=38; b0; b-) for (a=130; a0; a-); void EX0_INT(void) interrupt 0 /外部INT0 if(tone !=0 ) music = 2; if(music_1 = 0) Delay10ms(1); if(music_1 = 0) while(music_1 = 0); /等待松开按钮 keyValue = 1; /播放第一首音乐-送别 if(music_2 = 0) Delay10ms(1); if(music_2 = 0) while(music_2 = 0);
11、/等待松开按钮 keyValue = 2; /播放第二首音乐 if(music_3 = 0) Delay10ms(1); if(music_3 = 0) while(music_3 = 0); /等待松开按钮 keyValue = 3; /播放第三首音乐 /主函数 void main() init(); /初始化函数 /play(song1); /音阶播放函数 IT0=1; /外部中断INT0位下降沿触发 EX0=1; /开INT0中断允许 EA=1; while(1) if(keyValue = 1) play(songbie); /播放第一首音乐-送别 else if(keyValue = 2) play(qnzl); /播放第二首音乐 else if(keyValue = 3) play(laohu); /播放第三首音乐 Proteus仿真图: