Course Design: 89C51 MCU six locks

All my original content!
One way in which the program code is the only course design, modifications are interested in improving students welcome!
NOTE: Since each module code in the preparation of each blend, so the code of each module is difficult to separate and ornamental displayed poor, thus only the last code shows the total code display

This program functions:
1. Set 6 password, the password through the keyboard, if the password is correct, the indicator light.
2. Password settings can be modified by the user to change the password to open the lock. You must enter the password again before modifying the code, the need for secondary confirmation when you enter a new password to prevent misuse.
3. alarm, lock the keypad function. Password input error display an error message appears, if the number of incorrect password more than three times, the buzzer alarm, flashing lights, lock the keypad, unlock and digital control after about 8s, the buzzer alarm.

The overall concept is completed before the programming flowchart of: Here Insert Picture Description
after completion of the SCM in FIG simulation software:
Here Insert Picture Description
the main component of the present work includes 80C51 microcontroller, matrix keys, LED lights, buzzer, eight digital display.
Wherein the matrix keys and eight digital display focus.

首先介绍矩阵按键模块:
密码的输入用矩阵键盘实现,包括数字键和功能键。
如图一所示,矩阵键盘的每一条水平线与垂直线的交叉处不相通,二十通过一个按键来连接,利用这种行列式矩阵只需要N条行线和M条列线即可组成由N×M个按键的键盘。
Here Insert Picture Description
Here Insert Picture Description
本次设计需要0-9九个数字键、清零键、确认键和一个重置密码键,所以选用4×4的矩阵按键。
在这种行列式矩阵键盘编码的单片机系统中,键盘处理程序首先执行等待按键并确认有无按键按下的程序段,还要对按键进行消抖处理。
本程序中,当六位密码全部输入完成后,按下任意键即可进行密码正误判断,并进行消抖处理。
当数字键被按下后,就要识别是哪一个按键被按下。本次设计使用的是线反转法。给行线置为0x0f,给列线置为0xf0,。当接收到的数据,低四位不全为高电平时,说明有按键按下,然后通过接收的数据,判断是哪一列有按键按下,然后再反过来,高四位输出高电平,低四位输出低电平,然后根据接收到的高四位值判断是哪一行有按键按下,这样就能够确定是哪一个按键按下了。

显示模块(包含八位数码管、LED灯):
密码显示电路由单片机、电排阻和数码管组成。。P2控制位选,P0控制段选。为了防止手动错误,数码管显示数字。
数码管显示原理:
本设计采用数码管动态显示,在极短的时间内轮流显示各个字符。利用人眼视觉暂留的特点,循环顺序变更位码,同时数据线上发送相应的显示内容,即可表现出多位数字同时显示的特点。

报警模块(包含八位数码管,LED灯与蜂鸣器)
本报警电路由三个部分组成,让密码错误次数为3次时,触发报警电路,数码管滚动显示8,蜂鸣器持续间断发出声响,矩阵键盘锁死,D1灯亮,8秒后解除报警。

The code features:
① When a display is performed more than eight digital dynamic same time, a break statement, the number of bits is input to a criterion, such as when the third password being input, will make 1-3 cycle through the dynamic password, using persistence of vision effect of forming multiple password displayed simultaneously.
② Change Password total of four programs, each flag bit mark01, mark02, mark03, mark04.
The first pass is again input the correct password, if the password is correct mark01 set, press the button to change the password password reset procedure.
The second pass is repeated entered password, if set to 1 while the right mark02 mark01 zero.
After temporarily save for the third time to enter a new password, the password they have entered six password, and mark03 set 1, mark02 zero.
For the fourth time to re-enter the new password, enter the complete set mark04 1, mark03 zero.
Finally, determine the password, enter the new password twice if they are consistent, then change the password successfully, flag mark04 zero.

Note that, during the preparation of the program keys, the attention key debounce delay and pop!
The rest are basically in the comments of the code

The total program code for:

