|
用24c04与1602液晶屏设计的简易加密电子锁源程序+仿真文件下载:
用24c04与1602液晶屏设计的简易加密电子锁.rar
(96 KB, 下载次数: 228)
主程序预览:
- #include"LCD1602.h"
- #include"AT24C04.h"
- //上次按键和当前按键序号,该矩阵中序号范围为0~15,16表示无按键
- INT8U Key_Value = 0xFF, Pre_KeyNo = 0xFF;
- INT8U DSY_BUFFER[10]= ""; //显示缓冲区
- INT8U UserPassword[7]; //用户输入密码
- INT8U I2C_Password[7]; //从I2C存储器读取的密码
- sbit LED_OPEN = P2^7; //开锁亮灯
- sbit BEEP = P3^7; //蜂鸣器
- void Beep();
- void Clear_Pwd();
- void Read_I2C_Pwd();
- void Write_I2C_Pwd();
- void Keys_Scan();
- //----------主程序------------
- void main()
- {
- INT8U i = 0;
- INT8U IS_Valid_User = 0;
- P0 = 0xFF; P1 = 0xFF; P2 = 0xFF;
- Delay_ms(10);
- Init_LCD(); //初始化LCD
- Write_LCD_String(0, 0, "Your Password...");
- Read_I2C_Pwd(); //将密码读入I2C
- while(1)
- {
- P1 = 0x0F;
- while(P1 == 0x0F); Keys_Scan();
- Pre_KeyNo = Key_Value; //扫描键盘获取键序号
- switch(Pre_KeyNo)
- {
- case 0: case 1: case 2: case 3: case 4:
- case 5: case 6: case 7: case 8: case 9:
- if(i <= 5)
- {
- //如果i为0则执行一次清屏
- if(i == 0) Write_LCD_String(1, 0, " ");
- UserPassword[i] = Pre_KeyNo + '0';
- UserPassword[i+1] = '\0';
- DSY_BUFFER[i] = '*';
- DSY_BUFFER[i+1] = '\0'; i++;
- Write_LCD_String(1, 0,DSY_BUFFER );
- } break;
- case 10: //按A键开锁
- Read_I2C_Pwd(); //从I2C存储器读回密码
- if(strcmp(UserPassword,I2C_Password) == 0)
- {
- LED_OPEN = 0; //点亮LED
- Clear_Pwd();
- Write_LCD_String(1, 0, "Unlock OK! ");
- IS_Valid_User = 1;
- }
- else
- {
- LED_OPEN = 1; //关闭LED
- Clear_Pwd();
- Write_LCD_String(1, 0, "ERROR ! ");
- IS_Valid_User = 0;
- }
- i = 0; break;
- case 11: //按B键上锁
- LED_OPEN = 1;
- Clear_Pwd();
- Write_LCD_String(0, 0, "Your Password... ");
- Write_LCD_String(1, 0, " ");
- i = 0; IS_Valid_User = 0; break;
- case 12: //按C键设置新密码
- //如果是合法用户则提示输入新密码
- if(!IS_Valid_User)
- Write_LCD_String(1, 0, "No right ! ");
- else
- {
- i = 0; //密码输入缓冲索引归零
- Write_LCD_String(0, 0, "New Password:... ");
- Write_LCD_String(1, 0, " ");
- } break;
- case 13: //按D键保存新密码
- if(!IS_Valid_User)
- Write_LCD_String(1, 0, "No right ! ");
- else
- {
- //写入新设置的密码,并重新读回
- Write_I2C_Pwd(); Delay_ms(5); Read_I2C_Pwd();
- i = 0; //密码输入缓冲索引归零
- Write_LCD_String(0, 0, "Your Password ... ");
- Write_LCD_String(1, 0, "Password Saved ! ");
- } break;
- case 14: //按E键消除所有输入
- i = 0; Clear_Pwd();
- Write_LCD_String(0, 0, "Your Password... ");
- Write_LCD_String(1, 0, " ");
- }
- Beep(); Delay_ms(100);
- while(P1 != 0xF0); //如果有键未释放则等待
- }
-
- }
- //-----------蜂鸣器子程序------------
- void Beep()
- {
- INT8U i;
- for(i = 0; i < 100; i++)
- {
- Delay_ms(1);
- BEEP = ~BEEP;
- }
- BEEP = 0;
- }
- //------------清除密码--------------
- void Clear_Pwd()
- {
- UserPassword[0] = '\0';
- DSY_BUFFER[0]='\0';
- }
- //----------读取密码字符串(以'\0'为结束标志)----------
- void Read_I2C_Pwd()
- {
- INT8U i = -1; //只限于读取存储器前半部分
- do { i++; I2C_Password[i] = Random_Read(0xA0,(INT16U)i); }
- while(I2C_Password[i] != '\0' && i < 6);
- //如果循环结束后未遇到结束标志,则直接在字符串末尾补上结束标志
- if(I2C_Password[i] != '\0') I2C_Password[i] = '\0';
- }
- //----------写密码字符串(结束标志'\0'注意一并写入)---------
- void Write_I2C_Pwd()
- {
- INT8U i = 0;
- //循环写入密码字符,字符串末尾的'\0'也要写入,最多7个字符
- while(i <= 6)
- {
- Random_Write(0xA0, i,UserPassword[i]); Delay_ms(5);
- if(UserPassword[i] == '\0') break;
- i++;
- }
- //如果循环结束时密码字符串末尾未遇到'\0'则直接向I2C补充写入'\0'
- if(UserPassword[i] != '\0') Random_Write(0xA0, i,'\0');
- }
- //-----------------键扫描------------------------------------
- void Keys_Scan()
- {
- /*列判断*/
- P1 = 0x0F; Delay_ms(1);
- if(P1 == 0x0F)
- {
- Key_Value = 0xFF; return;
- }
- switch(P1)
- {
- case 0x0E: Key_Value = 0; break;
- case 0x0D: Key_Value = 1; break;
- case 0x0B: Key_Value = 2; break;
- case 0x07: Key_Value = 3; break;
- default : Key_Value = 0xFF; return;
- }
- /*行判断*/
- P1 = 0xF0; Delay_ms(1);
- switch(P1)
- {
- case 0xE0: Key_Value += 0; break;
- case 0xD0: Key_Value += 4; break;
- case 0xB0: Key_Value += 8; break;
- case 0x70: Key_Value += 12; break;
- default : Key_Value = 0xFF;
- }
- }
复制代码
|
评分
-
查看全部评分
|