#include <REG52.h>
#include<intrins.h>
/****************************************************************************************/
#include "string.h"
#include "delay.h"
#include "uart.h"
#include "timer1.h"
#include "SIMxxx.h"
void sms_open_or_close_func();
/****************************************************************************************/
#define LCM_Data P0
#define uchar unsigned char
#define uint unsigned int
#define w 6 //定义密码位数
sbit lcd1602_rs=P2^7;
sbit lcd1602_rw=P2^6;
sbit lcd1602_en=P2^5;
sbit Scl=P3^4; //24C02串行时钟
sbit Sda=P3^5; //24C02串行数据
sbit ALAM = P2^1; //报警
sbit KEY = P3^6; //开锁
sbit open_led=P2^2; //开锁指示灯
bit operation=0; //操作标志位
bit pass=0; //密码正确标志
bit ReInputEn=0; //重置输入充许标志
bit s3_keydown=0; //3秒按键标志位
bit key_disable=0; //锁定键盘标志
unsigned char countt0,second; //t0中断计数器,秒计数器
void Delay5Ms(void);
unsigned char code a[]={0xFE,0xFD,0xFB,0xF7}; //控盘扫描控制表
unsigned char code start_line[] = {"password: "};
unsigned char code name[] = {"===Coded Lock==="}; //显示名称
unsigned char code Correct[] = {" correct "}; //输入正确
unsigned char code Error[] = {" error "}; //输入错误
unsigned char code codepass[] = {" pass "};
unsigned char code LockOpen[] = {" open "}; //OPEN
unsigned char code SetNew[] = {"SetNewWordEnable"};
unsigned char code Input[] = {"input: "}; //INPUT
unsigned char code ResetOK[] = {"ResetPasswordOK "};
unsigned char code initword[] = {"Init password..."};
unsigned char code Er_try[] = {"error,try again!"};
unsigned char code again[] = {"input again "};
unsigned char InputData[6]; //输入密码暂存区
unsigned char CurrentPassword[6]={1,3,1,4,2,0}; //当前密码值
unsigned char TempPassword[6];
unsigned char N=0; //密码输入位数记数
unsigned char ErrorCont; //错误次数计数
unsigned char CorrectCont; //正确输入计数
unsigned char ReInputCont; //重新输入计数
unsigned char code initpassword[6]={0,0,0,0,0,0};
//=====================5ms延时==============================
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
//===================400ms延时==============================
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
}
}
//=============================================================================================
//================================24C02========================================================
//=============================================================================================
void mDelay(uint t) //延时
{
uchar i;
while(t--)
{
for(i=0;i<125;i++)
{;}
}
}
void Nop(void) //空操作
{
_nop_();
_nop_();
_nop_();
_nop_();
}
/*起始条件*/
void Start(void)
{
Sda=1;
Scl=1;
Nop();
Sda=0;
Nop();
}
/*停止条件*/
void Stop(void)
{
Sda=0;
Scl=1;
Nop();
Sda=1;
Nop();
}
/*应答位*/
void Ack(void)
{
Sda=0;
Nop();
Scl=1;
Nop();
Scl=0;
}
/*反向应答位*/
void NoAck(void)
{
Sda=1;
Nop();
Scl=1;
Nop();
Scl=0;
}
/*发送数据子程序,Data为要求发送的数据*/
void Send(uchar Data)
{
uchar BitCounter=8;
uchar temp;
do
{
temp=Data;
Scl=0;
Nop();
if((temp&0x80)==0x80)
Sda=1;
else
Sda=0;
Scl=1;
temp=Data<<1;
Data=temp;
BitCounter--;
}
while(BitCounter);
Scl=0;
}
/*读一字节的数据,并返回该字节值*/
uchar Read(void)
{
uchar temp=0;
uchar temp1=0;
uchar BitCounter=8;
Sda=1;
do{
Scl=0;
Nop();
Scl=1;
Nop();
if(Sda)
temp=temp|0x01;
else
temp=temp&0xfe;
if(BitCounter-1)
{
temp1=temp<<1;
temp=temp1;
}
BitCounter--;
}
while(BitCounter);
return(temp);
}
void WrToROM(uchar Data[],uchar Address,uchar Num)
{
uchar i;
uchar *PData;
PData=Data;
for(i=0;i<Num;i++)
{
Start();
Send(0xa0);
Ack();
Send(Address+i);
Ack();
Send(*(PData+i));
Ack();
Stop();
mDelay(20);
}
}
void RdFromROM(uchar Data[],uchar Address,uchar Num)
{
uchar i;
uchar *PData;
PData=Data;
for(i=0;i<Num;i++)
{
Start();
Send(0xa0);
Ack();
Send(Address+i);
Ack();
Start();
Send(0xa1);
Ack();
*(PData+i)=Read();
Scl=0;
NoAck();
Stop();
}
}
//==================================================================================================
//=======================================LCD1602====================================================
//==================================================================================================
#define yi 0x80 //LCD第一行的初始位置,因为LCD1602字符地址首位D7恒定为1(100000000=80)
#define er 0x80+0x40 //LCD第二行初始位置(因为第二行第一个字符位置地址是0x40)
//----------------延时函数,后面经常调用----------------------
void delay(uint xms)//延时函数,有参函数
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
//--------------------------写指令---------------------------
write_1602com(uchar com)//****液晶写入指令函数****
{
lcd1602_rs=0;//数据/指令选择置为指令
lcd1602_rw=0; //读写选择置为写
P0=com;//送入数据
delay(1);
lcd1602_en=1;//拉高使能端,为制造有效的下降沿做准备
delay(1);
lcd1602_en=0;//en由高变低,产生下降沿,液晶执行命令
}
//-------------------------写数据-----------------------------
write_1602dat(uchar dat)//***液晶写入数据函数****
{
lcd1602_rs=1;//数据/指令选择置为数据
lcd1602_rw=0; //读写选择置为写
P0=dat;//送入数据
delay(1);
lcd1602_en=1; //en置高电平,为制造下降沿做准备
delay(1);
lcd1602_en=0; //en由高变低,产生下降沿,液晶执行命令
}
//-------------------------初始化-------------------------
void lcd_init(void)
{
write_1602com(0x38);//设置液晶工作模式,意思:16*2行显示,5*7点阵,8位数据
write_1602com(0x0c);//开显示不显示光标
write_1602com(0x06);//整屏不移动,光标自动右移
write_1602com(0x01);//清显示
}
//========================================================================================
//=========================================================================================
//==============将按键值编码为数值=========================
unsigned char coding(unsigned char m)
{
unsigned char k;
switch(m)
{
case (0x11): k=1;break;
case (0x21): k=2;break;
case (0x41): k=3;break;
case (0x81): k='A';break;
case (0x12): k=4;break;
case (0x22): k=5;break;
case (0x42): k=6;break;
case (0x82): k='B';break;
case (0x14): k=7;break;
case (0x24): k=8;break;
case (0x44): k=9;break;
case (0x84): k='C';break;
case (0x18): k='*';break;
case (0x28): k=0;break;
case (0x48): k='#';break;
case (0x88): k='D';break;
}
return(k);
}
//=====================按键检测并返回按键值===============================
unsigned char keynum(void)
{
unsigned char row,col,i;
P1=0xf0;
if((P1&0xf0)!=0xf0)
{
Delay5Ms();
Delay5Ms();
if((P1&0xf0)!=0xf0)
{
row=P1^0xf0; //确定行线
i=0;
P1=a[i]; //精确定位
while(i<4)
{
if((P1&0xf0)!=0xf0)
{
col=~(P1&0xff); //确定列线
break; //已定位后提前退出
}
else
{
i++;
P1=a[i];
}
}
}
else
{
return 0;
}
while((P1&0xf0)!=0xf0);
return (row|col); //行线与列线组合后返回
}
else return 0; //无键按下时返回0
}
//=======================一声提示音,表示有效输入========================
void OneAlam(void)
{
ALAM=0;
Delay5Ms();
ALAM=1;
}
//=======================