新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)電子密碼鎖程序(2)

單片機(jī)電子密碼鎖程序(2)

作者: 時(shí)間:2016-11-30 來源:網(wǎng)絡(luò) 收藏

仿真文件和完整源碼下載地址:http://www.51hei.com/bbs/dpj-22586-1.html
論文下載地址:http://www.51hei.com/f/電子密碼鎖論文最終稿.docx
下面是main.c文件:

本文引用地址:http://m.butianyuan.cn/article/201611/323839.htm
#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 //關(guān)鎖#define enter 0x0c //確認(rèn)#define backspace 0x0f //退格鍵sbit baojing = P3^7; //蜂鳴器接口sbit lock = P3^3;//鎖信號(hào)sbit set = P3^2;uchar idata mima_5[6] = {1,2,3,4,5,6}; //初始密碼uchar flag_change; uchar aa;//存放密碼錯(cuò)誤的次數(shù),夠三次報(bào)警uchar bb;uchar flag_t0;void baoj1()//報(bào)警函數(shù){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();}/*密碼處理函數(shù)*/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);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_com(0x0f);//顯示光標(biāo)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);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char(" password right ");aa = 0; //清除密碼錯(cuò)誤次數(shù)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按下了關(guān)鎖}else //密碼 不正確{lcd_pos(0,0);write_com(0x0c); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char(" password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //密碼不正確報(bào)警while(i--)baoj();}}}else //密碼沒有輸入正確或完成{lcd_pos(0,0);write_com(0x0c); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char(" password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //密碼不正確報(bào)警while(i--)baoj();}}}if(key == B)//B被按下,修改密碼{read_n_add(mima_1,0x00,6);write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_com(0x0f);//顯示光標(biāo)lcd_pos(0,0);write_n_char(" input password");		write_com(0x0f);//顯示光標(biāo)	if(input_mima(mima_2)) //處理輸入密碼{if(mimaduibi(mima_2,mima_1)) //密碼正確{lcd_pos(0,0);write_com(0x0c);write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char("password right ");aa = 0; //清除密碼錯(cuò)誤次數(shù)delay_ms(1500);flag_change = 1;while(flag_change) //下面開始修改密碼{write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_com(0x0f);//顯示光標(biāo)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);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零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);//顯示光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零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);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char("password has");lcd_pos(1,0);write_n_char("change already");write_n_add(mima_3,0x00,6); //把修改的密碼存進(jìn)24C02delay_ms(2000);flag_change = 0;}else //密碼 不正確{lcd_pos(0,0);write_com(0x0c); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char("password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報(bào)警while(i--)baoj();}flag_change = 0;}}else //密碼沒有輸入正確或完成{lcd_pos(0,0);write_com(0x0c); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char(" password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報(bào)警while(i--)baoj();}}}else //密碼沒有輸入正確或完成{lcd_pos(0,0);write_com(0x0c); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char("password wrong");delay_ms(1000);flag_change = 0;aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報(bào)警while(i--)baoj();}}}}else //密碼 不正確{lcd_pos(0,0);write_com(0x0c); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char("password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報(bào)警while(i--)baoj();}}}else //密碼沒有輸入正確或完成{lcd_pos(0,0);write_com(0x0c); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char("password wrong");delay_ms(1000);aa++;if(aa == 4){aa = 0;i = 20; //3次輸入密碼不正確報(bào)警while(i--)baoj();}}		}}/*外部中斷函數(shù),恢復(fù)出廠設(shè)置,長按set鍵恢復(fù)*/void int0() interrupt 0{delay_ms(2000);if(!set){lcd_pos(0,0);write_com(0x0c); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零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); //關(guān)光標(biāo)write_com(0x06);//寫一個(gè)字符后地址指針加 1 write_com(0x01);//顯示清零,數(shù)據(jù)指針清零write_n_char("Input password");lcd_pos(1,0);write_n_char("Press key A");}// 引起了三個(gè)警告}void timer0() interrupt 1 //定時(shí)器0中斷,50ms定時(shí){TH0 = 0x4c;//50ms定時(shí)TL0 = 0x00;bb++;if(bb == 200)//10秒時(shí)間到{bb = 0;flag_t0 = 0;}}void main(){yjinit();//液晶初始化i2cinit();//24C02總線初始化baojing = 0;lock = 1; //關(guān)鎖TMOD = 0x01;//選擇定時(shí)器0方式1EA = 1;//打開總中斷ET0 = 1;//打開定時(shí)器0中斷EX0 = 1; //打開外部中斷0IT0 = 1;//下降沿觸發(fā)TR0 = 0;//關(guān)閉定時(shí)器TH0 = 0x4c;//50ms裝初值TL0 = 0x00;while(1){mimachuli(); }}


關(guān)鍵詞: 單片機(jī)電子密碼

評(píng)論


技術(shù)專區(qū)

關(guān)閉