#include <REGX51.H>
#include <led1602.h>
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
unsigned char temp,flagplay;
unsigned char key=0,clr=0;
unsigned char i,j,t=0;
unsigned char STH0;
unsigned char STL0;
unsigned int code tab[]={0,0 ,65110,65058,
65030,64968,64898,64820,
64777,64684,64580,64524,
64400,64260,64103,64021};
unsigned int tabekey[15]; ////键记忆
unsigned int tabetimer[15]; ///时间记忆
unsigned int stoptimer[15];
unsigned int tcont,toper=0,keycont=0;
////////////////////////////////////////
void initTimer(void)
{
TMOD=0x10;
TH0=0xfc;
TL0=0x18;
}
/////////////////////////////////////////
void main(void)
{
init();
delay(200);
writestring(0,0,"tangchao 07_03");
initTimer();
ET0=1;
ET1=1;
TR0=1;
EA=1;
while(1)
{
P2=0xff;
P2_4=0;
temp=P2;
temp=temp & 0x0f;
if (temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P2;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P2;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
key=0;
writestring(1,1,"do");
break;
case 0x0d:
key=1;
writestring(1,1,"re");
break;
case 0x0b:
key=2;
writestring(1,1,"me");
break;
case 0x07:
key=3;
writestring(1,1,"fa");
break;
}
temp=P2;
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
tabetimer[keycont]=tcont; ///开始按键时间
TR1=1;
temp=temp & 0x0f;
while(temp!=0x0f)
{
temp=P2;
temp=temp & 0x0f;
}
TR1=0; writestring(1,1," ");
tabekey[keycont]=tab[key];
stoptimer[keycont]=tcont; //结束按键时间
keycont++;
}
}
P2=0xff;
P2_5=0;
temp=P2;
temp=temp &0x0f;
if (temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P2;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P2;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
key=4;
writestring(1,1,"so");
break;
case 0x0d:
key=5;
writestring(1,1,"la");
break;
case 0x0b:
key=6;
writestring(1,1,"si");
break;
case 0x07:
key=7;
break;
}
temp=P2;
if(key!=7)
{
P0=table[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
TR1=1;
tabetimer[keycont]=tcont; ///开始按键时间
tabekey[keycont]=tab[key];
}
temp=temp & 0x0f;
while(temp!=0x0f)
{
temp=P2;
temp=temp & 0x0f;
}
TR1=0; //////////按键放开音乐停止
if(flagplay==0&&key!=7)
{
stoptimer[keycont]=tcont; //结束按键时间
writestring(1,1," ");
keycont++;
}
if(key==7&&flagplay==1) ////////////开始播放
{
writestring(5,1,"play");
TR0=1;
}
if(key==7&&flagplay!=1) ///////////////////停止记录按键
{
TR0=0;
TR1=0;
t=0;
// keycont=0;
tcont=0;
flagplay=1; ///用定时器播放音乐
writestring(5,1,"stop");
}
}
}
P2=0xff;
P2_6=0;
temp=P2;
temp=temp & 0x0f;
if (temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P2;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P2;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
key=8;
writestring(1,1,"re");
break;
case 0x0d:
key=9;
writestring(1,1,"me");
break;
case 0x0b:
key=10;
writestring(1,1,"fa");
break;
case 0x07:
key=11;
writestring(5,1,"clr ");
break;
}
temp=P2;
P0=table[key];
tabekey[keycont]=tab[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
if(key==11)
{
TR1=0;
for(clr=0;clr<15;clr++)
{
tabetimer[clr]=0; tabekey[clr]=0; ///清除所存的曲子
keycont=0;
}
}
else TR1=1;
tabetimer[keycont]=tcont; ///开始按键时间
temp=temp & 0x0f;
while(temp!=0x0f)
{
temp=P2;
temp=temp & 0x0f;
}
TR1=0; writestring(1,1," ");
stoptimer[keycont]=tcont; //结束按键时间
keycont++;
}
}
P2=0xff;
P2_7=0;
temp=P2;
temp=temp & 0x0f;
if (temp!=0x0f)
{
for(i=50;i>0;i--)
for(j=200;j>0;j--);
temp=P2;
temp=temp & 0x0f;
if (temp!=0x0f)
{
temp=P2;
temp=temp & 0x0f;
switch(temp)
{
case 0x0e:
key=12;
writestring(1,1,"fa");
break;
case 0x0d:
key=13;
writestring(1,1,"me");
break;
case 0x0b:
key=14;
writestring(1,1,"re");
break;
case 0x07:
key=15;
writestring(1,1,"do");
break;
}
temp=P2;
P1_0=~P1_0;
P0=table[key];
tabekey[keycont]=tab[key];
STH0=tab[key]/256;
STL0=tab[key]%256;
tabetimer[keycont]=tcont; ///开始按键时间
TR1=1;
temp=temp & 0x0f;
while(temp!=0x0f)
{
temp=P2;
temp=temp & 0x0f;
}
TR1=0; writestring(1,1," ");
stoptimer[keycont]=tcont; //结束按键时间
keycont++;
}
}
}
}
//////////////////////////////////////////////////
void timer0(void) interrupt 1
{
TH0=0xfc;
TL0=0x18;
toper++;
//if(tcont==1000) P0=0x00;
if(toper==5)
{
toper=0; P3_7=~P3_7;// flagplay=~flagplay;
tcont++;
if(tcont==65535