#include <reg51.h> #include<math.h> #include <stdio.h> #include<stdlib.h>
sbit SA=P0^0; sbit SB=P0^1; sbit SC=P0^2; sbit E3=P0^3; sbit P1_4=P1^4; sbit P1_5=P1^5; sbit P1_6=P1^6; sbit P1_7=P1^7; char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};// 0,1,2,3,4,5,6,7,8,9
unsigned char key,temp,ch[6];
void time_init(void)//定时器0初始化 {
TMOD=0x09; //TH0=0xf4;TL0=0xc0;//定时3.125ms //TR0=1; ET0=1; EX0=1; IT0=0; EA=1; }
void display(unsigned char k) //显示程序 { P2=table[k]; }
void delay(char x)//延时函数 { char i; while(x--) for(i=0;i<125;i++) continue; }
void check(void)//判断键盘松开 { while(temp!=0x0f) { temp=P1; temp=temp & 0x0f; } }
/*void check_down(void)//判断键盘按下 { while(temp==0x0f)
{ temp=P1; temp=temp & 0x0f; }
}*/
unsigned char keyscan(void) //按键扫描子程序 { //P1=0x00; P1_7=0; temp=P1; temp&=0x0f; //check_down();
if(temp !=0x0f) { delay(9); temp=P1; temp&=0x0f; if(temp !=0x0f) { temp=P1; temp&=0x0f;
switch(temp) { case 0x0d:key=55;break; case 0x0b:key=22;break; case 0x07:key=11;break; } display(key); // check(); } }
P1=0xFF; P1_6=0; temp=P1; temp&=0x0f; if(temp !=0x0f) { temp=P1; delay(9); temp&=0x0f; if(temp !=0x0f) { temp=P1; temp&=0x0f;
switch(temp) { case 0x0e:key=33;break; case 0x0d:key=3;break; case 0x0b:key=2;break; case 0x07:key=1;break; } display(key); // check(); } }
P1=0xFF; P1_5=0; temp=P1; temp&=0x0f; if(temp !=0x0f) { temp=P1; delay(9); temp&=0x0f; if(temp !=0x0f) { temp=P1; temp&=0x0f;
switch(temp) { case 0x0e:key=44;break; case 0x0d:key=6;break; case 0x0b:key=5;break; case 0x07:key=4;break; } display(key); //check(); } }
P1=0xFF; P1_4=0; temp=P1; temp&=0x0f; if(temp !=0x0f) { temp=P1; delay(9); temp&=0x0f; if(temp !=0x0f) { temp=P1; temp&=0x0f;
switch(temp) { case 0x0e:key=0;break; case 0x0d:key=9;break; case 0x0b:key=8;break; case 0x07:key=7;break; } display(key); //check(); }} return(key); }
void time0() interrupt 0 using 0 { unsigned char i; P1=0x00;
for(i=0;i<2;i++) {
if(i==0) { E3=1;SA=0;SB=0;SC=0; ch[0]=keyscan(); delay(3); }
if(i==1) { E3=1;SA=1;SB=0;SC=0; ch[1]=keyscan(); delay(3); } }
/*if(1) { E3=1;SA=0;SB=0;SC=0; keyscan(); delay(1); }
if(1) { E3=1;SA=1;SB=0;SC=0; //P2=table[3]; keyscan(); delay(1); }*/
}
void main(void) { unsigned char i; SP=0x50; EA=0; time_init();
for(i=0;i<2;i++) { P0=1; E3=1; display(ch); delay(1); }
|