基于開發(fā)板設計的開機密碼鎖,功能如下:上電顯示----,密碼輸入正確,停留0.5秒然后顯示按鍵值,10秒沒有操作或者密碼輸入錯誤都返回----,適合想把數(shù)碼管和按鍵結合起來的學習,還有不同的窗口。
單片機源程序如下:
- #include<reg52.h>
- #define const_key_time 20
- #define const_no_key_push 4400
- #define const_05s 220
- void SystemInitial();
- void PeripheralInitial();
- void delay_short(unsigned char ucDelayShort);
- void delay_long(unsigned char ucDelayLong);
- void hc595_drive(unsigned char ucStatusTemp16_09,unsigned char ucStatusTemp08_01);
- void key_scan();
- void key_service();
- void display_drive();
- void display_service();
- void num_key_input(unsigned char ucWhichKey);
- sbit key_dr1=P0^0;
- sbit key_dr2=P0^1;
- sbit key_dr3=P0^2;
- sbit key_dr4=P0^3;
- sbit key_sr1=P0^4;
- sbit key_sr2=P0^5;
- sbit key_sr3=P0^6;
- sbit key_sr4=P0^7;
- sbit srclk_dr=P3^6;
- sbit rclk_dr=P3^5;
- sbit ser_dr=P3^4;
- unsigned char ucRowRecord=1;
- unsigned char ucKeyStep=1;
- unsigned char ucKeySec=0;
- unsigned char ucKeyLock=0;
- unsigned int uiKeyTimeCnt=0;
- unsigned char ucTemp=0;
- unsigned char ucDisplayDriveStep=1;
- unsigned char ucDig1;
- unsigned char ucDig2;
- unsigned char ucDig3;
- unsigned char ucDig4;
- unsigned char ucWd=1;
- unsigned char ucWd1Update=1;
- unsigned char ucWd2Update=0;
- unsigned char ucWd3Update=0;
- unsigned char ucPasswordInput[4];
- unsigned char ucPasswordCnt=0;
- unsigned char ucKeyNum=1;
- unsigned int uiNoKeyPushTimer=const_no_key_push;
- unsigned int uiPasswordTimer=const_05s;
- unsigned char ucTemp1=0;
- unsigned char ucTemp2=0;
- unsigned char ucTemp3=0;
- unsigned char ucTemp4=0;
- unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40,0x73};
- void main()
- {
- SystemInitial();
- delay_long(100);
- PeripheralInitial();
- while(1)
- {
- key_service();
- display_service();
- }
- }
- void display_service()
- {
- switch(ucWd)
- {
- case 1: if(ucWd1Update==1)
- {
- ucWd1Update=0;
- ucDig4=ucPasswordInput[0];
- ucDig3=ucPasswordInput[1];
- ucDig2=ucPasswordInput[2];
- ucDig1=ucPasswordInput[3];
- }
- break;
- case 2: if(ucWd2Update==1)
- {
- ucWd2Update=0;
- ucDig4=10;
- ucDig3=10;
- ucDig2=10;
- ucDig1=ucKeyNum;
- }
- break;
- case 3: if(ucWd3Update==1)
- {
- ucWd3Update=0;
- ucDig4=ucPasswordInput[0];
- ucDig3=ucPasswordInput[1];
- ucDig2=ucPasswordInput[2];
- ucDig1=ucPasswordInput[3];
- }
- break;
- }
- }
- void key_scan()
- {
- switch(ucKeyStep)
- {
- case 1: if(ucRowRecord==1)
- {
- key_dr1=0;
- key_dr2=1;
- key_dr3=1;
- key_dr4=1;
- }
- else if(ucRowRecord==2)
- {
- key_dr1=1;
- key_dr2=0;
- key_dr3=1;
- key_dr4=1;
- }
- else if(ucRowRecord==3)
- {
- key_dr1=1;
- key_dr2=1;
- key_dr3=0;
- key_dr4=1;
- }
- else
- {
- key_dr1=1;
- key_dr2=1;
- key_dr3=1;
- key_dr4=0;
- }
- uiKeyTimeCnt=0;
- ucKeyStep++;
- break;
- case 2: uiKeyTimeCnt++;
- if(uiKeyTimeCnt>1)
- {
- uiKeyTimeCnt=0;
- ucKeyStep++;
- }
- break;
- case 3: if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==1)
- {
- ucKeyStep=1;
- ucKeyLock=0;
- uiKeyTimeCnt=0;
- ucRowRecord++;
- if(ucRowRecord>4)
- ucRowRecord=1;
- }
- else if(ucKeyLock==0)
- {
- if(key_sr1==0&&key_sr2==1&&key_sr3==1&&key_sr4==1)
- {
- uiKeyTimeCnt++;
- if(uiKeyTimeCnt>const_key_time)
- {
- uiKeyTimeCnt=0;
- ucKeyLock=1;
- if(ucRowRecord==1)
- ucKeySec=1;
- else if(ucRowRecord==2)
- ucKeySec=2;
- else if(ucRowRecord==3)
- ucKeySec=3;
- else
- ucKeySec=4;
- }
- }
- if(key_sr1==1&&key_sr2==0&&key_sr3==1&&key_sr4==1)
- {
- uiKeyTimeCnt++;
- if(uiKeyTimeCnt>const_key_time)
- {
- uiKeyTimeCnt=0;
- ucKeyLock=1;
- if(ucRowRecord==1)
- ucKeySec=5;
- else if(ucRowRecord==2)
- ucKeySec=6;
- else if(ucRowRecord==3)
- ucKeySec=7;
- else
- ucKeySec=8;
- }
- }
- if(key_sr1==1&&key_sr2==1&&key_sr3==0&&key_sr4==1)
- {
- uiKeyTimeCnt++;
- if(uiKeyTimeCnt>const_key_time)
- {
- uiKeyTimeCnt=0;
- ucKeyLock=1;
- if(ucRowRecord==1)
- ucKeySec=9;
- else if(ucRowRecord==2)
- ucKeySec=10;
- else if(ucRowRecord==3)
- ucKeySec=11;
- else
- ucKeySec=12;
- }
- }
- if(key_sr1==1&&key_sr2==1&&key_sr3==1&&key_sr4==0)
- {
- uiKeyTimeCnt++;
- if(uiKeyTimeCnt>const_key_time)
- {
- uiKeyTimeCnt=0;
- ucKeyLock=1;
- if(ucRowRecord==1)
- ucKeySec=13;
- else if(ucRowRecord==2)
- ucKeySec=14;
- else if(ucRowRecord==3)
- ucKeySec=15;
- else
- ucKeySec=16;
- }
- }
- }
- break;
- }
- }
- void key_service()
- {
- switch(ucKeySec)
- {
- case 1: num_key_input(1);
- ucKeySec=0;
- break;
- case 2: num_key_input(2);
- ucKeySec=0;
- break;
- case 3: num_key_input(3);
- ucKeySec=0;
- break;
- case 4: num_key_input(4);
- ucKeySec=0;
- break;
- case 5: num_key_input(5);
- ucKeySec=0;
- break;
- case 6: num_key_input(6);
- ucKeySec=0;
- break;
- case 7: num_key_input(7);
- ucKeySec=0;
- break;
- case 8: num_key_input(8);
- ucKeySec=0;
- break;
- case 9: num_key_input(9);
- ucKeySec=0;
- break;
- case 10: num_key_input(0);
- ucKeySec=0;
- break;
- case 11: ucKeySec=0;
- break;
- case 12: ucKeySec=0;
- break;
- case 13: ucKeySec=0;
- break;
- case 14: ucKeySec=0;
- break;
- case 15: ucKeySec=0;
- break;
- case 16: ucKeySec=0;
- break;
- }
- }
- void num_key_input(unsigned char ucWhichKey)
- {
- switch(ucWd)
- {
- case 1: ucPasswordInput[ucPasswordCnt]=ucWhichKey;
- ucPasswordCnt++;
- if(ucPasswordCnt>=4)
- {
- ucPasswordCnt=0;
- ucWd=3;
- ucWd3Update=1;
- uiPasswordTimer=const_05s;
- }
- ucWd1Update=1;
- uiNoKeyPushTimer=const_no_key_push;
- break;
- case 2: ucKeyNum=ucWhichKey;
- ucWd2Update=1;
- uiNoKeyPushTimer=const_no_key_push;
- break;
- }
- }
- void display_drive()
- {
- switch(ucDisplayDriveStep)
- {
- case 1: ucTemp=table[ucDig1];
- hc595_drive(ucTemp,0xfe);
- break;
- case 2: ucTemp=table[ucDig2];
- hc595_drive(ucTemp,0xfd);
- break;
- case 3: ucTemp=table[ucDig3];
- hc595_drive(ucTemp,0xfb);
- break;
- case 4: ucTemp=table[ucDig4];
- hc595_drive(ucTemp,0xf7);
- break;
- }
- ucDisplayDriveStep++;
- if(ucDisplayDriveStep>4)
- ucDisplayDriveStep=1;
- }
- void hc595_drive(unsigned char ucStatusTemp16_09,unsigned char ucStatusTemp08_01)
- {
- unsigned char ucTempData=0;
- unsigned char i;
- srclk_dr=0;
- rclk_dr=0;
-
- ucTempData=ucStatusTemp16_09;
- for(i=0;i<8;i++)
- {
- if(ucTempData>=0x80)
- ser_dr=1;
- else ser_dr=0;
- srclk_dr=0;
- delay_short(1);
- srclk_dr=1;
- ucTempData=ucTempData<<1;
- }
-
- ucTempData=ucStatusTemp08_01;
- for(i=0;i<8;i++)
- {
- if(ucTempData>=0x80)
- ser_dr=1;
- else ser_dr=0;
- srclk_dr=0;
- delay_short(1);
- srclk_dr=1;
- delay_short(1);
- ucTempData=ucTempData<<1;
- }
-
- rclk_dr=0;
- delay_short(1);
- rclk_dr=1;
- delay_short(1);
- }
- void T0_time() interrupt 1
- {
- unsigned int i;
- if(ucWd==3)
- {
- if(uiPasswordTimer>0)
- uiPasswordTimer--;
- if(uiPasswordTimer==0)
- {
- if(ucPasswordInput[0]==1&&ucPasswordInput[1]==3&&ucPasswordInput[2]==1&&ucPasswordInput[3]==4)
- {
- ucWd=2;
- ucWd2Update=1;
- }
- else
- {
- for(i=0;i<4;i++)
- ucPasswordInput[i]=11;
- ucWd=1;
- ucWd1Update=1;
- }
- }
- }
- if(ucWd==2)
- {
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
24.開機密碼鎖.rar
(39.22 KB, 下載次數(shù): 20)
2017-11-29 15:04 上傳
點擊文件名下載附件
|