想通过按键K1,K2控制播放两首歌,不知道为什么只能播第一首,小白一只,请多指教
#include
#include
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;
}
}
}
}
}
你的程序,按k2都没有反应。
太乱了,各种花括号,你可能都不知道应该谁和谁配对。
我修改了一下,按k2有反应了。
但只能按一次,按k2,只能唱第二首,不停。
原因是你的那个break。
break只能跳一级循环,不能跳到最外去,所以,按了一个键后,就在循环里出不去了。
以下是两个按键都有反应的程序,但只能唱一首。(你的歌,第一首八月桂花、第二首叫不出名字)
#include
#include
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;
}
}
}
}
用外部中断吧,就是你板子上的k3、k4,用中断,按键随时都可以打断循环程序,设置为新的播放。
我的程序,用一个k4,随时中断本首歌,放下一首,无论多少歌都可以的。