void main()
{
while(key3==1);
led1=0;
led2=1;
InitTimer();
while(1)
{
Musics_Play();
if(key1==0)
{
delay1ms(10);
while(key1==0);
music_value++;
if(music_value>7) music_value=1;
}
if(key2==0)
{
delay1ms(10);
while(key2==0);
music_value--;
if(music_value==0) music_value=7;
}
}
}
3.4定时器初始函数的设计
void InitTimer(void)
{
TMOD=0x01;
IT0=1;
ET0=1;
EA=1;
}
3.5延时函数的设计
void delay150ms(void)
{
unsigned char a,b,c;
for(c=3;c>0;c--)
for(b=80;b>0;b--)
{
if(P3!=0xff) {break;}
for(a=214;a>0;a--);
}
}
void Musics_Play()
{
unsigned int j=0,i=0,k=0;
switch(music_value)
{
case 1 :song=song1;
led3=1;
P0=tab[1];
delay(50);
break;
.
.
.
while(*(song+j)!=0xFF&&cycle_flag)
{
beat=*(song+j)&0x0F;
sound=*(song+j)>>4;
TH0=tones[2*sound];
TL0=tones[2*sound+1];
TR0=1;
if ((tones[2*sound]==0xFF)&&(tones[2*sound+1]==0xFF)) TR0=0;
for(i=beat;i>0;i--){delay150ms();}
TR0=0;
if(sound>=8) sound=7;
led1=!led1;
led2=!led2;
j++;
k++;
if(k==7) k=0;
}
}
3.7定时器0中断函数的设计
void Timer0Interrupt(void) interrupt 1
{
TH0=tones[2*sound];
TL0=tones[2*sound+1];
speaker = !speaker;
}
链接:https://pan.baidu.com/s/150lid1U2jKpAtDhnf1whPg 提取码:rtup
|