#include<reg51.h>
sbit P14=P1^4;
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7;
unsigned char keyval;
sbit sound =P3^7;
unsigned int C;
unsigned int f;
#define l_dao 262
#define l_re 286
#define l_mi 311
#define l_fa 349
#define l_sao 392
#define l_la 440
#define l_xi 494
#define dao 523
#define re 587
#define mi 659
#define fa 698
#define sao 784
#define la 880
#define xi 987
#define h_dao 1046
#define h_re 1174
#define h_mi 1318
#define h_fa 1396
#define h_sao 1567
#define h_la 1760
#define h_xi 1975
void delay20ms(void)
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<60;j++);
}
void delay()
{
unsigned char i,j;
for(i=0;i<250;i++)
for(j=0;j<250;j++);
}
void output_sound(void)
{
C=(46083/f)*10;
TH0=(8192-C)/32;
TL0=(8192-C)%32;
TR0=1;
delay();
TR0=0;
sound=1;
keyval=0xff;
}
void main(void)
{
EA= 1;
ET0=1;
ET1=1;
TR1=1;
TMOD=0x10;
TH1=(65536-500)/256;
TL1=(65536-500)%256;
while(1)
{
switch(keyval)
{
case 1:f=dao;
output_sound();
break;
case 2:f=l_xi;
output_sound();
break;
case 3:f=l_la;
output_sound();
break;
case 4:f=l_sao;
output_sound();
break;
case 5:f=sao;
output_sound();
break;
case 6:f=fa;
output_sound();
break;
case 7:f=mi;
output_sound();
break;
case 8:f=re;
output_sound();
break;
case 9:f=h_re;
output_sound();
break;
case 10:f=h_dao;
output_sound();
break;
case 11:f=xi;
output_sound();
break;
case 12:f=la;
output_sound();
break;
case 13:f=h_la;
output_sound();
break;
case 14:f=h_sao;
output_sound();
break;
case 15:f=h_fa;
output_sound();
break;
case 16:f=h_mi;
output_sound();
break;
}
}
}
void timer0_serve(void)interrupt 1 using 1
{
TH0=(8192-C)/32;
TL0=(8192-C)%32;
sound=!sound;
}
void timer1_serve(void)interrupt 3 using 2
{
TR1=0;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{delay20ms();
if((P1&0xf0)!=0xf0)
{
P1=0xfe;
if(P14==0)
keyval=1;
if(P15==0)
keyval=2;
if(P16==0)
keyval=3;
if(P17==0)
keyval=4;
P1=0xfd;
if(P14==0)
keyval=5;
if(P15==0)
keyval=6;
if(P16==0)
keyval=7;
if(P17==0)
keyval=8;
P1=0xfb;
if(P14==0)
keyval=9;
if(P15==0)
keyval=10;
if(P16==0)
keyval=11;
if(P17==0)
keyval=12;
P1=0xf7;
if(P14==0)
keyval=13;
if(P15==0)
keyval=14;
if(P16==0)
keyval=15;
if(P17==0)
keyval=16;
}
}
TR1=1;
TH1=(65536-500)/256;
TL1=(65536-500)%256;
}
|