学习记录
按键密码锁,开锁时,按下键盘上的开锁按键后,利用键盘上的数字键0-9输入密码,每按下一个数字键后在显示器上显示一个数字,当密码输入完成时,按下确认键,如果输入的密码正确, LCD显示“YES”,电子密码锁被打开,如果密码不正确,LCD显示屏会显示“NO”,电子密码锁不能打开。通过LCD显示屏,可以清楚地判断出密码锁所处的状态。
原理图:
部分代码:
#include "main.h"
#include "lcd1602.h"
#include "timer.h"
#include "key4x4.h"
#include "beep.h"
void SystemClock_Config(void);
uint8_t flag = 0;
uint8_t password[6] = {1, 4, 7, 2, 5, 8};
uint8_t disp_num[6] = {27, 27, 27, 27, 27, 27};
uint8_t disp_num_tmp[6];
#define DISP_FORMAT "%d%d%d%d%d%d"
char *Disp_YES = " YES! ";
char *Disp_NO = " NO! ";
char *Disp_NONE = " ";
char *toDisplay;
int main(void)
{
uint8_t Key_Value = 27;
uint8_t i = 0;
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
TIM3_Init(2400-1, 72-1); // 10ms
Beep_Init();
Key_Init();
LCD_Init();
LCD_write_string(0, 0, "Input Password:");
while(1)
{
Key_Value = Key_Scan();
if(Key_Value < 10)
{
disp_num[i] = Key_Value;
i++;
if(i > 5)
i = 0;
Key_Value = 27;
}
if(Key_Value == 16)
{
for(i = 0; i < 6; i++)
{
if(disp_num[i] != password[i])
{
flag = 1;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
LCD_write_string(6, 1, Disp_NO);
LED_Blink (3, 200);
BEEP_Blink(3, 200);
LCD_write_string(6, 1, Disp_NONE);
}
else
{
LCD_write_string(6, 1, Disp_YES);
LED_Blink (1, 500);
BEEP_Blink(1, 500);
LCD_write_string(6, 1, Disp_NONE);
}
memset(disp_num, 27, 6);
Key_Value = 27;
i = 0;
}
if(Key_Value == 13)
{
memset(disp_num, 27, 6);
Key_Value = 27;
i = 0;
}
设计报告: