|
仿真原理圖:http://www.zg4o1577.cn/bbs/dpj-101350-1.html
單片機(jī)源程序如下:
- /********************************************************************************
- 功能鍵
- S6---S115 數(shù)字鍵0-9
- S16---更改密碼 S17---更改密碼完畢后確認(rèn)
- S18---重試密碼、重新設(shè)定 S19---關(guān)閉密碼鎖
- 初始密碼:000000 密碼位數(shù):6位
- 注意:掉電后,所設(shè)密碼會(huì)丟失,重新上點(diǎn)時(shí),密碼恢復(fù)為原始的000000
- 與P1相連的8位發(fā)光LED點(diǎn)亮代表鎖被打開(kāi);熄滅代表鎖被鎖上
- 程序功能:
- 1、開(kāi)鎖:
- 下載程序后,直接按六次S6(即代表數(shù)字0),8位LED亮,鎖被打開(kāi),輸入密碼時(shí),
- 六位數(shù)碼管依次顯示小橫杠。
- 2、更改密碼:
- 只有當(dāng)開(kāi)鎖(LED亮)后,該功能方可使用。
- 首先按下更改密碼鍵S16,然后設(shè)置相應(yīng)密碼,此時(shí)六位數(shù)碼管會(huì)顯示設(shè)置密碼對(duì)應(yīng)
- 的數(shù)字。最后設(shè)置完六位后,按下S17確認(rèn)密碼更改,此后新密碼即生效。
- 3、重試密碼:
- 當(dāng)輸入密碼時(shí),密碼輸錯(cuò)后按下鍵S18,可重新輸入六位密碼。
- 當(dāng)設(shè)置密碼時(shí),設(shè)置中途想更改密碼,也可按下此鍵重新設(shè)置。
- 4、關(guān)閉密碼鎖:
- 按下S19即可將打開(kāi)的密碼鎖關(guān)閉。
- 推薦初級(jí)演示步驟:輸入原始密碼000000按6次S6密碼正確后LED全部亮表示鎖已打開(kāi)---按下更
- 改密碼按鍵S16---按S6到S15設(shè)置密碼---按S17
- 確認(rèn)密碼更改---按S19關(guān)閉密碼鎖---輸入新的密碼打開(kāi)密碼鎖
- *******************************************************************************/
- #include<reg52.h>
- #include <intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- uchar old1,old2,old3,old4,old5,old6; //原始密碼000000
- uchar new1,new2,new3,new4,new5,new6; //每次MCU采集到的密碼輸入
- uchar a=16,b=16,c=16,d=16,e=16,f=16; //送入數(shù)碼管顯示的變量
- uchar wei,key,temp;
- bit allow,genggai,ok,wanbi,retry,close; //各個(gè)狀態(tài)位
- sbit dula=P2^6;
- sbit wela=P2^7;
- sbit beep=P2^3;
- sbit sda=P2^0; //IO口定義
- sbit scl=P2^1;
- unsigned char code table[]=
- {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
- 0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x40};
- /*****************IIC芯片24C02存儲(chǔ)器驅(qū)動(dòng)程序************************************/
- void nop()
- {
- _nop_();
- _nop_();
- }
- /////////24C02讀寫驅(qū)動(dòng)程序////////////////////
- void delay1(unsigned int m)
- { unsigned int n;
- for(n=0;n<m;n++);
- }
- void init() //24c02初始化子程序
- {
- scl=1;
- nop();
- sda=1;
- nop();
- }
- void start() //啟動(dòng)I2C總線
- {
- sda=1;
- nop();
- scl=1;
- nop();
- sda=0;
- nop();
- scl=0;
- nop();
- }
- void stop() //停止I2C總線
- {
- sda=0;
- nop();
- scl=1;
- nop();
- sda=1;
- nop();
- }
- void writebyte(unsigned char j) //寫一個(gè)字節(jié)
- {
- unsigned char i,temp;
- temp=j;
- for (i=0;i<8;i++)
- {
- temp=temp<<1;
- scl=0;
- nop();
- sda=CY; //temp左移時(shí),移出的值放入了CY中
- nop();
- scl=1; //待sda線上的數(shù)據(jù)穩(wěn)定后,將scl拉高
- nop();
- }
- scl=0;
- nop();
- sda=1;
- nop();
- }
- unsigned char readbyte() //讀一個(gè)字節(jié)
- {
- unsigned char i,j,k=0;
- scl=0; nop(); sda=1;
- for (i=0;i<8;i++)
- {
- nop(); scl=1; nop();
- if(sda==1)
- j=1;
- else
- j=0;
- k=(k<<1)|j;
- scl=0;
- }
- nop();
- return(k);
- }
- void clock() //I2C總線時(shí)鐘
- {
- unsigned char i=0;
- scl=1;
- nop();
- while((sda==1)&&(i<255))
- i++;
- scl=0;
- nop();
- }
- ////////從24c02的地址address中讀取一個(gè)字節(jié)數(shù)據(jù)/////
- unsigned char read24c02(unsigned char address)
- {
- unsigned char i;
- start();
- writebyte(0xa0);
- clock();
- writebyte(address);
- clock();
- start();
- writebyte(0xa1);
- clock();
- i=readbyte();
- stop();
- delay1(100);
- return(i);
- }
- //////向24c02的address地址中寫入一字節(jié)數(shù)據(jù)info/////
- void write24c02(unsigned char address,unsigned char info)
- {
- start();
- writebyte(0xa0);
- clock();
- writebyte(address);
- clock();
- writebyte(info);
- clock();
- stop();
- delay1(5000); //這個(gè)延時(shí)一定要足夠長(zhǎng),否則會(huì)出錯(cuò)。因?yàn)?4c02在從sda上取得數(shù)據(jù)后,還需要一定時(shí)間的燒錄過(guò)程。
- }
- /****************************密碼鎖程序模塊********************************************************/
- void delay(unsigned char i)
- {
- uchar j,k;
- for(j=i;j>0;j--)
- for(k=125;k>0;k--);
- }
- void display(uchar a,uchar b,uchar c,uchar d,uchar e,uchar f)
- {
- dula=0;
- P0=table[a];
- dula=1;
- dula=0;
-
- wela=0;
- P0=0xfe;
- wela=1;
- wela=0;
- delay(5);
-
- P0=table[b];
- dula=1;
- dula=0;
-
- P0=0xfd;
- wela=1;
- wela=0;
- delay(5);
- P0=table[c];
- dula=1;
- dula=0;
-
- P0=0xfb;
- wela=1;
- wela=0;
- delay(5);
-
- P0=table[d];
- dula=1;
- dula=0;
-
- P0=0xf7;
- wela=1;
- wela=0;
- delay(5);
-
- P0=table[e];
- dula=1;
- dula=0;
-
- P0=0xef;
- wela=1;
- wela=0;
- delay(5);
-
- P0=table[f];
- dula=1;
- dula=0;
-
- P0=0xdf;
- wela=1;
- wela=0;
- delay(5);
- }
- void keyscan()
- {
- {
- P3=0xfe;
- temp=P3;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xee:
- key=0;
- wei++;
- break;
- case 0xde:
- key=1;
- wei++;
- break;
- case 0xbe:
- key=2;
- wei++;
- break;
- case 0x7e:
- key=3;
- wei++;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- beep=0;
- }
- beep=1;
- }
- }
- P3=0xfd;
- temp=P3;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xed:
- key=4;
- wei++;
- break;
- case 0xdd:
- key=5;
- wei++;
- break;
- case 0xbd:
- key=6;
- wei++;
- break;
- case 0x7d:
- key=7;
- wei++;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- beep=0;
- }
- beep=1;
- }
- }
- P3=0xfb;
- temp=P3;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xeb:
- key=8;
- wei++;
- break;
- case 0xdb:
- key=9;
- wei++;
- break;
-
- case 0xbb:
- genggai=1;
- wei=0;
- break;
- case 0x7b:
- if(allow)
- ok=1;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- beep=0;
- }
- beep=1;
- }
- }
- P3=0xf7;
- temp=P3;
- temp=temp&0xf0;
- if(temp!=0xf0)
- {
- delay(10);
- if(temp!=0xf0)
- {
- temp=P3;
- switch(temp)
- {
- case 0xe7:
- retry=1;
- break;
- case 0xd7:
- close=1;
- break;
- }
- while(temp!=0xf0)
- {
- temp=P3;
- temp=temp&0xf0;
- beep=0;
- }
- beep=1;
- }
- }
- }
- }
- void shumima() //對(duì)按鍵采集來(lái)的數(shù)據(jù)進(jìn)行分配
- {
- if(!wanbi)
- {
- switch(wei)
- {
- case 1:new1=key;
- if(!allow) a=17;
- else a=key; break;
- case 2:new2=key;
- if(a==17) b=17;
- else b=key; break;
- case 3:new3=key;
- if(a==17) c=17;
- else c=key; break;
- case 4:new4=key;
- if(a==17) d=17;
- else d=key; break;
- case 5:new5=key;
- if(a==17) e=17;
- else e=key; break;
- case 6:new6=key;
- if(a==17) f=17;
- else f=key;
- wanbi=1; break;
- }
- }
- }
- void yanzheng() //驗(yàn)證密碼是否正確
- {
- if(wanbi) //只有當(dāng)六位密碼均輸入完畢后方進(jìn)行驗(yàn)證
- {
- if((new1==old1)&(new2==old2)&(new3==old3)&(new4==old4)&(new5==old5)&(new6==old6))
- allow=1; //當(dāng)輸入的密碼正確,會(huì)得到allowe置一
- }
- }
- void main()
- {
-
- init(); //初始化24C02
- /*********下面的一小段程序的功能為格式化密碼存儲(chǔ)區(qū)。************
- ******當(dāng)24c02中這些存儲(chǔ)區(qū)由于其他程序的運(yùn)行而導(dǎo)致***************
- *******所存數(shù)據(jù)發(fā)生了變化,或者密碼遺忘時(shí), ********************
- ******可以刪掉其前面的注釋線,然后重新編譯下載。****************
- ******而將密碼還原為000000后,請(qǐng)將下面的程序用******************
- ******注釋屏蔽掉,重新編譯、下載,方可正常使用****************/
- // write24c02(110,0x00);
- // write24c02(111,0x00);//24c02的第110到115地址單元作為密碼存儲(chǔ)區(qū)
- // write24c02(112,0x00);
- // write24c02(113,0x00);
- // write24c02(114,0x00);
- // write24c02(115,0x00);
- /*******************************************************************/
- old1=read24c02(110);
- old2=read24c02(111);
- old3=read24c02(112);
- old4=read24c02(113);
- old5=read24c02(114);
- old6=read24c02(115);
- while(1)
- {
- keyscan();
- shumima();
- yanzheng();
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
密碼鎖(掉電可記憶密碼).rar
(36.07 KB, 下載次數(shù): 25)
2017-12-12 11:43 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|