电路:
功能描述,开机无显示。
按下除号,开始输入密码,显示短线,初始密码为0000。
输入完后,按下on/c键,确定。正确显示open,错误显示err。
=键为取消键,输入密码过程中,按下取消键,数码管全亮1s,然后灭掉。恢复初始状态。 按下乘号,设置密码,此时数码管显示短线。 输入正确密码后,进入更改密码状态,显示全F。 输入更改后密码,按下on/C键,确认。
PS:1输入任意时刻,都可按等号返回初始状态。2输入时,数字会相应的显示。 通过Protues仿真,电路图为protues输出的。
送给初学者,学习参考。
#include
#define uint8 unsigned char #define uint16 unsigned int
//数码管编码。
uint8 code SEG7LED[26] = {0x3f,0x06,0x5b,0X4F, 0x66,0x6D,0x7D,0x07, 0x7F,0x6F,0x77,0x7C, 0x39,0x5E,0x79,0x71,
0x00,0x40,0xff, 0x3f,0x73,0x79,0x37, //open 0x79,0x77,0x77}; //err //变量声明
uint8 Ge,Shi,Bai,Qian;//数码管各个位 uint8 WeiNum=1;
uint8 code P1Pin[9] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};
uint8 state=0;//记录密码锁状态,0为待机状态,1为输入密码,2,3为设置密码 uint8 InputNum=1;//记录输入密码时,目前密码的位数。 uint8 InPassWord[4];
uint8 PassWord[4]={0,0,0,0}; //函数声明
void initTimer0();
void display(uint8 WeiNum); uint8 keyscan();
void delayms(uint16 Xms); void password(uint8 key);
uint8 Compare(uint8 InPassWord[4]);
void main() { uint8 key; initTimer0(); while(1) { key = keyscan(); password(key); } }
void initTimer0() { EA = 1; ET0 = 1; TMOD = 0X01; TH0 = (65535-5000)/256; TL0 = (65535-5000)%6; TR0 = 1; }
void Timer0() interrupt 1 { TH0 = (65535-5000)/256; TL0 = (65535-5000)%6;
display(WeiNum); WeiNum ++; if(WeiNum==5) WeiNum = 1; }
void display(uint8 WeiNum) { switch(WeiNum) { case 1: { P2 = 0xff; P0 = SEG7LED[Qian]; P2 = 0xfe; break; } case 2: { P2 = 0xff; P0 = SEG7LED[Bai]; P2 = 0xfd; break; } case 3: { P2 = 0xff; P0 = SEG7LED[Shi]; P2 = 0xfb; break; } case 4: { P2 = 0xff; P0 = SEG7LED[Ge]; P2 = 0xf7; break; } default: { P2 = 0xff; P0 = 0xff; P2 = 0xff;
break; } } }
void delayms(uint16 Xms) { uint16 i,j; for(i=Xms;i>0;i--) for(j=110;j>0;j--); }
uint8 keyscan() { uint8 i=0; uint8 key8=0xff; uint8 key=0xff; uint8 Button; for(i=0;i<4;i++) { P1 = P1Pin[i]; Button = P1; if(Button != P1Pin[i]) { delayms(5); Button = P1; if(Button != P1Pin[i]) { key8 = Button; } while(Button != P1Pin[i]) { Button = P1; } } } switch(key8) {//0~9数字键,12确定,14取消,10输入密码,11设置密码。 case 0xee:{key = 7;break;} case 0xde:{key = 8;break;} case 0xbe:{key = 9;break;} case 0x7e:{key = 10;break;} case 0xed:{key = 4;break;} case 0xdd:{key = 5;break;} case 0xbd:{key = 6;break;} case 0x7d:{key = 11;break;}
case 0xeb:{key = 1;break;} case 0xdb:{key = 2;break;} case 0xbb:{key = 3;break;} case 0x7b:{key = 15;break;} case 0xe7:{key = 12;break;} case 0xd7:{key = 0;break;} case 0xb7:{key = 14;break;} case 0x77:{key = 16;break;} default:{key = 0xff;break;} } return key; }
void password(uint8 key)
{//0~9数字键,12确定,14取消,10输入密码,11设置密码。 uint8 i; if(key!=0xff) { switch(state) { case 0: { if(key == 10) { state = 1; Ge =17;Shi=17;Bai=17;Qian=17; } else if(key == 11) { state = 2; Ge =17;Shi=17;Bai=17;Qian=17; } break; } case 1: { if(key>=0 && key <= 9) { switch(InputNum) { case 1: Ge = key;InPassWord[0]=key; break; case 2:
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库基于51单片机的密码锁(C语言)在线全文阅读。
相关推荐: