目录
前言
今天编写了昨天的博客,而且还得学习我的课,今天就没有学习新的内容,将上节矩阵按键密码进行了优化,优化的内容有:将密码的位数增加为8位,在原来的基础上增添了退格功能,此功能的实现在八位密码和四位密码的实现都有。
代码
八位密码是通过数组实现的,主要思路是:通过for循环遍历来展示密码以及输入密码,因为没有未来将重复使用此代码的打算,所以我就没用模块化编程,优化前的代码被我注释掉了,留下来可以作一个对比的作用。
#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "MatriKey.h"
#include <stdbool.h>
unsigned char KeyNum;
unsigned int Password, count, temp;
unsigned int Password_plus[9]; // 表示的是输入的密码
unsigned int Password_plus_in[9]; // 表示的是设置的密码
void showPassword() // 展示密码
{
unsigned char i = 0;
unsigned char count_column = 1;
for (i = 0; i < 8; i++) // 便利
{
LCD_ShowNum(2, count_column, Password_plus[i], 1);
count_column++;
}
}
void Init_Arr() // 对输入的密码进行初始化
{
unsigned char i = 0;
for (i = 0; i < 8; i++)
{
Password_plus[i] = 0;
}
}
void Init_Password_in() // 初始化原始作为判断的密码
{
Password_plus_in[0] = 8;
Password_plus_in[1] = 7;
Password_plus_in[2] = 6;
Password_plus_in[3] = 5;
Password_plus_in[4] = 4;
Password_plus_in[5] = 3;
Password_plus_in[6] = 2;
Password_plus_in[7] = 1;
}
int JudgePassword()
{
unsigned char i = 0;
for (i = 0; i < 8; i++)
{
if (Password_plus[i] != Password_plus_in[i])
{
return 0;
}
}
return 1;
}
int main()
{
LCD_Init();
Init_Arr();
LCD_ShowString(1, 1, "Password:");
showPassword();
Init_Password_in();
while (1)
{
KeyNum = MatrixKey();
if (KeyNum)
{
if (KeyNum <= 10)
{
if (count < 8)
{
Password_plus[count] = KeyNum;
count++;
}
showPassword(); // 更新显示
}
if (KeyNum == 11)
{
if (JudgePassword())
{
LCD_ShowString(1, 14, "OK ");
Init_Arr();
count = 0;
showPassword(); // 更新显示
}
else
{
LCD_ShowString(1, 14, "ERR");
Init_Arr();
count = 0;
showPassword(); // 更新显示
}
}
if (KeyNum == 12) // 如果s12按下,直接取消
{
{
Init_Arr();
count = 0;
showPassword(); // 更新显示
LCD_ShowString(1, 14, " "); // 将上面的ok 或者ERR显示为空白
}
}
if (KeyNum == 13) // 如果S13按下就退位操作
{
if (count >= 0)
{
count--;
Password_plus[count] = 0;
if (count < 0)//避免count为负数造成数组越界
{
count = 0;
}
}
showPassword(); // 更新显示
}
}
}
// while (1)
// {
// KeyNum = MatrixKey();
// if (KeyNum)
// {
// if (KeyNum <= 10) // 如果按下的键为1-10才显示
// {
// if (count < 8) // 如果输入次数小于4才加入密码
// {
// Password *= 10;
// Password += KeyNum % 10;
// count++;
// }
// }
// LCD_ShowNum(2, 1, Password, 4); // 更新显示
// if (KeyNum == 11) // 如果按下s11按键表示确认密码
// {
// if (Password == 2345)
// { // 如果密码等于2345 显示ok
// LCD_ShowString(1, 14, "OK ");
// Password = 0;
// count = 0;
// LCD_ShowNum(2, 1, Password, 4); // 更新显示
// }
// else // 如果密码不等于2345 显示error
// {
// 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, " "); // 将上面的ok 或者ERR显示为空白
// }
// if (KeyNum == 13) // 如果S13按下就退位操作
// {
// if (Password > 0 && count > 0)
// {
// Password /= 10;
// count--;
// LCD_ShowNum(2, 1, Password, 4); // 更新显示
// }
// }
// }
// }
}
问题
1.不知道为什么使用不了bool用作函数返回值,报错提示为函数前缺少分号,但是我将bool改为int后就没有这个问题产生了
2.本来是想用字符串的方式来展现的,但是展示起来有问题,后来就改为int类型了
3.写了函数但是main函数里面不写,导致得不到预期结果,以后得注意这个问题
4.有点逻辑不清晰,应该先开始有思路,然后再编写代码,这次就是没用先捋清楚思路,导致完成的时间就比较长