|
單片機(jī)源程序如下:
-
- 程序功能: 本程序結(jié)合了24C02存儲器的存儲功能,可以掉電保存密碼。
- 第一次運(yùn)行時(shí),若輸入000000原始密碼后無反應(yīng),可以試驗(yàn)著將主程序中前面的
- 一小段被注釋線屏蔽的程序前的注釋線刪掉,然后重新編譯下載(可以將密碼還原為000000)。
- 此后,再將這小段程序屏蔽掉,再編譯下載。方可正常使用。
- 1、開鎖:
- 下載程序后,直接按六次S1(即代表數(shù)字1),8位LED亮,鎖被打開,輸入密碼時(shí),
- 六位數(shù)碼管依次顯示小橫杠。
- 2、更改密碼:
- 只有當(dāng)開鎖(LED亮)后,該功能方可使用。
- 首先按下更改密碼鍵S11,然后設(shè)置相應(yīng)密碼,此時(shí)六位數(shù)碼管會(huì)顯示設(shè)置密碼對應(yīng)
- 的數(shù)字。最后設(shè)置完六位后,按下S12確認(rèn)密碼更改,此后新密碼即生效。
- 3、重試密碼:
- 當(dāng)輸入密碼時(shí),密碼輸錯(cuò)后按下鍵S13,可重新輸入六位密碼。
- 當(dāng)設(shè)置密碼時(shí),設(shè)置中途想更改密碼,也可按下此鍵重新設(shè)置。
- 4、關(guān)閉密碼鎖:
- 按下S14即可將打開的密碼鎖關(guān)閉。
- 推薦初級演示步驟:輸入原始密碼000000---按下更改密碼按鍵S11---按0到9設(shè)置密碼---按S12
- 確認(rèn)密碼更改---按S14關(guān)閉密碼鎖---輸入新的密碼打開密碼鎖
- *******************************************************************************/
- #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存儲器驅(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í)一定要足夠長,否則會(huì)出錯(cuò)。因?yàn)?4c02在從sda上取得數(shù)據(jù)后,還需要一定時(shí)間的燒錄過程。
- }
- /****************************密碼鎖程序模塊********************************************************/
- 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() //對按鍵采集來的數(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
- /*********下面的一小段程序的功能為格式化密碼存儲區(qū)。************
- ******當(dāng)24c02中這些存儲區(qū)由于其他程序的運(yùn)行而導(dǎo)致***************
- *******所存數(shù)據(jù)發(fā)生了變化,或者密碼遺忘時(shí), ********************
- ******可以刪掉其前面的注釋線,然后重新編譯下載。****************
- ******而將密碼還原為000000后,請將下面的程序用******************
- ******注釋屏蔽掉,重新編譯、下載,方可正常使用****************/
- // write24c02(110,0x00);
- // write24c02(111,0x00);//24c02的第110到115地址單元作為密碼存儲區(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();
- if(allow) //驗(yàn)證完后,若allow為1,則開鎖
- {
- P1=0x00;
- if(!genggai)
- wanbi=0;
- }
- if(genggai) //當(dāng)S16更改密碼鍵被按下,genggai會(huì)被置一
- {
- if(allow) //若已經(jīng)把鎖打開,才有更改密碼的權(quán)限
- {
- while(!wanbi) //當(dāng)新的六位密碼沒有設(shè)定完,則一直在這里循環(huán)
- {
- keyscan();
- shumima();
- if(retry|close) //而當(dāng)探測到重試鍵S18或者關(guān)閉密碼鎖鍵S19被按下時(shí),則跳出
- { wanbi=1;
- break;
- }
- display(a,b,c,d,e,f);
- }
- }
- }
- if(ok) //更改密碼時(shí),當(dāng)所有六位新密碼均被按下時(shí),可以按下此鍵,結(jié)束密碼更改
- { //其他時(shí)間按下此鍵無效
- ok=0; wei=0;
- genggai=0;
- old1=new1;old2=new2;old3=new3; //此時(shí),舊的密碼將被代替
- old4=new4;old5=new5;old6=new6;
- //新密碼寫入存儲區(qū)。
- write24c02(110,old1);
- write24c02(111,old2);
- write24c02(112,old3);
- write24c02(113,old4);
- write24c02(114,old5);
- write24c02(115,old6);
- a=16;b=16;c=16;d=16;e=16;f=16;
- }
- if(retry) //當(dāng)重試按鍵S18被按下,retry會(huì)被置位
- {
- retry=0; wei=0;wanbi=0;
- a=16;b=16;c=16;d=16;e=16;f=16;
- new1=0;new2=0;new3=0;new4=0;new5=0;new6=0;
- }
- if(close) //當(dāng)關(guān)閉密碼鎖按鍵被按下,close會(huì)被置位
- {
- close=0;genggai=0;//所有變量均被清零。
- wei=0; wanbi=0;
- allow=0;
- P1=0xff;
- a=16;b=16;c=16;d=16;e=16;f=16;
- new1=0;new2=0;new3=0;new4=0;new5=0;new6=0;
- }
- display(a,b,c,d,e,f); //實(shí)時(shí)顯示
- }
- }
復(fù)制代碼
|
|