51单片机 矩阵密码锁

代码编译环境: Keil uVision5
烧入软件: stc-isp

由于代码较多,所以需要采用模块化

主函数,代码从此处开始执行
main.c

#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "MatrixKey.h"

unsigned char KeyNum;
unsigned int Password, Count;
void main()
{
   
	
	LCD_Init();
	LCD_ShowString(1,1,"Password:");
	while(1)
	{
   
		KeyNum = MatrixKey();
		if(KeyNum)
		{
   
			if(KeyNum <= 10)			//如果按键S1~S10按下,输入密码
			{
   
				if(Count < 4)			//如果输入次数小于4
				{
   
					Password *= 10;			//密码左移一位
					Password += KeyNum%10;	//获取一位密码
					Count++;
				}
				LCD_ShowNum(2,1,Password,4);	//更新显示
			}
			if(KeyNum == 11) 	//如果S11按键按下,确认
			{
   
				if(Password == 2345)
				{
   
					LCD_ShowString(1,14, "OK!");
					Password = 0;
					Count = 0;
					LCD_ShowNum(2,1,Password,4);	//更新显示
				} else 
				{
   
					LCD_ShowString(1,14, "ERR");
					Password = 0;
					Count = 0;
					LCD_ShowNum(2,1,Password,4);	//更新显示
				}
			}
			if(KeyNum == 12)	//如果按下按键S12,取消,密码归零
			{
   
				Password = 0;
				Count = 0;
				LCD_ShowNum(2,1,Password,4);	//更新显示
				LCD_ShowString(1,14, "    ");
			}

		}
	}
}

延时函数源文件,可实现延时x ms
Delay.c

void Delay(unsigned int xms)		//@12.000MHz
{
   
	unsigned char i, j;
	while(xms--)
	{
   
		i = 2;
		j = 239;
		do
		{
   
			while (--j);
		} while (--i);
	}
}	

延时函数头文件
Delay.h

#ifndef __DELAY_H__
#define __DELAY_H__
void Delay(unsigned int xms);

#endif

实现对液晶屏的基本操作,详见注释
LCD1602.c

#include <REGX52.H>

//引脚配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#
  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值