#include<reg51.h>
#include<math.h>
#define u8 unsigned char
#define u16 unsigned int
#define u32 unsigned long
sbit led0=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P14=P1^4;
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7;
#define _debug 1
code u16 fout[7]={262,294,330,349,392,440,494};
u16 _f=262;
u8 bufTL0,bufTH0;
void interrupt0 (void) interrupt 0 using 0
{
static u8 cnt=0;
u8 bai=0,ge=0,shi=0;
if(P11==0)
cnt=0;
else if(P12==0)
cnt=1;
else if(P13==0)
cnt=2;
else if(P14==0)
cnt=3;
else if(P15==0)
cnt=4;
else if(P16==0)
cnt=5;
else if(P17==0)
cnt=6;
_f=fout[cnt];
bai=_f/100;
shi=_f/10%10;
ge=_f%10;
P3=(bai<<4)+0x0f;
P2=shi;
P2<<=4;
P2+=ge;
bufTL0=(65536-1000000*1/_f/1/2)%256;
bufTH0=(65536-1000000*1/_f/1/2)/256;
}
void timer0(void) interrupt 1 using 0
{
TL0=bufTL0;
TH0=bufTH0;
led0=!led0;
}
void main(void)
{
u8 bai=0,ge=0,shi=0;
bai=_f/100;
shi=_f/10%10;
ge=_f%10;
P3=(bai<<4)+0x0f;
P2=shi;
P2<<=4;
P2+=ge;
EA=1;
EX0=1;
IT0=1;
ET0=1;
TMOD=0X01;
bufTL0=(65536-1000000*1/_f/1/2)%256;
bufTH0=(65536-1000000*1/_f/1/2)/256;
TL0=bufTL0;
TH0=bufTH0;
TR0=1;
while(1);
}
沒有用矩陣鍵盤,用了個七個管腳的與門,實現 |