用51单片机做个密码锁(LCD屏上显示),按键输入,限次5次,超过5次蜂鸣器响的c语言代码
时间: 2023-09-07 22:04:14 浏览: 135
### 回答1:
这里是一份简单的51单片机密码锁的C语言代码的例子:
```
#include <reg51.h>
#include <LCD.h>
#define password "1234"
#define MAX_TRY 5
unsigned char i, j, flag, count, password_input[5];
void delay(unsigned int t)
{
while (t--);
}
void buzzer()
{
P1 = 0x01;
delay(20000);
P1 = 0x00;
delay(20000);
}
void main()
{
LCD_Init();
count = MAX_TRY;
while (1)
{
LCD_Clear();
LCD_WriteString("Password Lock");
LCD_WriteString("Enter password:");
for (i = 0; i < 4; i++)
{
password_input[i] = getkey();
LCD_WriteData(password_input[i]);
delay(50000);
}
password_input[i] = '\0';
flag = 1;
for (j = 0; j < 4; j++)
{
if (password_input[j] != password[j])
{
flag = 0;
break;
}
}
if (flag)
{
LCD_Clear();
LCD_WriteString("Access granted");
delay(50000);
break;
}
else
{
count--;
LCD_Clear();
LCD_WriteString("Access denied");
LCD_WriteString("Remaining tries:");
LCD_WriteData(count + '0');
delay(50000);
if (count == 0)
{
buzzer();
break;
}
}
}
}
```
注意:代码中使用了LCD驱动库,需要自行实现`LCD_Init()`、`LCD_Clear()`、`LCD_WriteString()`、`LCD_WriteData()`等函数。代码中使用了按键驱动库,需要自行实现`getkey()`函数。此代码仅作为参考,请根据您实际的硬件环境和需求进行修改。
### 回答2:
下面是一个使用51单片机搭建密码锁的C语言代码,通过LCD屏幕显示并限制了5次尝试的次数。超过5次的尝试将会触发蜂鸣器发出警报。
```c
#include <reg51.h>
#include <stdio.h>
#define MAX_ATTEMPTS 5
sbit buzz = P1^0;
sbit rs = P2^0; //LCD1602模块的接口输入端RS
sbit rw = P2^1; //LCD1602模块的接口输入端RW
sbit en = P2^2; //LCD1602模块的接口输入端E
#define LCD_data P0 //LCD1602的数据端口
void LCD_Init();
void LCD_Command(unsigned char cm);
void LCD_Show(unsigned char dat);
void LCD_String(unsigned char *s);
void delay(int n)
{
int i, j;
for(i=0;i<n;i++)
for(j=0;j<120;j++);
}
void main()
{
unsigned char password[6] = "12345"; // 设置密码
unsigned char input[6]; // 存储输入的密码
unsigned char attempts = 0;
LCD_Init();
while(1) {
LCD_Command(0x01); // 清除LCD屏幕
LCD_String("Enter Password:");
LCD_Command(0xC0); // 设置光标位置到第二行
for(int i=0; i<5; i++) {
input[i] = LCD_ReadKey(); // 读取按键输入
LCD_Show('*'); // 显示星号代替按下的按键
if(input[i] != password[i]) { // 验证密码位是否正确
attempts++;
if(attempts >= MAX_ATTEMPTS) { // 如果尝试次数超过最大次数,则发出警报
buzz = 1;
delay(5000);
buzz = 0;
while(1); // 进入死循环,锁定系统
}
LCD_Command(0x01); // 清除LCD屏幕
LCD_String("Wrong Password");
delay(2000);
break; // 输入错误后跳出循环
}
}
if(attempts < MAX_ATTEMPTS) { // 如果输入次数没达到最大限制则通过
LCD_Command(0x01); // 清除LCD屏幕
LCD_String("Access Granted");
delay(2000);
break; // 跳出循环,解锁系统
}
}
while(1);
}
void LCD_Init()
{
LCD_Command(0x38); // LCD16x2设置,8位数据,两行显示
delay(5);
LCD_Command(0x0C); // 开启显示,无光标
delay(5);
LCD_Command(0x06); // 光标右移,无显现移动
delay(5);
LCD_Command(0x01); // 清除LCD屏幕
delay(5);
}
void LCD_Command(unsigned char cm)
{
LCD_data = cm; // 将指令写入数据端口
rs = 0; // RS=0为指令
rw = 0; // RW=0为写
en = 1; // 使能E信号
delay(5);
en = 0; // 关闭E信号
}
void LCD_Show(unsigned char dat)
{
LCD_data = dat; // 将数据写入数据端口
rs = 1; // RS=1为显示字符
rw = 0; // RW=0为写
en = 1; // 使能E信号
delay(5);
en = 0; // 关闭E信号
}
void LCD_String(unsigned char *s)
{
while(*s) {
LCD_Show(*s); // 循环显示字符串
s++;
}
}
unsigned char LCD_ReadKey()
{
unsigned char read_key = 0xFF; // 设置初始值
rs = 1; // RS=1为显示字符
rw = 1; // RW=1为读
en = 1; // 使能E信号
delay(5);
read_key = LCD_data; // 读取输入值
en = 0; // 关闭E信号
return read_key;
}
```
请注意,此代码仅提供了基本的密码验证和界面显示功能。你可以根据需求进一步优化和扩展代码。
### 回答3:
使用51单片机设计密码锁的C语言代码如下:
```c
#include <reg51.h>
#define PASSWORD_LENGTH 4
sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;
sbit LCD_D4 = P2^4;
sbit LCD_D5 = P2^5;
sbit LCD_D6 = P2^6;
sbit LCD_D7 = P2^7;
sbit BUZZ = P3^0;
unsigned char password[PASSWORD_LENGTH] = "1234"; // 设置密码为"1234"
unsigned char input[PASSWORD_LENGTH];
void LCD_Init();
void LCD_Command(unsigned char command);
void LCD_WriteChar(unsigned char c);
void LCD_WriteString(unsigned char *string);
void LCD_Clear();
void Delay(unsigned int time);
int ValidatePassword(unsigned char *input, unsigned char *password);
void main() {
unsigned char i;
unsigned char attempt = 0;
LCD_Clear();
LCD_Init();
LCD_WriteString("Enter password:");
while (1) {
for (i = 0; i < PASSWORD_LENGTH; i++) {
while (!P1);
input[i] = P1;
Delay(100);
P1 = 0xFF;
LCD_WriteChar('*');
}
if (ValidatePassword(input, password)) {
LCD_Clear();
LCD_WriteString("Access granted!");
break;
} else {
attempt++;
if (attempt >= 5) {
BUZZ = 1; // 蜂鸣器响
LCD_Clear();
LCD_WriteString("Access denied!");
break;
}
LCD_Clear();
LCD_WriteString("Wrong password.");
Delay(1000);
LCD_Clear();
LCD_WriteString("Enter password:");
}
}
while (1); // 无限循环,防止程序退出
}
int ValidatePassword(unsigned char *input, unsigned char *password) {
unsigned char i;
for (i = 0; i < PASSWORD_LENGTH; i++) {
if (input[i] != password[i]) {
return 0;
}
}
return 1;
}
void LCD_Init() {
LCD_Command(0x33);
LCD_Command(0x32);
LCD_Command(0x28);
LCD_Command(0x0C);
LCD_Command(0x01);
Delay(1);
}
void LCD_Command(unsigned char command) {
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 1;
LCD_D4 = command >> 4;
LCD_D5 = command >> 5;
LCD_D6 = command >> 6;
LCD_D7 = command >> 7;
Delay(1);
LCD_EN = 0;
Delay(1);
LCD_D4 = command >> 0;
LCD_D5 = command >> 1;
LCD_D6 = command >> 2;
LCD_D7 = command >> 3;
Delay(1);
LCD_EN = 1;
Delay(1);
LCD_EN = 0;
Delay(1);
}
void LCD_WriteChar(unsigned char c) {
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 1;
LCD_D4 = c >> 4;
LCD_D5 = c >> 5;
LCD_D6 = c >> 6;
LCD_D7 = c >> 7;
Delay(1);
LCD_EN = 0;
Delay(1);
LCD_D4 = c >> 0;
LCD_D5 = c >> 1;
LCD_D6 = c >> 2;
LCD_D7 = c >> 3;
Delay(1);
LCD_EN = 1;
Delay(1);
LCD_EN = 0;
Delay(1);
}
void LCD_WriteString(unsigned char *string) {
while(*string) {
LCD_WriteChar(*string++);
}
}
void LCD_Clear() {
LCD_Command(0x01);
Delay(1);
}
void Delay(unsigned int time) {
unsigned int i, j;
for(i = 0; i < time; i++)
for(j = 0; j < 125; j++);
}
```
这段代码利用51单片机控制LCD显示屏和蜂鸣器,通过按键输入实现密码输入功能。程序中,首先初始化LCD显示屏后,设定一个4位数的密码(设定为"1234"),然后进入密码输入的循环中。在循环中,通过按键输入获取用户的输入,然后逐位比对输入密码和设定密码,如果匹配则显示"Access granted!",否则累积输入次数,超过5次后蜂鸣器响,并显示"Access denied!"。如果密码正确或错误次数超过5次,则程序退出,进入无限循环。
阅读全文