想通过按键K1,K2控制播放两首歌,不知道为什么只能播第一首,小白一只,请多指教
#include <REG52.H> #include <INTRINS.H>
sbit Beep = P1^5; volatile bit Flag=0; sbit k1=P3^1; sbit k2=P3^0;
unsigned char n=0; //n为节拍常数变量 、 unsigned char code music_tab1[] ={ 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 }; unsigned char code music_tab2[] ={ 0x1c,0x30,0x19,0x30,//格式为: 频率常数, 节拍常数, 频率常数, 节拍常数, 0x18,0x40,0x19,0x30, 0x18,0x30,0x13,0x30, 0x19,0x80,0x26,0x30, 0x26,0x30,0x1c,0x40, 0x20,0x30,0x1c,0x30, 0x18,0x30,0x20,0x60, 0x26,0x30,0x26,0x30, 0x24,0x40,0x26,0x30, 0x24,0x30,0x18,0x40, 0x26,0x60,0x18,0x20, 0x18,0x20,0x18,0x20, 0x19,0x40,0x24,0x30, 0x24,0x30,0x19,0x30, 0x19,0x60,0x1c,0x30, 0x19,0x30,0x18,0x40, 0x19,0x30,0x18,0x30, 0x13,0x30,0x19,0x60, 0x26,0x30,0x26,0x30, 0x1c,0x40,0x20,0x30, 0x1c,0x30,0x18,0x30, 0x20,0x80,0x26,0x30, 0x24,0x30,0x24,0x30, 0x18,0x30,0x19,0x20, 0x19,0x30,0x18,0x30, 0x15,0x20,0x15,0x20, 0x13,0x20,0x18,0x40, 0x18,0x30,0x19,0x30, 0x1c,0x20,0x1c,0x20, 0x19,0x30,0x20,0x30, 0x1c,0x60,0x18,0x30, 0x15,0x30,0x13,0x40, 0x15,0x30,0x13,0x30, 0x10,0x30,0x15,0x60, 0x20,0x30,0x20,0x30, 0x18,0x30,0x19,0x30, 0x18,0x30,0x13,0x30, 0x13,0x60,0x1c,0x20, 0x19,0x20,0x18,0x30, 0x19,0x30,0x15,0x20, 0x15,0x20,0x18,0x40, 0x20,0x30,0x20,0x30, 0x12,0x30,0x13,0x30, 0x15,0x30,0x18,0x30, 0x13,0xc0,0x13,0x80, 0x13,0x30,0x0e,0x80, 0x10,0x30,0x10,0x30, 0x13,0x20,0x15,0x20, 0x18,0x40,0x18,0x30, 0x15,0x30,0x18,0x20, 0x15,0x20,0x15,0x30, 0x10,0x30,0x13,0x60, 0x13,0x30,0x0e,0x60, 0x10,0x60,0x13,0x20, 0x15,0x20,0x18,0x40, 0x18,0x30,0x15,0x30, 0x15,0x20,0x18,0x20, 0x15,0x30,0x19,0x30, 0x1c,0x60,0x1c,0x30, 0x19,0x30, 0x1c,0xc0, 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;
if(k1==0&&Flag==0) Flag=1; while(k1==0&&Flag==1) { play: while(Flag) { a: p=music_tab1; 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_tab1[i++], n=music_tab1[i++];} //取频率常数 和 节拍常数 TR0=1; //开定时器0 while(n!=0) Beep=~Beep,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!) TR0=0; //关定时器0 if(k1==0&&Flag==1) { Flag=0; break; } }
if(k2==0&&Flag==0) Flag=1; while(k2==0&&Flag==1) { play2: while(Flag) { b: p=music_tab2; if(p==0x00) { i=0, delayms(1000); goto play2;} //如果碰到结束符,延时1秒,回到开始再来一遍 else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto b;} //若碰到休止符,延时100ms,继续取下一音符 else {m=music_tab2[i++], n=music_tab2[i++];} //取频率常数 和 节拍常数 TR0=1; //开定时器0 while(n!=0) Beep=~Beep,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!) TR0=0; //关定时器0 if(k2==0&&Flag==1) { Flag=0; break; } } }
} }
|