#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define GPIO P1
#define count 50000			   //计数值,约为0.05s
#define TH	(65536-count)/256  //定时器初始化设置
#define TL	(65536-count)%256
sbit LSA=P2^2;				   //数码管位选设置
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit bee=P1^5;
sbit LED=P2^0;
int key=0;					   //矩阵键盘按键标志
int password[6];			   //输入数字数组
int right[6]={6,6,6,6,6,6};	   //正确密码数组
int temporary[6];			   //零时密码数组
uchar code DSY[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};	 //数码管显示数字数组

void delay(int n)			   //延时函数
{	int t;
	while(n--)
	{	for(t=0;t<12;t++);
	}
}
void main()
{	int flag=1;
    int sign=0;				   //重置密码位数
	int mark01=0;			   //重置密码第一遍(原密码)标志
	int mark02=0;			   //重置密码第二遍(原密码重复)标志
	int mark03=0;			   //重置密码第三遍(新密码)标志
	int mark04=0;			   //重置密码第四遍(新密码重复)标志
	int mistake=0;			   //错误次数
	int temp=0;				   //正确位数
	int N=0;				   //输入位数
	int i=0;				   //计数
	int a=0;				   //计数
	bee=0;
	TMOD&=0X0F;				   //定时器初始化设置
	TMOD|=0X01;
	P2=0X03;
	while(1)
	{	
	while(1)				   //进入循环
		{   GPIO=0x0f;
			if(GPIO!=0x0f)		   //判断按下
			{	delay(100);
				if(GPIO!=0x0f)	   //重新判断按下
				{	switch(N)      //位选   
		            {
		                case(0):
		                        LSA=1;LSB=1;LSC=1; break;
		                case(1):
		                        LSA=0;LSB=1;LSC=1; break;
		                case(2):
		                        LSA=1;LSB=0;LSC=1; break;
		                case(3):
		                        LSA=0;LSB=0;LSC=1; break;
		                case(4):
		                        LSA=1;LSB=1;LSC=0; break;
		                case(5):
		                        LSA=0;LSB=1;LSC=0; break;
					}
					GPIO=0X0F;						//测试列
						switch(GPIO)
						{
							case(0x07):key=0;break;
							case(0x0b):key=1;break;
							case(0x0d):key=2;break;
							case(0x0e):key=3;break;
						}						   //测试行
					GPIO=0XF0;
					switch(GPIO)
						{
							case(0x70):key=key;break;
							case(0xb0):key=key+4;break;
							case(0xd0):key=key+8;break;
							case(0xe0):key=key+12;break;
						}
					
					P0=DSY[key];				  //矩阵键盘按键输出至数码管并显示
					password[N]=key;			  //保存按键输入
					if(mark04==1)				  //第四遍密码标志开启时,将第三遍输入密码与第四遍输入密码比对
					{if(key==temporary[N])
					 sign++;
					}
					if(mark03==1)				  //第三遍密码标志开启时,将输入新密码保存到临时密码数组中
					{temporary[N]=key;
					}
					if(key==right[N])			  //判断这一位密码是否正确并计数
					   temp++;
					if(key==15)					  //重新输入密码功能
					  {N=0;
					   if(mark01==1)			  //输入正确密码后按S16,可输入第二遍密码
					   {mark02=1;				  //第二遍密码标志开启
					    mark01=0;
					}
					   break;
					  }
					while((GPIO!=0xf0)&&(i<500))  //按键弹起
					{	delay(100);
						i++;
					}
					i=0; 
					while(1)					  //进入显示循环
						{	while(1)
							{LSA=1;LSB=1;LSC=1;	  //显示第一位
							 key=password[0];
							 P0=DSY[key];
							 delay(15); 
							 if(N==0)			  
							 break;
							 LSA=0;LSB=1;LSC=1;	  //显示第二位
							 key=password[1];
							 P0=DSY[key];
							 delay(15);
							 if(N==1)
							 break;
							 LSA=1;LSB=0;LSC=1;	 //显示第三位
							 key=password[2];
							 P0=DSY[key];
							 delay(15);
							 if(N==2)
							 break;
							 LSA=0;LSB=0;LSC=1;	 //显示第四位
							 key=password[3];
							 P0=DSY[key];
							 delay(15);
							 if(N==3)
							 break;
							 LSA=1;LSB=1;LSC=0;	 //显示第五位
							 key=password[4];
							 P0=DSY[key];
							 delay(15);
							 if(N==4)
				             break;
							 LSA=0;LSB=1;LSC=0;	 //显示第六位
						 	 key=password[5];
							 P0=DSY[key];
							 delay(15);
							 if(N==5)
				             break;
							}
							if(GPIO!=0xf0)		 //按键再次按下退出循环
							{	delay(100);
								if(GPIO!=0xf0)
								break;
							}
						}
					 
				N++;	 
				}
		        
			}
			if(N==6)							//六位密码输入完成
			{	if(mark03==1)					//第三遍密码标志开启并输入完六位新密码后,可进行第四遍密码输入
				{mark04=1;
				 mark03=0;
				}
				if(temp!=6)						//输入错误,计数加一
				mistake++;
				while((GPIO!=0xf0)&&(i<500))	//按键弹起
					{	delay(100);
						i++;
					}
					i=0; 
				if(sign==6)						//第三遍密码与第四遍密码相同时,将此密码设置为新密码
				{   delay(15);
				    right[0]=temporary[0];
					right[1]=temporary[1];
					right[2]=temporary[2];
					right[3]=temporary[3];
					right[4]=temporary[4];
					right[5]=temporary[5];
					sign=0;
					mark04=0;
					mistake=0;
					for(flag=1;flag<100;flag++)	//密码修改正确蜂鸣器响
					{ bee=~bee;
					  delay(15);
					}
				}
				while(1)						//进入显示循环
				{	while(1)
					{	LSA=1;LSB=1;LSC=1;		//第一位显示
				    	if(temp==6)				//密码正确显示1
						{mistake=0;
						 P0=DSY[1];
						 mark01=1;				//密码输入正确时,第一遍密码标志开启
						 if(mark02==1)			//第二遍密码标志开启并第二遍输入密码正确时,第三遍密码标志开启
						 {mark03=1;
						  mark02=0;
						 }
						} 
						 else					//密码错误显示0
						{P0=DSY[0];
						 if(mistake==3)			//错误次数为3时
						 {LED=0;
						  for(a=0;a<100;a++)
						 	{	TH0=TH;
								TL0=TL;
								TR0=1;
								bee=~bee;			//蜂鸣器响
								LSA=1;LSB=1;LSC=1;	//数码管循环显示
								P0=DSY[8];
								delay(50); 
								LSA=0;LSB=1;LSC=1;
								P0=DSY[8];
								delay(50);
								LSA=1;LSB=0;LSC=1;
								P0=DSY[8];
								delay(50);
								LSA=0;LSB=0;LSC=1;
								P0=DSY[8];
								delay(50);
								LSA=1;LSB=1;LSC=0;
								P0=DSY[8];
								delay(50);
								LSA=0;LSB=1;LSC=0;
							 	P0=DSY[8];
								delay(50);
								LSA=1;LSB=0;LSC=0;
							 	P0=DSY[8];
								delay(50);
								LSA=0;LSB=0;LSC=0;
							 	P0=DSY[8];
								delay(50);
							}
						 mistake=0; 			//错误次数重置
						 LED=1;
						 }
						 
						}
						break;
					}
					if(GPIO!=0xf0)			 //按键再次按下退出循环
					{	delay(100);
						if(GPIO!=0xf0)
						break;
					}
				}
				N=0;					   //密码位数重置
				temp=0;
			}	
		}
	
	}
}
Published an original article · won praise 0 · Views 112

Guess you like

Origin blog.csdn.net/Schofiled/article/details/104324916