基于stc15系列芯片的电子密码锁代码

/**********************************************************************************
	功能:			包含必要头文件,宏定义,位定义,以及变量和函数的声明
***********************************************************************************/
#include"STC15W4K.h"
#define uint unsigned int
#define uchar unsigned char

//on_off的状态值,表示电子密码的状态
#define FLAG_OFF			  0           //关闭
#define FLAG_ON				  1						//打开
#define FLAG_SETTING		2						//设置

//nset状态值,表示是否处于重置状态
#define FLAG_RESET_ON   1						//重置
#define FLAG_RESET_OFF  0						//非重置

//key的几个特殊值
#define KEY_BORDER 			0x0a        //键值边界,密码数字不超过9
#define KEY_ENSURE			0x0c				//关闭
#define KEY_RESET       0x0d				//重置


//按键的位定义
sbit L1 = P1^0;											
sbit L2 = P1^1;
sbit L3 = P1^2;
sbit L4 = P1^3;

//led以及蜂鸣器定义,test用作测试
sbit LED_ON  = P3^2;
sbit LED_OFF = P3^3;
sbit LED_SET = P3^4;
sbit test    = P3^5;
sbit fmq     = P3^6; 
sbit led 		 = P5^5;


//全局声明变量
extern uchar key_times;
extern uchar nset;
extern uchar on_off;
extern uchar i,j,temp;
extern uchar key;
extern uchar key_delay;

//函数声明
void delayms(uint xms);
void scankey();
void led_change(uchar option);
bit judge();
void init();
void scomm();

#include "function.h"

//保存当前密码,输入密码
uchar password[] = {0x01,0x01,0x01,0x01,0x01,0x01};
uchar input[] = {0xff,0xff,0xff,0xff,0xff,0xff};


/**********************************************************************************
	功能:			延时,最小单位为ms(晶振为11.0592
	计算方法:一个for循环 = 8个指令周期 = 8*12个机器周期 = 8*12*1/11.0592 = 0.0086ms
						xms = 0.0086 * 110 = 0.946 ms
***********************************************************************************/
void delayms(uint xms)
{
	uint i,j;
	for(i=xms;i>0;i--)			
		for(j=110;j>0;j--);
}


/**********************************************************************************
	功能:			改变几个led的状态
***********************************************************************************/
void led_change(uchar option)
{
	if(option == 0)      //关闭状态
	{ 
		LED_ON = 1;
		LED_OFF = 0;
		LED_SET = 1;
	}
	else if(option == 1) //打开状态
	{
		LED_ON = 0;
		LED_OFF = 1;
		LED_SET = 1;
	}
	else if(option == 2) //设置状态
	{
		LED_ON = 1;
		LED_OFF = 1;
		LED_SET = 0;
	}
}


/**********************************************************************************
	功能:			扫描按键得出键值
	方法:		先扫行,再扫列
***********************************************************************************/
void scankey()
{
		P1  = 0xef;
	  //key = 0xff;
		for(i=0;i<4;i++)
	  { 
	    if(L1==0)
			{
				delayms(key_delay);
				if(L1==0)
					key = i*4+0; 
				if(key < KEY_BORDER && on_off != FLAG_ON)//开锁后按键不计数
					key_times = key_times+1;
			}
			if(L2==0)
			{	
				delayms(key_delay);
				if(L2==0)
					key = i*4+1;  
				if(key < KEY_BORDER && on_off != FLAG_ON)
					key_times = key_times+1;
			}
			if(L3==0 && on_off != FLAG_ON)
			{
				delayms(key_delay);
				if(L3==0)
					key = i*4+2;
				if(key < KEY_BORDER)
					key_times = key_times+1;
			}
			if(L4==0 && on_off != FLAG_ON)
			{
				 delayms(key_delay);
				 if(L4==0)
					 key = i*4+3; 
				 if(key < KEY_BORDER)
					key_times = key_times+1;
			}
			
			delayms(5); 				//准备下一次扫描的时间间隔
			temp=P1;
			temp=temp|0x0f;
			temp=temp<<1;
			temp=temp|0x0f;
			P1=temp; 
	  }
		
		//扫描后判断键值
		switch(key)
		{
			case	KEY_ENSURE:
			{
				led_change(0);		//改变灯的状态
				key_times = 0;
				on_off = FLAG_OFF;//定义宏
			}
			break;
			case  KEY_RESET:
			{
				if(on_off == FLAG_ON)
				{
					led_change(2);
					key_times = 0;
					on_off = FLAG_SETTING;
					nset = FLAG_RESET_ON;
				}
				else if(on_off == FLAG_OFF)
				{
					fmq = 0;
					delayms(10);
					fmq = 1;
					key = 0x00;			//防止在此处循环
				}
			}
			break;
			default:break;
		}
}


