您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 金融资料 > 单片机-利用蜂鸣器演奏音乐
实验三-利用蜂鸣器演奏音乐一、实验目的1.了解BlueSkyC51单片机实验板中蜂鸣器的硬件电路2.学会利用蜂鸣器实现音乐的演奏3.掌握蜂鸣器实现音乐演奏的编程二、实验硬件设计及电路1.BlueSkyC51单片机实验板2.单片机最小系统3.蜂鸣器电路连接三极管主要是做驱动用的。因为单片机的IO口驱动能力不够让蜂鸣器发出声音,所以我们通过三极管放大驱动电流,从而可以让蜂鸣器发出声音,你要是输出高电平,三极管导通,集电极电流通过蜂鸣器让蜂鸣器发出声音,当输出低电平时,三极管截止,没有电流流过蜂鸣器,所以就不会发出声音。三、实验原理1.音调及节拍用一个口,输出方波,这个方波输入进蜂鸣器就会产生声音,通过控制方波的频率、时间,就能产生简单的音乐。一般说来,单片机演奏音乐基本都是单音频率,因此单片机奏乐只需控制音调和节拍。(1)音调的确定音调是由频率来确定的。通过单片机的定时器定时中断,将单片机上对应蜂鸣器的I/O口来回取反,从而让蜂鸣器发出不同频率的声音。只需将定时器给以不同的定时值就可实现。通过延时,即可发出所需要的频率。(2)节拍的确定一拍的时长大约为400—500ms,每个音符的时长通过节拍来计算。详细见程序代码。2.软件设计相关(1)头文件#includereg51.h#defineuintunsignedint#defineucharunsignedchar#defineulongunsignedlongsbitbeep=P1^4;//蜂鸣器与P1.4口连接(2)歌曲曲谱ucharcodediaodata[30]={//音调代码0x10,0x0e,0x0c,0x10,0x0e,0x0c,0x10,0x0e,0x10,0x0c,0x10,0x15,0x13,0x10,0x13,0x15,0x15,0x13,0x15,0x13,0x10,0x0e,0x0c,0x0e,0x0e,0x10,0x0e,0x0c,0x0e,0x00};ucharcodejiedata[30]={//音长代码0x04,0x04,0x08,0x06,0x02,0x08,0x04,0x04,0x04,0x04,0x02,0x06,0x08,0x02,0x02,0x04,0x04,0x04,0x04,0x08,0x02,0x02,0x04,0x04,0x02,0x02,0x04,0x04,0x0c,0x00};(3)音阶表ucharcodefreq[36*2]={//音阶码表0xf7,0xd8,//440hz,1//00xf8,0x50,//466hz,1#//10xf8,0xbc,//494hz,2//20xf9,0x26,//524hz,2#//30xf9,0x85,//554hz,3//40xf9,0xe5,//588hz,4//50xfa,0x3d,//622hz,4#//60xfa,0x92,//660hz,5//70xfa,0xdd,//698hz,5#//80xfb,0x29,//740hz,6//90xfb,0x70,//784hz,6#//100xfb,0xb0,//830hz,7//110xfb,0xef,//880hz,1//120xfc,0x29,//932hz,1#//130xfc,0x62,//988hz,2//140xfc,0x95,//1046hz,2#//150xfc,0xc7,//1108hz,3//160xfc,0xf5,//1174hz,4//170xfd,0x20,//1244hz,4#//180xfd,0x4c,//1318hz,5//190xfd,0x72,//1396hz,5#//200xfd,0x97,//1480hz,6//210xfd,0xbb,//1568hz,6#//220xfd,0xdc,//1662hz,7//230xfd,0xfb,//1769hz,`1//240xfe,0x18,//1864hz,`1#//250xfe,0x34,//1976hz,`2//260xfe,0x4e,//2092hz,`2#//270xfe,0x67,//2218hz,`3//280xfe,0x7d,//2350hz,`4//290xfe,0x94,//2488hz,`4#//300xfe,0xa8,//2639hz,`5//310xfe,0xbc,//2794hz,`5#//320xfe,0xcf,//2960hz,`6//330xfe,0xe0,//3136hz,`6#//340xfe,0xf1,//3322hz,`7//35};(4)音符发生程序(中断服务程序)voidtimer0()interrupt1//用于产生音符的T0中断服务程序{TH0=th0_f;TL0=tl0_f;beep=~beep;//取反beep引脚,发声}三、实验过程及问题1.Keil开发过程(1)新建“Project”,选择单片机为“AT89C52”(2)新建c文件“music.c”,并添加到工程中(3)设置生成“HEX”文件,编译(4)将生成的“HEX”文件烧录到单片机,检验(5)不断修改完善2.编译实验相关问题(1)实际发音颤音重解决方法为修改蜂鸣器的驱动频率.(2)实际节奏过快或者过慢调整延时四、C51程序代码(部分来源于网络)#includereg51.h#defineuintunsignedint#defineucharunsignedchar#defineulongunsignedlongsbitbeep=P1^4;//蜂鸣器与P1.4口连接ucharth0_f;//中断装载T0高8位uchartl0_f;//T0低8位ucharcodefreq[36*2]={//音阶码表0xf7,0xd8,//440hz,1//00xf8,0x50,//466hz,1#//10xf8,0xbc,//494hz,2//20xf9,0x26,//524hz,2#//30xf9,0x85,//554hz,3//40xf9,0xe5,//588hz,4//50xfa,0x3d,//622hz,4#//60xfa,0x92,//660hz,5//70xfa,0xdd,//698hz,5#//80xfb,0x29,//740hz,6//90xfb,0x70,//784hz,6#//100xfb,0xb0,//830hz,7//110xfb,0xef,//880hz,1//120xfc,0x29,//932hz,1#//130xfc,0x62,//988hz,2//140xfc,0x95,//1046hz,2#//150xfc,0xc7,//1108hz,3//160xfc,0xf5,//1174hz,4//170xfd,0x20,//1244hz,4#//180xfd,0x4c,//1318hz,5//190xfd,0x72,//1396hz,5#//200xfd,0x97,//1480hz,6//210xfd,0xbb,//1568hz,6#//220xfd,0xdc,//1662hz,7//230xfd,0xfb,//1769hz,`1//240xfe,0x18,//1864hz,`1#//250xfe,0x34,//1976hz,`2//260xfe,0x4e,//2092hz,`2#//270xfe,0x67,//2218hz,`3//280xfe,0x7d,//2350hz,`4//290xfe,0x94,//2488hz,`4#//300xfe,0xa8,//2639hz,`5//310xfe,0xbc,//2794hz,`5#//320xfe,0xcf,//2960hz,`6//330xfe,0xe0,//3136hz,`6#//340xfe,0xf1,//3322hz,`7//35};ucharcodediaodata[30]={//音调代码0x10,0x0e,0x0c,0x10,0x0e,0x0c,0x10,0x0e,0x10,0x0c,0x10,0x15,0x13,0x10,0x13,0x15,0x15,0x13,0x15,0x13,0x10,0x0e,0x0c,0x0e,0x0e,0x10,0x0e,0x0c,0x0e,0x00};ucharcodejiedata[30]={//音长代码0x04,0x04,0x08,0x06,0x02,0x08,0x04,0x04,0x04,0x04,0x02,0x06,0x08,0x02,0x02,0x04,0x04,0x04,0x04,0x08,0x02,0x02,0x04,0x04,0x02,0x02,0x04,0x04,0x0c,0x00};voidtimer0()interrupt1//用于产生音符的T0中断服务程序{TH0=th0_f;TL0=tl0_f;beep=~beep;//取反beep引脚,发声}voidmain(void){uchari,j,k=0;uintn;TMOD=0X01;//T0方式1TR0=0;//关闭T0(不发声)ET0=1;//允许T0中断EA=1;//允许总中断while(1){TR0=1;//开T0for(i=0;diaodata[i]!=0;i++){th0_f=freq[diaodata[i]*2];tl0_f=freq[diaodata[i]*2+1];for(j=0;j20000;n++);//(jiedata*20000)TR0=0;for(n=0;n256;n++);//音符之间的短暂延时TR0=1;}TR0=0;}}
本文标题:单片机-利用蜂鸣器演奏音乐
链接地址:https://www.777doc.com/doc-5274541 .html