您好,欢迎访问三七文档
当前位置:首页 > 金融/证券 > 金融资料 > 用51单片机播放音乐(附C程序)
1用51单片机IO口播放几首音乐(附C程序)以下为51单片机播放音乐简谱的函数库:/*说明************************************************************************曲谱存贮格式unsignedcharcodeMusicName{音高,音长,音高,音长....,0,0};末尾:0,0表示结束(Important)音高由三位数字组成:个位是表示1~7这七个音符;十位是表示音符所在的音区:1-低音,2-中音,3-高音;百位表示这个音符是否要升半音:0-不升,1-升半音。音长最多由三位数字组成:个位表示音符的时值,其对应关系是:|数值(n):|0|1|2|3|4|5|6|几分音符:|1|2|4|8|16|32|64音符=2^n;十位表示音符的演奏效果(0-2):0-普通,1-连音,2-顿音;百位是符点位:0-无符点,1-有符点。调用演奏子程序的格式:Play(乐曲名,调号,升降八度,演奏速度);|乐曲名:要播放的乐曲指针,结尾以(0,0)结束;|调号(0-11):是指乐曲升多少个半音演奏;|升降八度(1-3):1:降八度,2:不升不降,3:升八度;|演奏速度(1-12000):值越大速度越快;***************************************************************************/#ifndef__SOUNDPLAY_H_REVISION_FIRST__#define__SOUNDPLAY_H_REVISION_FIRST__#include//**************************************************************************#defineSYSTEM_OSC11059200//12000000//定义晶振频率12000000HZ#defineSOUND_SPACE4/5//定义普通音符演奏的长度分率,//每4分音符间隔sbitBeepIO=P2^6;//定义输出管脚unsignedintcodeFreTab[12]={262,277,294,311,330,349,369,392,415,440,466,494};//原始频率表unsignedcharcodeSignTab[7]={0,2,4,5,7,9,11};//1~7在频率表中的位置unsignedcharcodeLengthTab[7]={1,2,4,8,16,32,64};unsignedcharSound_Temp_TH0,Sound_Temp_TL0;//音符定时器初值暂存unsignedcharSound_Temp_TH1,Sound_Temp_TL1;//音长定时器初值暂存//**************************************************************************voidInitialSound(void){BeepIO=1;Sound_Temp_TH1=(65535-(1/1200)*SYSTEM_OSC)/256;//计算TL1应装入的初值(10ms的初装值)2Sound_Temp_TL1=(65535-(1/1200)*SYSTEM_OSC)%256;//计算TH1应装入的初值TH1=Sound_Temp_TH1;TL1=Sound_Temp_TL1;TMOD|=0x11;ET0=1;ET1=0;TR0=0;TR1=0;EA=1;}voidBeepTimer0(void)interrupt1//音符发生中断{BeepIO=!BeepIO;TH0=Sound_Temp_TH0;TL0=Sound_Temp_TL0;}//**************************************************************************voidPlay(unsignedchar*Sound,unsignedcharSignature,unsignedOctachord,unsignedintSpeed){unsignedintNewFreTab[12];//新的频率表unsignedchari,j;unsignedintPoint,LDiv,LDiv0,LDiv1,LDiv2,LDiv4,CurrentFre,Temp_T,SoundLength;unsignedcharTone,Length,SL,SH,SM,SLen,XG,FD;for(i=0;i12;i++)//根据调号及升降八度来生成新的频率表{j=i+Signature;if(j11){j=j-12;NewFreTab[i]=FreTab[j]*2;}elseNewFreTab[i]=FreTab[j];if(Octachord==1)NewFreTab[i]=2;elseif(Octachord==3)NewFreTab[i]=2;}SoundLength=0;while(Sound[SoundLength]!=0x00)//计算歌曲长度{SoundLength+=2;3}Point=0;Tone=Sound[Point];Length=Sound[Point+1];//读出第一个音符和它时时值LDiv0=12000/Speed;//算出1分音符的长度(几个10ms)LDiv4=LDiv0/4;//算出4分音符的长度LDiv4=LDiv4-LDiv4*SOUND_SPACE;//普通音最长间隔标准TR0=0;TR1=1;while(PointSoundLength){SL=Tone%10;//计算出音符SM=Tone/10%10;//计算出高低音SH=Tone/100;//计算出是否升半CurrentFre=NewFreTab[SignTab[SL-1]+SH];//查出对应音符的频率if(SL!=0){if(SM==1)CurrentFre=2;//低音if(SM==3)CurrentFre=2;//高音Temp_T=65536-(50000/CurrentFre)*10/(12000000/SYSTEM_OSC);//计算计数器初值Sound_Temp_TH0=Temp_T/256;Sound_Temp_TL0=Temp_T%256;TH0=Sound_Temp_TH0;TL0=Sound_Temp_TL0+12;//加12是对中断延时的补偿}SLen=LengthTab[Length%10];//算出是几分音符XG=Length/10%10;//算出音符类型(0普通1连音2顿音)FD=Length/100;LDiv=LDiv0/SLen;//算出连音音符演奏的长度(多少个10ms)if(FD==1)LDiv=LDiv+LDiv/2;if(XG!=1)if(XG==0)//算出普通音符的演奏长度if(SLen=4)LDiv1=LDiv-LDiv4;elseLDiv1=LDiv*SOUND_SPACE;elseLDiv1=LDiv/2;//算出顿音的演奏长度elseLDiv1=LDiv;if(SL==0)LDiv1=0;4LDiv2=LDiv-LDiv1;//算出不发音的长度if(SL!=0){TR0=1;for(i=LDiv1;i0;i--)//发规定长度的音{while(TF1==0);TH1=Sound_Temp_TH1;TL1=Sound_Temp_TL1;TF1=0;}}if(LDiv2!=0){TR0=0;BeepIO=1;for(i=LDiv2;i0;i--)//音符间的间隔{while(TF1==0);TH1=Sound_Temp_TH1;TL1=Sound_Temp_TL1;TF1=0;}}Point+=2;Tone=Sound[Point];Length=Sound[Point+1];}BeepIO=1;}//**************************************************************************#endif附录:以下为曲谱编码文件,自己可以根据规则,对照简谱编写曲谱编码表。//挥着翅膀的女孩unsignedcharcodeMusic_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,50x17,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,60x19,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,
本文标题:用51单片机播放音乐(附C程序)
链接地址:https://www.777doc.com/doc-1855344 .html