用C语言写51程序设计通过串口输入密码显示在LCD1602上,当密码正确时,显示屏显示Welcome, LED灯全亮。当密码错误时串0显示Plesase Try Again,延时3秒后,LCD1 602显示输入密码界面。当输入3次错误密码时,蜂鸣器发出声响。
时间: 2023-05-12 11:02:42 浏览: 178
基于C51单片机的 程序设计 包括流水灯 蜂鸣器 温度传感器 串口通讯+源代码+文档说明
以下是用C语言写51程序设计通过串口输入密码显示在LCD1602上的代码:
```
#include <reg51.h>
#include <stdio.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_E = P2^7;
#define LED P1
#define BUZZER P3^7
void delay(unsigned int t)
{
unsigned int i, j;
for(i = 0; i < t; i++)
for(j = 0; j < 125; j++);
}
void LCD1602_WriteCmd(unsigned char cmd)
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
delay(1);
LCD1602_E = 0;
delay(1);
}
void LCD1602_WriteData(unsigned char dat)
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
delay(1);
LCD1602_E = 0;
delay(1);
}
void LCD1602_Init()
{
LCD1602_WriteCmd(0x38);
LCD1602_WriteCmd(0x0c);
LCD1602_WriteCmd(0x06);
LCD1602_WriteCmd(0x01);
}
void LCD1602_Clear()
{
LCD1602_WriteCmd(0x01);
}
void LCD1602_SetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if(y == 0)
addr = 0x80 + x;
else
addr = 0xc0 + x;
LCD1602_WriteCmd(addr);
}
void UART_Init()
{
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
SM0 = 0;
SM1 = 1;
REN = 1;
}
unsigned char UART_Receive()
{
while(!RI);
RI = 0;
return SBUF;
}
void UART_Send(unsigned char dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
void main()
{
unsigned char password[4] = {'1', '2', '3', '4'};
unsigned char input[4];
unsigned char i, j;
unsigned char correct = 0;
unsigned char tries = 0;
LCD1602_Init();
UART_Init();
while(1)
{
LCD1602_Clear();
LCD1602_SetCursor(0, 0);
LCD1602_WriteData('P');
LCD1602_WriteData('l');
LCD1602_WriteData('e');
LCD1602_WriteData('a');
LCD1602_WriteData('s');
LCD1602_WriteData('e');
LCD1602_WriteData(' ');
LCD1602_WriteData('E');
LCD1602_WriteData('n');
LCD1602_WriteData('t');
LCD1602_WriteData('e');
LCD1602_WriteData('r');
LCD1602_WriteData(' ');
LCD1602_WriteData('P');
LCD1602_WriteData('a');
LCD1602_WriteData('s');
LCD1602_WriteData('s');
LCD1602_WriteData('w');
LCD1602_WriteData('o');
LCD1602_WriteData('r');
LCD1602_SetCursor(0, 1);
for(i = 0; i < 4; i++)
{
input[i] = UART_Receive();
LCD1602_WriteData('*');
}
for(i = 0; i < 4; i++)
{
if(input[i] != password[i])
{
correct = 0;
break;
}
else
{
correct = 1;
}
}
if(correct)
{
LCD1602_Clear();
LCD1602_SetCursor(0, 0);
LCD1602_WriteData('W');
LCD1602_WriteData('e');
LCD1602_WriteData('l');
LCD1602_WriteData('c');
LCD1602_WriteData('o');
LCD1602_WriteData('m');
LCD1602_WriteData('e');
LED = 0xff;
BUZZER = 0;
delay(1000);
}
else
{
tries++;
if(tries >= 3)
{
BUZZER = 1;
delay(1000);
BUZZER = 0;
tries = 0;
}
else
{
LCD1602_Clear();
LCD1602_SetCursor(0, 0);
LCD1602_WriteData('P');
LCD1602_WriteData('l');
LCD1602_WriteData('e');
LCD1602_WriteData('a');
LCD1602_WriteData('s');
LCD1602_WriteData('e');
LCD1602_WriteData(' ');
LCD1602_WriteData('T');
LCD1602_WriteData('r');
LCD1602_WriteData('y');
LCD1602_WriteData(' ');
LCD1602_WriteData('A');
LCD1602_WriteData('g');
LCD1602_WriteData('a');
LCD1602_WriteData('i');
LCD1602_WriteData('n');
delay(3000);
}
}
}
}
```
这个程序可以通过串口输入密码,然后在LCD1602上显示星号,如果密码正确,LED灯全亮,LCD1602上显示Welcome,如果密码错误,LCD1602上显示Please Try Again,延时3秒后再次显示输入密码界面,如果连续输入3次错误密码,蜂鸣器会发出声响。
阅读全文