找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4344|回復: 2
打印 上一主題 下一主題
收起左側(cè)

帶AT24C02存儲的單片機六位密碼鎖Proteus仿真+代碼

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:443049 發(fā)表于 2018-12-10 09:43 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
按行列鍵盤0~9輸入六位密碼數(shù)據(jù)存儲到AT24C02,按“=”鍵從AT24C02中讀取數(shù)據(jù)并在液晶屏中顯示,再按“+/-”清屏。
按行列鍵盤0~9輸入六位密碼,按“=”驗證密碼,正確顯示“TURE”,并且亮綠燈;錯誤顯示“FLASE”,并且亮紅燈,蜂鳴器響起。
如果輸入驗證密碼過程中輸入錯誤,可按“%”退位,也可按“?”清除所有密碼。

仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機源程序如下:
  1. #include <reg52.H>
  2. #include "LCD1602_run.h"
  3. #include "LCD1602_mid_run.h"
  4. #include "keypad.h"
  5. #include "timer_Init.h"
  6. #include "iic.h"
  7. #include "at24c02.h"
  8. #include <string.h>

  9. sbit LedRed = P0^0;
  10. sbit LedGreen = P0^1;

  11. void main()
  12. {        
  13.         unsigned char Cou;
  14.         unsigned char KeyBuff[7];
  15.         unsigned char KeyPassword[7];
  16.         unsigned char i;
  17.         unsigned char KeyBuffIndex=0;
  18.         unsigned char KeyBuffIndex2=0;
  19.   Timer_Init();
  20.         LCD_initial();
  21.         P0=0xff;
  22.         while(1)
  23.         {     
  24.           if((KeyNum <= '9') && (KeyNum >= '0') && (KeyNum != '=')&& (KeyNum != 'c')) /*存密碼*/
  25.                 {
  26.                         
  27.                         KeyBuff[KeyBuffIndex] = KeyNum;
  28.                         At24c02_Write_Add(KeyBuffIndex, KeyNum);
  29. //                         LCD1602_write_char(        0,KeyBuffIndex,' ');
  30.                         LCD1602_write_char(        0,KeyBuffIndex,'*');
  31.                                        
  32.                         KeyBuffIndex++;
  33.                         if(KeyBuffIndex == 6)
  34.                         {
  35.                                 KeyBuff[KeyBuffIndex] = '\0';
  36.                                 KeyBuffIndex = 0;
  37.                         }

  38.                         while(KeyNum != 0x11);
  39.                 }        
  40.                
  41.                 if(KeyNum == '=')/*顯示存儲密碼*/
  42.                 {
  43.                         
  44.                         LCD_write_cmd(0X01);
  45.                         while(KeyNum != 0x11);
  46.                         for(i = 0; i < 6; i++)
  47.                         {
  48.                                 KeyPassword [i] = At24c02_Read_Add(i);
  49.                         }
  50.                         KeyPassword [i] = '\0';
  51.                         LCD1602_write_string(1,0,KeyPassword);
  52.                         Cou = 0;
  53.                         
  54.                 }
  55.                
  56.                 if(KeyNum == 'c') /*清屏*/
  57.                 {
  58.                         
  59.                         while(KeyNum != 0x11);
  60.                         LCD_write_cmd( 0x01 );
  61.                         break;
  62.                 }                        
  63.         }
  64.         
  65.         while(1)
  66.         {
  67.                 if((KeyNum <= '9') && (KeyNum >= '0') && (KeyNum != '=') && (KeyNum != '%') && (KeyNum != '?') ) /*輸入密碼*/
  68.                         {
  69.                                 KeyBuff[KeyBuffIndex2] = KeyNum;
  70. //                                 LCD1602_write_char(        1,KeyBuffIndex2,KeyNum);
  71.                                 LCD1602_write_char(        0,KeyBuffIndex2,'*');
  72.                                 
  73.                                 KeyBuffIndex2 ++;                                
  74.                                 if(KeyBuffIndex2 == 6)
  75.                                 {                                       
  76.                                         KeyBuff[KeyBuffIndex2] = '\0';
  77.                                         KeyBuffIndex2 = 0;
  78.                                 }
  79.                                 
  80.                                 //Cou ++;
  81.                                 while(KeyNum != 0x11);
  82.                         }        
  83.                
  84.                 if( (KeyNum == '%') && (KeyBuffIndex2 > 0 ) ) /*退位*/
  85.                 {
  86.                   KeyBuffIndex2 --;
  87. //                        KeyBuff[KeyBuffIndex] = KeyNum;
  88.                         LCD1602_write_char(        0,KeyBuffIndex2,' ');
  89. //                        KeyBuffIndex2 = KeyBuffIndex2 - 1;
  90.                         //LCD1602_write_char(        0,KeyBuffIndex2,'*');
  91.                         while(KeyNum != 0x11);
  92.                 }
  93.                
  94.                 if(KeyNum == '?') /*清屏*/
  95.                 {
  96.                         
  97.                         while(KeyNum != 0x11);
  98.                         LCD_write_cmd( 0x01 );
  99.                         KeyBuffIndex2 = 0;
  100.                         
  101.                 }                        
  102.                         
  103.                 if( KeyNum == '=' )/*檢驗密碼*/
  104.                 {
  105.                   for(i = 0; i < 6; i++)
  106.                         {
  107.                                 KeyPassword [i] = At24c02_Read_Add(i);
  108.                         }
  109.                          KeyPassword [i] = '\0';
  110.                          LCD_write_cmd( 0x01 );                        
  111.                         if( strcmp( KeyBuff,KeyPassword ) == 0 )
  112.                                 {
  113. ……………………

  114. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼

所有資料51hei提供下載:
密碼鎖.rar (115.17 KB, 下載次數(shù): 129)


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏4 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:447989 發(fā)表于 2018-12-17 15:56 | 只看該作者
謝謝分享!見識學習。
回復

使用道具 舉報

板凳
ID:442274 發(fā)表于 2021-5-18 22:15 | 只看該作者
初始界面液晶無顯示,要按鍵才顯示*號的密文
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表