標(biāo)題:
51單片機(jī)AT89S52 LCD 24C02密碼鎖代碼
[打印本頁(yè)]
作者:
sdmsmdld
時(shí)間:
2022-5-14 01:32
標(biāo)題:
51單片機(jī)AT89S52 LCD 24C02密碼鎖代碼
51單片機(jī)密碼鎖
單片機(jī)源程序如下:
//包含頭文件
#include <REG51.h>
#include<intrins.h>
//宏定義
#define LCM_Data P0 //將P0口定義為L(zhǎng)CM_Data
#define uchar unsigned char
#define uint unsigned int
//1602的控制腳
sbit lcd1602_rs=P2^5;
sbit lcd1602_rw=P2^6;
sbit lcd1602_en=P2^7;
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)鎖
bit pass=0; //密碼正確標(biāo)志
bit ReInputEn=0; //重置輸入允許標(biāo)志
bit s3_keydown=0; //3秒按鍵標(biāo)志位
bit key_disable=0; //鎖定鍵盤標(biāo)志
unsigned char countt0,second; //t0中斷計(jì)數(shù)器,秒計(jì)數(shù)器
void Delay5Ms(void); //聲明延時(shí)函數(shù)
unsigned char code a[]={0xFE,0xFD,0xFB,0xF7}; //控盤掃描控制表
//液晶顯示數(shù)據(jù)數(shù)組
unsigned char code start_line[] = {"password: "};
unsigned char code name[] = {"===Coded Lock==="}; //顯示名稱
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]={0,0,0,0,0,0}; //讀取EEPROM密碼暫存數(shù)組
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
unsigned char code adminpassword[6]={1,3,1,4,2,0}; //輸入管理員密碼后將密碼初始為000000
//=====================5ms延時(shí)==============================
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
//===================400ms延時(shí)==============================
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
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_();
_nop_();
}
/*****24c02程序參照24c02時(shí)序圖*****/
/*起始條件*/
void Start(void)
{
Sda=1;
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);
}
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();
……………………
…………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
Keil代碼與Proteus7.5仿真下載:
密碼鎖(AT89S52 LCD 24C02).zip
(127.98 KB, 下載次數(shù): 31)
2022-5-14 01:32 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1