单片机音乐的编程
单片机_音乐播放的c代码
单片机音乐播放的c代码#include "reg52.h"unsigned char Count;sbit _Speak =P2^3 ; //讯响器控制脚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,//路边的野华不要采0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C ,0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20 ,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20, 0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30, 0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30, 0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26 ,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20, 0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F ,0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20, 0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B ,0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20, 0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,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++ );}}/*-------------------------------------------------功能:歌曲播放子程序i为播放哪一段曲目-------------------------------------------------*/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); //播放}}6回答者:小崔凡凡- 二级2009-10-16 21:47我来评论>>提问者对于答案的评价:谢谢!相关内容• 单片机播放音乐里的音乐代码是怎么得到的?有什么软件能将歌曲直接转换成16进制代码 4 2008-11-14• 给个单片机C语言编的音乐程序,能够播放音乐 6 2009-7-22• 求用c语言编写的播放背景音乐的代码 5 2009-6-12• 单片机音乐代码问题 2009-9-28• 单片机音乐代码 5 2009-5-18更多相关问题>>查看同主题问题:单片机音乐音乐播放播放代码等待您来回答∙nokia 3100c音乐播放器如何跟新∙为什么用酷我音乐盒听歌后那歌会在C:\ProgramData\mcache里面呢∙深圳龙华天龙大道珍藏版狂嗨现场dj明仔vs女mc小黎茂名细飞11分钟半时候的那音乐谁知道叫什么名字!∙夏普9020c合上盖子怎么听音乐?∙C:Documents and SettingsAdministrator「开始」菜单酷我音乐盒2010.lnk∙nokia 2600c手机可以用什么音乐播放器?∙『windows』在网页制作中,下列不是背景音乐格式的是那一个?A.tem B mp3 C wav D∙3110c播放音乐时会卡,之后会自动到下一首,怎么办?其他回答共3 条我的博客上有三首连放的,你只要加两首就行了。
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); //播放}}。
单片机音乐代码
2, 2, 1, 1, 2, 1, 6, 1, 1, 1, 2, 1, 5, 1, 6, 0, 0, 0};
//--------------------------------------
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, //高音 234567
//3, 2, 2代表:3, 中音, 2个半拍;
//5, 2, 2代表:5, 中音, 2个半拍;
//1, 3, 2代表:1, 高音, 2个半拍;
//
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,
0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音 234567
0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567
// 频率-半周期数据表 低八位
code unsigned char FREQL[] = {
unsigned long t2;
for(t1 = 0; t1 < t; t1++) //双重循环, 共延时t个半拍
用51单片机实现的音乐程序
uint code notefreq[]={ 523, 587, 659, 698, 784, 880, 988,
+ _9 ~! I$ V8 V% L
: h+ H. t6 Z* Q% I! q& D/ `1 W{! c# l7 x0 c5 W' h1 C3 x, G2 W9 M
TH0=FreqSandH;
6 C/ U' p, \. } TL0=FreqSandL;
3 q) ?9 o) N( c! Q& { BEEP_PWR=!BEEP_PWR;2 Z8 V7 g8 G3 V
|! @1 S+ B' M' |) t
uchar i=0,ch,halfflg=0;: y e2 s$ _% N7 b3 C* L& j! h$ i
uchar lasttime;
7 q+ H' Q( F t2 w- f) _ uint freq;
; S5 g: L' f4 {& L% k if((ch==' ')||(ch=='|')||(ch=='\r')||(ch=='\n')) {i++; 7 l0 K9 b+ i3 l1 {6 R
continue;}# W6 W9 Y5 N3 Q
if(!ch) {SoundOff(); return;} //乐曲结束则播放完! a* ]8 R* p! T; x! {! A
#include <reg52.h>
单片机播放音乐的编程方法网络整理版
我将阐述播放音乐的软件编程方法。
原理:1’要让单片机控制扬声器发出音调只要计算出某一音频的频率,进而计算出周期,然后将这一周期除以二,即为半周期的时间。
利用定时器计数这半个周期。
每当计数到这个时间就将输出I/O口取反,重复这个步骤就可以得到这个音调的脉冲。
2’利用80C51扥内部定时器使用其工作在计数器模式MODE1下,改变计数值TH0,及TL0易产生不同频率的方法。
例如长生频率为532HZ的脉冲,其周期为T=1/523=1912us。
因此只要计数器计数956/1us=956。
在每次计数956次后将I/O口取反,就可得到中音DO(523HZ)。
计数脉冲值与频率的关系公式如下:N=Fi÷2÷Fr N:计算值Fi:内部计数时一次计数为1us,故其频率为1MHZ.3’其计数值的求法如下;T=65536-N=65536-Fi÷2÷Fr 其计数值的求法如下例如:设K=65536,F=1000000=Fi=1MHZ,求低音DO(261HZ),中音DO(523HZ),高音DO(1046HZ)的计算值。
T=65536-N=65536-Fi÷2÷Fr=65536-1000000÷2÷Fr=65536-500000/Fr。
低音DO的 T=65536-500000/262=63627中音DO的 T=65536-500000/523=64580高音DO的 T=65536-500000/1047=650594’ C调各音符频率与计数值T的对照表如下所示.在音乐中所谓音调其实就是我们所说的音高,在音乐中我们常把C 调上方的A作为标准音调,其频率等于440HZ。
当两个声音信号的频率相差一倍是也即f1=2f2,则称相差一个倍频程,在音乐中低音1和中音1,低和中2正好相差一个倍频程在音乐上称他们相差一个八度音,一个八音度有12个伴音,即为1-#1,#1-2,2-#2,#2-3,3-4,4-#4,#4-5,5-#5,#5-6,6-#6,#6-7,7-i.如果我们知道了这十二个基本音调的频率,我们就可以根据倍频程的关系得到其他调。
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); //播放}}。
用单片机制作简单音乐播放器代码
if(k==6) return 0xdf; if(k==7) return 0xbf; if(k>=8) return 0x7f; }
uint temp;
uchar jp;//jp 是简谱 //用于产生各种音调 j=0; while(!RI) {
temp=happynewyear[j]; jp=temp/16; //取数的高 4 位 P1=light(jp); if(jp!=0) {
timeh=cuzhi[jp*2]; timel=cuzhi[jp*2+1]; } else {
void main () {
TMOD=0x21; TH1=0xfd; TL1=0xfd; TR1=1; REN=1; SM0=0; SM1=1; while(1) {
if(RI==1) {
RI=0; a=SBUF; if (a==1) {
main1(); EA=0; TR0=0;
} if(a==2) {
ET0=1; song2();
} void song2() {
uint temp;
uchar jp;//jp 是简谱 //用于产生各种音调 j=0; while(!RI) {
temp=wnyhln[k]; jp=temp/16; //取数的高 4 位 P1=light(jp); //LED 灯伴随音调的变化而闪烁 if(jp!=0) {
} void song3() {
uint temp;
uchar jp;//jp 是简谱 //用于产生各种音调 j=0; while(!RI) {
temp=ysysljj[h]; jp=temp/16; //取数的高 4 位 P1=light(jp); if(jp!=0) {
51单片机音乐编辑程序详解
#include<reg51.h>#include<intrins.h>#define uc unsigned char#define ui unsigned intuc a;sbit speaker=P1^2;ui code yinpin[]={ //音频的赫兹0,0,0,0,0,0,0, //高两位单调个位表示节拍63628,63835,64021,64103,64260,64400,64524,//低音64680,64684,64777,64820,64898,64968,65030,//中音65085,65110,65157,65178,65217,65252,65282,//高音};uc code yuema[]={//音乐的编码001,001,001,001,134,134,154,132,152,168,184,001,001,001,001,164,164,182,162,162,182,199,197,154,164,182,162,164,132,124,134,122,132,154,164,184,192,162,169,167,001,001,001,204,202,192,198,204,192,202,184,164,204,202,192,194,198,122,132, 114,112,162,164,164,001,001,001,156,152,132,152,162,186,156,152,132,152,182,166,156,152,132,152,162,186,156,152,132,152,182,166,001,001,001,};void delay(ui m){ui i,j;for(i=0;i<121;i++)for(j=0;j<m;j++);}void init_t0(void){TMOD=0X01;//工作在定时器0的模式下ET0=1;//定时器中断允许EA=1;//开总中断TR0=1;//启动定时器}void sec_t0(void) interrupt 1//定时器服务子程序{TH0=yinpin[yuema[a]/10]/256;//复初值TL0=yinpin[yuema[a]/10]%256;//复初值speaker=!speaker;}void jiema(void)//音频的解码{if(yuema[a]/10)TR0=1;else TR0=0;//如果乐码的高两位为1则启动定时器,否则关闭定时器delay(yuema[a]%10*100);//节拍的延时,100表示把节拍的时间扩大100倍,不然时间会很快TR0=0;speaker=1;//关闭定时器和蜂鸣器为下次发生做准备a++;//乐码自加if(yuema[a]==0)a=0;//如果乐码遇到000的时候则表示音乐播放完毕。
单片机音乐播放___c语言程序
主程序:程序中附有详尽说明#include <REG52.H>#include "SoundPlay.h"void Delay1ms(unsigned int count){unsigned int 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,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,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,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,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 };//****************************************************************************** *****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);}}以下为h文件:/**************************************************************************SOUND PLAY FOR 51MCUCOPYRIGHT (c) 2005 BY JJJ.-- ALL RIGHTS RESERVED --File Name: SoundPlay.hAuthor: Jiang Jian JunCreated: 2005/5/16Modified: NORevision: 1.0******************************************************************************* //*说明表示结束(Important)音高由三位数字组成:个位是表示1~7 这七个音符十位是表示音符所在的音区:1-低音,2-中音,3-高音;百位表示这个音符是否要升半音: 0-不升,1-升半音。
单片机播放音乐汇编程序
单片机播放音乐汇编程序;标题’八月桂花香’发声程序ORG 0000HLJMP STARTORG 000BHINC 20H ;中断服务,中断计数器加1MOV TH0,#0D8HMOV TL0,#0EFH ;12M晶振,形成10毫秒中断RETISTART:MOV SP,#50HMOV TH0,#0D8HMOV TL0,#0EFHMOV TMOD,#01HMOV IE,#82HMUSIC0:NOPMOV DPTR,#DAT ;表头地址送DPTRMOV 20H,#00H ;中断计数器清0MUSIC1:NOPCLR A ;A清零MOVC A,@A+DPTR ;查表取代码JZ END0 ;是00H,则结束CJNE A,#0FFH,MUSIC5 ;如果是休止符,往下执行LJMP MUSIC3MUSIC5:NOPMOV R6,A ;R6=18H音符的频率INC DPTR DPTR加一MOV A,#0MOVC A,@A+DPTR ;取节拍代码送R7MOV R7,A ;R7=30H音符发音的时间SETB TR0 ;启动计数MUSIC2:NOPCPL P2.5 ;P2.5是音乐输出引脚MOV A,R6MOV R3,A ;R3=R6=18HLCALL DELMOV A,R7CJNE A,20H,MUSIC2 ;中断计数器(20H)=R7否?;不等,则继续循环MOV 20H,#00H ;等于,则取下一代码INC DPTRLJMP MUSIC1MUSIC3:NOP ;休止100毫秒CLR TR0MOV R2,#0DH ;R2=13MUSIC4:NOPMOV R3,#0FFH ;R3=255LCALL DELDJNZ R2,MUSIC4INC DPTRLJMP MUSIC1END0:NOPMOV R2,#0FFH ;歌曲结束,延时1秒后继续MUSIC6:MOV R3,#00HLCALL DELDJNZ R2,MUSIC6LJMP MUSIC0DEL:NOPDEL3:MOV R4,#02HDEL4:NOPDJNZ R4,DEL4NOPDJNZ R3,DEL3RETNOPDAT:DB 18H, 30H, 1CH, 10H DB 20H, 40H, 1CH, 10H DB 18H, 10H, 20H, 10H DB 1CH, 10H, 18H, 40H DB 1CH, 20H, 20H, 20H DB 1CH, 20H, 18H, 20H DB 20H, 80H, 0FFH, 20H DB 30H, 1CH, 10H , 18H DB 20H, 15H, 20H , 1CH DB 20H, 20H, 20H , 26H DB 40H, 20H , 20H , 2BH DB 20H, 26H, 20H , 20H DB 20H, 30H , 80H , 0FFH DB 20H, 20H, 1CH , 10H DB 18H, 10H, 20H , 20H DB 26H, 20H , 2BH , 20H DB 30H, 20H , 2BH , 40H DB 20H, 20H , 1CH , 10HDB 26H, 20H , 2BH, 20H DB 30H, 20H, 2BH , 40H DB 20H, 30H, 1CH , 10H DB 18H, 20H , 15H , 20H DB 1CH, 20H , 20H , 20H DB 26H, 40H, 20H , 20H DB 2BH, 20H, 26H , 20H DB 20H, 20H, 30H , 80H DB 20H, 30H, 1CH , 10H DB 20H, 10H, 1CH , 10H DB 20H, 20H, 26H , 20H DB 2BH, 20H, 30H , 20H DB 2BH, 40H, 20H , 15H DB 1FH, 05H, 20H , 10H DB 1CH, 10H, 20H , 20H DB 26H, 20H, 2BH , 20H DB 30H, 20H, 2BH , 40H DB 20H, 30H, 1CH , 10H DB 18H, 20H , 15H , 20H DB 1CH, 20H , 20H , 20H DB 26H, 40H, 20H , 20H DB 2BH, 20H, 26H , 20H DB 20H, 20H, 30H, 30HDB 18H, 40H, 1CH, 20HDB 20H, 20H, 26H, 40HDB 13H, 60H, 18H , 20HDB 15H, 40H, 13H , 40HDB 18H, 80H, 00H提问人的追问 2010-01-08 08:58原理是什么啊在线等啊我回答人的补充 2010-01-08 15:08一般说来,单片机演奏音乐基本都是单音频率,它不包含相应幅度的谐波频率,也就是说不能象电子琴那样能奏出多种音色的声音。
51单片机汇编语言音乐程序
51单片机汇编语言音乐程序51单片机汇编语言音乐程序 01两只老虎NEXT BIT 10H 定义标志位ORG 0000HJMP MAINORG 000BHJMP TONEORG 001BHJMP BEATORG 0100HMAIN MOV TMOD11HMOV TH0R2MOV TL0R3MOV TH13CHMOV TL10B0HMOV R400HMOV DPTRTABYD MOV AR4 取音调数据MOVC AADPTRMOV R2AXRL A00HJNZ JP 直接取节拍CALL PD 判断指针是否溢出 MOV AR4MOVC AADPTRMOV R3AXRL A0FFHJZ JPMOV AR2XRL A0FFHJNZ MAINJP INC R4 取节拍数据CALL PDMOV AR4MOVC AADPTRMOV R5ASETB EASETB ET0SETB ET1SETB TR0SETB TR1SETB NEXTJB NEXTINC R4CALL PDJMP YDCJNE A00HL1INC DPHL1 RET音调产生子程序TONE CPL P20MOV TH0R2MOV TL0R3RETI节拍产生子程序BEAT DJNZ R5L2MOV TH13CHMOV TL10B0HL3 RETIL2 CLR TR0CLR TR1CLR NEXTJMP L3两只老虎音符表TAB DB 0FBH0F9H08H0FCH066H08H0FCH0C7H08H0FBH0F9H08H DB 0FBH0F9H08H0FCH066H08H0FCH0C7H08H0FBH0F9H08H DB 0FCH0C7H08H0FCH0F3H08H0FDH4AH10H0FCH0C7H08H DB 0FCH0F3H08H0FDH4AH08H0FDH4AH04H0FDH95H04HDB 0FDH4AH04H0FCH0F3H04H0FCH0C7H08H0FBH0F9H08H DB 0FDH4AH04H0FDH95H04H0FDH4AH04H0FCH0F3H04HDB 0FCH0C7H08H0FBH0F9H08H0FCH66H08H0FAH93H08H DB 0FBH0F9H08H00H08H0FCH66H04H0FAH93H08HDB 0FBH0F9H08H00H10H0FFH0FFHEND02祝你平安ORG 0000HLJMP STARTORG 000BHINC 20H 中断服务中断计数器加1MOV TH00D8HMOV TL00EFH 12M晶振形成10毫秒中断RETISTARTMOV SP50HMOV TH00D8HMOV TL00EFHMOV TMOD01HMOV IE82HMUSIC0NOPMOV DPTRDAT 表头地址送DPTRMOV 20H00H 中断计数器清0MOV B00H 表序号清0MUSIC1NOPCLR AMOVC AADPTR 查表取代码JZ END0 是00H则结束CJNE A0FFHMUSIC5LJMP MUSIC3MUSIC5NOPMOV R6AINC DPTRMOV ABMOVC AADPTR 取节拍代码送R7MOV R7ASETB TR0 启动计数MUSIC2NOPCPL P33MOV AR6MOV R3ALCALL DELMOV AR7CJNE A20HMUSIC2 中断计数器 20H R7否不等则继续循环MOV 20H00H 等于则取下一代码INC DPTRINC BLJMP MUSIC1MUSIC3NOPCLR TR0 休止100毫秒MOV R20DHMUSIC4NOPMOV R30FFHLCALL DELDJNZ R2MUSIC4INC DPTRLJMP MUSIC1END0NOPMOV R264H 歌曲结束延时1秒后继续MUSIC6MOV R300HLCALL DELDJNZ R2MUSIC6LJMP MUSIC0DELNOPDEL3MOV R402HDEL4NOPDJNZ R4DEL4NOPDJNZ R3DEL3RETNOPDATdb 26h20h20h20h20h20h26h10h20h10h20h80h26h20h30h20h db 30h20h39h10h30h10h30h80h26h20h20h20h20h20h1ch20h db 20h80h2bh20h26h20h20h20h2bh10h26h10h2bh80h26h20h db 30h20h30h20h39h10h26h10h26h60h40h10h39h10h26h20h db 30h20h30h20h39h10h26h10h26h80h26h20h2bh10h2bh10h db 2bh20h30h10h39h10h26h10h2bh10h2bh20h2bh40h40h20h db 20h10h20h10h2bh10h26h30h30h80h18h20h18h20h26h20h db 20h20h20h40h26h20h2bh20h30h20h30h20h1ch20h20h20h db 20h80h1ch20h1ch20h1ch20h30h20h30h60h39h10h30h10h db 20h20h2bh10h26h10h2bh10h26h10h26h10h2bh10h2bh80h db 18h20h18h20h26h20h20h20h20h60h26h10h2bh20h30h20h db 30h20h1ch20h20h20h20h80h26h20h30h10h30h10h30h20h db 39h20h26h10h2bh10h2bh20h2bh40h40h10h40h10h20h10h db 20h10h2bh10h26h30h30h80h00HEND03生日歌ORG 0000HJMP STARTORG 000BHJMP TIM0ORG 0030HSTARTMOV TMOD01HMOV IE82H START0MOV 30H00H NEXT MOV A30HMOV DPTRTABLEMOVC AADPTRMOV R2AJZ END0ANL A0FHMOV R5AMOV AR2SWAP AANL A0FHJNZ SINGCLR TR0JMP D1SING DEC AMOV 22HARL AMOV DPTRTABLE1MOVC AADPTRMOV TH0AMOV 21HAMOV A22HRL AINC AMOVC AADPTRMOV TL0AMOV 20HASETB TR0D1 CALL DELAYINC 30HJMP NEXTEND0 CLR TR0JMP START0 TIM0 PUSH ACCPUSH PSWMOV TH021HMOV TL020HCPL P37POP PSWPOP ACCRETIDELAY MOV R702D2 MOV R4187D3 MOV R3248DJNZ R3DJNZ R4D3DJNZ R7D2DJNZ R5DELAYRETTABLE1DW 64260644006452464580DW 64684647776482064898DW 64968650306505865110DW 651576517865217 TABLEDB 82H01H81H94H84HDB 0B4H0A4H04HDB 82H01H81H94H84HDB 0C4H0B4H04HDB 82H01H81H0F4H0D4HDB 0B4H0A4H94HDB 0E2H01H0E1H0D4H0B4HDB 0C4H0B4H04HDB 82H01H81H94H84HDB 0B4H0A4H04HDB 82H01H81H94H84HDB 0C4H0B4H04HDB 82H01H81H0F4H0D4HDB 0B4H0A4H94HDB 0E2H01H0E1H0D4H0B4HDB 0C4H0B4H04HDB 00HEND04八月桂花香ORG 0000HLJMP STARTORG 000BHINC 20H 中断服务中断计数器加1 MOV TH00D8HMOV TL00EFH 12M晶振形成10毫秒中断RETISTARTMOV SP50HMOV TH00D8HMOV TL00EFHMOV TMOD01HMOV IE82HMUSIC0NOPMOV DPTRDAT 表头地址送DPTRMOV 20H00H 中断计数器清0MUSIC1NOPCLR A A清零MOVC AADPTR 查表取代码JZ END0 是00H则结束CJNE A0FFHMUSIC5 如果是休止符往下执行LJMP MUSIC3MUSIC5NOPMOV R6A R6 18H音符的频率INC DPTR DPTR加一MOV A0MOVC AADPTR 取节拍代码送R7MOV R7A R7 30H音符发音的时间SETB TR0 启动计数MUSIC2NOPCPL P23 P23是音乐输出引脚MOV AR6MOV R3A 籖3 R6 18HLCALL DELMOV AR7CJNE A20HMUSIC2 中断计数器 20H R7否不等则继续循环MOV 20H00H 等于则取下一代码INC DPTRLJMP MUSIC1MUSIC3NOP 休止100毫秒CLR TR0MOV R20DH R2 13MUSIC4NOPMOV R30FFH R3 255LCALL DELDJNZ R2MUSIC4INC DPTRLJMP MUSIC1END0NOPMOV R20FFH 歌曲结束延时1秒后继续MUSIC6MOV R300HLCALL DELDJNZ R2MUSIC6LJMP MUSIC0DELNOPDEL3MOV R402HDEL4NOPDJNZ R4DEL4NOPDJNZ R3DEL3RETNOPDATDB 18H 30H 1CH 10H DB 20H 40H 1CH 10H DB 18H 10H 20H 10H DB 1CH 10H 18H 40H DB 1CH 20H 20H 20H DB 1CH 20H 18H 20H DB 20H 80H 0FFH 20H DB 30H 1CH 10H 18H DB 20H 15H 20H 1CH DB 20H 20H 20H 26H DB 40H 20H 20H 2BH DB 20H 26H 20H 20H DB 20H 30H 80H 0FFH DB 20H 20H 1CH 10H DB 18H 10H 20H 20H DB 26H 20H 2BH 20H DB 30H 20H 2BH 40H DB 20H 20H 1CH 10H DB 18H 10H 20H 20H DB 26H 20H 2BH 20H DB 30H 20H 2BH 40H DB 20H 30H 1CH 10HDB 18H 20H 15H 20H DB 1CH 20H 20H 20H DB 26H 40H 20H 20H DB 2BH 20H 26H 20H DB 20H 20H 30H 80H DB 20H 30H 1CH 10H DB 20H 10H 1CH 10H DB 20H 20H 26H 20H DB 2BH 20H 30H 20H DB 2BH 40H 20H 15H DB 1FH 05H 20H 10H DB 1CH 10H 20H 20H DB 26H 20H 2BH 20H DB 30H 20H 2BH 40H DB 20H 30H 1CH 10H DB 18H 20H 15H 20H DB 1CH 20H 20H 20H DB 26H 40H 20H 20H DB 2BH 20H 26H 20H DB 20H 20H 30H 30H DB 20H 30H 1CH 10H DB 18H 40H 1CH 20H DB 20H 20H 26H 40H DB 13H 60H 18H 20H DB 15H 40H 13H 40HDB 18H 80H 00Hend05兰花草SPK EQU P33 蜂鸣器所在端口ORG 0000HJMP STARTSTARTMOV R300HNEXTMOV AR3MOV DPTRTABLEMOVC AADPTRJZ STARTMOV R7AINC R3MOV AR3MOVC AADPTRMOV R2AACALL SONGINC R3SJMP NEXT歌曲播放子程序SONGMOV AR2 取出节拍RL AJNZ KEEPMOV A01HKEEPMOV R2AREPEATACALL EIGHTHDJNZ R2REPEATRET产生18拍延时子程序EIGHTHMOV AR7 查表取出廷时参数保存到R4MOV DPTRDELAY_TMOVC AADPTRMOV R4AMOV AR7 查表取出18拍周期数保存到R5 MOV DPTRS_PARAMOVC AADPTRMOV R5ANEXTCYCACALL SOUNDDJNZ R5NEXTCYCRET发声子程序SOUNDSETB SPKACALL SDELAYCLR SPKACALL SDELAYRET延时子程序SDELAYMOV AR4 廷时值在R4内MOV R0AXL2MOV R103HDL1NOPDJNZ R1DL1DJNZ R0XL2RET18拍周期表S_PARADS 1DHDB 15H16H00DB 19H00H1CH00H1FH21H00H25HDB 00H29H2CH00H31H34H37H00HDB 3EH41H00H49H00H52H57H00HDB 62H延时参数表DELAY_TDS 1DHDB 7EH77H00HDB 6AH00H5EH00H54H4FH00H46HDB 00H3FH3BH00H35H32H2FH00HDB 2AH27H00H23H00H1FH1DH0C0HDB 1AH歌曲表TABLEDW 2202H2902H2902H2902H2906H2702H 我从山中来DW 2502H2702H2502H2402H2208H 带着兰花草DW 2E02H2E02H2E02H2E02H2E06H2C02H 种在小园中DW 2902H2C02H2D02H2A02H2908H 祈祷花开早DW 2902H2E02H2E02H2C02H2906H2702H 一日看三回DW 2502H2702H2502H2402H2206H1D02H 看得花时过DW 1D02H2502H2502H2402H2206H2902H 兰花却依然DW 2702H2502H2402H2002H2208H 苞也无一个DW 0000HEND。
51单片机唱歌 C51音乐程序
51单片机唱歌 C51音乐程序#i nclude <reg52.h>#i nclude <intrins.h>//本例采用89C52, 晶振为11.0592MHZ//关于如何编制音乐代码, 其实十分简单,各位可以看以下代码.//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;//所以拿出谱子, 试探编吧!unsigned char n=0; //n为节拍常数变量unsigned char code music_tab[] ={0x18, 0x30, 0x1C , 0x10, //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数, 0x20, 0x40, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x10,0x1C, 0x10, 0x18 , 0x40,0x1C, 0x20, 0x20 , 0x20,0x1C, 0x20, 0x18 , 0x20,0x20, 0x80, 0xFF , 0x20,0x30, 0x1C, 0x10 , 0x18,0x20, 0x15, 0x20 , 0x1C,0x20, 0x20, 0x20 , 0x26,0x40, 0x20, 0x20 , 0x2B,0x20, 0x26, 0x20 , 0x20,0x20, 0x30, 0x80 , 0xFF,0x20, 0x20, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,0x30, 0x20, 0x2B , 0x40,0x20, 0x20, 0x1C , 0x10,0x18, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,0x30, 0x20, 0x2B , 0x40,0x20, 0x30, 0x1C , 0x10,0x18, 0x20, 0x15 , 0x20,0x1C, 0x20, 0x20 , 0x20,0x26, 0x40, 0x20 , 0x20,0x2B, 0x20, 0x26 , 0x20,0x20, 0x20, 0x30 , 0x80,0x20, 0x30, 0x1C , 0x10,0x20, 0x10, 0x1C , 0x10,0x20, 0x20, 0x26 , 0x20,0x2B, 0x20, 0x30 , 0x20,0x2B, 0x40, 0x20 , 0x15,0x1F, 0x05, 0x20 , 0x10,0x1C, 0x10, 0x20 , 0x20,0x26, 0x20, 0x2B , 0x20,0x30, 0x20, 0x2B , 0x40,0x20, 0x30, 0x1C , 0x10,0x18, 0x20, 0x15 , 0x20,0x1C, 0x20, 0x20 , 0x20,0x26, 0x40, 0x20 , 0x20,0x2B, 0x20, 0x26 , 0x20,0x20, 0x20, 0x30 , 0x30,0x20, 0x30, 0x1C , 0x10,0x18, 0x40, 0x1C , 0x20,0x20, 0x20, 0x26 , 0x40,0x13, 0x60, 0x18 , 0x20,0x15, 0x40, 0x13 , 0x40,0x18, 0x80, 0x00};void int0() interrupt 1 //采用中断0 控制节拍{ TH0=0xd8;TL0=0xef;n--;}void delay (unsigned char m) //控制频率延时{unsigned i=3*m;while(--i);}void delayms(unsigned char a) //豪秒延时子程序{while(--a); //采用while(--a) 不要采用while(a--); 各位可编译一下看看汇编结果就知道了!}void main(){ unsigned char p,m; //m为频率常数变量unsigned char i=0;TMOD&=0x0f;TMOD =0x01;TH0=0xd8;TL0=0xef;IE=0x82;play:while(1){a: p=music_tab[i];if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到结束符,延时1秒,回到开始再来一遍else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延时100ms,继续取下一音符else {m=music_tab[i++], n=music_tab[i++];} //取频率常数和节拍常数TR0=1; //开定时器1while(n!=0) P1=~P1,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!)TR0=0; //关定时器1 }}。
单片机音乐播放c语言程序
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
4,2,1, 4,2,1, 3,2,2, 1,2,2, 2,2,2, 1,2,4
};
//生日歌
// 音阶频率表 高八位
code uchar FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
/* 函数声明 */
/*******************************************************/
uchar keyscan(void); //键盘扫描
void delay1(uint i); //演示程序
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
} ;
// 音阶频率表 低八位
code uchar FREQL[]={
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
延时程序
------------------------------------------------*/
cord_l=P3&0xf0; //读入行线值
return(cord_h+cord_l);//键盘最后组合码值
单片机音乐的编程
单片机音乐的编程我们知道,声音的频谱范围约在几十到几千赫兹,若能利用程序来控制单处机某个口线的高电平或低电平,则在该口线上就能产生一定频率的矩形波,接上喇叭就能发出一定频率的声音,若再利用延时程序控制“高”“低”电平的持续时间,就能改变输出频率,从而改变音调。
例如,要产生200HZ的音频信号,200HZ音频的变化周期为1/200秒,即5ms。
这样,当P2.5的高电平或低电平的持续时间为2.5ms时就能发出200HZ的音调。
乐曲中,每一音符对应着确定的频率,我们将每一音符的时间常数和其相应的节拍常数作为一组,按顺序将乐曲中的所有常数排列成一个表,然后由查表程序依次取出,产生音符并控制节奏,就可以实现演奏效果。
此外,结束符和体止符可以分别用代码00H和0XH来表示,若查表结果为00H,则表示曲子终了;若查表结果为FFH,则产生相应的停顿效果。
定时器T0工作在定时方式1,改变TH0及TL0,产生不同的音频频率。
必须考虑到中断响应时间的影响,尤其在高音部分,若忽略中断响应时间,会使音频频率比标准值低几十Hz,相当于1/4音程,很容易听出来,对低音部分影响不大。
一般中断响应时间为3~6个机器周期,经过反复试验取5个机器周期作为校正最为恰当,表1中所给的定时初值就是考虑中断响应后的定时常数。
另外,为避免T1中断可能引起杂音,应将定时器T0中断设为高优先级。
这样编写出来的程序播放的音与标准音叉进行差频校音,非常准确和谐。
本例中单片机晶振频率为6MHz。
现在以单片机12MHZ晶振为例,例出高中低音符与单片机计数T0相关的计数值如下表所示音符频率(HZ)简谱码音符频率(HZ)简谱码(T值)低1DO 262 63628 # 4 FA# 740 64860#1DO# 277 63731 中5 SO 784 64898低2RE 294 63835 # 5 SO# 831 64934#2 RE# 311 63928 中6 LA 880 64968低3 M 330 64021 # 6 932 64994低4 FA 349 64103 中7 SI 988 65030# 4 FA# 370 64185 高1 DO 1046 65058低5 SO 392 64260 # 1 DO# 1109 65085# 5 SO# 415 64331 高2 RE 1175 65110低6 LA 440 64400 # 2 RE# 1245 65134# 6 466 64463 高3 M 1318 65157低7 SI 494 64524 高 4 FA 1397 65178中1 DO 523 64580 # 4 FA# 1480 65198# 1 DO# 554 64633 高5 SO 1568 65217中2 RE 587 64684 # 5 SO# 1661 65235# 2 RE# 622 64732 高6 LA 1760 65252中3 M 659 64777 # 6 1865 65268中4 FA 698 64820 高7 SI 1967 65283DW 64260,64400,64524,64580DW 64684,64777,64820,64898DW 64968,65030,65058,65110DW 65157,65178,65217下面我们要为这个音符建立一个表格,有助于单片机通过查表的方式来获得相应的数据低音0-19之间,中音在20-39之间,高音在40-59之间TABLE: DW 0,63628,63835,64021,64103,64260,64400,64524,0,0DW 0,63731,63928,0,64185,64331,64463,0,0,0DW 0,64580,64684,64777,64820,64898,64968,65030,0,0DW 0,64633,64732,0,64860,64934,64994,0,0,0DW 0,65058,65110,65157,65178,65217,65252,65283,0,0DW 0,65085,65134,0,65198,65235,65268,0,0,0DW 02、音乐的音拍,一个节拍为单位(C调)拍数为1/8拍(0.10s)~8拍(6.40s)编写乐曲程序可以分四个步骤:第一步,为演奏的乐曲定义一个频率表和一个节拍时间表。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
单片机音乐的编程我们知道,声音的频谱范围约在几十到几千赫兹,若能利用程序来控制单处机某个口线的高电平或低电平,则在该口线上就能产生一定频率的矩形波,接上喇叭就能发出一定频率的声音,若再利用延时程序控制“高”“低”电平的持续时间,就能改变输出频率,从而改变音调。
例如,要产生200HZ的音频信号,200HZ音频的变化周期为1/200秒,即5ms。
这样,当P2.5的高电平或低电平的持续时间为2.5ms时就能发出200HZ的音调。
乐曲中,每一音符对应着确定的频率,我们将每一音符的时间常数和其相应的节拍常数作为一组,按顺序将乐曲中的所有常数排列成一个表,然后由查表程序依次取出,产生音符并控制节奏,就可以实现演奏效果。
此外,结束符和体止符可以分别用代码00H和0XH来表示,若查表结果为00H,则表示曲子终了;若查表结果为FFH,则产生相应的停顿效果。
定时器T0工作在定时方式1,改变TH0及TL0,产生不同的音频频率。
必须考虑到中断响应时间的影响,尤其在高音部分,若忽略中断响应时间,会使音频频率比标准值低几十Hz,相当于1/4音程,很容易听出来,对低音部分影响不大。
一般中断响应时间为3~6个机器周期,经过反复试验取5个机器周期作为校正最为恰当,表1中所给的定时初值就是考虑中断响应后的定时常数。
另外,为避免T1中断可能引起杂音,应将定时器T0中断设为高优先级。
这样编写出来的程序播放的音与标准音叉进行差频校音,非常准确和谐。
本例中单片机晶振频率为6MHz。
现在以单片机12MHZ晶振为例,例出高中低音符与单片机计数T0相关的计数值如下表所示音符频率(HZ)简谱码音符频率(HZ)简谱码(T值)低1DO 262 63628 # 4 FA# 740 64860#1DO# 277 63731 中5 SO 784 64898低2RE 294 63835 # 5 SO# 831 64934#2 RE# 311 63928 中6 LA 880 64968低3 M 330 64021 # 6 932 64994低4 FA 349 64103 中7 SI 988 65030# 4 FA# 370 64185 高1 DO 1046 65058低5 SO 392 64260 # 1 DO# 1109 65085# 5 SO# 415 64331 高2 RE 1175 65110低6 LA 440 64400 # 2 RE# 1245 65134# 6 466 64463 高3 M 1318 65157低7 SI 494 64524 高 4 FA 1397 65178中1 DO 523 64580 # 4 FA# 1480 65198# 1 DO# 554 64633 高5 SO 1568 65217中2 RE 587 64684 # 5 SO# 1661 65235# 2 RE# 622 64732 高6 LA 1760 65252中3 M 659 64777 # 6 1865 65268中4 FA 698 64820 高7 SI 1967 65283DW 64260,64400,64524,64580DW 64684,64777,64820,64898DW 64968,65030,65058,65110DW 65157,65178,65217下面我们要为这个音符建立一个表格,有助于单片机通过查表的方式来获得相应的数据低音0-19之间,中音在20-39之间,高音在40-59之间TABLE: DW 0,63628,63835,64021,64103,64260,64400,64524,0,0DW 0,63731,63928,0,64185,64331,64463,0,0,0DW 0,64580,64684,64777,64820,64898,64968,65030,0,0DW 0,64633,64732,0,64860,64934,64994,0,0,0DW 0,65058,65110,65157,65178,65217,65252,65283,0,0DW 0,65085,65134,0,65198,65235,65268,0,0,0DW 02、音乐的音拍,一个节拍为单位(C调)拍数为1/8拍(0.10s)~8拍(6.40s)编写乐曲程序可以分四个步骤:第一步,为演奏的乐曲定义一个频率表和一个节拍时间表。
频率表中的数据可以从表中查到,节拍时间表的数据是延迟时间的倍数;第二步,分别将两个表中的偏移地址送入两个指针寄存器;第三步,分别从两个表中取出频率值和音符的持续时间值,送入相应的寄存器中,供调用通用发声程序使用;第四步,调用通用发声程序发出音调。
我们知道,声音的频谱范围约在几十到几千赫兹,若能利用程序来控制单处机某个口线的高电平或低电平,则在该口线上就能产生一定频率的矩形波,接上喇叭就能发出一定频率的声音,若再利用延时程序控制“高”“低”电平的持续时间,就能改变输出频率,从而改变音调。
为了产生手弹的节奏感,在某些音符(例如两个相同音符)音插入一个时间单位的频率略有不同的音符。
ORG 0000HAJMP MAINORG 000BHJMP TIM0ORG 0030HMAIN: ;主程序MOV TMOD,#01H ;设定时方式MOV IE,#82H ;开中断START0: MOV 30H,#00H ;NEXT: MOV A,30HMOV DPTR,#TABLE ;节拍表格,MOVC A,@A+DPTR ;查表取音调和节拍MOV R2,A ;JZ END0 ;为零,不进行处理.ANL A,#0FH ;分离高低位MOV R5,A ;将节拍存入R5MOV A,R2 ;分离高低位SWAP A ;分离高低位ANL A,#0FH ;分离高低位JNZ SING ;根据音调决定是否演奏.CLR TR0JMP D1SING:DEC A ;a中放的是音阶位置.MOV 22H,A ;将音调存入22H.RL A ;乘2操作指向高字节MOV DPTR,#TABLE1MOVC A,@A+DPTR ;根据音调决定定时器初值.MOV TH0,A ;放入TH0MOV 21H,AMOV A,22HRL AINC A ;乘2后加一操作指向低字节MOVC A,@A+DPTRMOV TL0,A ;放入TL0MOV 20H,ASETB TR0 ;启动定时D1: CALL DELAYINC 30HJMP NEXTEND0: CLR TR0JMP START0TIM0: PUSH ACC ;定时器中断服务程序PUSH PSWMOV TL0,20H ;重赋初值.MOV TH0,21HCPL P1.4POP PSWPOP ACCRETIDELAY:MOV R7,#01H ; 100MS延时子程序D2: MOV R4,#200D3: MOV R3,#250DJNZ R3,$DJNZ R4,D3DJNZ R7,D2DJNZ R5,DELAY ;R5是1/8拍的倍率RETTABLE1: ;决定音调的定时初值.DW 0FB05H,0FDCDH,0FE0BH; 1-5, 2-6, 3-7DW 0FE26H,0FE5AH,0FE89H,0FE9EH; 4 1, 5 2, 6 3, 7 4 DW 0FEC5H,0FEE8H,0FF07H ; 8 5, 9 6, A 7DW 0FF15H,0FF2FH,0FF46H,0FF51H; B+1, C+2, D+3, E+4TABLE: ;决定音调和节拍的表格,不同的歌曲只是此表不同5562 1162 5561 65 1162DB 82H,81H, 91H,54H,42H,41H,21H ;第一个数字表示音阶位置,DB 54H,82H,82H,91H,0b1H ;第二个数字表示音拍长度DB 91H,81H,42H,41H,21HDB 54H,82H,52H,42H,31H,21HDB 12H,82H,52H,61H,51HDB 42H,41H,21H,51H,61HDB 51H,41H,51H,41H,31HDB 21H,14HDB 00ENDORG 0000HAJMP MAINORG 000BHMOV TL0,R2MOV TH0,R3CPL P1.4RETIORG 001BHDJNZ R4,GOON;中断次数不够,继续。
MOV IE,#80H ;持续时间到,该音符停止。
SJMP RE01GOON: MOV TL1,40HMOV TH1,41HRE01: RETIORG 0100HMAIN: MOV R5,#02H ;乐曲循环演奏次数LOOP: MOV TMOD,#11HMOV IP,#02HMOV 41H,#3CH; T1初值放在41H,40H单元MOV 40H,#0B0H ;值越大,演奏越快SETB EACLR AMOV R1,A ;R1作为音符指针MOV DPTR,#TAB;乐曲数据表的表头L1: MOVC A,@A+DPTRNOPNOPNOPINC R1 ;指向音符的拍数字节MOV R6,A ;音级字节暂存入R6JZ L3ACALL TONEMOV TH0,R3MOV TL0,R2SETB TR0SETB ET0MOV A,R1MOVC A,@A+DPTRINC R1ACALL LONG ;MOV A,R1AJMP L1 ;L3: MOV IE,#00HMOV TCON,#00HMOV TMOD,#00HDJNZ R5,LOOPAJMP $ORG 0160HTONE: PUSH DPHPUSH DPLMOV DPTR, #TAB1MOV A,R6 ;取出音级字节RL A ;乘2,定时常数为双字节MOV 42H,A ;暂存入42H单元MOVC A,@A+DPTR;取出TH0MOV R3,AMOV A,42HINC AMOVC A,@A+DPTR;取出TL0MOV R2,A ;POP DPLPOP DPHRETLONG: PUSH DPH ;音长子程序PUSH DPLMOV DPTR,#TAB2MOVC A,@A+DPTR;取出定时器T1应中断的次数MOV R4,A ;T1应中断的次数存入R4中MOV TH1,41HMOV TL1,40HSETB TR1SETB ET1CJNE R4,#00H,$CLR TR0POP DPLPOP DPHRET ;音长子程序结束TAB1: ;音符的拍数DB 0FCH,49H,0FCH,0B2H ,0FDH,0FH,0FDH,39H;0-1,2-1,4-3,6-4 DB 0FDH,87H,0FDH,0CDH,0FEH,0BH;8-5,10-6,12-7DB 0FEH,26H,0FEH,5AH,0FEH,89H,0FEH,9EH;14 1,16 2,18 3,20 4 DB 0FEH,0C5H,0FEH,0E8H,0FFH,07H ;22 5,24 6,26 7DB 0FFH,15H,0FFH,2FH,0FFH,46H,0FFH,51H;28+1,30+2,32+3,34+4 DB 0FFH,65H,0FFH,76H,0FFH,85H;36+5,38+6,40+7;应中断的次数TAB2: DB 01,02,04,06DB 08,10,12,14DB 16,20,24,28DB 32,40,48,64;乐曲字节数据表TAB: DB 0BH,04H,0BH,04H,0BH,04H,09H,02H,0BH,02HDB 0CH,02H,0EH,02H,0CH,02H,09H,02H,0BH,04HDB 09H,02H,08H,02H,07H,08H,08H,08H,09H,0CHDB 00H,0FH ;结束标记END。