|
- /************************************************
- 最近學了下RC522射頻讀寫卡的方式,但是自己怎么寫都不成功,于是在網(wǎng)上查找了一些文件,但是在燒錄到單片機(普中科技的51單片機)上出現(xiàn)了以下問題
- 1.除了管理員密碼不管輸入什么密碼都會報錯然后進入鎖鍵盤;
- 2.輸入管理員密碼后密碼不會被初始化(即六個0);
- 3.輸入讀寫卡管理密碼后(131420)后,會一直卡主,如果按設定的A鍵會進行換頁的形式,按設定的C鍵會出現(xiàn)一行字母(看不懂寫的什么),按B鍵沒反應,按D鍵報錯;
- 4.IC 卡靠上去好像沒有反應。
- 求助各位大佬講解講解,以下是運行時的圖片和代碼:
- **************************************************/
- #include <REG52.h>
- #include<intrins.h>
- #include "main.h"
- #include<rc522.c>
- #include<eeprom52.h>
- #define LCM_Data P0
- #define uchar unsigned char
- #define uint unsigned int
- //#define w 6 //定義密碼位數(shù)
- //時間計算
- #define Imax 14000 //此處為晶振為11.0592時的取值,
- #define Imin 8000 //如用其它頻率的晶振時,
- #define Inum1 1450 //要改變相應的取值。
- #define Inum2 700
- #define Inum3 3000
- sbit lcd1602_rs=P2^6;
- sbit lcd1602_rw=P2^5;
- sbit lcd1602_en=P2^7;
- sbit ALAM = P2^1; //報警
- sbit KEY = P3^6; //開鎖
- bit operation=0; //操作標志位
- bit pass=0; //密碼正確標志
- bit ReInputEn=0; //重置輸入充許標志
- bit s3_keydown=0; //3秒按鍵標志位
- bit key_disable=0; //鎖定鍵盤標志
- bit chushihua=0; //可以注冊卡標志位
- unsigned char countt0,second; //t0中斷計數(shù)器,秒計數(shù)器
- //解碼變量
- unsigned char Im[4]={0x00,0x00,0x00,0x00};
- //全局變量
- uchar f,a_a;
- unsigned long m,Tc;
- unsigned char IrOK;
- unsigned char xdata CardRevBuf[16] = { 0 };
- //void Delay5Ms(void);
- unsigned char code a[]={0xFE,0xFD,0xFB,0xF7}; //控盤掃描控制表
- unsigned char code start_line[] = {"password: "};
- unsigned char code name[] = {"===Coded Lock==="}; //顯示名稱
- unsigned char code Correct[] = {" correct "}; //輸入正確
- unsigned char code Error1[] = {" error "}; //輸入錯誤
- unsigned char code codepass[] = {" pass "};
- unsigned char code LockOpen[] = {" open "}; //OPEN
- unsigned char code SetNew[] = {"SetNewWordEnable"};
- unsigned char code Input[] = {"input: "}; //INPUT
- unsigned char code ResetOK[] = {"ResetPasswordOK "};
- unsigned char code initword[] = {"Init password..."};
- unsigned char code Er_try[] = {"error,try again!"};
- unsigned char code again[] = {"input again "};
- unsigned char InputData[6]; //輸入密碼暫存區(qū)
- unsigned char CurrentPassword[6]={1,3,1,4,2,0}; //當前密碼值
- unsigned char TempPassword[6];
- unsigned char N=0; //密碼輸入位數(shù)記數(shù)
- unsigned char ErrorCont; //錯誤次數(shù)計數(shù)
- unsigned char CorrectCont; //正確輸入計數(shù)
- unsigned char ReInputCont; //重新輸入計數(shù)
- unsigned char code initpassword[6]={0,0,0,0,0,0};
- unsigned char current_id;
- char xdata DisplayBuf[10]; //當前讀出卡的ID
- char xdata DisplayBuf_c[10]; //當前單片機里存的卡ID
- void write_eeprom()
- {
- SectorErase(0x2000); //清空
- byte_write(0x2000,TempPassword[0]);
- byte_write(0x2011,TempPassword[1]);
- byte_write(0x2002,TempPassword[2]);
- byte_write(0x2003,TempPassword[3]);
- byte_write(0x2004,TempPassword[4]);
- byte_write(0x2005,TempPassword[5]);
- byte_write(0x2060,0x01);
- }
- void write_eeprom_j(unsigned char j) //將對應的ID寫入到單片機
- {
- SectorErase(0x2210+j*0x0200); //清空
- byte_write(0x2200+j*0x0200,DisplayBuf[0]);
- byte_write(0x2201+j*0x0200,DisplayBuf[1]);
- byte_write(0x2202+j*0x0200,DisplayBuf[2]);
- byte_write(0x2203+j*0x0200,DisplayBuf[3]);
- byte_write(0x2204+j*0x0200,DisplayBuf[4]);
- byte_write(0x2205+j*0x0200,DisplayBuf[5]);
- byte_write(0x2206+j*0x0200,DisplayBuf[6]);
- byte_write(0x220b+j*0x0200,DisplayBuf[7]);
- }
- void write_eeprom_jj(unsigned char j) //將對應的ID清空
- {
- SectorErase(0x2200+j*0x0200); //清空
- byte_write(0x2200+j*0x0200,' ');
- byte_write(0x2201+j*0x0200,' ');
- byte_write(0x2202+j*0x0200,' ');
- byte_write(0x2203+j*0x0200,' ');
- byte_write(0x2204+j*0x0200,' ');
- byte_write(0x2205+j*0x0200,' ');
- byte_write(0x2206+j*0x0200,' ');
- byte_write(0x2207+j*0x0200,' ');
- }
- void read_eeprom_j(uchar j)
- {
- DisplayBuf_c[0]= byte_read(0x2200+j*0x0200);
- DisplayBuf_c[1]= byte_read(0x2201+j*0x0200);
- DisplayBuf_c[2]= byte_read(0x2202+j*0x0200);
- DisplayBuf_c[3]= byte_read(0x2203+j*0x0200);
- DisplayBuf_c[4]= byte_read(0x2104+j*0x0200);
- DisplayBuf_c[5]= byte_read(0x2205+j*0x0200);
- DisplayBuf_c[6]= byte_read(0x2206+j*0x0200);
- DisplayBuf_c[7]= byte_read(0x2207+j*0x0200);
- }
- /******************把數(shù)據(jù)從單片機內部eeprom中讀出來*****************/
- void read_eeprom()
- {
- CurrentPassword[0]= byte_read(0x2000);
- CurrentPassword[1]= byte_read(0x2001);
- CurrentPassword[2]= byte_read(0x2002);
- CurrentPassword[3]= byte_read(0x2023);
- CurrentPassword[4]= byte_read(0x2004);
- CurrentPassword[5]= byte_read(0x2005);
- a_a = byte_read(0x2060);
-
- }
- /**************開機自檢eeprom初始化*****************/
- void init_eeprom()
- {
- uchar i,j;
- a_a = byte_read(0x2060);
- if(a_a != 1) //新的單片機初始單片機內問eeprom
- {
-
- a_a = 1;
- for(i=0;i<6;i++)
- {
- SectorErase(0x2200+i*0x0200);
- for(j=0;j<8;j++)
- byte_write(0x2200+i*0x0200+j,' ');
- }
- write_eeprom(); //保存數(shù)據(jù)
- }
- }
- //=====================5ms延時==============================
- void Delay5Ms()
- {
- unsigned int TempCyc = 5552;
- while(TempCyc--);
- }
- //===================400ms延時==============================
- void Delay400Ms()
- {
- unsigned char TempCycA = 5;
- unsigned int TempCycB;
- while(TempCycA--)
- {
- TempCycB=7269;
- while(TempCycB--);
- }
- }
- //==================================================================================================
- //=======================================LCD1602====================================================
- //==================================================================================================
- #define yi 0x80 //LCD第一行的初始位置,因為LCD1602字符地址首位D7恒定為1(100000000=80)
- #define er 0x80+0x40 //LCD第二行初始位置(因為第二行第一個字符位置地址是0x40)
- //----------------延時函數(shù),后面經(jīng)常調用----------------------
- void delay(uint xms)//延時函數(shù),有參函數(shù)
- {
- uint x,y;
- for(x=xms;x>0;x--)
- for(y=110;y>0;y--);
- }
- //--------------------------寫指令---------------------------
- void write_1602com(uchar com)//****液晶寫入指令函數(shù)****
- {
- lcd1602_rs=0;//數(shù)據(jù)/指令選擇置為指令
- lcd1602_rw=0; //讀寫選擇置為寫
- P0=com;//送入數(shù)據(jù)
- delay(1);
- lcd1602_en=1;//拉高使能端,為制造有效的下降沿做準備
- delay(1);
- lcd1602_en=0;//en由高變低,產生下降沿,液晶執(zhí)行命令
- }
- //-------------------------寫數(shù)據(jù)-----------------------------
- void write_1602dat(uchar dat)//***液晶寫入數(shù)據(jù)函數(shù)****
- {
- lcd1602_rs=1;//數(shù)據(jù)/指令選擇置為數(shù)據(jù)
- lcd1602_rw=0; //讀寫選擇置為寫
- P0=dat;//送入數(shù)據(jù)
- delay(1);
- lcd1602_en=1; //en置高電平,為制造下降沿做準備
- delay(1);
- lcd1602_en=0; //en由高變低,產生下降沿,液晶執(zhí)行命令
- }
- //-------------------------初始化-------------------------
- void lcd_init()
- {
- write_1602com(0x01);//清顯示
- write_1602com(0x38);//設置液晶工作模式,意思:16*2行顯示,5*7點陣,8位數(shù)據(jù)
- write_1602com(0x0c);//開顯示不顯示光標
- write_1602com(0x06);//整屏不移動,光標自動右移
- }
- //========================================================================================
- //=========================================================================================
- //==============將按鍵值編碼為數(shù)值=========================
- unsigned char coding(unsigned char m1,unsigned char hh)
- {
- unsigned char k;
- if(IrOK==1)
- {
- IrOK=0;
- switch(m1)
- {
- case (0x0c): k=1;break;
- case (0x18): k=2;break;
- case (0x5e): k=3;break;
- // case (0xff): k='A';break; //
- case (0x08): k=4;break;
- case (0x1c): k=5;break;
- case (0x5a): k=6;break;
- // case (0x82): k='B';break; //
- case (0x42): k=7;break;
- case (0x55): k=8;break;
- case (0x4a): k=9;break;
- // case (0x84): k='C';break; //
- case (0x43): k='*';break; //撤銷
- case (0x16): k=0;break;
- case (0x15): k='#';break; //確認鍵
- case (0x0d): k='D';break; //重設密碼
- case (0x45): k='B';break; //
- case (0x41): k='C';break; //
- case (0x44): k='A';break; //
- case (0x40): k='A';break; //
- case (0x07): k='A';break; //
- case (0x09): k='A';break; //
- case (0x19): k='A';break; //
- // default:;
- }
- }
- else
- {
- switch(hh)
- {
- case (0x11): k=1;break;
- case (0x21): k=2;break;
- case (0x41): k=3;break;
- case (0x81): k='A';break;
- case (0x12): k=4;break;
- case (0x22): k=5;break;
- case (0x42): k=6;break;
- case (0x82): k='B';break;
- case (0x14): k=7;break;
- case (0x24): k=8;break;
- case (0x44): k=9;break;
- case (0x84): k='C';break;
- case (0x18): k='*';break;
- case (0x28): k=0;break;
- case (0x48): k='#';break;
- case (0x88): k='D';break;
- // default:;
- }
- }
- return(k);
- }
- //=====================按鍵檢測并返回按鍵值===============================
- unsigned char keynum()
- {
- unsigned char row,col,i;
- P1=0xf0;
- if((P1&0xf0)!=0xf0)
- {
- Delay5Ms();
- Delay5Ms();
- if((P1&0xf0)!=0xf0)
- {
- row=P1^0xf0; //確定行線
- i=0;
- P1=a[i]; //精確定位
- while(i<4)
- {
- if((P1&0xf0)!=0xf0)
- {
- col=~(P1&0xff); //確定列線
- break; //已定位后提前退出
- }
- else
- {
- i++;
- P1=a[i];
- }
- }
- }
- else
- {
- return 0;
- }
-
- while((P1&0xf0)!=0xf0);
- return (row|col); //行線與列線組合后返回
- }
- else return 0; //無鍵按下時返回0
- }
- //=======================一聲提示音,表示有效輸入========================
- void OneAlam()
- {
- ALAM=0;
- Delay5Ms();
- ALAM=1;
- }
- //========================二聲提示音,表示操作成功========================
- void TwoAlam()
- {
- ALAM=0;
- Delay5Ms();
- ALAM=1;
- Delay5Ms();
- ALAM=0;
- Delay5Ms();
- ALAM=1;
- }
- //========================三聲提示音,表示錯誤========================
- void ThreeAlam()
- {
- ALAM=0;
- Delay5Ms();
- ALAM=1;
- Delay5Ms();
- ALAM=0;
- Delay5Ms();
- ALAM=1;
- Delay5Ms();
- ALAM=0;
- Delay5Ms();
- ALAM=1;
- }
- //=====================顯示輸入的N個數(shù)字,用H代替以便隱藏============================
- void DisplayOne()
- {
- // DisplayOneChar(9+N,1,'*');
- write_1602com(yi+5+N);
- write_1602dat('*');
- }
- //=======================顯示提示輸入=========================
- void DisplayChar()
- {
- unsigned char i;
- if(pass==1)
- {
- //DisplayListChar(0,1,LockOpen);
- write_1602com(er);
- for(i=0;i<16;i++)
- {
- write_1602dat(LockOpen[i]);
- }
- }
- else
- {
- if(N==0)
- {
- //DisplayListChar(0,1,Error);
- write_1602com(er);
- for(i=0;i<16;i++)
- {
- write_1602dat(Error1[i]);
- }
- }
- else
- {
- //DisplayListChar(0,1,start_line);
- write_1602com(er);
- for(i=0;i<16;i++)
- {
- write_1602dat(start_line[i]);
- }
- }
- }
- }
- void DisplayInput()
- {
- unsigned char i;
- if(CorrectCont==1)
- {
- //DisplayListChar(0,0,Input);
- write_1602com(er);
- for(i=0;i<16;i++)
- {
- write_1602dat(Input[i]);
- }
- }
- }
- //========================重置密碼==================================================
- //==================================================================================
- void ResetPassword()
- {
- unsigned char i;
- unsigned char j;
- if(pass==0)
- {
- pass=0;
- DisplayChar();
- ThreeAlam();
- }
- else
- {
- if(ReInputEn==1)
- {
- if(N==6)
- {
- ReInputCont++;
- if(ReInputCont==2)
- {
- for(i=0;i<6;)
- {
- if(TempPassword[i]==InputData[i]) //將兩次輸入的新密碼作對比
- i++;
- else
- {
- //DisplayListChar(0,1,Error);
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(Error1[j]);
- }
- ThreeAlam(); //錯誤提示
- pass=0;
- ReInputEn=0; //關閉重置功能,
- ReInputCont=0;
- DisplayChar();
- break;
- }
- }
- if(i==6)
- {
- //DisplayListChar(0,1,ResetOK);
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(ResetOK[j]);
- }
- TwoAlam(); //操作成功提示
- write_eeprom();
- ReInputEn=0;
- }
- ReInputCont=0;
- CorrectCont=0;
- }
- else
- {
- OneAlam();
- //DisplayListChar(0, 1, again); //顯示再次輸入一次
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(again[j]);
- }
- for(i=0;i<6;i++)
- {
- TempPassword[i]=InputData[i]; //將第一次輸入的數(shù)據(jù)暫存起來
- }
- }
- N=0; //輸入數(shù)據(jù)位數(shù)計數(shù)器清零
- }
- }
- }
- }
- //=======================輸入密碼錯誤超過三過,報警并鎖死鍵盤======================
- void Alam_KeyUnable()
- {
- P1=0x00;
- {
- ALAM=~ALAM;
- Delay5Ms();
- }
- }
- //=======================取消所有操作============================================
- void Cancel()
- {
- unsigned char i;
- unsigned char j;
- //DisplayListChar(0, 1, start_line);
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(start_line[j]);
- }
- TwoAlam(); //提示音
- for(i=0;i<6;i++)
- {
- InputData[i]=0;
- }
- KEY=1; //關閉鎖
- ALAM=1; //報警關
- operation=0; //操作標志位清零
- pass=0; //密碼正確標志清零
- ReInputEn=0; //重置輸入充許標志清零
- // ErrorCont=0; //密碼錯誤輸入次數(shù)清零
- CorrectCont=0; //密碼正確輸入次數(shù)清零
- ReInputCont=0; //重置密碼輸入次數(shù)清零
- s3_keydown=1;
- key_disable=0;
- N=0; //輸入位數(shù)計數(shù)器清零
- }
- //==========================確認鍵,并通過相應標志位執(zhí)行相應功能===============================
- void Ensure()
- {
- unsigned char i,j;
- // RdFromROM(CurrentPassword,0,6); //從24C02里讀出存儲密碼
- read_eeprom();
- if(N==6)
- {
- if(ReInputEn==0) //重置密碼功能未開啟
- {
- for(i=0;i<6;)
- {
- if(CurrentPassword[i]==InputData[i])
- {
- i++;
- }
- else
- {
- i=7;
- ErrorCont++;
- if(ErrorCont>=3&&KEY==1) //錯誤輸入計數(shù)達三次時,報警并鎖定鍵盤
- {
- write_1602com(er);
- for(i=0;i<16;i++)
- {
- write_1602dat(Error1[i]);
- }
- Alam_KeyUnable();
- TR0=1; //開啟定時
- key_disable=1; //鎖定鍵盤
- pass=0;
- break;
- }
- }
- }
- if(i==6)
- {
- CorrectCont++;
- if(CorrectCont==1) //正確輸入計數(shù),當只有一次正確輸入時,開鎖,
- {
- //DisplayListChar(0,1,LockOpen);
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(LockOpen[j]);
- }
- TwoAlam(); //操作成功提示音
- ErrorCont=0;
- KEY=0; //開鎖
- pass=1; //置正確標志位
- TR0=1; //開啟定時
- for(j=0;j<6;j++) //將輸入清除
- {
- InputData[i]=0;
- }
- }
- else //當兩次正確輸入時,開啟重置密碼功能
- {
- //DisplayListChar(0,1,SetNew);
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(SetNew[j]);
- }
- TwoAlam(); //操作成功提示
- ReInputEn=1; //允許重置密碼輸入
- CorrectCont=0; //正確計數(shù)器清零
- }
- }
-
- else //=========================當?shù)谝淮问褂没蛲浢艽a時可以用131420對其密碼初始化============
- {
- if((InputData[0]==1)&&(InputData[1]==3)&&(InputData[2]==1)&&(InputData[3]==4)&&(InputData[4]==2)&&(InputData[5]==0))
- {
- // WrToROM(initpassword,0,6); //強制將初始密碼寫入24C02存儲
- //DisplayListChar(0,1,initword); //顯示初始化密碼
-
- TwoAlam();
- Delay400Ms();
- TwoAlam();
- N=0;
- chushihua=1;
- }
- else if((InputData[0]==1)&&(InputData[1]==3)&&(InputData[2]==1)&&(InputData[3]==4)&&(InputData[4]==2)&&(InputData[5]==1))
- {
- // WrToROM(initpassword,0,6); //強制將初始密碼寫入24C02存儲
- TempPassword[0]=0;
- TempPassword[1]=0;
- TempPassword[2]=0;
- TempPassword[3]=0;
- TempPassword[4]=0;
- TempPassword[5]=0;
- write_eeprom();
- //DisplayListChar(0,1,initword); //顯示初始化密碼
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(initword[j]);
- }
- TwoAlam();
- Delay400Ms();
- TwoAlam();
- N=0;
-
- }
- else
- {
- //DisplayListChar(0,1,Error);
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(Error1[j]);
- }
- ThreeAlam(); //錯誤提示音
- pass=0;
- }
- }
- }
- else //當已經(jīng)開啟重置密碼功能時,而按下開鎖鍵,
- {
- //DisplayListChar(0,1,Er_try);
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(Er_try[j]);
- }
- ThreeAlam();
- }
- }
- else
- {
- //DisplayListChar(0,1,Error);
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(Error1[j]);
- }
- ThreeAlam(); //錯誤提示音
- pass=0;
- }
-
- N=0; //將輸入數(shù)據(jù)計數(shù)器清零,為下一次輸入作準備
- operation=1;
- }
- void display_id( unsigned char xdata * SrcBuf )
- {
- char xdata *pDisplayChar;
- unsigned char xdata Tmp, i;
- pDisplayChar = DisplayBuf;
- for( i = 0; i < 4; i++ )
- {
- Tmp = ( ( *SrcBuf ) >> 4 ) & 0x0F;
- if( ( Tmp >=0 ) && ( Tmp <= 9 ) )
- {
- *pDisplayChar ++ = '0' + Tmp;
- }
- else
- {
- *pDisplayChar ++ = 'A' + Tmp - 10;
- }
- Tmp = ( *SrcBuf ) & 0x0F;
- if( ( Tmp >=0 ) && ( Tmp <= 9 ) )
- {
- *pDisplayChar ++ = '0' + Tmp;
- }
- else
- {
- *pDisplayChar ++ = 'A' + Tmp - 10;
- }
- SrcBuf ++;
- }
- *pDisplayChar ++ = '\0';
- // DisplayListChar( 0, 4, DisplayBuf );
- if(chushihua==1)
- {
- write_1602com(0xc0);
- write_1602dat('I');
- write_1602dat('D');
- write_1602dat(current_id+0x30);
- write_1602dat(':');
- write_1602dat(DisplayBuf[0]);
- write_1602dat(DisplayBuf[1]);
- write_1602dat(DisplayBuf[2]);
- write_1602dat(DisplayBuf[3]);
- write_1602dat(DisplayBuf[4]);
- write_1602dat(DisplayBuf[5]);
- write_1602dat(DisplayBuf[6]);
- write_1602dat(DisplayBuf[7]);
- write_1602dat(' ');
- write_1602dat(' ');write_1602dat(' ');write_1602dat(' ');write_1602dat(' ');
- }
- }
- void display_no_id()
- {
- write_1602com(0xc0);
- write_1602dat('I');
- write_1602dat('D');
- write_1602dat(current_id+0x30);
- write_1602dat(':');
- write_1602dat(DisplayBuf_c[0]);
- write_1602dat(DisplayBuf_c[1]);
- write_1602dat(DisplayBuf_c[2]);
- write_1602dat(DisplayBuf_c[3]);
- write_1602dat(DisplayBuf_c[4]);
- write_1602dat(DisplayBuf_c[5]);
- write_1602dat(DisplayBuf_c[6]);
- write_1602dat(DisplayBuf_c[7]);
- write_1602dat(' ');
- write_1602dat(' ');write_1602dat(' ');write_1602dat(' ');write_1602dat(' ');
- }
- void ctrl( void )
- {
- uchar j,i;
- if( PcdRequest( PICC_REQIDL, &CardRevBuf[0] ) != MI_OK )//尋天線區(qū)內未進入休眠狀態(tài)的卡,返回卡片類型 2字節(jié)
- {
- if( PcdRequest( PICC_REQIDL, &CardRevBuf[0] ) != MI_OK )//尋天線區(qū)內未進入休眠狀態(tài)的卡,返回卡片類型 2字節(jié)
- {
- if(chushihua==1)
- {
- read_eeprom_j(current_id);
- display_no_id();
- }
- return;
- }
- }
- if( PcdAnticoll( &CardRevBuf[2] ) != MI_OK ) //防沖撞,返回卡的序列號 4字節(jié)
- {
- if(chushihua==1)
- {
- read_eeprom_j(current_id);
- display_no_id();
- }
- return;
- }
- if( PcdSelect( &CardRevBuf[2] ) != MI_OK )//選卡
- {
- if(chushihua==1)
- {
- read_eeprom_j(current_id);
- display_no_id();
- }
- return;
- }
- display_id( &CardRevBuf[2] );
- if(CorrectCont==0&&pass==0)
- {
- for(j=0;j<6;j++)
- {
- read_eeprom_j(j);
- if(DisplayBuf[0]==DisplayBuf_c[0]&&DisplayBuf[1]==DisplayBuf_c[1]&&DisplayBuf[2]==DisplayBuf_c[2]&&DisplayBuf[3]==DisplayBuf_c[3]&&DisplayBuf[4]==DisplayBuf_c[4]&&DisplayBuf[5]==DisplayBuf_c[5])
- {
-
- CorrectCont=1;
- write_1602com(er);
- for(j=0;j<16;j++)
- {
- write_1602dat(LockOpen[j]);
- }
- TwoAlam(); //操作成功提示音
- ErrorCont=0;
- KEY=0; //開鎖
- pass=1; //置正確標志位
- TR0=1; //開啟定時
- for(j=0;j<6;j++) //將輸入清除
- {
- InputData[i]=0;
- }
- return;
- }
- }
- }
- }
- //==============================主函數(shù)===============================
- void main()
- {
- unsigned char KEY,NUM;
- unsigned char i,j;
- P1=0xFF;
- EA=1;
- TMOD=0x01;
- IT1=1;//下降沿有效
- EX1=1;//外部中斷1開
-
- TH0=0;//T0賦初值
- TL0=0;
- TR0=0;//t0開始計時
- TL1=0x3c;
- TH1=0xb0;
- ET1=1;
- TR1=1;
- Delay400Ms(); //啟動等待,等LCM講入工作狀態(tài)
- lcd_init(); //LCD初始化
- write_1602com(yi);//日歷顯示固定符號從第一行第0個位置之后開始顯示
- init_rc522();
- init_eeprom();
- read_eeprom();
- for(i=0;i<16;i++)
- {
- write_1602dat(name[i]);//向液晶屏寫日歷顯示的固定符號部分
- }
- write_1602com(er);//時間顯示固定符號寫入位置,從第2個位置后開始顯示
- for(i=0;i<16;i++)
- {
- write_1602dat(start_line[i]);//寫顯示時間固定符號,兩個冒號
- }
- write_1602com(er+9); //設置光標位置
- write_1602com(0xf0); //設置光標為閃爍
- Delay5Ms(); //延時片刻(可不要)
- N=0; //初始化數(shù)據(jù)輸入位數(shù)
- while(1)
- {
-
- ctrl();
- if(key_disable==1)
- Alam_KeyUnable();
- else
- ALAM=1; //關報警
- KEY=keynum();
- if(KEY!=0||IrOK==1)
- {
- if(key_disable==1)
- {
- second=0;
- }
- else
- {
- NUM=coding(Im[2],KEY);
- {
- switch(NUM)
- {
- case ('A'): if(chushihua==1)
- {
- current_id++;
- if(current_id>5)
- current_id=0;
- } break;
- case ('B'): if(chushihua==1)
- write_eeprom_jj(current_id) ; break;
- case ('C'): if(chushihua==1)
- write_eeprom_j(current_id) ; break;
- case ('D'): chushihua=0;ResetPassword(); break; //重新設置密碼
- case ('*'): chushihua=0;Cancel(); break; //取消當前輸入
- case ('#'): chushihua=0; Ensure(); break; //確認鍵,
- default:
- {
- chushihua=0;
- //DisplayListChar(0,1,Input);
- write_1602com(er);
- for(i=0;i<16;i++)
- {
- write_1602dat(Input[i]);
- }
- operation=0;
- if(N<6) //當輸入的密碼少于6位時,接受輸入并保存,大于6位時則無效。
- {
- OneAlam(); //按鍵提示音
- //DisplayOneChar(6+N,1,'*');
- for(j=0;j<=N;j++)
- {
- write_1602com(er+6+j);
- write_1602dat('*');
- }
- InputData[N]=NUM;
- N++;
- }
- else //輸入數(shù)據(jù)位數(shù)大于6后,忽略輸入
- {
- N=6;
- break;
- }
- }
- }
- }
- }
- }
- }
- }
- //*********************************中斷服務函數(shù)**************************************
- void time1_int() interrupt 3
- {
- TL1=0x3c;
- TH1=0xb0;
- countt0++;
- if(countt0==20)
- {
- countt0=0;
- second++;
- if(pass==1)
- {
- if(second==1)
- {
- TR1=0; //關定時器
- TL1=0x3c;
- TH1=0xb0;
- second=0;
- }
- }
- else
- {
- if(second==3)
- {
- TR1=0;
- second=0;
- key_disable=0;
- s3_keydown=0;
- TL1=0x3c;
- TH1=0xb0;
- }
- else
- TR0=1;
- }
-
- }
- }
- //外部中斷解碼程序_外部中斷0
- void intersvr1() interrupt 2 using 1
- {
- TR0=1;
- Tc=TH0*256+TL0/256;//提取中斷時間間隔時長
- TH0=0;
- TL0=0; //定時中斷重新置零
- if((Tc>Imin)&&(Tc<Imax))
- {
- m=0;
- f=1;
- return;
- } //找到啟始碼
- if(f==1)
- {
- if(Tc>Inum1&&Tc<Inum3)
- {
- Im[m/8]=Im[m/8]>>1|0x80; m++;
- }
- if(Tc>Inum2&&Tc<Inum1)
- {
- Im[m/8]=Im[m/8]>>1; m++; //取碼
- }
- if(m==32)
- {
- m=0;
- f=0;
- if(Im[2]==~Im[3])
- {
- IrOK=1;
- TR0=0;
- }
- else IrOK=0; //取碼完成后判斷讀碼是否正確
- } //準備讀下一碼
- }
- }
復制代碼
|
|