單片機電子密碼鎖程序(2)
仿真文件和完整源碼下載地址:http://www.51hei.com/bbs/dpj-22586-1.html
論文下載地址:http://www.51hei.com/f/電子密碼鎖論文最終稿.docx
下面是main.c文件:
#include#include "delay_ms.h"#include "矩陣鍵盤.h"#include "i2c.h"#include "1602.h"#include "delay_ms.h"#include "mima.h"#define uchar unsigned char#define uint unsigned int#define A 0x0a //輸入密碼#define B 0x0b //修改密碼#define D 0x0d //關鎖#define enter 0x0c //確認#define backspace 0x0f //退格鍵sbit baojing = P3^7; //蜂鳴器接口sbit lock = P3^3;//鎖信號sbit set = P3^2;uchar idata mima_5[6] = {1,2,3,4,5,6}; //初始密碼uchar flag_change; uchar aa;//存放密碼錯誤的次數,夠三次報警uchar bb;uchar flag_t0;void baoj1()//報警函數{int i;for(i = 0;i < 5; i++){baojing = 1;delay_ms(5);baojing = 0;delay_ms(5); }}void baoj(){uchar i;for(i = 0; i < 10; i++)baoj1();}/*密碼處理函數*/void mimachuli() {uchar key,i;uchar idata mima_1[6]={ , , , , , };//存放密碼緩沖區(qū)uchar idata mima_2[6]={ , , , , , };uchar idata mima_3[6]={ , , , , , };key = step_choose();if(key == A)//A被按下,接收輸入密碼,處理{read_n_add(mima_1,0x00,6);write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_com(0x0f);//顯示光標lcd_pos(0,0);write_n_char(" press password "); if(input_mima(mima_2)) //處理輸入密碼{if(mimaduibi(mima_2,mima_1)) //密碼正確{lcd_pos(0,0);write_com(0x0c);write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char(" password right ");aa = 0; //清除密碼錯誤次數lock = 0; //開鎖baojing = 1; //響一下delay_ms(1000);baojing = 0;TR0 = 1;flag_t0 = 1;while(key != D && flag_t0) //D沒按下一直開{ key = keyscan();}TR0 = 0;lock = 1;//D按下了關鎖}else //密碼 不正確{lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char(" password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //密碼不正確報警while(i--)baoj();}}}else //密碼沒有輸入正確或完成{lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char(" password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //密碼不正確報警while(i--)baoj();}}}if(key == B)//B被按下,修改密碼{read_n_add(mima_1,0x00,6);write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_com(0x0f);//顯示光標lcd_pos(0,0);write_n_char(" input password"); write_com(0x0f);//顯示光標 if(input_mima(mima_2)) //處理輸入密碼{if(mimaduibi(mima_2,mima_1)) //密碼正確{lcd_pos(0,0);write_com(0x0c);write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("password right ");aa = 0; //清除密碼錯誤次數delay_ms(1500);flag_change = 1;while(flag_change) //下面開始修改密碼{write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_com(0x0f);//顯示光標lcd_pos(0,0);write_n_char("In new password");delay_ms(1500);if(input_mima(mima_2)) //處理輸入密碼{lcd_pos(0,0);write_com(0x0c);write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("Input new pass"); //確定新密碼lcd_pos(1,0);write_n_char("word again");delay_ms(2000);lcd_pos(0,0);write_com(0x0f);//顯示光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("In new password"); if(input_mima(mima_3)) //處理輸入密碼{if(mimaduibi(mima_2,mima_3)) //密碼正確{lcd_pos(0,0);write_com(0x0c);write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("password has");lcd_pos(1,0);write_n_char("change already");write_n_add(mima_3,0x00,6); //把修改的密碼存進24C02delay_ms(2000);flag_change = 0;}else //密碼 不正確{lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報警while(i--)baoj();}flag_change = 0;}}else //密碼沒有輸入正確或完成{lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char(" password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報警while(i--)baoj();}}}else //密碼沒有輸入正確或完成{lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("password wrong");delay_ms(1000);flag_change = 0;aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報警while(i--)baoj();}}}}else //密碼 不正確{lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報警while(i--)baoj();}}}else //密碼沒有輸入正確或完成{lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報警while(i--)baoj();}} }}/*外部中斷函數,恢復出廠設置,長按set鍵恢復*/void int0() interrupt 0{delay_ms(2000);if(!set){lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零i2cinit();//24C02總線初始化write_n_add(mima_5,0x00,6);write_n_char("password renew");lcd_pos(1,0);write_n_char("already");delay_ms(1000);lcd_pos(0,0);write_com(0x0c); //關光標write_com(0x06);//寫一個字符后地址指針加 1 write_com(0x01);//顯示清零,數據指針清零write_n_char("Input password");lcd_pos(1,0);write_n_char("Press key A");}// 引起了三個警告}void timer0() interrupt 1 //定時器0中斷,50ms定時{TH0 = 0x4c;//50ms定時TL0 = 0x00;bb++;if(bb == 200)//10秒時間到{bb = 0;flag_t0 = 0;}}void main(){yjinit();//液晶初始化i2cinit();//24C02總線初始化baojing = 0;lock = 1; //關鎖TMOD = 0x01;//選擇定時器0方式1EA = 1;//打開總中斷ET0 = 1;//打開定時器0中斷EX0 = 1; //打開外部中斷0IT0 = 1;//下降沿觸發(fā)TR0 = 0;//關閉定時器TH0 = 0x4c;//50ms裝初值TL0 = 0x00;while(1){mimachuli(); }}
評論