怎樣用stc89c52單片機控制蜂鳴器發出音樂的c語言程序?
#include\r\n#include\r\n//本例采用89C52,晶振為11.0592MHZ\r\n//關於如何編制音樂代碼,其實十分簡單,各位可以看以下代碼.\r\n//頻率常數即音樂術語中的音調,而節拍常數即音樂術語中的多少拍;\r\n//所以拿出譜子,試探編吧!\r\nsbitBeep=P1^5;\r\n\r\nunsignedcharn=0;//n為節拍常數變量\r\nunsignedcharcodemusic_tab[]={\r\n0x18,0x30,0x1C,0x10,//格式為:頻率常數,節拍常數,頻率常數,節拍常數,\r\n0x20,0x40,0x1C,0x10,\r\n0x18,0x10,0x20,0x10,\r\n0x1C,0x10,0x18,0x40,\r\n0x1C,0x20,0x20,0x20,\r\n0x1C,0x20,0x18,0x20,\r\n0x20,0x80,0xFF,0x20,\r\n0x30,0x1C,0x10,0x18,\r\n0x20,0x15,0x20,0x1C,\r\n0x20,0x20,0x20,0x26,\r\n0x40,0x20,0x20,0x2B,\r\n0x20,0x26,0x20,0x20,\r\n0x20,0x30,0x80,0xFF,\r\n0x20,0x20,0x1C,0x10,\r\n0x18,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x20,0x1C,0x10,\r\n0x18,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x20,0x15,0x20,\r\n0x1C,0x20,0x20,0x20,\r\n0x26,0x40,0x20,0x20,\r\n0x2B,0x20,0x26,0x20,\r\n0x20,0x20,0x30,0x80,\r\n0x20,0x30,0x1C,0x10,\r\n0x20,0x10,0x1C,0x10,\r\n0x20,0x20,0x26,0x20,\r\n0x2B,0x20,0x30,0x20,\r\n0x2B,0x40,0x20,0x15,\r\n0x1F,0x05,0x20,0x10,\r\n0x1C,0x10,0x20,0x20,\r\n0x26,0x20,0x2B,0x20,\r\n0x30,0x20,0x2B,0x40,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x20,0x15,0x20,\r\n0x1C,0x20,0x20,0x20,\r\n0x26,0x40,0x20,0x20,\r\n0x2B,0x20,0x26,0x20,\r\n0x20,0x20,0x30,0x30,\r\n0x20,0x30,0x1C,0x10,\r\n0x18,0x40,0x1C,0x20,\r\n0x20,0x20,0x26,0x40,\r\n0x13,0x60,0x18,0x20,\r\n0x15,0x40,0x13,0x40,\r\n0x18,0x80,0x00\r\n};\r\n\r\nvoidint0()interrupt1//采用中斷0控制節拍\r\n{TH0=0xd8;\r\nTL0=0xef;\r\nn--;\r\n}\r\n\r\nvoiddelay(unsignedcharm)//控制頻率延時\r\n{\r\nunsignedi=3*m;\r\nwhile(--i);\r\n}\r\n\r\nvoiddelayms(unsignedchara)//豪秒延時子程序\r\n{\r\nwhile(--a);//采用while(--a)不要采用while(a--);各位可編譯壹下看看匯編結果就知道了!\r\n}\r\n\r\nvoidmain()\r\n{unsignedcharp,m;//m為頻率常數變量\r\nunsignedchari=0;\r\nTMOD&=0x0f;\r\nTMOD|=0x01;\r\nTH0=0xd8;TL0=0xef;\r\nIE=0x82;\r\nplay:\r\nwhile(1)\r\n{\r\na:p=music_tab[i];\r\nif(p==0x00){i=0,delayms(1000);gotoplay;}//如果碰到結束符,延時1秒,回到開始再來壹遍\r\nelseif(p==0xff){i=i+1;delayms(100),TR0=0;gotoa;}//若碰到休止符,延時100ms,繼續取下壹音符\r\nelse{m=music_tab[i++],n=music_tab[i++];}//取頻率常數和節拍常數\r\nTR0=1;//開定時器1\r\nwhile(n!=0)Beep=~Beep,delay(m);//等待節拍完成,通過P1口輸出音頻(可多聲道哦!)\r\nTR0=0;//關定時器1\r\n}\r\n}\r\n\r\n妳參考這個吧