|
#include "reg52.h" //此文件中定义了单片机的一些特殊功能寄存器
#include "i2c.h"
#include "XPT2046.h"
#include "lcd.h"
#define GPIO_KEY P1
typedef unsigned int u16; //对数据类型进行声明定义
typedef unsigned char u8;
u8 mimaweishu=0;//用于判断输入密码位数
u8 count1,count2,count3,count4;//指针标志
u8 a=0;//密码错误次数
u8 p,q;
u8 temp;
bit querenwei=0;//确认键标志位
bit mimawei=0;//用于确认密码是否正确
bit FANGSHI=0;//用于确认开锁时是否需要密码
bit k=1;//判断继电器开关状态1为关 0为开
sbit jidianqi=P2^2;
sbit red=P2^3;
sbit bell=P2^4;//继电器,蜂鸣器,警示灯控制
u16 yaoshi=0; //用于判断钥匙状态,为0时钥匙插入并转到底
float dyz;
int tem;
int d0;
int d1;
int d2;
int d3; //d0-d3是电位AD值处理过后的四位数值,用于判断是否插入钥匙并转到底
u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
u8 mima[8];//初始密码存储数组
u8 shurumima[30];//输入密码存储数组
u8 xitongmima[8]={8,8,8,8,8,8,8,8};//系统密码数组
u8 code table1[]="input the passco";
u8 code table2[]="de: --------";
u8 code table3[]="*";
u8 code table4[]="right (^_^) ";
u8 code table5[]="first error!!!!";
u8 code table6[]="second error!!!!";
u8 code table7[]="third error see ";
u8 code table8[]="u tomorrow (^_^)";
u8 code table9[]=" welcom ";
u8 code table10[]=" (^_^) ";
u8 code table11[]=" no passcode "; //指令数组
/*******************************************************************************
* 函 数 名 : delay
* 函数功能 : 延时函数,i=1时,大约延时10us
*******************************************************************************/
void delay2(u8 x)
{
u8 a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--);
}
void delay(u16 z)
{
u16 x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay0(u16 r)
{
while(r--);
}
/*******************************************************************************
* 函 数 名 : yaoshichuli
* 函数功能 : 用于转换钥匙转动所改变的电位器AD值
*******************************************************************************/
void yaoshichuli()
{
u16 temp;
static u8 z;
if(z==50)
{
z=0;
temp = Read_AD_Data(0x94); // AIN0 电位器
dyz=(float)temp/4096.*5;
tem=dyz*1000;
d0=tem/1000;
d1=tem%1000/100;
d2=tem%100/10;
d3=tem%10;
yaoshi=d0+d1;
}
z++;
}
void Timer1Init()
{
TMOD|=0X10;
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
ET1=1;
EA=1;
TR1=0;
}
/*******************************************************************************
* 函 数 名 : mimabijiao
* 函数功能 : 用于确认密码是否正确?
*******************************************************************************/
bit mimabijiao()
{
bit i;
u8 m;
for(m=0;m<8;m++)
{
if(mima[m]==shurumima[m])
i=1;
else
{
i=0;
m=8;
}
}
return i;
}
///**********************************LCD跟随密码显示’*‘函数**************************************
void lcd_display()
{
u8 i=0;
LcdWriteCom(0x80+0x40+8);
for(i=0;i<mimaweishu;i++)
{
LcdWriteData(table3[0]);
}
}
/*******************************************************************************
* 函 数 名 : k1-k16chuli
* 函数功能 : 处理各个按键功能
k1-k10为数字:0-9
k11 k12 为无效键 k13用于取消密码锁 k14为关锁键 k15 k16分别是确认键和取消键
*******************************************************************************/
void k1chuli()
{
shurumima[mimaweishu]=0;
mimaweishu++;
}
void k2chuli()
{
shurumima[mimaweishu]=1;
mimaweishu++;
}
void k3chuli()
{
shurumima[mimaweishu]=2;
mimaweishu++;
}
void k4chuli()
{
shurumima[mimaweishu]=3;
mimaweishu++;
}
void k5chuli()
{
shurumima[mimaweishu]=4;
mimaweishu++;
}
void k6chuli()
{
shurumima[mimaweishu]=5;
mimaweishu++;
}
void k7chuli()
{
shurumima[mimaweishu]=6;
mimaweishu++;
}
void k8chuli()
{
shurumima[mimaweishu]=7;
mimaweishu++;
}
void k9chuli()
{
shurumima[mimaweishu]=8;
mimaweishu++;
}
void k10chuli()
{
shurumima[mimaweishu]=9;
mimaweishu++;
}
void k11chuli()
{
shurumima[mimaweishu]=10;
mimaweishu++;
}
void k12chuli()
{
shurumima[mimaweishu]=11;
mimaweishu++;
}
//方式改变键每按一次方式转换一次,只有两种方式
void k13chuli()
{
if(a==0&&(yaoshi==0))
{
FANGSHI=~FANGSHI;
} //修改.............................
if(FANGSHI==1)
{
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table11[count3]); //????,?? first erro
delay(5);
}
}
else if(FANGSHI==0)
{
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table1[count3]);
}
LcdWriteCom(0x80+0x40);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table2[count3]);
}
}
}
void k14chuli()
{
jidianqi=1;
k=1;
}
//确认键函数****************************************************、//
void k15chuli()
{
querenwei=1;
if(mimaweishu==8)
{
mimawei=mimabijiao();
}
else
{
mimawei=0;
}
if(querenwei==1)
{
querenwei=0;
mimaweishu=0;
for(count3=0;count3<8;count3++)
{
delay(5);
shurumima[count3]=0x0f;
}
TR1=1;
count1=0;
if(mimawei==1)
{
a=0;
jidianqi=0;
k=0;
mimawei=0;
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table4[count3]);
delay(5);
}
}
else
{
mimaweishu=0;
red=0;
bell=0;//开启警示灯和电铃
delay0(100);
a++;
if(a==1)
{
for(count3=0;count3<8;count3++)
{
delay(5);
shurumima[count3]=0x0f;
}
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table5[count3]);
delay(5);
}
TR1=1;
}
if(a==2)
{
for(count3=0;count3<8;count3++)
{
delay(5);
shurumima[count3]=0x0f;
}
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table6[count3]);
delay(5);
}
TR1=1;
}
if(a==3)
{
for(count3=0;count3<8;count3++)
{
delay(5);
shurumima[count3]=0x0f;
}
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table7[count3]);
delay(5);
}
LcdWriteCom(0x80+0x40);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table8[count3]);
delay(5);
}
TR1=0;
}
}
}
}
void k16chuli()
{
mimaweishu=0;
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table1[count3]);
delay(5);
}
LcdWriteCom(0x80+0x40);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table2[count3]);
delay(5);
shurumima[count3]=0x0f;
}
}
//**************************************************第一行扫描********************************
void key_scan()
{
GPIO_KEY=0XFE;
temp=GPIO_KEY&0Xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=GPIO_KEY;
switch(temp)
{
case 0xee:
k1chuli();
break;
case 0xde:
k2chuli();
break;
case 0xbe:
k3chuli();
break;
case 0x7e:
k4chuli();
break;
}
while(temp!=0xf0)
{
temp=GPIO_KEY&0xf0;
red=0;
}
}
}
GPIO_KEY=0xfd;
temp=GPIO_KEY;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=GPIO_KEY;
switch(temp)
{
case 0xed:
k5chuli();
break;
case 0xdd:
k6chuli();
break;
case 0xbd:
k7chuli();
break;
case 0x7d:
k8chuli();
break;
}
while(temp!=0xf0)
{
temp=GPIO_KEY;
temp=temp&0xf0;
}
}
}
GPIO_KEY=0xfb;
temp=GPIO_KEY;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=GPIO_KEY;
switch(temp)
{
case 0xeb:
k9chuli();
break;
case 0xdb:
k10chuli();
break;
case 0xbb:
k11chuli();
break;
case 0x7b:
k12chuli();
break;
}
while(temp!=0xf0)
{
temp=GPIO_KEY;
temp=temp&0xf0;
}
}
}
GPIO_KEY=0xf7;
temp=GPIO_KEY;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=GPIO_KEY;
switch(temp)
{
case 0xe7:
k13chuli();
break;
case 0xd7:
k14chuli();
break;
case 0xb7:
k15chuli();
break;
case 0x77:
k16chuli();
break;
}
while(temp!=0xf0)
{
temp=0xf7;
temp=temp&0xf0;
}
}
}
}
//用于无钥匙工作方式时扫描按键,专门改变工作方式无其他功能
void key_scan1()
{
u16 temp;
P1=0xf7;
temp=P1;
temp=temp&0xf0;
if(temp!=0xf0)
{
delay(100);
if(temp!=0xf0)
{
temp=P1;
if(temp==0xe7)
{
k13chuli();
}
}
}
}
//初始化函数,集合定时器,LCD显示,初始显示,以及各种变量希望的初始值
void chushihua()
{
u8 i;
Timer1Init();
LcdInit();
for(i=0;i<8;i++)
{
At24c02Write(i,xitongmima[i]);
delay2(100);
}
for(i=0;i<8;i++)
{
mima[i]=At24c02Read(i);
delay(5);
}
count1=0;
count3=0;
jidianqi=1;
k=1;
red=1;
bell=1;
p=2;
q=2;
a=0;
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table9[count3]);
}
LcdWriteCom(0x80+0x40);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table10[count3]);
}
}
void chuli()
{
if((yaoshi!=0)&&(a!=3))
{
q=2;
for(;p>1;p--)
{
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table9[count3]);
}
LcdWriteCom(0x80+0x40);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table10[count3]);
}
}
mimaweishu=0;
}
else
{
p=2;
if(FANGSHI==1)
{
if(yaoshi==0&&(a!=3))
{
jidianqi=0;
}
key_scan1();
}
else if(FANGSHI==0)
{
if(yaoshi==0&&(a!=3))
{
// key_scan();
// lcd_display();
if(mimaweishu==0)
{
if(k==1)
{
for(;q>1;q--)
{
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table1[count3]);
}
LcdWriteCom(0x80+0x40);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table2[count3]);
}
}
}
}
key_scan();
lcd_display();
}
}
}
}
void main()
{
chushihua();
while(1)
{
yaoshichuli();
chuli();
}
}
//****************************************定时器1中断函数*****************************
void Timer1() interrupt 3
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%256;
if(count1<20)
{
count1++;
}
else
{
TR1=0;
count1=0;
mimawei=0;
red=1;
bell=1;
if(a!=0)
{
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table1[count3]);
}
LcdWriteCom(0x80+0x40);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table2[count3]);
}
}
else
{
LcdWriteCom(0x01);
LcdWriteCom(0x80);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table9[count3]);
}
LcdWriteCom(0x80+0x40);
for(count3=0;count3<16;count3++)
{
LcdWriteData(table10[count3]);
}
}
}
}
|
评分
-
查看全部评分
|