#include<reg51.h>
#include<INTRINS.H>
sbit spk=P2^0;
unsigned char i,a;
unsigned char m_tone,m_time;
unsigned char code seg[]=
{
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e
};
unsigned int code Tone[]=
{
64103,64260,64400,64524,64580,64671,64777,64820,64898,64968,65030,65058,65110,65175,65178,65217
};
unsigned char code music1[]=
{
6,2,6,2,6,4,6,2,6,2,6,4,
6,2,8,2,4,3,5,1,6,8,
7,2,7,2,7,3,7,1,7,2,6,2,6,2,6,1,6,1,
6,2,5,2,5,2,4,2,5,4,8,4,
6,2,6,2,6,4,6,2,6,2,6,4,
6,2,8,2,4,3,5,1,6,8,
7,2,7,2,7,3,7,1,7,2,6,2,6,2,6,1,6,1,
8,2,8,2,7,2,5,1,4,6,
0xff
};
unsigned char code music2[]=
{
1,2,1,2,2,4,1,4,4,4,3,8,
1,2,1,2,2,4,1,4,5,4,4,8,
1,2,1,2,8,4,6,4,4,4,3,4,2,1,
7,2,7,2,6,4,4,4,5,4,4,8,
0xff
};
unsigned char code music3[]=
{
4,4,5,4,6,4,4,4,
4,4,5,4,6,4,4,4,
6,4,7,4,8,8,
6,4,7,4,8,8,
8,3,9,1,8,3,7,1,6,4,4,4,
8,3,9,1,8,3,7,1,6,4,4,4,
4,4,1,4,4,8,
4,4,1,4,4,8,
0xff
};
unsigned char code music4[]=
{
4,2,5,2,6,2,7,2,8,4,6,2,4,2,11,4,9,2,7,2,8,2,8,2,6,4,
4,2,5,2,6,2,7,2,8,2,6,2,5,2,4,2,5,4,6,4,5,4,8,4,
4,2,5,2,6,2,7,2,8,4,6,2,4,2,11,4,9,2,7,2,8,4,6,4,
4,2,5,2,6,2,7,2,8,2,6,2,5,2,4,2,5,4,6,4,4,4,4,4,
11,4,9,2,7,2,8,2,8,2,4,4,
11,4,9,2,7,2,8,4,6,4,
4,2,5,2,6,2,7,2,8,2,6,2,5,2,4,2,5,4,6,4,4,4,4,4,
0xff
};
unsigned char code music5[]=
{
8,2,6,2,8,2,6,2,8,2,6,2,4,4,
5,2,7,2,6,2,5,2,8,8,
8,2,6,2,8,2,6,2,8,2,6,2,4,4,
5,2,7,2,6,2,5,2,4,8,
5,2,5,2,7,2,7,2,6,2,4,2,8,4,
5,2,7,2,6,2,5,2,8,8,
8,2,6,2,8,2,6,2,8,2,6,2,4,4,
5,2,7,2,6,2,5,2,4,4,
0xff
};
unsigned char code music6[]=
{
4,4,4,4,8,4,8,4,9,4,9,4,8,8,
7,4,7,4,6,4,6,2,6,2,5,4,5,4,4,8,
8,4,8,4,7,4,7,4,6,4,6,4,5,8,
8,4,8,4,7,4,7,4,6,4,6,4,5,8,
4,4,4,4,8,4,8,4,9,4,9,4,8,8,
7,4,7,4,6,4,6,2,6,2,5,4,5,4,4,8,
0xff
};
unsigned char code music7[]=
{
8,4,8,2,9,2,8,8,
11,4,11,2,9,2,8,8,
9,4,9,2,11,2,9,4,6,2,
8,4,8,2,6,2,5,8,
6,4,6,2,8,1,6,2,5,2,4,4,9,4,11,2,11,1,9,2,8,2,6,4,
4,2,4,2,4,2,5,2,
6,2,6,2,6,2,8,2,
11,4,9,2,8,4,6,2,5,4,4,4,
0xff
};
unsigned char code music8[]=
{
6,2,8,2,11,2,9,2,8,8,
6,2,8,2,9,2,11,2,8,8,
11,4,13,2,12,4,11,4,12,2,11,2,9,2,11,2,8,8,
6,4,8,2,9,4,8,2,9,2,11,4,12,2,8,2,9,8,
6,2,5,2,4,4,5,8,
5,4,5,2,6,2,8,4,8,4,11,4,5,2,6,2,8,8,
6,2,8,2,11,2,9,2,8,8,
6,2,8,2,9,2,11,2,8,8,
11,4,13,2,12,4,11,4,12,2,11,2,9,2,11,2,8,8,
6,4,8,2,9,4,8,2,9,2,11,4,12,2,8,2,9,8,
6,4,11,2,9,4,8,4,6,2,5,2,4,4,5,8,
5,4,6,2,8,8,11,4,13,2,12,4,11,4,12,2,11,2,8,2,9,2,11,8,
0xff
};
unsigned char code music9[]=
{
8,2,9,2,8,2,9,2,8,2,9,2,8,4,
8,2,11,2,11,2,9,2,8,2,8,2,6,4,
8,2,8,2,6,2,6,2,
8,2,8,2,6,4,
5,2,7,2,6,2,5,2,4,2,4,2,4,4,
0xff
};
unsigned char code music10[]=
{
8,4,6,2,8,2,11,8,
9,4,11,4,8,8,
8,4,4,2,5,2,6,4,5,2,4,2,5,8,
8,4,6,2,8,2,11,4,10,2,9,4,11,4,8,8,
8,4,5,2,6,2,7,4,3,2,4,8,
9,4,11,4,11,8,
10,4,9,2,11,2,11,8,
9,2,10,2,11,2,9,2,9,2,8,2,6,2,4,2,5,8,
8,4,6,2,8,2,11,4,10,2,9,4,11,4,8,8,
6,4,5,2,6,2,7,4,3,2,4,8,
0xff
};
unsigned char code music11[]=
{
8,4,11,2,9,2,8,4,8,4,
6,2,8,2,9,2,11,2,8,4,8,4,
9,4,8,2,6,2,5,4,5,4,
6,4,8,2,6,2,5,2,6,2,4,4,
9,4,8,4,9,4,8,4,6,4,9,4,8,4,8,2,8,2,6,2,5,2,4,8,
4,2,4,2,5,2,6,2,4,8,
0xff
};
void delayMS(unsigned int ms)
{
unsigned char t;
while(ms--)
{
for(t=0;t<120;t++);
}
}
delay(unsigned int b)
{
while(b--);
}
display()
{
unsigned char j,wk=0x01;
unsigned char buf[2];
buf[0]=seg[a/10%10];
buf[1]=seg[a%10];
for(j=0;j<2;j++)
{
P3=wk;
P0=buf[j];
delay(5);
wk=_crol_(wk,1);
P0=0xff;
}
}
void key_press()
{
unsigned char temp;
temp=0xff;
P1=0xf0;
if(P1!=0xf0)
{
delay(100);
if(P1!=0xf0)
{
P1=0xf0;
temp=P1;
P1=0x0f;
temp=temp|P1;
switch(temp)
{
case 0xee:a=0; break;
case 0xde:a=1; break;
case 0xbe:a=2; break;
case 0x7e:a=3; break;
case 0xed:a=4; break;
case 0xdd:a=5; break;
case 0xbd:a=6; break;
case 0x7d:a=7; break;
case 0xeb:a=8; break;
case 0xdb:a=9; break;
case 0xbb:a=10;break;
case 0x7b:a=11; break;
case 0xe7:a=12; break;
case 0xd7:a=13; break;
case 0xb7:a=14; break;
case 0x77:a=15; break;
}
P0=seg[a];
P1=0xf0;
while(P1!=0xf0);
display();
}
}
}
int main()
{
TMOD=0x01;
EA=1;
ET0=1;
P0=seg[a];
while(1)
{
display();
key_press();
switch(a)
{
case 0:ET0=1;display();break;
case 1:display();m_tone=music1[i];m_time=music1[i+1];break;
case 2:m_tone=music2[i];m_time=music2[i+1];break;
case 3:m_tone=music3[i];m_time=music3[i+1];break;
case 4:m_tone=music4[i];m_time=music4[i+1];break;
case 5:m_tone=music5[i];m_time=music5[i+1];break;
case 6:m_tone=music6[i];m_time=music6[i+1];break;
case 7:m_tone=music7[i];m_time=music7[i+1];break;
case 8:m_tone=music8[i];m_time=music8[i+1];break;
case 9:m_tone=music9[i];m_time=music9[i+1];break;
case 10:m_tone=music10[i];m_time=music10[i+1];break;
case 11:m_tone=music11[i];m_time=music11[i+1];break;
}
if(m_tone==0xff)
{
i=0;
delay(2000);
continue;
}
TR0=1;
delayMS(m_time*105);
TR0=0;
i+=2;
delayMS(5);
}
}
void time0() interrupt 1
{
TH0=Tone[m_tone]/256;
TL0=Tone[m_tone]%256;
spk=!spk;
}
|