#include<reg52.h>
#include"1602.h"
#include <stdio.h>
#include"KEY4X4.h"
#include "eeprom2.h"
static bit flag; //定义刷新标志
volatile uchar Key_Value = 0xff;
uint count; //定义计数标志
uchar input_state = 0;//输入状态 0显示时间 1输入密码 2指纹录入
extern void DS1302Init();
extern void read_time(uchar curr_time[]);
extern void set_time(uchar *pSecDa);
/*typedef struct Timer{
uchar year;
uchar month;
uchar day;
uchar hour;
uchar min;
}Timer;
Timer Timer_now;*/
uchar getTimebuf[7];//存放时间数据
uchar position_input = 0;
uchar key_buf[6];
uchar password[6];
uchar mode;
char shwobuf[15]="";
#define CHK_PASSWORD (password[0] == key_buf[0]) && (password[1] == key_buf[1]) && (password[2] == key_buf[2]) && (password[3] == key_buf[3]) && (password[4] == key_buf[4]) && (password[5] == key_buf[5])
void Timer0Init() //定时计0数器初始化
{
TMOD=0x01;
TH0=0xf8; //2ms定时
TL0=0x30;
ET0=1;
TR0=1;
EA=1;
flag=1; //定时1秒标志
}
void Initial_com()
{
TMOD &= 0x0f; //设置定时器模式
TMOD|=0x20; //定时器T1,在方式2中断产生波特率
PCON=0x00; //SMOD=0
SCON=0x50; // 方式1 由定时器控制
TH1=0xfd; //波特率设置为9600
TL1=0xfd;
TR1=1; //开定时器T1运行控制位
ES = 1;
}
void com_send(uchar byte1)
{
SBUF = byte1;
while(TI == 0);
TI = 0;
}
//-----------------------------------------------------------------
// 在UART显示字符串函数
//-----------------------------------------------------------------
void uart_Show_String(uchar *str)
{
uchar i = 0;
while(*str != '\0')
{
com_send(*str++);
}
// for ( i = 0; i< lth ;i++) //输出字符串,如果是2004,16需要改成20,下同。
// com_send(s[i]);
com_send(0x0D);
com_send(0x0A);
}
uchar usrcnt = 0;
uchar redflg = 0;
uchar user[8];
uchar msg_cnt = 0;
void delaylong()
{
uint x,y;
for (x=0;x<400;x++)
for (y=0;y<1000;y++);
}
void main()
{
uchar usr_tmp,sign_user;
uint chk_cnt;
init_1602();
Timer0Init();
Initial_com();
user[0] = 0;
user[1] = 0;
user[2] = 0;
user[3] = 0;
user[4] = 0;
user[5] = 0;
user[6] = 0;
user[7] = 0;
RW24XX(&redflg,1,0x0000,READEEP, M24128);
if (redflg!=0xaa)
{
redflg = 0xaa;
usrcnt = 0;
msg_cnt = 0;
RW24XX(&redflg,1,0x0000,RECONDEEP, M24128);
RW24XX(&usrcnt,1,0x0001,RECONDEEP, M24128);
RW24XX(&msg_cnt,1,0x0100,RECONDEEP, M24128);
}
RW24XX(&msg_cnt,1,0x0100,READEEP, M24128);
RW24XX(&usrcnt,1,0x0001,READEEP, M24128);//读用户总数
if (usrcnt != 0)//如果用户存在 读用户标志(指纹)
RW24XX(user,usrcnt,0x0002,READEEP, M24128);
mode = 0;
//系统密码
password[0] = 1;
password[1] = 2;
password[2] = 3;
password[3] = 4;
password[4] = 5;
password[5] = 6;
DS1302Init();
read_time(getTimebuf);
while(1)
{
P1 = 0x0f;
if(P1!= 0x0f)
{
KEY4X4_Delay_1ms(15);
if(P1!= 0x0f)
{
Key_Value = KEY4X4_Keyscan();
if (Key_Value != 0xff)
{
switch(Key_Value)
{
case 3: Key_Value = 10;break;
case 2: Key_Value = 1;break;
case 1: Key_Value = 4;break;
case 0: Key_Value = 7;break;
case 7: Key_Value = 0;break;
case 6: Key_Value = 2;break;
case 5: Key_Value = 5;break;
case 4: Key_Value = 8;break;
case 11: Key_Value = 15;break;
case 10: Key_Value = 3;break;
case 9: Key_Value = 6;break;
case 8: Key_Value = 9;break;
case 15: Key_Value = 14;break;
case 14: Key_Value = 13;break;
case 13: Key_Value = 12;break;
case 12: Key_Value = 11;break;
}
}
//sprintf(shwobuf,"%bd ", Key_Value);
//DisplayListChar(0,0,0, shwobuf);
}
}
if (input_state==0 && Key_Value == 14)//进入输入密码状态
{
input_state = 1;
write_com(0x01);//清屏
position_input=0;
DisplayListChar(0,0,0, "input password");
}
else if (Key_Value == 10)//退出
{
input_state = 0;
write_com(0x01);//清屏
}
else if (input_state==1 && (Key_Value == 15))//退格键
{
if (position_input > 0)
position_input--;
switch (position_input)
{
case 0: DisplayListChar(1,0,0, " ");break;
case 1: DisplayListChar(1,0,0, "* ");break;
case 2: DisplayListChar(1,0,0, "** ");break;
case 3: DisplayListChar(1,0,0, "*** ");break;
case 4: DisplayListChar(1,0,0, "**** ");break;
case 5: DisplayListChar(1,0,0, "***** ");break;
}
}
else if (input_state==1 && (Key_Value < 10))//输入密码
{
key_buf[position_input] = Key_Value;
position_input++;
switch (position_input)
{
case 1: DisplayListChar(1,0,0, "*");break;
case 2: DisplayListChar(1,0,0, "**");break;
case 3: DisplayListChar(1,0,0, "***");break;
case 4: DisplayListChar(1,0,0, "****");break;
case 5: DisplayListChar(1,0,0, "*****");break;
case 6: DisplayListChar(1,0,0, "*******");break;
}
if (position_input == 6)
{
if (CHK_PASSWORD)
{
write_com(0x01);//清屏
DisplayListChar(0,0,0, "recond finger");//密码正确 进行指纹录入模式
input_state = 2;
}
else//密码错误 重新输入
{
position_input = 0;
write_com(0x01);//清屏
DisplayListChar(0,0,0, "input password");
}
}
}
else if (input_state== 2 && (Key_Value == 14))//删除所有用户
{
usrcnt = 0;
user[0] = 0;
user[1] = 0;
user[2] = 0;
user[3] = 0;
user[4] = 0;
user[5] = 0;
user[6] = 0;
user[7] = 0;
DisplayListChar(1,0,0, "delete all users");
RW24XX(&usrcnt,1,0x0001,RECONDEEP, M24128);
}
else if (input_state==0 && (Key_Value == 13))//进入查询状态
{
input_state = 4;
chk_cnt = msg_cnt;
RW24XX(&sign_user,1,0x0102+chk_cnt*8,READEEP, M24128);//一组数据8字节
RW24XX(getTimebuf,7,0x0103+chk_cnt*8,READEEP, M24128);
sprintf(shwobuf,"user%02bu sign at ", sign_user);
uart_Show_String(shwobuf);
DisplayListChar(0,0,0, shwobuf);
sprintf(shwobuf,"20%02bu-%02bu-%02bu %02bu:%02bu", getTimebuf[6],getTimebuf[4],getTimebuf[3],getTimebuf[2],getTimebuf[1]);
uart_Show_String(shwobuf);
DisplayListChar(1,0,0, shwobuf);
}
else if (input_state==4 && (Key_Value == 11))//向上查
{
if (chk_cnt == 1)
chk_cnt = msg_cnt+1;
chk_cnt--;
RW24XX(&sign_user,1,0x0102+chk_cnt*8,READEEP, M24128);//一组数据8字节
RW24XX(getTimebuf,7,0x0103+chk_cnt*8,READEEP, M24128);
sprintf(shwobuf,"user%02bu sign at ", sign_user);
uart_Show_String(shwobuf);
DisplayListChar(0,0,0, shwobuf);
sprintf(shwobuf,"20%02bu-%02bu-%02bu %02bu:%02bu", getTimebuf[6],getTimebuf[4],getTimebuf[3],getTimebuf[2],getTimebuf[1]);
uart_Show_String(shwobuf);
DisplayListChar(1,0,0, shwobuf);
}
else if (input_state==4 && (Key_Value == 12))//向下查
{
chk_cnt++;
if (chk_cnt > msg_cnt)
chk_cnt = 1;
RW24XX(&sign_user,1,0x0102+chk_cnt*8,READEEP, M24128);//一组数据8字节
RW24XX(getTimebuf,7,0x0103+chk_cnt*8,READEEP, M24128);
sprintf(shwobuf,"user%02bu sign at ", sign_user);
uart_Show_String(shwobuf);
DisplayListChar(0,0,0, shwobuf);
sprintf(shwobuf,"20%02bu-%02bu-%02bu %02bu:%02bu", getTimebuf[6],getTimebuf[4],getTimebuf[3],getTimebuf[2],getTimebuf[1]);
uart_Show_String(shwobuf);
DisplayListChar(1,0,0, shwobuf);
}
if(input_state == 2)//指纹录入状态
{
if ((P3 & 0xfc ) != 0xfc)
{
usr_tmp = P3 & 0xfc;
while ((P3 & 0xfc ) != 0xfc);
if (usrcnt < 8)
{
if (usr_tmp != user[0] && usr_tmp != user[1] && usr_tmp != user[2] && usr_tmp != user[3] && usr_tmp != user[4] && usr_tmp != user[5] && usr_tmp != user[6] && usr_tmp != user[7])
{
user[usrcnt] = usr_tmp;
usrcnt++;
sprintf(shwobuf,"%02bu had recond ", usrcnt);
DisplayListChar(1,0,0,