附录
#ifndef __DATAPROC__H__ #define __DATAPROC__H__ #include \#define uint unsigned int
#define uchar unsigned char
/*根据共阳极字型码表获取0~9,A~B字型代码*/ unsigned char getcode(unsigned char i) {
unsigned char p; switch(i)
{ case 0: p=0xc0; break; /* 0 */
case 1: p=0xcf; break; /* 1 */ case 2: p=0xa4; break; /* 2 */ case 3: p=0xb0; break; /* 3 */ case 4: p=0x99; break; /* 4 */ case 5: p=0x92; break; /* 5 */ case 6: p=0x82; break; /* 6 */ case 7: p=0xf8; break; /* 7 */ case 8: p=0x80; break; /* 8 */ case 9: p=0x90; break; /* 9 */ case 10: p=0x88; break; /* A */ case 11: p=0x80; break; /* B */ case 12: p=0xc6; break; /* C */ case 13: p=0xc0; break; /* D */ case 14: p=0x86; break; /* E */ case 15: p=0x8e; break; /* F */ default: break;
}
return(p);
}
/* 键扫描函数 */ uchar keyscan(void) {
uchar scancode,tmpcode;
P1 = 0xf0; // 发全0行扫描码 if ((P1&0xf0)!=0xf0) // 若有键按下 {
delay(); // 延时去抖动
if ((P1&0xf0)!=0xf0) // 延时后再判断一次,去除抖动影响 {
scancode = 0xfe;
}
while((scancode&0x10)!=0) // 逐行扫描 { }
P1 = scancode; // 输出行扫描码 if ((P1&0xf0)!=0xf0) // 本行有键按下 {
tmpcode = (P1&0xf0)|0x0f;
/* 返回特征字节码,为1的位即对应于行和列 */
return((~scancode)+(~tmpcode)); }
else scancode = (scancode<<1)|0x01; // 行扫描码左移一位
}
return(0); // 无键按下,返回值为0 }
/*显示函数*/ void display() { uchar i;
switch(digbit) {
case 0x01: case 0x02: case 0x04: case 0x08:
i=0; break; i=1; break; i=2; break; i=3; break;
default: break; }
P2=0x00; //关闭显示 P0=getcode(wordbuf[i]); //送字型码 P2=digbit; //送字位码 if(digbit<=0x08) //共四位 digbit=digbit*2; //左移一位 else digbit=0x01;
}
/*密码比较函数*/ bit pwcmp(void) {
bit flag; uchar i; for(i=0;i<4;i++) {
if(pw[i]==pwbuf[i]) {
}
flag=1;
} else { flag=0; }
i=4;
}
return(flag);
#endif
#ifndef __DELAY_H__ #define __DELAY_H__ /*键消抖延时函数*/ void delay() { } #endif
#include \#include \#include %uchar key; /*主程序*/ void main() { uchar j;
uint i;
P2=0x00; //关闭数码管显示 TMOD=0x11; //T0,T1工作方式1 /*2ms定时设置*/ TH0=-2000/256; TL0=-2000%6; /*50ms定时设置*/
TH1=-50000/256; TL1=-50000%6;
/*启动计数器0,关闭计数器1*/ EA=1; TR0=1; ET0=1; TR1=0;
unsigned char i; for(i=300;i>0;i--);
ET1=1;
count=0; //初始没有输入密码,计数器设为0 enterflag=0; //没有按下确认键
pwflag=0; //密码标志先设为0 beep=0; //蜂鸣器不响 /*假设内定密码为0627*/ pw[0]=0; pw[1]=6; pw[2]=2; pw[3]=7;
showflag=1; //打开数码管显示 /*刚加电时,显示8888*/ for(j=0;j<4;j++)
wordbuf[j]=8;
P2=digbit;
digbit=0x01; //从第一位数码管开始动态扫描 while(1) {
key=keyscan(); switch(key) {
case 0x11: if(count<4)
{ }
//1行1列,数字0
wordbuf[count]=0x0f; //对应密码位上显示‘F’ pwbuf[count]=0; count++;
for(i=0;i<2000;i++) delay();
break; case 0x21: if(count<4)
{
//1行2列,数字1
wordbuf[count]=0x0f; //对应密码位上显示‘F’ pwbuf[count]=1; count++;
}
for(i=0;i<2000;i++) delay();