按鍵輸入密碼值 可以修改密碼 密碼被保存在24CO2中 數碼管顯示鍵入的密碼 通過LED燈的亮滅表示鎖的開閉
另外限制輸錯的次數 輸錯次數過多則必須通過串口助手解鎖 可以直接用在清翔電子的開發板上
電路原理圖:
QQ截圖20180326162212.jpg (103.7 KB, 下載次數: 46)
下載附件
2018-3-26 16:22 上傳
功能描述.jpg (182.01 KB, 下載次數: 48)
下載附件
2018-3-26 16:21 上傳
單片機源程序如下:
- #include "STC12C5A60S2.h"
- #include "at24c02.h"
- #include "uart.h"
- #include "timer.h"
- sbit BEEP = P2^2;
- char Password[6] = {0,0,0,0,0,0}; //初始化密碼
- char input_code[6] = {-1,-1,-1,-1,-1,-1}; //鍵盤鍵入的密碼
- char in_count = 0 ,Error = 0; //鍵入次數、輸入密碼錯誤次數累加器
- char Key_Value = -1 ; //鍵盤暫存變量
- unsigned char Lock_status = 1 ; // 密碼鎖狀態指示,默認處于上鎖狀態
- void delay(unsigned int xms)
- {
- unsigned int i,j;
- for(i=xms;i>0;i--)
- for(j=110;j>0;j--);
- } //內部服務延遲函數
- void Beep(unsigned int xms) //蜂鳴器函數
- {
- BEEP=0 ;
- delay(xms);
- BEEP=1;
- delay(xms);
- }
- void input_password(void)
- {
- unsigned char i;
-
- in_count++;
- for(i=0;i<5;i++)
- {
- input_code[i]=input_code[i+1]; //將新鍵碼作為密碼低位放在右位
- }
- input_code[5]= Key_Value;
- disp_num((in_count-1),Key_Value);
- if(in_count<6) disp_Flicker(in_count);
- }
- void check_password(void)
- {
- unsigned char i;
- Beep(100);
- Beep(100); //正常開鎖響兩聲
- disp_Com();
- in_count = 0 ;
- for(i=0;i<6;i++)
- {
- if(input_code[i]!=Password[i])
- {
- delay(50);
- Error++;
- SendString("Warning!!\n") ;
- write_add(6,Error); //實時寫入當前錯誤次數,保證意外斷電時輸錯次數能被保留
- return;
- }
- }
- Lock_status = 0; //解鎖成功的標志
- Error = 0;
- write_add(6,Error);
- SendString("Success!!\n") ;
- P1 = 0x00 ;
- }
- void Modify_password(void)
- {
- unsigned char i,n;
- //Error=0;
- for(n = 0 ; n < 6; n++)
- {
- disp_Flicker(n);
- for(i = 0; i < 5 ; i++)
- Password[i] = Password[i+1]; //將新鍵碼作為密碼低位放在右位
- Password[5] = Key_Value;
- disp_num(n,Password[5]);
- Beep(100);
- }
- for(n = 0 ;n < 6 ; n++)
- {
- write_add(n,Password[n]);
- delay(100);
- }
- SendString("The Password is modified!!\n") ;
- //delay(500);
-
- }
- void main(void)
- {
- char ncount;;
- init_24c02(); //AT24C02 初始化函數
- Timer_Config();
- UartInit();
- /*——————————————————首次上電燒錄代碼—————————————————————————————————————
- for(ncount = 0;ncount < 6 ; ncount++)
- {
- write_add(ncount,Password[ncount]) ;
- }
- write_add(6+1,0);//首次燒錄時使用,用于初始化密碼、以及輸入密碼錯誤的次數
- ————————————————————————————————————————————————————————————————————————*/
- for(ncount = 0;ncount < 6; ncount++)
- {
- Password[ncount] = read_add(ncount);
- delay(100);
- }
- Error = read_add(6+1);
-
- disp_Com();
- while(1)
- {
- if(Error <5)
- {
- if(Key_Value < 10 && Key_Value >= 0 &&Lock_status && in_count < 6)
- {
- input_password();
- delay(10);
- }
-
- if( in_count == 6)
- {
- check_password();
- }
- if(Key_Value == 13 && Lock_status == 0 )
- {
- Lock_status = 1;
- P1 = 0xff ;
- // SendString("Locked!!\n") ;
- }
- if(Key_Value == 14 && Lock_status )
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
電子密碼鎖.rar
(95.92 KB, 下載次數: 74)
2018-3-26 16:22 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|