资源描述:
《51单片机唱歌 C51音乐程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、51单片机唱歌C51音乐程序#include#include//本例采用89C52,晶振为11.0592MHZ//关于如何编制音乐代码,其实十分简单,各位可以看以下代码.//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;//所以拿出谱子,试探编吧!unsignedcharn=0;//n为节拍常数变量unsignedcharcodemusic_tab[]={0x18,0x30,0x1C,0x10,//格式为:频率常数,节拍常数,频率常数,节拍常
2、数,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,0x2
3、0,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,0x2
4、0,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,0x2
5、0,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};voidint0()interrupt1//采用中断0控制节拍{TH0=0xd8;TL0=0xef;n--;}voiddel
6、ay(unsignedcharm)//控制频率延时{unsignedi=3*m;while(--i);}voiddelayms(unsignedchara)//豪秒延时子程序{while(--a);//采用while(--a)不要采用while(a--);各位可编译一下看看汇编结果就知道了!}voidmain(){unsignedcharp,m;//m为频率常数变量unsignedchari=0;TMOD&=0x0f;TMOD=0x01;TH0=0xd8;TL0=0xef;IE=0x82;pla
7、y:while(1){a:p=music_tab[i];if(p==0x00){i=0,delayms(1000);gotoplay;}//如果碰到结束符,延时1秒,回到开始再来一遍elseif(p==0xff){i=i+1;delayms(100),TR0=0;gotoa;}//若碰到休止符,延时100ms,继续取下一音符else{m=music_tab[i++],n=music_tab[i++];}//取频率常数和节拍常数TR0=1;//开定时器1while(n!=0)P1=~P1,dela
8、y(m);//等待节拍完成,通过P1口输出音频(可多声道哦!)TR0=0;//关定时器1}}