#include <reg51.h>
#include <intrins.h>
sbit Beep = P1^5 ;
sbit key1=P3^2; //按键
sbit key2=P3^3;
unsigned char flage,n=0;
unsigned char code music_tab[] ={ //音乐代码省略};
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);
}
void main()
{ unsigned char p,m;
unsigned char i=0;
TMOD&=0x0f;
TMOD|=0x01;
TH0=0xd8;TL0=0xef;
IE=0x82;
play:
while(1)
{
if(key1==0) //判断是否有键按下
{
if(key1==0)
flage=1; //标志位置1
}
if(flage==1) //如果flage==1 才唱歌
{
if(key2==0)
{
if(key2==0)
{
while(flage)
{
if(key1==0)
{
if(key1==0)
{
flage=0;
i=0;
}while(!key1);
}
}
}
}
a: p=music_tab[i];
if(p==0x00) { i=0, delayms(1000); goto play;}
else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;}
else {m=music_tab[i++], n=music_tab[i++];}
TR0=1;
while(n!=0) Beep=~Beep,delay(m);
TR0=0;
if(i>96)//唱一遍之后进入
{ flage=0;
i=0;
while(!flage)//判断 是否有键按下
{
if(key1==0)
{
if(key1==0)
{
flage=1;
delay(1);
}
}
}
} }
} }