密码输入
逻辑流程图
SUN的贴心提示
不知道你有没有发现:这个设计思路比你想的更详细
为啥这么想?
- 要定义变量接收密码吧
- 你要显示输入密码的UI吧,那就先得开个显示屏吧
- 键盘要反复扫吧
- 我们要有输入密码,和确认键,删除键,取消键吧
- 输入的密码要存起来吧
- 要按键扫描吧
- 数字按键,删除按键,确认按键,取消按键是四个模块吧
- 按键扫描的返回值是按键编号吧
- 四个模块得判断下到底按键编号对应那个模块吧
- 这些UI要互相切换,清屏(用其他ui覆盖)吧
开码!
- 启动 LCD1602
LCD_Init();
- 显示密码输入界面
LCD_ShowString(1, 1, "PassWord:");
showPasswordFormat(6);
- 按键是否按下?
KeyNum = Matrixkey();
if(KeyNum)
{
// 塞上逻辑
}
- 按键是否为数字键?
if(KeyNum <= 10)
{
// 按键1-10都是数字
}
- 按键是否为确认键?
if(KeyNum == 11)
{
// 确认
}
- 按键是否为退位键?
if(KeyNum == 12) // 退位
{
if(count > 0)
{
if(count <= 5)
{
Password1 /= 10;
}
else
{
Password2 = 0;
}
count--;
showPasswordFormat(6);
}
}
- 按键是否为取消键?
if(KeyNum == 14) // 取消键
{
clearPassword();
LCD_ShowString(1, 1, "PassWord: "); // 恢复显示
}
- 密码输入和没输入的区别解释
void showPasswordFormat(unsigned char length) {
unsigned char i;
for(i = 0; i < length; i++) {
if (i < count) {
LCD_ShowChar(2, 1 + i, '*');
} else {
LCD_ShowChar(2, 1 + i, '-');
}
}
}
- 输入的密码要存起来
unsigned int Password1 = 0, Password2 = 0, count = 0;
- 清屏的具体逻辑
void clearPassword() {
Password1 = 0;
Password2 = 0;
count = 0;
LCD_ShowString(2, 1, "------ ");
}
组合逻辑
至此,大框架有了
#include <REGX52.H>
#include "LCD1602.h"
#include "matrixkey.h"
#include "Buzzer.h"
#include "Delay.h"
#include <math.h> //引用一点库和已经抄好的库
unsigned char KeyNum;
unsigned int Password1 = 0, Password2 = 0, count = 0;
unsigned int secret1 = 11111;
unsigned int secret2 = 1; //具有SUN特色的sz密码存储逻辑
void main()
{
LCD_Init();
LCD_ShowString(1, 1, "PassWord:");
showPasswordFormat(6);
while(1) //反复扫
{
// 处理按键输入
KeyNum = Matrixkey();
if(KeyNum)
{
if(KeyNum <= 10) // 如果s1~s10按键按下,输入密码
{
if(count < 5)
{
Password1 *= 10; // 密码左移一位
Password1 += KeyNum % 10; // 获取一位密码
count++; // 计次
}
else if (count == 5)
{
Password2 = KeyNum % 10;
count++;
}
}
showPasswordFormat(6);
}
if(KeyNum == 11) // 确认键
{
}
if(KeyNum == 12) // 退位
{
if(count > 0)
{
if(count <= 5) //这和SUN存储的sz密码存储的方式有关,总之就是分开存了,怎么地吧
{
Password1 /= 10;
}
else
{
Password2 = 0;
}
count--;
showPasswordFormat(6);
}
}
if(KeyNum == 13) // 进入修改密码模式
{
}
if(KeyNum == 14) // 取消键
{
clearPassword();
LCD_ShowString(1, 1, "PassWord: "); // 恢复显示
}
}
}
void clearPassword() {
Password1 = 0;
Password2 = 0;
count = 0;
LCD_ShowString(2, 1, "------ ");
} //清屏
void showPasswordFormat(unsigned char length) {
unsigned char i;
for(i = 0; i < length; i++) {
if (i < count) {
LCD_ShowChar(2, 1 + i, '*');
} else {
LCD_ShowChar(2, 1 + i, '-');
}
}
}