|
#include <REG51.h>
#include<intrins.h>
//宏定義
#define LCM_Data P0 //將P0口定義為L(zhǎng)CM_Data
#define uchar unsigned char
#define uint unsigned int
#define w 6 //定義密碼位數(shù)
//1602的控制腳
sbit lcd1602_rs=P2^7;
sbit lcd1602_rw=P2^6;
sbit lcd1602_en=P2^5;
sbit Scl=P3^4; //24C02串行時(shí)鐘
sbit Sda=P3^5; //24C02串行數(shù)據(jù)
sbit ALAM = P2^1; //報(bào)警
sbit KEY = P3^6; //開(kāi)鎖
sbit open_led=P2^2; //開(kāi)鎖指示燈(選配)
bit operation=0; //操作標(biāo)志位
bit pass=0; //密碼正確標(biāo)志
bit ReInputEn=0; //重置輸入允許標(biāo)志
bit s3_keydown=0; //3秒按鍵標(biāo)志位
bit key_disable=0; //鎖定鍵盤(pán)標(biāo)志
unsigned char countt0,second; //t0中斷計(jì)數(shù)器,秒計(jì)數(shù)器
void Delay5Ms(void); //聲明延時(shí)函數(shù)
unsigned char code a[]={0xFE,0xFD,0xFB,0xF7}; //控盤(pán)掃描控制表
//液晶顯示數(shù)據(jù)數(shù)組
unsigned char code start_line[] = {"password: "};
unsigned char code name[] = { "===Coded Lock==="}; //顯示名稱(chēng)
unsigned char code Correct[] = {" correct "}; //輸入正確
unsigned char code Error[] = {" error "}; //輸入錯(cuò)誤
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]; //輸入密碼暫存區(qū)
unsigned char CurrentPassword[6]={1,3,1,4,2,0}; //管理員密碼(只可在程序中修改)
unsigned char TempPassword[6];
unsigned char N=0; //密碼輸入位數(shù)記數(shù)
unsigned char ErrorCont; //錯(cuò)誤次數(shù)計(jì)數(shù)
unsigned char CorrectCont; //正確輸入計(jì)數(shù)
unsigned char ReInputCont; //重新輸入計(jì)數(shù)
unsigned char code initpassword[6]={0,0,0,0,0,0}; //輸入管理員密碼后將密碼初始為000000
//=====================5ms延時(shí)==============================
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
//===================400ms延時(shí)==============================
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
}
}
//=============================================================================================
//================================24C02========================================================
//=============================================================================================
void mDelay(uint t) //延時(shí)
{
uchar i;
while(t--)
{
for(i=0;i<125;i++)
{;}
}
}
void Nop(void) //空操作
{
_nop_(); //僅作延時(shí)用一條語(yǔ)句大約1us
_nop_();
_nop_();
}
/*****24c02程序參照24c02時(shí)序圖*****/
/*起始條件*/
void Start(void)
{
Scl=1;
Nop();
Sda=0;
Nop();
}
/*停止條件*/
void Stop(void)
{
Sda=0;
Scl=1;
Nop();
Sda=1;
Nop();
}
/*應(yīng)答位*/
void Ack(void)
{
Sda=0;
Nop();
Scl=1;
Nop();
Scl=0;
}
/*反向應(yīng)答位*/
void NoAck(void)
{
Sda=1;
Nop();
Scl=1;
Nop();
Scl=0;
}
/*發(fā)送數(shù)據(jù)子程序,Data為要求發(fā)送的數(shù)據(jù)*/
void Send(uchar Data)
{
uchar BitCounter=8;
uchar temp;
do
{
temp=Data; //將待發(fā)送數(shù)據(jù)暫存temp
Scl=0;
Nop();
if((temp&0x80)==0x80) //將讀到的數(shù)據(jù)&0x80
Sda=1;
else
Sda=0;
Scl=1;
temp=Data<<1; //數(shù)據(jù)左移
Data=temp; //數(shù)據(jù)左移后重新賦值Data
BitCounter--; //該變量減到0時(shí),數(shù)據(jù)也就傳送完成了
}
while(BitCounter); //判斷是否傳送完成
Scl=0;
}
/*讀一字節(jié)的數(shù)據(jù),并返回該字節(jié)值*/
uchar Read(void)
{
uchar temp=0;
uchar temp1=0;
uchar BitCounter=8;
Sda=1;
do
{
Scl=0;
Nop();
Scl=1;
Nop();
if(Sda) //數(shù)據(jù)位是否為1
temp=temp|0x01; //為1 temp的最低位為1(|0x01,就是將最低位變?yōu)?)
else //如果為0
temp=temp&0xfe; //temp最低位為0(&0xfe(11111110)最低位就是0)
if(BitCounter-1) //BitCounter減1后是否為真
{
temp1=temp<<1; //temp左移
temp=temp1;
}
BitCounter--; //BitCounter減到0時(shí),數(shù)據(jù)就接收完了
}
while(BitCounter); //判斷是否接收完成
return(temp);
}
|
評(píng)分
-
查看全部評(píng)分
|