新手設計的簡易密碼鎖
單片機源程序如下:
- /**************************************************************************************
- * LCD1602液晶顯示實驗 *
- 實現現象: 具體接線操作請參考視頻教程
- 下載程序后插上LCD1602液晶在開發板上,即可顯示
- 注意事項:
- ***************************************************************************************/
- #include "reg52.h" //此文件中定義了單片機的一些特殊功能寄存器
- #include "lcd.h"
- typedef unsigned int u16; //對數據類型進行聲明定義
- typedef unsigned char u8;
- #define GPIO_DIG P0
- #define GPIO_KEY P1
- sbit beep=P3^5;
- sbit motor=P3^6;
- u8 KeyValue; //用來存放讀取到的鍵值
- int count=0; //用來存放按鍵的次數
- u8 Disp[]="set initial code";
- u8 DispCode[]="input code";
- u8 Dispcorrect[]="welcome back";
- u8 Disperror[]="catch thief";
- char code1[8],inicode[8];
- /*******************************************************************************
- * 函 數 名 : delay
- * 函數功能 : 延時函數,i=1時,大約延時10us
- *******************************************************************************/
- void delay(u16 i)
- {
- while(i--);
- }
- sbit LSA=P2^2;
- sbit LSB=P2^3;
- sbit LSC=P2^4;
- u8 code smgduan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //顯示0~F的值
- /****************************************************
- 函數名: DigDisplay 數碼管顯示函數
- 函數功能: 輸入顯示的數字和點亮的數碼管x,i;在對應的數碼管上顯示對應的數
- *****************************************************/
- void DigDisplay(int x,int i) //輸入顯示的數字和點亮的數碼管x,i
- {
-
-
-
- switch(i) //位選,選擇點亮的數碼管,
- {
- case(0):
- LSA=0;LSB=0;LSC=0; break;//顯示第0位
- case(1):
- LSA=1;LSB=0;LSC=0; break;//顯示第1位
- case(2):
- LSA=0;LSB=1;LSC=0; break;//顯示第2位
- case(3):
- LSA=1;LSB=1;LSC=0; break;//顯示第3位
- case(4):
- LSA=0;LSB=0;LSC=1; break;//顯示第4位
- case(5):
- LSA=1;LSB=0;LSC=1; break;//顯示第5位
- case(6):
- LSA=0;LSB=1;LSC=1; break;//顯示第6位
- case(7):
- LSA=1;LSB=1;LSC=1; break;//顯示第7位
- }
- P0=smgduan[x];//發送段碼
- P0=0x00;//消隱
- delay(100); //間隔一段時間掃描
-
-
- }
- /***************************************************
- 函數名:KeyDown
- 矩陣鍵盤
- 1 2 3 A
- 4 5 6 B
- 7 8 9 C
- * 0 # D
- 函數功能:
- 注意重點:成功按下一次后count記一次數,count初值為0
- ***************************************************/
- void KeyDown()
- {
- int a=0;
- int i=0;
-
- GPIO_KEY=0x0f;
- if(GPIO_KEY!=0x0f) //讀取按鍵是否按下
- {
- delay(1000); //延時10ms進行消抖
- if(GPIO_KEY!=0x0f) //再次檢測鍵盤是否按下
- {
- count++; //按鍵按下一次記一次數
- //測試列
- GPIO_KEY=0X0F;
- switch(GPIO_KEY)
- {
- case(0X07): KeyValue=1;break;
- case(0X0b): KeyValue=2;break;
- case(0X0d): KeyValue=3;break;
- case(0X0e): KeyValue=10;break;
- }
- }
- }
- //測試行
- /* 找出第四列單獨處理*/
- if(GPIO_KEY==0X0e)
- {
- GPIO_KEY=0XF0;
- switch(GPIO_KEY)
- {
- case(0X70): KeyValue=KeyValue;break;
- case(0Xb0): KeyValue=KeyValue+1;break;
- case(0Xd0): KeyValue=KeyValue+2;break;
- case(0Xe0): KeyValue=KeyValue+3;break;
- }
- }
- //第一二三列的處理
- else
- {
- GPIO_KEY=0XF0;
- //單獨處理第四行
- if(GPIO_KEY==(0Xe0)&&KeyValue==1)
- KeyValue='*';
- if(GPIO_KEY==(0Xe0)&&KeyValue==2)
- KeyValue=0;
- if(GPIO_KEY==(0Xe0)&&KeyValue==3)
- KeyValue='#';
-
- switch(GPIO_KEY)
- {
- case(0X70): KeyValue=KeyValue;break;
- case(0Xb0): KeyValue=KeyValue+3;break;
- case(0Xd0): KeyValue=KeyValue+6;break;
-
- }
- }
- while((a<50)&&(GPIO_KEY!=0xf0)) //檢測按鍵松手檢測
- {
- delay(1000);
- a++;
- }
-
-
-
-
- }
-
- /*******************************************************************************
- * 函 數 名 : main
- * 函數功能 : 主函數
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void main()
- {
-
- u8 i;
- int inicount;
- int j,flag=0,count1=0,j1=0;
- LcdInit();
- for(i=0;i<16;i++) //顯示set initial code
- {
- LcdWriteData(Disp[i]);
- delay(1000000);
- }
- delay(100000000);
- LcdWriteCom(0x01); //清屏
- KeyValue=0; //給KeyValue一個初值,避免亂碼
- motor=0; //給motor一個初值,避免亂轉
- while(KeyValue!='#'||count<8)
- { //按鍵判斷函數 輸入初始密碼 并存入inicode
- KeyDown();
-
- if(inicount!=count)
- {
- inicode[count1]=KeyValue;
- count1++;
- beep=0;
- delay(200);
- beep=1;
-
-
- }
-
- DigDisplay(count1,4);
- DigDisplay(KeyValue,0);
- inicount=count;
-
- }
-
-
- i=0;
- while(i<200) //在數碼管顯示inicode
- {
- for(j=0;j<8;j++)
- DigDisplay(inicode[j],j);
-
- i++;
- }
- while(1)
- {
-
- for(i=0;i<10;i++)
- {
- LcdWriteData(DispCode[i]);
- delay(10000);
- }
-
- KeyValue=0;
- count=0;
- count1=0;
- inicount=0;
- P0=0X00;
- flag=0;
- while(KeyValue!='#'||count<8)
- { //按鍵判斷函數 輸入密碼并存入code1
- KeyDown();
-
- if(inicount!=count)
- {
- code1[count1]=KeyValue;
- count1++;
- beep=0;
- delay(200);
- beep=1;
- }
- DigDisplay(count1,4);
- DigDisplay(KeyValue,0);
- inicount=count;
-
- }
-
-
- i=0;
-
- while(i<200)
- {
- for(j=0;j<8;j++)
- DigDisplay(code1[j],j);
- i++;
- }
-
- P0=0X00; LcdWriteCom(0x01); //清屏
- flag=1;
- for(j=0;j<8;j++)
- {
- if(code1[j]==inicode[j]) //密碼匹配
- ++flag;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
密碼鎖.zip
(51.32 KB, 下載次數: 55)
2017-11-11 16:56 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|