这个是电子琴加播放音乐的程序。。。 这个程序在播放音乐的过程中 ,,,如果按其他的键 没反应 只能在放完音乐后才能实现弹奏功能,,,,谁能帮我改下使这程序它在播放的过程中按下按键时可以能实现弹奏的功能。。。,,最好能再加一首歌曲上去。。。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
void PlayMusic();
sbit a=P3^2 ;
sbit BEEP = P3^0;
uchar code DSY_Table[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xBF
};
uint code Tone_Delay_Table[]=
{64021,64103,64260,64400,64524,64580,64684,64777,64820,64898,64968,65030,65058,65110,65157,65178};
uchar code SONG_TONE[]=
{
212,212,190,212,159,169,212,212,190,212,142,159,212,212,106,126,129,169,190,119,119,126,159,142,159,0
};
uchar code SONG_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
};
void DelayMS(uint ms)
{
uchar t;
while(ms--)
{
for(t=0;t<120;t++);
}
}
uchar KeyNo;
void Keys_Scan()
{
uchar Tmp,k;
P1 = 0x0f;
DelayMS(2);
Tmp = P1 ^ 0x0f;
switch(Tmp)
{
case 1: k = 0; break;
case 2: k = 1; break;
case 4: k = 2; break;
case 8: k = 3; break;
default: return;
}
P1 = 0xf0;
DelayMS(2);
Tmp = (P1 >> 4) ^ 0x0f;
switch(Tmp)
{
case 1: k+= 0; break;
case 2: k+= 4; break;
case 4: k+= 8; break;
case 8: k+= 12;break;
default: return;
}
KeyNo=k;
}
void Play_Tone() interrupt 1
{
TH0=Tone_Delay_Table[KeyNo]/256;
TL0=Tone_Delay_Table[KeyNo]%256;
BEEP=~BEEP;
}
void main()
{
P0 = 0xBF;
TMOD=0x01;
IE=0x82;
while(1)
{
if(a==0)
PlayMusic();
else
{P1 = 0xf0;
if(P1 != 0xf0)
{ Keys_Scan();
P0 = DSY_Table[KeyNo];
TR0=1;}
else
{
TR0=0 ;
}
DelayMS(2);
}
}
}
void PlayMusic()
{
uint i =0,j,k;
while(SONG_LONG!=0||SONG_TONE!=0)
{
for(j=0;j<SONG_LONG*20;j++)
{
BEEP = ~BEEP;
for(k=0;k<SONG_TONE/3;k++);
}
DelayMS(50);
i++;
}
}
|