⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lock.c

📁 用4*4键盘组成0-9数字键及确认键。 用8位数码管显示电路提示信息
💻 C
字号:
//实验任务
/*用4*4键盘组成0-9数字键及确认键。
用8位数码管显示电路提示信息,当输入密码
时,只显示“8.”,当密码位数输入完毕按确
认键时,对应的密码与设定的密码进行比较,
若密码正确,则门开,此处继电器发出“叮咚”声;若
密码不正确,禁止按键输入3秒,同时发出“滴答”
报警声;若在3秒内仍有按键按下,则禁止按键
输入3秒被重新禁止*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar pslenth=6;     		// length of secret code
uchar getps[6];				// input secret code buffer area
uchar ps[]={0,1,2,3,4,5};	    //secret code
uchar temp,num,count;
uchar aa,bb,cc;
uchar keycount;
uint key;
uint second3;
bit errorflag;
bit rightflag;
bit keyoverflag;
bit alarmflag;
sbit SPK=P3^4;		  //蜂鸣器
sbit LIGHT=P2^0; 	  //指示灯
sbit JDQ=P3^5;		  //继电器
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};
void Display(uchar count); 
void IO_int()
{
	errorflag=0;
	rightflag=0;
	keyoverflag=0;
	keycount=0;		
}
void Delay(unsigned int i)
{
	unsigned char j;
	for(;i>0;i--)
	{
		for(j=0;j<110;j++);
	}
}
uint Scankey()
{
	num=16;
	P0=0xfe;
	temp=P0;
	Delay(20);
	temp=P0;
	temp=temp&0xf0;
	if(temp!=0xf0)
	{
		Delay(50);
		temp=P0;
		temp=temp&0xf0;
		if(temp!=0xf0)
		{	
			switch(temp)
			{
				case 0xe0:num=0;
					break;
				case 0xd0:num=1;
					break;
				case 0xb0:num=2;
					break;
				case 0x70:num=3;
					break;
			}
			temp=P0;
			temp=temp&0xf0;
			while(temp!=0xf0)   //松手检测
			{
				temp=P0;
				temp=temp&0xf0;
			}
		}
	}
	P0=0xfd;
	temp=P0;
	Delay(20);
	temp=P0;
	temp=temp&0xf0;
	if(temp!=0xf0)
	{
		Delay(50);
		temp=P0;
		temp=temp&0xf0;
		if(temp!=0xf0)
		{	
			switch(temp)
			{
				case 0xe0:num=4;
					break;
				case 0xd0:num=5;
					break;
				case 0xb0:num=6;
					break;
				case 0x70:num=7;
					break;
			}
			temp=P0;
			temp=temp&0xf0;
			while(temp!=0xf0)   //松手检测
			{
				temp=P0;
				temp=temp&0xf0;
			}
		}
	}	
	P0=0xfb;
	temp=P0;
	Delay(20);
	temp=P0;
	temp=temp&0xf0;
	if(temp!=0xf0)
	{
		Delay(50);
		temp=P0;
		temp=temp&0xf0;
		if(temp!=0xf0)
		{	
			switch(temp)
			{
				case 0xe0:num=8;
					break;
				case 0xd0:num=9;
					break;
//				case 0xb0:num=10;   //no usage 
//					break;
//				case 0x70:num=11;	 //no usage
//					break;
			}
			temp=P0;
			temp=temp&0xf0;
			while(temp!=0xf0)   //松手检测
			{
				temp=P0;
				temp=temp&0xf0;
			}
		}
	}	
	P0=0xf7;
	temp=P0;
	Delay(20);
	temp=P0;
	temp=temp&0xf0;
	if(temp!=0xf0)
	{
		Delay(50);
		temp=P0;
		temp=temp&0xf0;
		if(temp!=0xf0)
		{	
			switch(temp)
			{
//				case 0xe0:num=12;    //no usage
//					break;
//				case 0xd0:num=13;	 //no usage
//					break;
				case 0xb0:num=14; //Del  key
					break;
				case 0x70:num=15; //Enter key
					break;
			}
			temp=P0;
			temp=temp&0xf0;
			while(temp!=0xf0)   //松手检测
			{
				temp=P0;
				temp=temp&0xf0;
			}
		}
	}
	return(num);				
}
void main()
{	
	unsigned char i;
	TMOD=0x01;
	TH0=(65536-500)/256;
	TL0=(65536-500)%256;
	EA=1;
	ET0=1;
	TR0=1;
	IO_int();
	while(1)
	{
		key=Scankey();
		if((key>=0)&&(key<10))
		{
			if(keycount<6)
			{
				getps[keycount]=key;
			}	
			keycount++;
			if(keycount==6)
			{
				keycount=6;
			}
			if(keycount>6)
			{
				keycount=6;
				keyoverflag=1;//keyoverflow
			}
		}
		if(key==14)
		{
			if(keycount>0)
			{
				keycount--;
				getps[keycount]=0;
			}
			else
			{
				keyoverflag=1;//delete completely
			}
		}
		if(key==15)		//enter key
		{
			if(keycount!=pslenth)
			{
				errorflag=1;
				rightflag=0;
				second3=0;
			}
			else
			{
				i=0;
				while(i<keycount)
				{
					if(getps[i]!=ps[i])
					{
						i=keycount;
						errorflag=1;
						rightflag=0;
						second3=0;
						goto next;
					}
					i++;
				}
				errorflag=0;
				rightflag=1;
			next:break;	   
			}
		}
		Display(keycount);
	}
}
void time0() interrupt 1
{
	TH0=(65536-500)/256;
	TL0=(65536-500)%256;
	if((errorflag==1)&&(rightflag==0))
	{
		bb++;
		if(bb==800)
		{
			bb=0;
			alarmflag=~alarmflag;
		}
		if(alarmflag==1)	//sound alarm signal
		{
			SPK=~SPK;	
		}
		aa++;
		{
			if(aa==800)
			{
				aa=0;
				LIGHT=~LIGHT;
			}	
		}
		second3++;
		if(second3==6400)
		{
			second3=0;
			errorflag=0;
			rightflag=0;
			alarmflag=0;
			bb=0;
			aa=0;	
		}
	}
	if((errorflag==0)&&(rightflag==1))
	{
		JDQ=0;
		cc++;
		if(cc>200)
		{
			errorflag=0;
			rightflag=0;
			JDQ=1;
		}
		SPK=1;
	}
	if(keyoverflag==1)
	{
		SPK=~SPK;
	}
}
void Display(uchar count)
{
	unsigned int k;
	if(count==0)
	{
		P1=0x00;
		P2=0x00;
	}
	else
	{
		for(k=0;k<count;k++)
		{
			P1=0x07&k;
			P2=table[getps[k]];
			Delay(50);
			P2=0;
		}
	}		
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -