51单片机密码锁
单片机源程序如下:
- //包含头文件
- #include <REG51.h>
- #include<intrins.h>
- //宏定义
- #define LCM_Data P0 //将P0口定义为LCM_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串行时钟
- sbit Sda=P3^5; //24C02串行数据
- sbit ALAM = P2^1; //报警
- sbit KEY = P3^6; //开锁
- 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]={0,0,0,0,0,0}; //读取EEPROM密码暂存数组
- 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}; //输入管理员密码后将密码初始为000000
- unsigned char code adminpassword[6]={1,3,1,4,2,0}; //输入管理员密码后将密码初始为000000
- //=====================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_(); //仅作延时用一条语句大约1us
- _nop_();
- _nop_();
- _nop_();
- }
- /*****24c02程序参照24c02时序图*****/
- /*起始条件*/
- 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; //将待发送数据暂存temp
- Scl=0;
- Nop();
- if((temp&0x80)==0x80) //将读到的数据&0x80
- Sda=1;
- else
- Sda=0;
- Scl=1;
- temp=Data<<1; //数据左移
- Data=temp; //数据左移后重新赋值Data
- BitCounter--; //该变量减到0时,数据也就传送完成了
- }
- 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) //数据位是否为1
- temp=temp|0x01; //为1 temp的最低位为1(|0x01,就是将最低位变为1)
- else //如果为0
- temp=temp&0xfe; //temp最低位为0(&0xfe(11111110)最低位就是0)
- if(BitCounter-1) //BitCounter减1后是否为真
- {
- temp1=temp<<1; //temp左移
- temp=temp1;
- }
- BitCounter--; //BitCounter减到0时,数据就接收完了
- }
- 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();
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
Keil代码与Proteus7.5仿真下载:
密码锁(AT89S52 LCD 24C02).zip
(127.98 KB, 下载次数: 31)
|