以下程序如需电路图,请给我留言
/************************************************************
*Author:findmu
*Found Date:2009-01-15
*Revision:v1.0.0
*Modify Date:
*E-mail:
[email protected]*function:Display the key value
************************************************************/
#include "reg52.h"
sbit LED=P1^0; //LED锁存器
sbit DUAN=P1^1; //数码管段选锁存器
sbit WEI=P1^2; //数码管位选锁存器
//**********************数码管显示函数************************
void display(unsigned char W, unsigned char D)
//数码管显示数字,W显示数字,D为显示数字
{
unsigned char D_Code[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x67,
/*A*/0x77,0x7C,0x39,0x5E,0x79,0x71,/*空白*/0x00};
unsigned char W_Code[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};
P0=0xFF;
WEI=1;
P0=W_Code[W];;
WEI=0;
P0=0x00;
DUAN=1;
P0=D_Code[D];
DUAN=0;
}
//**********************延迟函数,1ms************************
void delay(unsigned int ms)
{
unsigned char i,j;
for(i=0;i<ms;i++)
for(j=0;j<100;j++)
;
}
//*********************键盘扫描函数*************************
unsigned char keyscan(void)
{
unsigned char i,msk,keyin;
unsigned char num=16;
msk=1;
for(i=0;i<4;i++)
{
P3=~msk;
msk<<=1;
keyin=P3;
if((keyin&0xF0)!=0xF0)
{
delay(5);
keyin=P3;
if((keyin&0xF0)!=0xF0)
{
keyin=P3;
keyin>>=4;
switch(keyin)
{
case 0x0E: num=0+4*i; break;
case 0x0D: num=1+4*i; break;
case 0x0B: num=2+4*i; break;
case 0x07: num=3+4*i; break;
}
while(keyin!=0xF0)
{
keyin=P3;
keyin=keyin&0xF0;
delay(10);
}
}
}
}
return num;
}
void main()
{
unsigned char i,j,n;
unsigned char a,k;
unsigned char shu[8];
LED=0;
n=0;
while(1)
{
a=keyscan();
if(a!=16)
{
if(!a&&!i)
;
else
{
shu[i]=a;
n=i;
i++;
if(i==8)
{
i=0;
}
}
}
k=n;
for(j=0;j<=n;j++)
{
display(j,shu[k]);
delay(5);
k--;
}
}
}