/*密码锁 2010-3-13*/
#include<reg51.h>
#include<pass.h>
void main()
{
init();
while(1)
{
deal();
}
}
void init()//定时器1初始化
{
TMOD=0X11;
TH1=0xc0;
TL1=0x00;
TR1=1;
ET1=1;
EA=1;
}
void deal()//处理按键子程序 //去抖处理
{
if(keyT==0)
{
flag_t=1;//输入方式标记
FM=0;
delay(5000);
FM=1;
}
if(flag_t==1)//进入输入方式
{
if(key1==0)
{
buffer[0]++;
if(buffer[0]==10) buffer[0]=0;
}while(key1==0);
if(key2==0)
{
buffer[1]++;
if(buffer[1]==10) buffer[1]=0;
}while(key2==0);
if(key3==0)
{
buffer[2]++;
if(buffer[2]==10) buffer[2]=0;
}while(key3==0);
if(key4==0)
{
buffer[3]++;
if(buffer[3]==10) buffer[3]=0;
}while(key4==0);
a=buffer[0]+buffer[1]*10+buffer[2]*100+buffer[3]*1000;//确定输入数字
if(a==mima)//输入数字与密码一致 清零
{
unsigned char i=0;
for(i=0;i<4;i++)
{
buffer[i]=0;
}
FM=0;
}
}
if(keyI==0)//取消输入并xx缓冲
{
buffer[0]=0;buffer[1]=0;buffer[2]=0;buffer[3]=0;
FM=1;
}
}
void display() interrupt 3
{
TH1=0xc0;
TL1=0x00;
led_display();
}
void led_display()
{
unsigned char i;
for(i=0;i<4;i++)
{
P2=table[buffer[3-i]];
P0=choose[i];
delay(100);
}
P2=0XFF;
P0=0XFF;
}
void delay(unsigned int i)//延时函数
{
unsigned int j;
for(j=0;j<i;j++){;}
}
//#define uchar unsigned char
//#define uint unsigned int
sbit keyT=P3^4;
sbit keyI=P3^2;
//按键定义
sbit key1=P1^4;
sbit key2=P1^5;
sbit key3=P1^6;
sbit key4=P1^7;
sbit FM=P3^6;
//函数定义
void deal();
void init();//初始化定时器
void led_display();
void delay(unsigned int i);
//字符编码 led片选
unsigned char code table[]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};
unsigned char code choose[]={0xfe,0xfd,0xfb,0xf7};
unsigned char buffer[]={0,0,0,0};//输入缓冲
unsigned char count=0;//测试计数 程序完成删除
unsigned int mima=2110;//设置密码
unsigned int a;
unsigned char flag_t=0;//进入输入密码方式标记
unsigned char flag_i=0;//取消编辑按键标记并重新赋值