按键输入密码值 可以修改密码 密码被保存在24CO2中 数码管显示键入的密码 通过LED灯的亮灭表示锁的开闭
另外限制输错的次数 输错次数过多则必须通过串口助手解锁 可以直接用在清翔电子的开发板上
电路原理图:
单片机源程序如下:
- #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)
|