单片机音乐程序(六首歌)
单片机_音乐播放的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单片机实现的音乐程序
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>
单片机蜂鸣器播放音乐代码生日快乐两只蝴蝶祝你平安
/生日快乐歌曲/ include<reg51.h>defineuintunsignedintdefineucharunsignedcharsbitbeep=P1^5;ucharcodeSONG_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};ucharcodeSONG_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};//延时voidDelayMSuintx{uchart;whilex--fort=0;t<120;t++;}voidPlayMusic{uinti=0;j;k;whileSONG_LONGi=0||SONG_TONEi=0{//播放各个音符;SONG_LONG为拍子长度forj=0;j<SONG_LONGi20;j++{//SONG_TONE延时表决定了每个音符的频率fork=0;k<SONG_TONEi/3;k++;}DelayMS10;i++;}}voidmain{beep=0;while1{PlayMusic;//播放生日快乐DelayMS500;//播放完后暂停一段时间}}两只蝴蝶:include<reg51.h>//这是单片机音乐代码生成器生成的代码defineucharunsignedcharsbitbeepIO=P1^5;//输出为P1.5可以修改成其它IO口ucharm;n;ucharcodeT492={{0;0};{0xF8;0x8B};{0xF8;0xF2};{0xF9;0x5B};{0xF9;0xB7};{0xFA;0x14};{0xFA;0x66};{0xFA;{0xFC;0x43};{0xFC;0x78};{0xFC;0xAB};{0xFC;0xDB};{0xFD;0x08};{0xFD;0x33};{0xFD; 0x5B};{0xFD;0x81};{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}};ucharcodemusic2={{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}};voiddelayucharpuchari;j;for;p>0;p--fori=181;i>0;i--forj=181;j>0;j--;}voidpause{uchari;j;fori=150;i>0;i--forj=150;j>0;j--;}voidT0_intinterrupt1 {beepIO=beepIO;TH0=Tm0;TL0=Tm1;}voidmain{uchari=0;TMOD=0x01;EA=1;ET0=1; while1{m=musici0;n=musici1;{TR0=0;delayn;i++;}elseifm==0xFF{TR0=0;delay30;i=0;}elseifm==musici+10{TR0=1;delayn;TR0=0;pause;i++;}else{TR0=1;delayn;i++;}}}祝你平安:include"reg52.h"unsignedcharCount;sbit_Speak=P1^5;unsignedcharcodeSONG={//祝你平安0x26;0x20;0x20;0x20;0x20;0x20;0x26;0x10;0x20;0x10;0x20;0x80;0x26;0x20;0x30;0x2 0;0x30;0x20;0x39;0x10;0x30;0x10;0x30;0x80;0x26;0x20;0x20;0x20;0x20;0x20;0x1c;0x2 0;0x20;0x80;0x2b;0x20;0x26;0x20;0x20;0x20;0x2b;0x10;0x26;0x10;0x2b;0x80;0x26;0x2 0;0x30;0x20;0x30;0x20;0x39;0x10;0x26;0x10;0x26;0x60;0x40;0x10;0x39;0x10;0x26;0x2 0;0x30;0x20;0x30;0x20;0x39;0x10;0x26;0x10;0x26;0x80;0x26;0x20;0x2b;0x10;0x2b;0x10x2b;0x20;0x30;0x10;0x39;0x10;0x26;0x10;0x2b;0x10;0x2b;0x20;0x2b;0x40;0x40;0x2 0;0x20;0x10;0x20;0x10;0x2b;0x10;0x26;0x30;0x30;0x80;0x18;0x20;0x18;0x20;0x26;0x2 0;0x20;0x20;0x20;0x40;0x26;0x20;0x2b;0x20;0x30;0x20;0x30;0x20;0x1c;0x20;0x20;0x2 0;0x20;0x80;0x1c;0x20;0x1c;0x20;0x1c;0x20;0x30;0x20;0x30;0x60;0x39;0x10;0x30;0x1 0;0x20;0x20;0x2b;0x10;0x26;0x10;0x2b;0x10;0x26;0x10;0x26;0x10;0x2b;0x10;0x2b;0x8 0;0x18;0x20;0x18;0x20;0x26;0x20;0x20;0x20;0x20;0x60;0x26;0x10;0x2b;0x20;0x30;0x2 0;0x30;0x20;0x1c;0x20;0x20;0x20;0x20;0x80;0x26;0x20;0x30;0x10;0x30;0x10;0x30;0x2 0;0x39;0x20;0x26;0x10;0x2b;0x10;0x2b;0x20;0x2b;0x40;0x40;0x10;0x40;0x10;0x20;0x1 0;0x20;0x10;0x2b;0x10;0x26;0x30;0x30;0x80;0x00};voidTime0_Init{TMOD=0x01;IE=0x82;TH0=0xD8;TL0=0xEF;//12MZ晶振;10msvoidTime0_Intinterrupt1{TH0=0xD8;TL0=0xEF;Count++;//长度加1}/-------------------------------------------------功能:1MS延时子程序-------------------------------------------------/ voidDelay_xMsunsignedintx{unsignedinti;j;fori=0;i<x;i++{forj=0;j<3;j++;}}voidPlay_Songunsignedchari{unsignedcharTemp1;Temp2;unsignedintAddr;Count=0;//中断计数器清0Addr=i217;Temp1=SONGAddr++;ifTemp1==0xFF//休止符{TR0=0;Delay_xMs100;}elseifTemp1==0x00//歌曲结束符{return;}else{Temp2=SONGAddr++;TR0=1;while1{_Speak=~_Speak;Delay_xMsTemp1;ifTemp2==Count{Count=0;break;}}}/-------------------------------------------------功能:主程序-------------------------------------------------/ voidmain{Time0_Init;//定时器0中断初始化while1{Play_Song0;//播放}}。
单片机音乐播放___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单片机音乐盒程序
51单⽚机⾳乐盒程序#include<reg52.h>#define uchar unsigned char#define uint unsigned int#define ulong unsigned longcode unsigned char gao[]={0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,} ;code unsigned char di[]={0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,};uchar data gepu[55]; //必须要指明长度uchar data time[55];uchar a;//中间变量把ram数组的值赋给它,传递给定时器中断//第⼀⾸:⽣⽇歌uchar code gepu1[]={5,0,5,6,5,8,7, 5,0,5,6,5,9,8, 5,0,5,12,10,8,7,6, 11,0,11,10,8,9,8,8};//30uchar code time1[]={2,0,2,4,4,4,6, 2,0,2,4,4,4,6, 2,0,2,4,4,4,4,6, 2,0,2,4,4,4,4,4 };//第⼆⾸:两只⽼虎 //⼤于14是指低⾳uchar code gepu2[]={1,2,3,1, 1,2,3,1, 3,4,5,0, 3,4,5,0, 5,6,5,4,3,1, 5,6,5,4,3,1, 2,19,1,0, 2,19,1,0};//36uchar code time2[]={4,4,4,4, 4,4,4,4, 4,4,4,4, 4,4,4,4, 3,1,3,1,4,4, 3,1,3,1,4,4, 4,4,4,4, 4,4,4,4};////第三⾸:多年以前uchar code gepu3[]={1,0,1,2,3,0,3,4, 5,6,5,3, 5,4,3,2, 4,3,2,1,0, 1,0,1,2,3,0,3,4, 5,6,5,3, 5,4,3,2,3,2,1,0};//41uchar code time3[]={4,0,2,2,4,0,2,2, 4,2,2,8, 4,2,2,8, 4,2,2,8,0, 4,0,2,2,4,0,2,2, 4,2,2,8, 4,2,2,4,2,2,10,1};//第四⾸:⼩步舞曲uchar code gepu4[]={5,1,2,3,4,5,1,0,1, 6,4,5,6,7,8,1,0,1, 4,5,4,3,2,3, 4,3,2,1,2, 3,2,1,21,1};//34uchar code time4[]={4,2,2,2,2,4,4,0,4, 4,2,2,2,2,4,4,0,4, 4,2,2,2,2,4, 2,2,2,2,4, 2,2,2,2,10};//第五⾸:字母歌uchar code gepu5[]={1,0,1,5,0,5,6,0,6,5, 4,0,4,3,0,3,2,0,2,1, 5,0,5,4,3,0,3,2, 5,0,5,4,0,4,3,0,3,2, 1,3,5,6,8,5, 4,0,4,3,0,3,2,0,2,1,0};//55 uchar code time5[]={2,0,2,2,0,2,2,0,2,4, 2,0,2,2,0,2,2,0,2,4, 2,0,2,4,2,0,2,4, 2,0,2,2,0,2,2,0,2,4, 2,2,4,2,2,4, 2,0,2,2,0,2,2,0,2,4,1}; uchar i,s;sbit led1=P3^3;sbit led2=P3^4;sbit led3=P3^5;sbit led4=P3^6;sbit led5=P3^7;sbit buzzer=P1^0;sbit key1=P2^0;//开始停⽌sbit key2=P2^1;//上⼀⾸sbit key3=P2^2;//下⼀⾸void delay(uchar t);void delayms(uchar t);void main(){uchar flag=0;uchar k=5;uchar stop;TMOD=0X01;EA=1;ET0=1;while(1){while(key2==0 || key3==0 || flag==0) {flag=1;if(key3==0){k++;delay(5);if(k>5)k=k%5;}if(key2==0){k--;delay(5);if(k==0)k=5;}switch(k%5){case 0:{P3=0x7f;s=30;for(i=0;i<s;i++){gepu[i]=gepu1[i];time[i]=time1[i];}break;}case 1:{P3=0xbf;s=36;for(i=0;i<s;i++){gepu[i]=gepu2[i];time[i]=time2[i];}break;}case 2:{P3=0xdf;s=41;for(i=0;i<s;i++){gepu[i]=gepu3[i];time[i]=time3[i];}break;}case 3:{P3=0xef;s=34;for(i=0;i<s;i++){gepu[i]=gepu4[i];time[i]=time4[i];}break;}case 4:{s=55;for(i=0;i<s;i++){gepu[i]=gepu5[i];time[i]=time5[i];}break;}}}while(key2!=0 && key3!=0){for(i=0;i<s;i++){TR0=1;a=gepu[i];///////////////////////////P0=(~gepu[i]);////////////////////////////if(time[i]>0)delay(time[i]);elsedelayms(1);if(key1==0){delay(5);stop++;}if(stop%2==0)TR0=1;elseTR0=0;while(stop%2){if(key1==0){delay(5);stop++;}}if(key2==0 || key3==0){TR0=0;break;}}}}}void dingshi0() interrupt 1 using 0 {if(a<15){TH0=gao[a+6];TL0=di[a+6];}else{TH0=gao[a-14-1];TL0=di[a-14-1];}buzzer=0;elsebuzzer=~buzzer;}void delay(uchar t) {uint i;while(t--){for(i = 0; i < 10001; i++); }}void delayms(uchar t) {uchar t1; uint t2;for(t1=0;t1<t;t1++){for(t2=0;t2<800;t2++) {;}}}。
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 }}。
51单片机入门例程之一个按键选择播放六首音乐
changedata(songdata,diaodata,jiedata); //解释音乐符号串
TR0=1;
for(i=0;diaodata[i]!=0;i++) //逐个符号演奏
{
tl0_f=freq[diaodata[i]*2]; //取出对应的定时值送给T0
0x4C,0xF6,//00370HZ ,6
0xD7,0xF6,//00392HZ ,6#
0x5A,0xF7,//00415HZ ,7
0xD8,0xF7,//00440HZ 1 //12
0x4D,0xF8,//00466HZ 1# //13
0xBD,0xF8,//00494HZ 2 //14
break;
case 2:
play(mamahao); //播放音乐
break;
case 3:
play(boluose 4:
play(xingxing); //播放音乐
break;
uchar th0_f; //在中断中装载的T0的值高8位
uchar tl0_f; //在中断中装载的T0的值低8位
//T0的值,及输出频率对照表
uchar code freq[36*2]={
0xA9,0xEF,//00220HZ ,1 //0
0x93,0xF0,//00233HZ ,1#
",7--,5_|1.3_5.1_|,7.3_5 5_|"
"6.7_`1.6_|6_5_5-3_2_|1.1_13_2_|"
音乐盒单片机程序
0x81,0x81,0x61,0x62,0x61,0x62,0xB1,0xA2,0x93, //不断拨弄离人的眼泪 0x92,0xA1,0xB1,0xA2,0x91,0x91,0x82,0x61,0x51,0x62,0x81,0 x84, //那样浓烈的爱再也无法给 0x52,0x42,0x41,0x42,0x41,0x22,0x22,0x44,0x66,0x62,0x56, //伤感一夜一夜 0x62,0x91,0x81,0x81,0x61,0x61,0x51,0x61,0x81,0x91,0x81,0 x61,0x81, //当记忆的线缠绕过往支离破碎 0x02,0x62,0xB1,0xB1,0xB1,0xB1,0x92,0x82,0x64, //是慌乱占据了心扉 0x61,0xB1,0xB1,0xB1,0x91,0xC1,0xB2, //有花儿伴着蝴蝶 0x81,0x81,0x81,0x81,0x61,0x91,0x83, //孤雁可以双飞 0x51,0x41,0x51,0x41,0x52,0x41,0x51,0x56, //夜深人静独徘徊 0x62,0x91,0x81,0x81,0x61,0x61,0x51,0x61,0x81,0x91,0x81,0 x81,0x81,0x82, //当幸福恋人寄来红色分享喜悦 0x61,0x61,0xB1,0xB1,0xB1,0x91,0xC1,0xB1,0x91,0x61,0x86, //闭上双眼难过头也不敢回 0x62,0x62,0xB1,0xB1,0xB1,0xB1,0xC1,0xB1,0x91,0x91,0x91,0 x81,0x61,0x81,0x64, //仍然捡尽寒枝不肯安歇微带着后悔 0x51,0x61,0x81,0x61,0x51,0x41,0x21,0x51,0x46,0x0a //寂寞沙洲我该思念谁 }; uchar code chuzhi[]={ 0xff,0xff,
单片机音乐播放
正好做了一个音乐程序,贡献出来给你吧,希望你能满意。
电路连接很简单,在P3.0端口接个蜂鸣器,不过效果一般。
如果想效果好点,就加个放大电路,接个小功率喇叭就行。
因为这里发电路不方便,所以如果需要放大电路,可加我QQ:7468485。
音乐程序的设计原理和程序如下:设计原理⑴总体原理:乐曲中不同的音符,实质就是不同频率的声音。
通过单片机产生不同的频率的脉冲信号,经过放大电路,由蜂鸣器放出,就产生了美妙和谐的乐曲。
⑵单片机产生不同频率脉冲信号的原理:1)要产生音频脉冲,只要算出某一音频的脉冲(1/频率),然后将此周期除以2,即为半周期的时间,利用定时器计时这个半周期的时间,每当计时到后就将输出脉冲的I/O反相,然后重复计时此半周期的时间再对I/O反相,就可以在I/O脚上得到此频率的脉冲。
2)利用8051的内部定时器使其工作在计数器模式MODE1下,改变计数值TH0及TL0以产生不同频率的方法如下:例如,频率为523Hz,其周期天/523 S=1912uS,因此只要令计数器计时956uS/1us=956,在每计数956次时就将I/O反接,就可得到中音DO(532Hz)。
计数脉冲值与频率的关系公式如下:N=Fi/2/Fr(N:计数值,Fi:内部计时一次为1uS,故其频率为1MHz,Fr:要产生的频率)⑶其计数值的求法如下: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=65059⑷C调个音符频率与计数值T的对照表如下表所示:表9.1 C调各音符频率与计数值T的对照表音符频率(Hz)简谱码T值音符频率(Hz)简谱码T值低1DO 262 63628 #4FA# 740 64860#1DO# 277 63731 中5SO 784 64898低2RE 294 63835 #5SO# 831 64923#2RE# 311 63928 中6LA 880 64968低3M 330 64103 #6 932 64994低4FA 349 64103 中7SI 988 65030#4FA# 370 64260 高1DO 1046 65058低5SO 392 64260 #1DO# 1109 65085#5SO# 415 64331 高2RE 1175 65110低6LA 440 64400 #2RE# 1245 65124#6 466 64463 高3M 1318 65157低7SI 494 64524 高4FA 1397 65178中1DO 523 64580 #4FA# 1480 65198⑸每个音符使用1个字节,字节的高4位代表音符的高低,低4位代表音符的节拍,下表为节拍码的对照。
单片机音乐播放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);//键盘最后组合码值
单片机音乐播放C语言程序
单片机音乐播放C语言程序#include ;#define uchar unsigned char#define uint unsinged intsbit speaker = P1^7;uchar t0h,t0l,time;//--------------------------------------//单片机晶振采用11.0592MHz// 频率-半周期数据表高八位本软件共保存了四个八度的28个频率数据uchar code FREQH[] = {0x01, //0的时候没有音符 0xF2, 0xF3, 0xF5, 0xF5, 0xF6, 0xF7, 0xF8, //低音12345670xF9, 0xF9, 0xFA, 0xFA, 0xFB, 0xFB, 0xFC,0xFC,//1,2,3,4,5,6,7,i0xFC, 0xFD, 0xFD, 0xFD, 0xFD, 0xFE, //高音2345670xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFF}; //超高音 1234567//-----------------------------------------// 频率-半周期数据表低八位uchar code FREQL[] = {0x01, //0的时候没有音符 0x42, 0xC1, 0x17, 0xB6, 0xD0, 0xD1, 0xB6, //低音12345670x21, 0xE1, 0x8C, 0xD8, 0x68, 0xE9, 0x5B, 0x8F, //1,2,3,4,5,6,7,i0xEE, 0x44, 0x6B, 0xB4, 0xF4, 0x2D, //高音2345670x47, 0x77, 0xA2, 0xB6, 0xDA, 0xFA, 0x16}; //超高音 1234567//--------------------------------------//一个音符有三个数字。
单片机实现音乐
******************************************************************************* *************************** ** 单片机音乐编程方法(一) * * ******************************************************************************** **************************///摘抄自/ 源程序没有试过!原理就是利用单片机的引脚发出某一定频率的信号驱动外部发声设备发出声音,或者放出美妙的音乐.参数两个:前一个按照曲谱对照编码表给定,第二个,为节拍数,8->1拍,4->半拍.************************曲谱编码,这是定义的曲谱表,对应如下******************************// 1 低音DO 2 低音RE 3 低音ME 4 低音FA 5 低音SO 6 低音LA 7 低音SI mustab: db 'h','j', 0f8h,8bh, 0f9h,5bh, 0fah,14h, 0fah,0ch, 0fbh,03h, 0fbh,8fh, 0fch,0bh// 8 中音DO 9 中音RE 0ah 中音ME 0bh 中音FA 0ch 中音SO 0dh 中音LA 0eh 中音SI db 0fch,43h, 0fch,0abh, 0fdh,08h, 0fdh,33h, 0fdh,81h, 0fdh,0c7h, 0feh,05h// 0fh 高音DO 10h 高音RE 11h 高音ME 12h 高音FA 13h 高音SO 14h 高音LA 15h 高音SIdb 0feh,21h, 0feh,55h, 0feh,84h, 0fdh,99h, 0feh,0c0h, 0feh,0e3h, 0ffh,02h// 以下为音乐乐谱代码,前两位表示曲谱,后一位表示节拍// 8为1 拍,4 ->半拍mu_tab: db 6,4,8,4,10,4,1,4, 9,8,8,4,7,4,10,8,9,8,6,16db 8,4,10,4,12,4,12,4, 13,8,12,4,11,4,10,16db 00h//以下是//c语言//*******************************************// 音乐编程//通过蜂鸣器发出讯响//只要通过修改后面的音乐程序里面的码//表就可以自己谱曲唱歌!//前一个参数为按照编码表给定,第二个参数为节拍数#i nclude <80d51.h>#define uchar unsigned char#define beep P3_7 //蜂鸣器定义uchar timer_h;uchar timer_l;//**************************************************************************** void reset();void delay(uchar times);void dems(); //休止符延时void display(uchar disseg,uchar disdata);uchar keyb();// 这里定义音乐音符表code uchar musictabl[]={0xf0,0xf0,0xf8,0x8b,0xf9,0x5b,0xfa,0x14,0xfa,0x0c,0xfb,0x03,0xfb,0x8f,0xfc,0x0b,0xfc,0x43,0xfc,0xab,0xfd,0x08,0xfd,0x33,0xfd,0x81,0xfd,0xc7,0xfe,0x05,0xfe,0x32,0xfe,0x55,0xfe,0x84,0xfe,0x99,0xfe,0xc0,0xfe,0xe3,0xff,0x02};//这里定义音乐曲谱表格code uchar musicdata[]={6,4, 8,4,10,4, 1,4, 9,8, 8,4, 7,4,10,8,9,8,6,16,8,4,10,4,12,4,12,4,13,8,12,4,11,4,10,16,0};// *************************************************************************** void ClockIrqHandler (void) interrupt 1 using 3; //定义定时器T0中断服务程序//下面是定时器T0的中断服务程序void ClockIrqHandler (void) interrupt 1 using 3 {TR0 = 0; //关闭定时器TL0 = timer_l;TH0 = timer_h;beep = ~beep;TR0 = 1; //打开定时器}// ***************** 定时器程序 **********************************uchar keybuf;void main()uchar counts,mustmp,tmp,keym = 0; //键盘返回结果的缓冲区reset(); //调用初始化程序while(1) //设置一个无限制循环{for(counts=0;musicdata[counts]!=0;counts+=2){mustmp = musicdata[counts];TR0 = 0;tmp = musictabl[mustmp];timer_h = tmp;tmp = musictabl[mustmp+1];timer_l = tmp;TR0 = 1;display(0,musicdata[counts+1]);delay(musicdata[counts+1]);TR0 = 0;dems();}}}void reset(){keybuf = 0;P2 = 255; ////********* 上面是一些基础定义**************TL0 = 0xff; //初值TH0 = 0xff; //初值ET0=1; // 允许定时器T0中断EA =1; // 中断总允许}//***************************************************************************** //下面是延时程序.具体的延时时间不能通过表面程序看出,(为什么?)//如果我们需要一个精密的延时程序,那么我们可以采用内嵌汇编代码的方式void delay(uchar times)int t=24000; //延时倍数uchar i=times;for(;i!=0;i--){for(;t!=0;t--){}}}void dems() //休止符延时{_asmmov R0,#0llp: mov R1,#100llk: djnz R1,llkdjnz R0,llp_endasm;}uchar keyb(){uchar key,keytmp;key1 = 1; //将输出线拉高key2 = 1;key3 = 1;key = P3 ; //读回来key = key & 0xf0; //获得键盘结果if(key == 112 ) return 0; //如果用户没有按键返回0else{keytmp = key;delay(1); //判断是不是干扰key = P3 & 0xf0;if (key != keytmp ) return 0; //是干扰,返回0else //不是干扰,等待用户释放按键{do{key1 = 1; //输出拉高key2 = 1;key3 = 1;key = P3 & 0xf0; //读回来P1_0 = ~P1_0; //如果用户不释放,闪烁p1.0}while(key != 112 ); //等待用户释放P1_0 = 1; //用户释放以后,清除p1.0指示灯switch(keytmp){case 96: return 3;//返回用户按键结果case 80: return 2;case 48: return 1;}}}}。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
movc a,@a+dptr ;至table取相对的低位字节计数值
mov tl0,a ;取到的低位字节存入tl0
jnb p3.4,$ ;放开否?
inc 31h ;计次地址(31h)加1
mov 30h,#low s学林电子有限公司 All Rights Reserved *
;* 【声明】 此程序仅用于学习与参考,引用请注明版权和作者信息! *
;********************************************************************************
jmp next
end0: clr tr0 ;停止计数器
mov a,31h ;载入计次指针
xrl a,#00h ;是否按第1次?
org 00h ;主程序起始地址
jmp start ;跳动主程序
org 0bh ;timer0中断起始地址
inc 31h ;计次地址(31h)加1
mov 30h,#low song2 ;第3首歌指针
jmp next
end2:
mov a,31h ;载入计次指针
jnb p3.4,$ ;to放开?
mov 31h,#00 ;按to次数计数指针初值为00h
start0:
mov 30h,#low song ;取简谱码指针(第1首)
;六首唱歌的音乐程序按p3.4循环 ,直到一首歌唱完及停 p3.3口接小喇叭 *
;********************************************************************************
jmp tim0 ;跳至timer0在mode1
org 0080h
start: mov tmod,#00000001b ;设timer0在mode1
jnz end4 ;不是则跳至end4
jb p3.4,$ ;按第5次?
call delay1 ;消除抖动
mov a,r2
swap a
anl a,#0fh ;取音频值(高4位)
jnz sing ;是否为0,是0则不发音
jmp next
end1:
mov a,31h ;载入计次指针
xrl a,#01h ;是否按第2次?
jnz end2 ;不是则跳至end2
mov 21h,a ;取到的高位字节存入(21h)
mov a,22h ;再载入取到的音符码
rl a ;乘2
call delay1 ;消除抖动
jnb p3.4,$ ;放开否?
inc 31h ;计次地址(31h)加1
next: mov a,30h
mov dptr,#table
movc a,@a+dptr ;至相关页码,高4位为音符的高低
mov r2,a ;低4位为音符的节拍
clr tr0
jmp d1
sing: dec a ;因0不列入
mov 22h,a ;存入(22h)
jnb p3.4,$ ;放开否?
inc 31h ;计次地址(31h)加1
mov 30h,#low song4 ;第5首歌指针
xrl a,#02h ;是否按第3次?
jnz end3 ;不是则跳至end3
jb p3.4,$ ;按第4次?
rl a ;乘2
mov dptr,#table
movc a,@a+dptr ;至table取码,取t的值
mov th0,a ;取到的高位字节存入th0
jb p3.4,$ ;按第3次?
call delay1 ;消除抖动
jnb p3.4,$ ;放开否?
;********************************************************************************
;* 硬件:此程序在本公司xlisp系列单片机实验仪运行通过 *
;********************************************************************************
jnz end1 ;不是则跳至end1
jb p3.4,$ ;按第2次?
call delay1 ;消除抖动
;* 作者: 深圳学林电子有限公司 专业8051单片机教学站 *
;* 邮箱: sxj1974@ *
;* 网站: 更多例程请登陆网站 *
mov 20h,a ;取到的低位字节存入(20h)
setb tr0 ;启动timer0
d1: call delay
inc 30h ;取简谱码指针加1
mov ie,#10000010b ;中断使能
jb p3.4,$ ;第一次按to?
call delay1 ;消除抖动
mov 30h,#low song3 ;第4首歌指针
jmp next
end3:
mov a,31h ;载入计次指针
xrl a,#03h ;是否按第4次?
jz end0 ;检查简谱码是否已结束(有无00?)
anl a,#0fh ;取节拍(低4位)
mov r5,a ;存入r5,节拍的时间
;* 日期: 2007-3-25 *
;* 版本: 3.0 *