基于51单片机实现简易密码锁功能

2023-02-01  

具体功能的实现:

按下不同的按键,在LCD1602显示屏中可以出现不同的数字密码,如果输入错误,会显示ERR,反之显示ON;如果密码输错3次,则蜂鸣器报警30s同时在此期间无法输入密码。按键的功能有重置密码,取消,确认三种功能。

器件:AT89C51,排阻,LCD1602,若干电阻,电源,按键,蜂鸣器,三极管

Proteus仿真原理图:

知识介绍:

存储器介绍:

存储器主要芯片AT24C02:

AT24C02是一个2K位串行CMOS E2PROM, 内部含有256个8位字节,CATALYST公司的先进CMOS技术实质上减少了器件的功耗。AT24C02有一个16字节页写缓冲器。该器件通过IIC总线接口进行操作,有一个专门的写保护功能。

②为什么蜂鸣器要接NPN晶体管?

加NPN型三极管进行驱动,因为单片机的引脚驱动能力有限,蜂鸣器的功率比较大,所以需要通过三极管来驱动。

③为什么P0口接上拉电阻?

因为P0口是准双向口,即是开漏输出的,当P0口作为并行口使用时,只能输出低电平,不能输出高电平,需要接一个上拉电阻才能输出高电平。

主要代码(C语言)KEIL5实现:

主函数代码:

#include

#include "Delay.h"

#include "LCD1602.h"

#include "MatrixKey.h"

#include "Beep.h"

#include "AT24C02.h"


unsigned char KeyNum;

unsigned int password,count; 

unsigned int scount; 

unsigned int rightpass ; 

sbit led = P3^0;

sbit P22 = P2^2;


void main()

{  

  P22 = 0;

  rightpass = 2001;

  LCD_Init(); 

  while(1)

  {

      

      KeyNum = MatrixKey();

      LCD_ShowString(1,1,"PASSWORD");

      if(KeyNum)

    {

      

        if(KeyNum <= 10) 

      {

        

            led = 1;

            if(count < 4)

          {

            password*=10; 

            password += KeyNum % 10; 

            count++;    

          }

        LCD_ShowNum(2,1,password,4); 

      }

        if(KeyNum == 13)

      {

        rightpass = password;

        password = 0;

        count = 0;   

        LCD_ShowNum(2,1,password,4);

      }    

        if(KeyNum == 11) 

      {

      

            if(password == rightpass)   

          {

              LCD_ShowString(1,14,"ON "); 

              led = 0;

              password = 0;

              count = 0;  

              LCD_ShowNum(2,1,password,4);

          }else{                       

              LCD_ShowString(1,14,"ERR"); 

              scount++;

                if(scount == 3)  

              {

                int i = 60;   

                while(i--)    

                {

                  Beep();

                }

                Delay(30000); 

                scount = 0; 


              }

                password = 0;

                count = 0;  

                LCD_ShowNum(2,1,password,4);

          }

        

      }

        if(KeyNum == 12)

      {

        password = 0;

        count = 0;

        LCD_ShowNum(2,1,password,4);

      }  

      

   }  

  }

}


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。