1C51音乐程序#includereg52.h#includeintrins.h//本例采用89C52,晶振为11.0592MHZ//关于如何编制音乐代码,其实十分简单,各位可以看以下代码.//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;//所以拿出谱子,试探编吧!unsignedcharn=0;//n为节拍常数变量unsignedcharcodemusic_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};voidint0()interrupt1{//采用中断0控制节拍TH0=0xd8;2TL0=0xef;n--;}voiddelay(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;play:while(1){a:p=music_tab[i];if(p==0x00){//如果碰到结束符,延时1秒,回到开始再来一遍i=0;delayms(1000);gotoplay;}elseif(p==0xff){//若碰到休止符,延时100ms,继续取下一音符i=i+1;delayms(100);TR0=0;gotoa;}else{//取频率常数和节拍常数m=music_tab[i++];n=music_tab[i++];}TR0=1;//开定时器1while(n!=0)P1=~P1,delay(m);//等待节拍完成,通过P1口输出音频(可多声道哦!)TR0=0;//关定时器1}}