单片机蜂鸣器播放音乐
单片机蜂鸣器播放音乐
#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单片机播放音乐的实际
电路注意事项
备注:由于在proteus软件中选取元件比较麻烦,此文档中的仿真图在仿真过程中未必能实现,但实际电路中一定是可行的。
1.常见仿真电路
这是一个简单的使用单片机播放音乐的仿真电路,但是在实际应用中我们使用的蜂鸣器规格一般为5V左右,所以做出来的电路播放音乐时的声音很小,就像耳机一样,塞在耳朵里才可以勉强听到音乐。
2.加上拉电阻加以改善时的电路
为此,让流过蜂鸣器的电流变大很有必要,以下是使用一个上拉电阻使情况加以改善的仿真图:
使用上拉电阻虽然可以使蜂鸣器的音量变大,但这需要上拉电阻的阻值比较小时才能实现。
而一旦上拉电阻过小,当单片机无输出时蜂鸣器会一直鸣叫,这样设计出来的音乐电路就很难与其他的功能模块相搭配。
3.使用放大电路加以改善
若想在单片机无输出时蜂鸣器也不工作,第一种方法是增大上拉电阻的阻值,但这样就必然会使得蜂鸣器的声音无法放大。
第二种方法是给蜂鸣器设置一个开关或是一个控制电路,用开关必然是不可取的,这样电路的智能性就被抹杀掉了。
所以就需要一种控制电路。
而使用晶体管放大电路就是一种简单有效的方法,给单片机的输出进行合适的放大,单片机无输出时蜂鸣器自然也不会工作。
而且用这种方法一个普通蜂鸣器播放音乐时的音质也会的到最大程度的保留.。
4.下面是添加一个绿色LED以美化电路的仿真图。
单片机蜂鸣器唱歌程序(二)2024
单片机蜂鸣器唱歌程序(二)引言概述:本文档主要介绍了单片机蜂鸣器唱歌程序(二),包括使用单片机控制蜂鸣器发出不同音乐的方法和具体实现步骤。
本文将从五个大点进行阐述,每个大点包含5-9个小点,以便读者更好地理解和实践。
正文:一、引脚连接设置1. 确定单片机的输出引脚和蜂鸣器的输入引脚2. 将单片机的输出引脚与蜂鸣器的输入引脚连接3. 确保连接的稳定性和正确性4. 利用电路图进行布线二、编程环境配置1. 安装适合单片机的编程软件2. 创建新的项目3. 配置单片机的型号及选项4. 导入相关的库文件5. 编写代码框架三、发声原理及代码实现1. 理解蜂鸣器工作原理2. 使用单片机的PWM输出功能控制蜂鸣器的频率3. 利用PWM输出的方式实现不同音调的发声4. 编写音调转换函数5. 编写歌曲的音乐片段代码四、优化和调试1. 测试不同频率的声音2. 调整蜂鸣器的音量3. 避免噪音的干扰4. 检查代码的正确性和合理性5. 不断尝试,优化代码和音效五、实验结果及总结1. 运行程序,测试蜂鸣器的唱歌效果2. 记录实验结果和观察结果3. 分析实验过程中遇到的问题和解决方法4. 总结实验经验和注意事项5. 展望将来的改进和研究方向总结:本文详细介绍了单片机蜂鸣器唱歌程序(二)的实现方法和步骤。
通过连接设置、编程环境配置、发声原理及代码实现、优化和调试、实验结果及总结等五个大点的阐述,读者可以深入了解单片机控制蜂鸣器发声的原理和方法,并通过实验得到具体的唱歌效果。
同时,读者在实践过程中也要注意优化和调试,不断尝试和改进,以实现更好的音效效果。
希望本文对读者有所帮助,为单片机蜂鸣器唱歌程序的开发提供了指导和参考。
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); //播放}}。
51单片机蜂鸣器播放单音节音乐
0xFD,0x23,0x7F, 0xFC,0xAC,0x7F, 0xFD,0x23,0xFF, 0xFC,0x44,0xFF,
// 1_ 3_ 2 .5
0xFC,0x44,0x7F, 0xFD,0x23,0x7F, 0xFC,0xAC,0xFF, 0xFA,0x68,0xFF,
{
for (i =0; i < 363; i++)
{;}
}
}
*名称:Init()
*功能:设置计数器0工作方式,16位计数,溢出中断方式
**********************************************************************************/
void Init()
{
TMOD = 0x01; //定时器0处于计时方式,16位
*名称:Count1(void) interrupt 1
*功能:设置计时器0溢出中断,每中断一次改变P2_3引脚电平
*********************************************************************************/
void Count1(void) interrupt 1
void main()
{
uchar time;
Init();
TH0 = high;
TL0 = low;
while (1)
{
if (music[ptr] != 0xFF && music[ptr] != 0x00)//判断是否是正常音符
{
TR0 = 0;
P2_3 = 1;
单片机控制蜂鸣器播放音乐
制作人:王宇浩
• 首先要了解,蜂鸣器播放音乐的原理。
• 演奏一个音符,是通过引脚,周期性的输 出一个特定频率的方波。这就需要单片机, • 在半个周期内输出低电平、另外半个周期 输出高电平,周而复始。 • 每一个不同的频率对应不同的声音。 • 如图
• 所以要让单片机在半个周期内输出低电平、另外半个周期输 出高电平,周而复始。 • 可以用中断或者是直接延时的方法,为了简洁明了我这里用 了自己写的delay函数控制蜂鸣器的频率
• 然后是蜂鸣器的程序
• 最后在主函数里全部写相应的程序
ቤተ መጻሕፍቲ ባይዱ
谢谢观看
单片机课程设计报告利用蜂鸣器播放音乐
课程设计:嵌入式系统应用题目名称:利用蜂鸣器实现音乐播放功能姓名:学号:班级:完成时间:1设计的任务设计内容:动手焊接一个51单片机设计目标:利用单片机上的蜂鸣器实现音乐播放功能2设计的过程基本结构在本次的试验中采用了STC89C52RC单片机,STC89C52RC单片机是宏晶科技推出的新一代高速/低功耗/超强抗干扰的单片机,指令代码完全兼容传统8051单片机,12时钟/机器周期,工作电压:~5V单片机/~3V单片机,工作频率范围:0~40MHz,相当于普通8051的0~80MHz,实际工作频率可达48MHz,用户应用程序空间为8K字节;STC89C52RC引脚图STC89C52RC单片机的工作模式:(1)典型功耗<μA,可由外部中断唤醒,中断返回后,继续执行原程序(2)空闲模式:典型功耗2mA(3)正常工作模式:典型功耗4Ma~7mA4唤醒,适用于水表、气表等电池供电系统及便携设备2.蜂鸣器及其工作原理:蜂鸣器按其结构分主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型;电磁式蜂鸣器由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成;接通电源后,振荡器产生的音频信号电流通过电磁线圈,使电磁线圈产生磁场,振动膜片在电磁线圈和磁铁的相互作用下,周期性地振动发声;本实验采用的是电磁式蜂鸣器;蜂鸣器按其是否带有信号源又分为有源和无源两种类型;有源蜂鸣器只需要在其供电端加上额定直流电压,其内部的震荡器就可以产生固定频率的信号,驱动蜂鸣器发出声音;无源蜂鸣器可以理解成与喇叭一样,需要在其供电端上加上高低不断变化的电信号才可以驱动发出声音;本实验采用的是有源蜂鸣器;蜂鸣器与单片机连接电路图软件设计过程1.蜂鸣器发声原理本实验由于采用有源蜂鸣器,只需将引脚端口P3^4清零,蜂鸣器即可发声;P3^4置位,蜂鸣器停止发声;采用置1置0的方法只能使蜂鸣器发声或停止发声,想要使蜂鸣器发出声音,必须对蜂鸣器发出声音的音频和节拍进行控制;音乐基础音调:不同音高的乐音是用C、D、E、F、G、A、B来表示,这7个字母就是音乐的音名,它们一般依次唱成DO、RE、MI、FA、SO、LA、SI,即唱成简谱的1、2、3、4、5、6、7,相当于汉字“多来米发梭拉西”的读音,这是唱曲时乐音的发音,所以叫“音调”,即Tone;把C、D、E、F、G、A、B这一组音的距离分成12个等份,每一个等份叫一个“半音”;两个音之间的距离有两个“半音”,就叫“全音”;在钢琴等键盘乐器上,C–D、D–E、F–G、G–A、A–B两音之间隔着一个黑键,他们之间的距离就是全音;E–F、B–C两音之间没有黑键相隔,它们之间的距离就是半音;通常唱成1、2、3、4、5、6、7的音叫自然音,那些在它们的左上角加上﹟号或者b号的叫变化音;﹟叫升记号,表示把音在原来的基础上升高半音,b叫降记音,表示在原来的基础上降低半音;例如高音DO的频率1046Hz刚好是中音DO的频率523Hz的一倍,中音DO的频率523Hz刚好是低音DO频率266Hz的一倍;同样的,高音RE 的频率1175Hz刚好是中音RE的频率587Hz的一倍,中音RE的频率587Hz 刚好是低音RE频率294Hz的一倍;节拍:节拍是让音乐具有旋律固定的律动,而且可以调节各个音的快满度;“节拍”,即Beat,简单说就是打拍子,就像我们听音乐不自主的随之拍手或跺脚;若1拍实,则1/4拍为;至于1拍多少s,并没有严格规定,就像人的心跳一样,大部分人的心跳是每分钟72下,有些人快一点,有些人慢一点,只要听的悦耳就好;音持续时间的长短即时值,一般用拍数表示;休止符表示暂停发音;1)控制发声频率要产生音频脉冲,只要算出某一音频的周期1/频率,然后将此周期除以2,即为半周期的时间;利用定时器计时这半个周期时间,每当计时到后就将输出脉冲的I/O反相,然后重复计时此半周期时间再对I/O反相,就可在I/O 脚上得到此频率的脉冲;利用STC89C52RC的内部定时器使其工作在计数器模式MODE1下,改变计数值TH0及TL0从而产生不同频率;此外结束符和休止符可以分别用代码00H和FFH来表示,若查表结果为0x00,则表示曲子终了;若查表结果为0xff,则产生相应的停顿效果;以标准音高A为例,A的频率是440Hz,周期T=1/440=2272us;在占空比为50%的情况下,导通时间=断开时间=半周期t=2272us/2=1136us,利用P3^4端口的位操作,经过不断地反相变换即可得到标准音高A的音频脉冲;端口导通时间与断开时的时间利用定时器实现;具体的方法是将单片机定时器的中断触发时间设为半周期t,这样每隔半周期端口反相,输出连续的对应音高的频率;设晶振的频率为f0,中断触发时间半周期为t,定时器工作在模式1时计数器的初值为THL,高8位为THL,低8位为TL;时钟周期即为1/f0,定时器每一次累加用去一个机器周期,一个机器周期包含12个时钟周期,即定时器每次加一所用时间是12/f0;定时器在模式1下计时采用16位数,最大计数为2^16-165535,再次加一65536溢出触发中断;根据以上分析可得如下关系:音频对应定时器初值的高8位TH=THL/2^8=65536-tf/12/256;音频对应定时器初值的低8位TL=THL%2^8=65536-tf/12%256;附:八度12音阶定时器初值表只含自然音低音音名 频率Hz 晶振12MHz 晶振 中音音名 频率Hz 晶振12MHz 晶振 高音音名 频率Hz 晶振12MHz 晶振 Do 262 0xF885 0xF91B Do 523 0xFC43 0xFC8E Do 1046 0xFE21 0xFE47 Re 294 0xF95A 0xF9DF Re 587 0xFCAD 0xFCED Re 1175 0xFE56 0xFE76 Mi 330 0xFA13 0xFA8A Mi 659 0xFD0A 0xFD43 Mi 1318 0xFE85 0xFEA1 Fa 349 0xFA68 0xFAD8 Fa 698 0xFD34 0xFD6A Fa 1397 0xFE9A 0xFEC7 So 392 0xFB04 0xFB68 So 784 0xFD82 0xFDB3 So 1568 0xFEC1 0xFED9 La 440 0xFB90 0xFBE9 La 880 0xFDC8 0xFDF3 La 1760 0xFEE4 0xFEF9 Si4940xFC0C0xFC5BSi9880xFE060xFE2DSi19760xFF030xFF162控制发声节拍每个音符的节拍可通过延时一定的时间来实现,在具体实现时需要有一个基本的带参延时程序,用于主函数根据不同的音符调用不同的时延;若以十六分之一音符的时长为基本延时时间,则十六分音符只需调用一次延时程序,八分音符则需调用两次延时程序,以此类推;3) 简谱及其编码用数字1至7表示;这7个数字就等于的;如果是C 大调,加上音名,就会是这样:八度如果是高一个八度,就会在数字上方加上一点;如果是低一个八度,就会数字下方加上一点;在中间的那一个就什么也不用加;如果要再高一个八度,就在上方垂直加上两点如:;要再低一个八度,就在下方垂直加上两点如:,如此类推;见下表所示: 大调 ... ... 小调......音长通常只有数字的是;数字下加一条横线,就可令四分音符的长度减半,即成为;两条横线可令八分音符的长度减半,即成为,余此类推;简单来説,下音阶CDEFGAB唱名 do re mi fa so la si数字 1 2 3 4 5 6 7加横綫数目与五綫谱的符尾数目相对应;数字后方的横线延长音符,每加一条横线延长一个的长度;正如五綫谱的附点一样,数字后方加一点会将音符长度增加一半;休止符休止符用“0”来表示;比四分休止符长,就只需在每一个四分休止符再多加一个“0”,不需在后方加上横线;若整个小节均是休止状态的话,就在每个四分音符加一个“0”,就像是3拍4就写成“|000|”,而4拍4就写成“|0000|”;无音高音符就像敲击乐的乐器那样是没有音高的,这时,简谱就需要用“X”或“x”表达;例如拍手时,简谱上就需如此表达:小节线正如五綫谱一样,小节与小节之间以纵綫称分隔,乐曲终止以一粗一细的直綫表示但是一般不分粗细;重复乐段以纵綫后两点表示开始,纵綫前两点表示终止;拍号的表达方式为:“2/4”、“3/4”、“4/4”、“6/8”等等;拍号一般都是置于的后方;变调与调号表示有两种方法:可以直接写“C调”,“C小调”或“Key:C”,“Key:Cm”;亦可写清楚主音代表哪一个音,例如1=C,就是C大调大调以1为主音;6=C就是C小调小调以6为主音;2=D就是D多利安调以2为主音;五线谱有,简谱同样有升降号;在简谱中,若需表达升降号,就把符号加在数字1至7的前方,让该音升高或降低;若要把升降号加在调号跟CDEFGAB七个字母,就须加在它们的后方;在五綫谱中,C小调要把B-E-A三个音降低半音;所以表达它的和声小调时,就会把一个放在B前面;简谱就一定得写 5,因为5不能下降;延音线、圆滑线与其他音乐符号简谱Tie跟slur都是跟五线谱相同,一定得置在数字上方;表达staccato时需要特别小心,因为很容易会跟低八度的那一点混淆,因此在表达跳音时把那一点写得粗点及低点;简谱编码将简谱中的每个音符进行编码,每个音符用一个unsignedchar字符类型表示,简谱可用一个unsignedchar字符数组表示;字符的前四位表示音频,可以表示0-f共十六个音符;本实验中采用了中音区和高音区;中音do-si分别编码为1~7,高音do-si分别编码为8~E,停顿编为0;字符的后四位表示节拍,节拍以十六分音符为单位在本程序中为165ms,一拍即四分音符等于4个十六分音符,编为4,其它的播放时间以此类推;以0xff作为曲谱的结束标志;程序从数组中取出一个数,然后分离出高4位得到音调,将值赋给定时器0,得到音调;接着分离出该数的低4位,得到节拍;本实验中播放音乐使用简谱如下:将其编码成:ucharcodezbjbxf={0x64,0xA3,0xC1,0xA2,0x62,0x84,0x61,0x81,0x61,0xA4,0xA1,0x91,0xA1,0x81,0 x64,0xA3,0xC1,0xD2,0xD2,0xD2,0xA2,0xC4,0xA1,0xC1,0xA1,0xC1,0xD2,0xD2,0x D2,0xA2,0xC4,0xC2,0x62,0xC2,0x62,0xA2,0xA2,0x84,0x94,0x94,0x92,0x81,0x9 1,0xA2,0xC2,0xD4,0xE4,0xA4,0xE4,0xA2,0xE2,0xA2,0xE2,0xA2,0xA2,0x84,0x94 ,0x94,0x92,0x81,0x91,0xA2,0xC2,0xD8};程序流图及说明主程序流程图程序代码:include<> defineucharunsignedchardefineuintunsignedintsbitfm=P3^4;算机与单片机的连接不上问题计算机与单片机的连接需要在电脑端安装相应的USB转串的驱动,由于我组没有使用实验室windowsxp系统,而使用的是个人计算机windows7系统,所以需要针对win7的驱动;后来联网搜索下载相应的驱动之后解决了这一问题;2.如何使蜂鸣器发出声音而非Be-Be声为了解决这个问题,我们深入研究了蜂鸣器发声原理与乐谱相关的知识;通过学习相关知识发现,不同的音符对应不同的音高,音高取决于发声频率;所以可以通过构造不同频率的方波来使蜂鸣器发出不同频率的声音,具体实现时利用定时器计时,定时器每一次累加消耗一个机器周期,即12个时钟周期;本实验采用晶振频率为12MHz,即定时器每一次累加消耗1us,通过设置定时器初值设定蜂鸣器取法的时间间隔,从而产生相应的音频;每个音符不但有频率属性,还有节拍属性;对不同节拍的控制可以采用延迟一定的时间来得到; 5总结在本次课程设计中,我们进一步加深了对小型嵌入式系统的认识;实验初期单片机需要自己焊接,通过亲手焊接电路板,了解了如何将实验原理图转化为具体的硬件实物连接;通过使用TN单片机开发板和STC89C52RC系统,对单片机应用开发有了初步的熟悉;在实验的过程中,使用到了C51的编译环境KeilC和单片机通讯程序STC_ISP,两者是单片机开发所不可缺少的工具;在具体实施的过程中,通过一一解决遇到的问题,增强了动手实践能力;。
单片机驱动蜂鸣器音乐生日快乐的播放程序
单片机驱动蜂鸣器音乐生日快乐的播放程序;音乐生日快乐的播放;P1.6是喇叭输出端口,用杜邦线连接到J42插针 B1针脚,喇叭即播放音乐numtim equ 20hgewei equ 21hshiwei equ 22hscanled equ 23horg 000hjmp startorg 00bhjmp tim0org 100hstart:; JB P3.7,$ ;检测播放按键是否按下mov tmod,#00000001bmov ie,#10000010bmov numtim,#01h start0:mov 30h,#00hnext:mov a,30hmov dptr,#tablemovc a,@a+dptrmov r2,ajz end0anl a,#0fhmov r5,amov a,r2swap aanl a,#0fhjnz singclr tr0jmp d1sing: dec amov 22h,arl amov dptr,#table1movc a,@a+dptrmov th0,amov 21h,amov a,22hrl ainc amovc a,@a+dptrmov tl0,amov 20h,asupplies and get to use, the registration of company assets, record maintenance, maintenance of effective management; 2.6 cost management: preparation of the annual administrative budget, control administrative costs, determine the scope of cost-sharing, sharing the office expenses by month; 2.7 information management: responsible for documents,materials, information and publicity, communication links, and ensure promptly, ranking officers reporting; responsible for the collation and analysis of information, provide the basis for corporate decisions; corporate culture construction, planning, editing, internal publications;2.8 vehicle management: responsible for the management, repairs and maintenance of vehicles; 2.9 legal disputes: the company responsible for the handling of disputes; 2.10 other: maintaining normal working order, and security. General Manager General Manager job description 1, 1.1 develop strategies and plans: according to company strategic planning, establish the annual management plan, reported to the Board of Directors for approval; 1.2 decomposition measures: according to the Board of Directors issued the annual management plan, annual index of decomposition, through the Organization of coordination mandate incharge of the Department Organization and implementation, ensuring comprehensive implementation of annual indicators; 1.3 inspection: convenes the General Manager Office, throughsetb tr0d1:lcall delayinc 30hjmp nextend0:clr tr0jmp start0tim0:push accpush pswmov tl0,20hmov th0,21hcpl p1.6 ;由该口输出音频数据pop pswpop accretidelay: mov r7,#02hd2: mov r4,#187d3: mov r3,#248djnz r3,$djnz r4,d3djnz r7,d2djnz r5,delayrettable1: ;音乐码表dw 64260,64400,64524,64580 dw 64684,64777,64820,64898 dw 64968,65030,65058,65110 dw 65157,65178,65217table:db 82h,01h,81h,94h,84h db 0b4h,0a4h,04hdb 82h,01h,81h,94h,84hdb 0c4h,0b4h,04hdb 82h,01h,81h,0f4h,0d4hdb 0b4h,0a4h,94hdb 0e2h,01h,0e1h,0d4h,0b4hdb 0c4h,0b4h,04hdb 82h,01h,81h,94h,84hdb 0b4h,0a4h,04hdb 82h,01h,81h,94h,84hdb 0c4h,0b4h,04h supplies and get to use, the registration of company assets, record maintenance, maintenance of effective management;2.6 cost management: preparation of the annual administrative budget, control administrative costs, determine the scope of cost-sharing, sharing the office expenses by month; 2.7 information management: responsible for documents, materials, information and publicity, communication links, and ensure promptly, ranking officers reporting; responsible for the collation and analysis of information, provide the basis for corporate decisions; corporate culture construction, planning, editing, internal publications; 2.8 vehicle management: responsible for the management, repairs and maintenance of vehicles; 2.9 legal disputes: the company responsible for the handling of disputes; 2.10 other: maintaining normal working order, and security. General Manager General Manager job description 1, 1.1 develop strategies and plans: according to company strategic planning, establish the annual management plan, reported to the Board of Directors for approval; 1.2 decomposition measures: according to the Board of Directors issued the annual management plan, annual index of decomposition, through the Organizationof coordination mandate in charge of the Department Organization and implementation, ensuring comprehensive implementation of annual indicators; 1.3 inspection: convenes the General Manager Office, through db 82h,01h,81h,0f4h,0d4hdb 0b4h,0a4h,94hdb 0e2h,01h,0e1h,0d4h,0b4hdb 0c4h,0b4h,04hdb 00TABLE2:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90Hend另外的一个C文件。
单片机蜂鸣器音乐
单片机蜂鸣器音乐单片机在我们的生活中无处不在,它被广泛地应用在各种电子产品中,为我们的生活带来了便利。
今天,我要向大家介绍的是一种基于单片机的蜂鸣器音乐播放器。
一、硬件部分1、单片机:我们选用的是AT89C51单片机,它具有低功耗、高性能的特点,非常适合用于音乐播放器。
2、蜂鸣器:蜂鸣器是用来发出声音的,我们将其连接在单片机的输出口上。
3、存储芯片:为了能够播放存储在芯片中的音乐,我们需要将音乐以某种格式存储在芯片中。
常用的存储芯片有EEPROM和Flash芯片。
4、按键:为了能够选择播放不同的音乐,我们需要添加一个按键。
二、软件部分1、音乐编码:我们需要将音乐转换成二进制编码,这样才能被单片机读取并播放。
常用的音乐编码格式有MIDI、WAV等。
2、音乐播放:当按下按键时,单片机读取存储芯片中的音乐数据,并通过蜂鸣器播放。
3、音乐选择:通过按键可以选择不同的音乐进行播放。
4、音量控制:我们可以通过编程来控制蜂鸣器的音量大小。
三、调试与测试1、硬件调试:检查连接是否正确,确保没有短路或断路的情况。
2、软件调试:将程序下载到单片机中进行调试,确保能够正常播放音乐。
3、综合测试:将所有硬件和软件都连接起来进行测试,确保能够正常工作。
四、总结与展望通过本次实验,我们成功地制作了一个基于单片机的蜂鸣器音乐播放器。
它具有简单、实用的特点,可以用来播放存储在芯片中的音乐。
未来,我们可以进一步扩展其功能,例如添加更多的按键来选择不同的音乐、添加显示屏来显示歌曲名称等。
我们也可以将其应用到其他领域,例如智能家居、智能安防等。
单片机蜂鸣器唱歌程序在许多应用中,单片机蜂鸣器经常被用来发出声音或音乐。
下面是一个使用单片机蜂鸣器唱歌的程序示例。
我们需要确定单片机和蜂鸣器的连接方式。
通常,单片机具有一个内置的蜂鸣器输出引脚,可以将蜂鸣器连接到这个引脚上。
在以下的示例中,我们将假设单片机具有一个内置蜂鸣器输出引脚,并将其连接到P1.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); //播放}}。
单片机课程设计报告利用蜂鸣器播放音乐样本
课程设计: 电子设计题目名称: 音乐流水灯姓名: 戴锦超学号: 08123447班级: 信科12-3班完成时间: 10月23日1设计的任务设计内容: 动手焊接一个51单片机设计目标: 利用单片机上的蜂鸣器以及二极管实现音乐播放以及根据音乐的节奏而规律性闪亮的二极管。
而且经过程序调节音乐节奏的快慢。
2 设计的过程2.1 基本结构1.STC89C52RC在本次的试验中采用了STC89C52RC单片机, STC89C52RC 单片机是宏晶科技推出的新一代高速/低功耗/超强抗干扰的单片机, 指令代码完全兼容传统8051单片机, 12时钟/机器周期, 工作电压: 5.5V~3.3V( 5V单片机) /3.8V~2.0V( 3V单片机) , 工作频率范围: 0~40MHz, 相当于普通8051的0~80MHz, 实际工作频率可达48MHz, 用户应用程序空间为8K字节。
( STC89C52RC引脚图)STC89C52RC单片机的工作模式:(1)典型功耗<0.1μA,可由外部中断唤醒, 中断返回后, 继续执行原程序(2)空闲模式: 典型功耗2mA(3)正常工作模式: 典型功耗4Ma~7mA( 4) 唤醒, 适用于水表、气表等电池供电系统及便携设备2.蜂鸣器及其工作原理:蜂鸣器按其结构分主要分为压电式蜂鸣器和电磁式蜂鸣器两种类型。
电磁式蜂鸣器由振荡器、电磁线圈、磁铁、振动膜片及外壳等组成。
接通电源后, 振荡器产生的音频信号电流经过电磁线圈, 使电磁线圈产生磁场, 振动膜片在电磁线圈和磁铁的相互作用下, 周期性地振动发声。
本实验采用的是电磁式蜂鸣器。
蜂鸣器按其是否带有信号源又分为有源和无源两种类型。
有源蜂鸣器只需要在其供电端加上额定直流电压, 其内部的震荡器就能够产生固定频率的信号, 驱动蜂鸣器发出声音。
无源蜂鸣器能够理解成与喇叭一样, 需要在其供电端上加上高低不断变化的电信号才能够驱动发出声音。
本实验采用的是有源蜂鸣器。
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安/*生日快乐歌曲*/#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);//播放}}。
单片机打铃的原理和作用
单片机打铃的原理和作用
单片机打铃的原理是通过控制单片机的IO口输出高低电平来控制蜂鸣器(或其他音响设备)发出声音。
具体实现方法:
1. 首先,将蜂鸣器的一个引脚连接至单片机的IO口,另一个引脚连接至单片机的GND或VCC。
2. 在程序中,通过控制IO口的输出电平,使其在高电平和低电平之间切换,从而控制蜂鸣器发声。
3. 根据需求,可以通过控制IO口输出的时间长度和频率来控制蜂鸣器发出不同的音调和节奏。
单片机打铃的作用:
1. 提示功能:在某些场景下,单片机打铃可以作为一种警示或提醒的方式,引起人们的注意。
2. 信号传达:通过不同的音调和节奏,可以传达不同的信号或指令,实现与人机交互的功能。
3. 音乐播放:通过控制蜂鸣器的发声,可以实现一些简单的音乐播放功能。
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安
/生日快乐歌曲/ include<reg51.h>defineuintunsignedintdefineucharunsignedcharsbitbeep=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};//延时voidDelayMSuintx{uchart;whilex--fort=0;t<120;t++;}voidPlayMusic{uinti=0;j;k;whileSONG_LONGi=0||SONG_TONEi=0{//播放各个音符;SONG_LONG为拍子长度forj=0;j<SONG_LONGi20;j++{//SONG_TONE延时表决定了每个音符的频率fork=0;k<SONG_TONEi/3;k++;}DelayMS10;i++;}}voidmain{beep=0;while1{PlayMusic;//播放生日快乐DelayMS500;//播放完后暂停一段时间}}两只蝴蝶:include<reg51.h>//这是单片机音乐代码生成器生成的代码defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharm;n;ucharcodeT492={{0;0};{0xF8;0x8B};{0xF8;0xF2};{0xF9;0x5B};{0xF9;0xB7};{0xFA;0x14};{0xFA;0x66};{0xFA;{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}};ucharcodemusic2={{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}};voiddelayucharpuchari;j;for;p>0;p--fori=181;i>0;i--forj=181;j>0;j--;}voidpause{uchari;j;fori=150;i>0;i--forj=150;j>0;j--;}voidT0_intinterrupt1 {beepIO=beepIO;TH0=Tm0;TL0=Tm1;}voidmain{uchari=0;TMOD=0x01;EA=1;ET0=1; while1{m=musici0;n=musici1;{TR0=0;delayn;i++;}elseifm==0xFF{TR0=0;delay30;i=0;}elseifm==musici+10{TR0=1;delayn;TR0=0;pause;i++;}else{TR0=1;delayn;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;0x2 0;0x30;0x20;0x39;0x10;0x30;0x10;0x30;0x80;0x26;0x20;0x20;0x20;0x20;0x20;0x1c;0x2 0;0x20;0x80;0x2b;0x20;0x26;0x20;0x20;0x20;0x2b;0x10;0x26;0x10;0x2b;0x80;0x26;0x2 0;0x30;0x20;0x30;0x20;0x39;0x10;0x26;0x10;0x26;0x60;0x40;0x10;0x39;0x10;0x26;0x2 0;0x30;0x20;0x30;0x20;0x39;0x10;0x26;0x10;0x26;0x80;0x26;0x20;0x2b;0x10;0x2b;0x10x2b;0x20;0x30;0x10;0x39;0x10;0x26;0x10;0x2b;0x10;0x2b;0x20;0x2b;0x40;0x40;0x2 0;0x20;0x10;0x20;0x10;0x2b;0x10;0x26;0x30;0x30;0x80;0x18;0x20;0x18;0x20;0x26;0x2 0;0x20;0x20;0x20;0x40;0x26;0x20;0x2b;0x20;0x30;0x20;0x30;0x20;0x1c;0x20;0x20;0x2 0;0x20;0x80;0x1c;0x20;0x1c;0x20;0x1c;0x20;0x30;0x20;0x30;0x60;0x39;0x10;0x30;0x1 0;0x20;0x20;0x2b;0x10;0x26;0x10;0x2b;0x10;0x26;0x10;0x26;0x10;0x2b;0x10;0x2b;0x8 0;0x18;0x20;0x18;0x20;0x26;0x20;0x20;0x20;0x20;0x60;0x26;0x10;0x2b;0x20;0x30;0x2 0;0x30;0x20;0x1c;0x20;0x20;0x20;0x20;0x80;0x26;0x20;0x30;0x10;0x30;0x10;0x30;0x2 0;0x39;0x20;0x26;0x10;0x2b;0x10;0x2b;0x20;0x2b;0x40;0x40;0x10;0x40;0x10;0x20;0x1 0;0x20;0x10;0x2b;0x10;0x26;0x30;0x30;0x80;0x00};voidTime0_Init{TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振;10msvoidTime0_Intinterrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}/-------------------------------------------------功能:1MS延时子程序-------------------------------------------------/ voidDelay_xMsunsignedintx{unsignedinti;j;fori=0;i<x;i++{forj=0;j<3;j++;}}voidPlay_Songunsignedchari{unsignedcharTemp1;Temp2;unsignedintAddr;Count=0;//中断计数器清0Addr=i217;Temp1=SONGAddr++;ifTemp1==0xFF//休止符{TR0=0;Delay_xMs100;}elseifTemp1==0x00//歌曲结束符{return;}else{Temp2=SONGAddr++;TR0=1;while1{_Speak=~_Speak;Delay_xMsTemp1;ifTemp2==Count{Count=0;break;}}}/-------------------------------------------------功能:主程序-------------------------------------------------/ voidmain{Time0_Init;//定时器0中断初始化while1{Play_Song0;//播放}}。
ATmega16控制蜂鸣器播放音乐
N6,32,/**/H3,24,N6,8,/**/H2,24,H3,4,H2,4,/**/H1,16,H1,4,N6,4,H1,4,H2,4,/**/
{
unsigned i;
for(i=1;i<1714;i++);
}
void delay_ms(unsigned int n)
{
unsigned int i;
for(i=0;i<n;i++)
{
delay_1ms();
}
}
音乐数据
这段是《孤星独吟》,自己对着简谱翻的
*************************************/
unsigned int song[]=
{
N6,12,N7,4,H1,8,H2,4,H3,4,/**/H2,32,/**/N7,12,N6,4,N5,8,N3,4,N5,4,/**/
#define H5 61709 //1568
#define H6 62127 //1760
#define H7 62500 //1976
#define HH1 62669 //2093
#define O 0 //休止符
/************************************
#include <iom16v.h>
#include <macros.h>
//计时值=65536-系统时钟/分频系数/音调频率/2
蜂鸣器播放音乐原理
关于“世上只有妈妈好”的单片机音乐演奏程序2009-11-22 21:45单片机演奏一个音符,是通过引脚,周期性的输出一个特定频率的方波。
这就需要单片机,在半个周期内输出低电平、另外半个周期输出高电平,周而复始。
半个周期的时间是多长呢?众所周知,周期为频率的倒数,可以通过音符的频率计算出半周期。
演奏时,要根据音符频率的不同,把对应的、半个周期的定时时间初始值,送入定时器,再由定时器按时输出高低电平。
下面是个网上广泛流传的单片机音乐演奏程序,它可以循环的播放“世上只有妈妈好”这首乐曲。
很多人都关心如何修改这个乐曲的内容,但是不知如何入手。
做而论道对这个程序,给出说明,希望对大家有所帮助,以后大家自己就能够编写进去新的乐曲。
在这个程序中,有两个数据表,其中存放了事先算好的、各种音符频率所对应的、半周期的定时时间初始值。
有了这些数据,单片机就可以演奏从低音、中音、高音和超高音,四个八度共28个音符。
演奏乐曲时,就根据音符的不同数值,从半周期数据表中找到定时时间初始值,送入定时器即可控制发音的音调。
比如把表中的0xF2和0x42送到定时器,定时器按照这个初始值来产生中断,输出的方波,人们听起来,这就是低音1。
乐曲的数据,也要写个数据表,程序中以 code unsigned char sszymmh[] 命名。
这个表中每三个数字,说明了一个音符,它们分别代表:第一个数字是音符的数值1234567之一,代表多来咪发...;第二个数字是0123之一,代表低音、中音、高音、超高音;第三个数字是时间长度,以半拍为单位。
乐曲数据表的结尾是三个0。
程序如下:#include <reg52.h>sbit speaker = P1^7;unsigned char timer0h, timer0l, time;//--------------------------------------//单片机晶振采用11.0592MHz// 频率-半周期数据表高八位本软件共保存了四个八度的28个频率数据code unsigned char FREQH[] = {0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音1234567 0xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC, 0xFC,//1,2,3,4,5,6,7,i 0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音 2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567// 频率-半周期数据表低八位code unsigned char FREQL[] = {0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音1234567 0x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i 0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音 2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音 1234567 //--------------------------------------//世上只有妈妈好数据表要想演奏不同的乐曲, 只需要修改这个数据表code unsigned char sszymmh[] = {6, 2, 3, 5, 2, 1, 3, 2, 2, 5, 2, 2, 1, 3, 2, 6, 2, 1, 5, 2, 1,//一个音符有三个数字。
单片机驱动蜂鸣器唱国歌的设计与原理
蜂鸣器演奏国歌(11机自创新1班王丽红1110100736)1设计任务本设计是以AT89C51芯片的电路为基础,外部加上三级管驱动蜂鸣器,以此来实现音乐演奏控制的硬件电路,通过软件程序来控制单片机内部的定时器使其演奏出优美动听的音乐。
按照要求选择音乐,在本次设计中以演奏《国歌》为例,将其转化为机器码存入单片机的存储器中。
2设计原理蜂鸣器发声原理是电流通过电磁线圈,使电磁线圈产生磁场来驱动振动膜发声的,因此驱动需要一定的电流,单片机引脚输出的电流较小,基本上驱动不了蜂鸣器,因此需要增加一个电流放大的电路。
三极管的发射极E接电源,集电极C接蜂鸣器正极,蜂鸣器负极接地,三极管的基级B经过限流电阻R后由单片机引脚控制。
当输出高电平时,三极管T1截止,没有电流流过线圈,蜂鸣器不发声;当输出低电平时,三极管导通,这样蜂鸣器的电流形成回路,发出声音。
程序中改变单片引脚输出波形的频率,就可以调整控制蜂鸣器音调,产生各种不同音色、音调的声音。
另外,改变输出电平的高低电平占空比,则可以控制蜂鸣器的声音大小,这些我们都可以通过编程实验来验证。
3 软件设计3.1音调、节拍以及编码的确定一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率,也就是说不能像电子琴那样能奏出多种音色的声音。
因此单片机奏乐只需弄清楚两个概念即可,也就是“音调”和节拍表示一个音符唱多长的时间。
3.1.1音调的确定不同音高的乐音是用C、D、E、F、G、A、B来表示,这7个字母就是音乐的音名,它们一般依次唱成DO、RE、MI、FA、SO、LA、SI,即唱成简谱的1、2、3、4、5、6、7,这是唱曲时乐音的发音,所以叫“音调”。
把C、D、E、F、G、A、B这一组音的距离分成12个等份,每一个等份叫一个“半音”。
两个音之间的距离有两个“半音”,就叫“全音”。
1) 要产生音频脉冲,只要算出某一音频的周期(1/频率),然后将此周期除以2,即为半周期的时间。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit sp eaker = P1^7;uint j;uchar m=1;uchar flag;uchar line;uchar code * data song;//中4 高3 中5休止符低6中7低7高1中1低3中2低5中3低4uchar code yin[30]={0xFF,0xFF,0xFB,0x90,0xFC,0x0C,0xFC,0x44,0xFC,0xAC,0xFD,0x09,0xFD,0x34,0xFD,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,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};//**********************************************************************// //**********************************************************************////延时 d 微秒,最大延时 65536 微妙/*延时 1/4 拍时间即 187ms*/ void delay187ms(void){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};//<< 喜欢// 延时程序 // void delay(uint d){ while(d--); }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; case12 :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;}。