第3个回答 2013-09-02
#include<reg51.h>
typedef unsigned char UINT8; //8位字节定义数据类型
typedef unsigned int UINT16; //16位字节定义数据类型
#define S1 0xEE; //按键的位定义
#define S2 0xDE; //按键的位定义
#define S3 0xBE; //按键的位定义
#define S4 0x7E; //按键的位定义
#define S5 0xED; //按键的位定义
#define S6 0xDD; //按键的位定义
#define S7 0xBD; //按键的位定义
#define S8 0x7D; //按键的位定义
#define S9 0xEB; //按键的位定义
#define S10 0xDB; //按键的位定义
#define S11 0xBB; //按键的位定义
#define S12 0x7B; //按键的位定义
#define S13 0xE7; //按键的位定义
#define S14 0xD7; //按键的位定义
#define S15 0xB7; //按键的位定义
#define S16 0x77; //按键的位定义
code UINT8 SEGMENT[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
code UINT8 SELECT[8]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}; void delay(UINT16 t) //延时函数
{
UINT8 i,j;
for(i=0;i<t;i++)
for(j=0;j<114;j++);
}UINT8 Scankey1(void) //按键函数
{
UINT16 row,col,key;
key=0;
P1=0x0f;
if(P1==0x0f)
return(0xff);
delay(10);
if(P1==0x0f)
return(0xff);
switch(P1)
{
case 0x0e:
row=0;
break;
case 0x0d:
row=1;
break;
case 0x0b:
row=2;
break;
case 0x07:
row=3;
break;
}
P1=0xf0;
switch(P1)
{
case 0xe0:
col=0;
break;
case 0xd0:
col=1;
break;
case 0xb0:
col=2;
break;
case 0x70:
col=3;
break;
}
key=row*4+col;
while(P1!=0xf0);
return(key);
} void Display(UINT8 n) //数码管显示函数
{
static UINT8 num=0;
P2=0xff;
switch(num)
{
case 0:
P0=0xff;
break;
case 1:
P0=0xff;
break;
case 2:
P0=0xff;
break;
case 3:
P0=0xff;
break;
case 4:
P0=0xff;
break;
case 5:
P0=SEGMENT[n/100];
break;
case 6:
P0=SEGMENT[n%100/10];
break;
case 7:
P0=SEGMENT[n%10];
break;
}
P2=SELECT[num];
num++;
num%=8;//if(num==8) num=0;
}
main() // 主函数
{
UINT8 c,k ;
while(1)
{
c=Scankey1();
if(c!=0xff)
k=c;
Display(k);
}
}
程序里面都是先定义,后调用的,就像你先得有名字,然后才知道在叫你呢?