/**********************************************************************************
	功能:			判断密码是否符合设置密码,以及重置新密码
***********************************************************************************/
bit judge()
{
	 if(nset == FLAG_RESET_OFF)  //默认状态下
	 {
		 //test密码输入后可以进入
		 switch(key_times)
		 {
			case 1:input[0] = key;break;
			case 2:input[1] = key;break;
			case 3:input[2] = key;break;
			case 4:input[3] = key;break;
			case 5:input[4] = key;break;
			case 6:input[5] = key;break;
			default:break;
		 }
		 
	 }
	 if(nset == FLAG_RESET_ON) //重置状态下,替换旧密码同时打开锁
	 {
			switch(key_times)
		  {
			case 1:password[0] = key;input[0] = key;break;
			case 2:password[1] = key;input[1] = key;break;
			case 3:password[2] = key;input[2] = key;break;
			case 4:password[3] = key;input[3] = key;break;
			case 5:password[4] = key;input[4] = key;break;
			case 6:password[5] = key;input[5] = key;break;
			default:break;
		  }
	 }
	 
	 
	 
	if(key_times == 6)
	{
		if(nset == FLAG_RESET_ON )
			nset = FLAG_RESET_OFF;
		key_times = 0;
		for(j=0;j<6;j++)
		{
			if(password[j] != input[j]) //报警
			{
				LED_ON = 1;
				fmq = 0;
				delayms(30);
				LED_OFF = ~LED_OFF;
				delayms(30);
				LED_OFF = ~LED_OFF;
				delayms(30);
				LED_OFF = ~LED_OFF;
				delayms(30);
				LED_OFF = ~LED_OFF;
				fmq = 1;
				return 0;
			}
		}
		on_off = FLAG_ON;
		scomm();
		return 1;
	}
	return 0;
}


/**********************************************************************************
	功能:			初始化,包括对led,定时器,串口的初始化
***********************************************************************************/
void init()
{
	led_change(0);
	fmq = 0;
	delayms(20);
	fmq = 1;
	
	P1M0 = 0;P1M1 = 0;		
	SCON = 0x40;					
	T2L	= 0xe0;			//定时器2作为波特率发生器
	T2H	= 0xfe;
	AUXR = 0x14;
	AUXR |= 0x01;
	TI = 1;
}


/**********************************************************************************
	功能:			发送单片机信息,一次七个字节,其中首字节为状态,其余六字节为当前密码
***********************************************************************************/
void scomm()
{
	uchar i;
	SBUF = on_off; 
	while(TI == 0);
	TI = 0;
	
	for(i=0;i<6;i++)
	{
		while(TI == 0);
		{
			TI = 0;
			SBUF = password[i];
		}
	}
}


/********************************************************************************************
	File name:			    	电子密码锁
	Main fouction:			  六位密码开解锁,包含重置功能,并将实时信息发送到上位机
	Last modified Date:  	2017-10-22
	Created by:          	zzh
	Steps for test				1 输入原始密码111111,看是否打开
												2 再锁定,看是否关闭
												3 此时输入错误密码,看是否报警
												4 此时重设,因未解锁看是否报警
												5 输入正确密码,此时重设,输入新密码,此时应保持解锁状态
												6 关锁,输入新密码
												7 重复2~6
*********************************************************************************************/
#include "function.h"

//变量初始化
uchar key_times = 0;
uchar nset = FLAG_RESET_OFF;
uchar on_off = FLAG_OFF;
uchar i,j,temp;
uchar key = 0xff;
uchar key_delay = 5;

void main()
{
	uint t;
  init();
	
	while(1)
	{
		scankey();
		
		if(judge() == 1)
		{
			led_change(1);
			on_off = FLAG_ON;
		}
		if(key_times > 6) 
			key_times = 0;
	}
}




  • 3
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
目 录 摘要..........................................(03) 关键字........................................(03) 1 前言..........................................(04) 2 系统设计......................................(04) 2.1 电子密码锁的工作原理......................(04) 2.2 系统硬件组成..............................(04) 2.3 矩阵键盘扫面工作原理......................(05) 2.4 LED数码管动态显示原理.....................(05) 2.5 电子密码锁系统硬件设计..... ..............(06) 2.6 电子密码锁系统软件设计....... ............(07) 3 设计体会与结论................................(07) 4附录...........................................(08) 5参考文献.......................................(13) 摘要 电子密码锁是一种通过密码输入来控制电路或是芯片工作,从而控制机械开关的闭合,完成开锁、闭锁任务的电子产品。它的种类很多,有简易的电路产品,也有基于芯片的性价比较高的产品。现在应用较广的电子密码锁是以芯片为核心,通过编程来实现的。其性能和安全性已大大超过了机械锁,特点如下: 1.保密性好,编码量多,远远大于弹子锁。随机开锁成功率几乎为零。 2.密码可变。 用户可以经常更改密码,防止密码被盗,同时也可以避免因人员的更替而使锁的密级下降。 3.误码输入保护。当输入密码多次错误时,报警系统自动启动。 4. 电子密码锁操作简单易行,一学即会。 关键字;单片机 LED数码管 矩阵键盘 密码锁 Abstract Electronic Code Lock is a password input through the control circuit, or chip, thus controlling the closed mechanical switch to complete the lock, closed the task of electronic products. It is of many types, have a simple circuit products, but also the higher cost chip-based products. Now the widely used electronic code lock is a chip at the core, achieved through programming. Performance and safety have greatly exceeded the mechanical lock, the following features: 1. Privacy is good, coding quantity is far greater than the spring lock. Random unlocking success rate is almost zero. 2. Password variable. Users can always change the password, to prevent password theft, but also avoids the lock because of staff turnover Er Shi and drawing down. 3. Error input protection. When you enter your password wrong several times, the alarm system started automatically. 4. E-lock operation is simple, a school that will. Keywords; MCU LED digital tube matrix keypad lock 1 前言 随着人们生活水平的提高,如何实现家庭防盗这一问题也变的尤其的突出,传统的机械锁由于其构造的简单,被撬的事件屡见不鲜,电子锁由于其保密性高,使用灵活性好,安全系数高,受到了广大
很抱歉,我无法提供代码。不过,我可以给您一些指导和建议来编写该代码: 1. 首先,确定密码锁的设计和功能。例如,密码长度、密码存储方式、输入密码的方法等等。 2. 然后,选择合适的开发板和编译器进行开发。 3. 编写代码,包括输入密码、存储密码、比较密码等功能,并且根据需要加入其他功能,例如密码输错次数限制等。 4. 调试程序,确保代码能够正确运行。 以下是一个简单的密码锁代码示例: ``` #include <reg52.h> #define PASSWORD_LENGTH 4 #define MAX_ATTEMPTS 3 unsigned char password[PASSWORD_LENGTH] = {1, 2, 3, 4}; // 设置密码为1, 2, 3, 4 unsigned char attempts = 0; // 输入密码失败的次数 void delay(unsigned int time) // 延时函数 { unsigned int i, j; for(i=0; i<time; i++) for(j=0; j<1275; j++); } void main() { unsigned char input[PASSWORD_LENGTH]; // 输入的密码 unsigned char i; while(1) { for(i=0; i<PASSWORD_LENGTH; i++) // 循环输入密码 { P1 = 0xff; // 显示屏清屏 input[i] = P0; // 读取按键输入 P1 = input[i]; // 显示屏显示输入的按键值 delay(50); // 延时等待下一个按键输入 } for(i=0; i<PASSWORD_LENGTH; i++) // 比较输入的密码与设置的密码是否相同 { if(input[i] != password[i]) { attempts++; // 如果输入错误,则次数加1 break; } } if(i == PASSWORD_LENGTH) // 如果所有密码都输入正确 { P1 = 0x00; // 显示屏显示开锁成功 attempts = 0; // 输入失败次数清零 } if(attempts >= MAX_ATTEMPTS) // 如果失败次数达到上限 { P1 = 0xff; // 显示屏显示开锁失败 while(1); // 程序停止运行 } } } ``` 该示例代码使用了 STC89C52 单片机,设置了 4 位密码,并且输入密码需要按顺序输入。如果输入错误 3 次,则程序停止运行,需要重新启动。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值