#include<reg52.h> #define LCD1602_DB P0 sbit LCD1602_RS=P1^0; sbit LCD1602_RW=P1^1; sbit LCD1602_E=P1^5; void cntUART(unsigned int baud); void InitLcd1602(); void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str); void LcdWriteCmd(unsigned char cmd); void LcdWaitReady(); void LcdSet(unsigned char x,unsigned char y); void LcdWriteDat(unsigned char dat); void renewstr(); unsigned char str[10]={0}; unsigned char RxdByte=0; unsigned char renew=0; void main() { EA=1; cntUART(9600); InitLcd1602(); LcdShowStr(2,0,str); while(1); } void cntUART(unsigned int baud) { SCON=0x50;//波特率发生器使用模式一并且使能REN TMOD&=0x0f; TMOD|=0x20; TH1=256-(11059200/12/32)/baud; TL1=TH1; ET1=0; ES=1; TR1=1; } void InitLcd1602() { LcdWriteCmd(0x38); LcdWriteCmd(0x0c); LcdWriteCmd(0x06); LcdWriteCmd(0x01); } void LcdWriteCmd(unsigned char cmd) { LcdWaitReady(); LCD1602_RS=0; LCD1602_RW=0; LCD1602_DB=cmd; LCD1602_E=1; LCD1602_E=0; } void LcdWaitReady() { unsigned char sta; LCD1602_DB=0xff; LCD1602_RS=0; LCD1602_RW=1; do{ LCD1602_E=1;//打开肯定先要打开,毕竟要P0读状态,但不能一直打开,后面用到再打开 sta=LCD1602_DB; LCD1602_E=0; }while(sta&0x80); } void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str) { LcdSet(x,y); while(*str!='\0') { LcdWriteDat(*str++); } } void LcdSet(unsigned char x,unsigned char y) { unsigned char addr; if(y==0) addr=0x00+x; else addr=0x40+x; LcdWriteCmd(addr|0x80); } void LcdWriteDat(unsigned char dat) { LcdWaitReady(); LCD1602_RS=1; LCD1602_RW=0; LCD1602_DB=dat; LCD1602_E=1; LCD1602_E=0; } void renewstr() { static unsigned char i=0; if(renew) { renew=0; str[i]=RxdByte; i++; } } void Inter2() interrupt 4 { if(RI) { RI=0; RxdByte=SBUF; SBUF=RxdByte; renew=1; renewstr(); } if(TI) { TI=0; } }
时间: 2023-10-25 20:08:00 浏览: 90
这段代码是一个基于8051单片机的LCD1602液晶显示屏的驱动程序,同时也包括了串口通信部分。主要功能是通过串口接收数据,并在LCD1602屏幕上显示接收到的数据。其中,cntUART函数用于设置串口通信的波特率;InitLcd1602函数用于初始化LCD1602屏幕;LcdWriteCmd函数用于向LCD发送命令;LcdWaitReady函数用于等待LCD准备好;LcdShowStr函数用于在LCD上显示字符串;LcdSet函数用于设置LCD的显示位置;LcdWriteDat函数用于向LCD发送数据;renewstr函数用于更新接收到的数据到str数组中;Inter2函数是串口中断处理函数,用于处理串口接收和发送中断。
代码的主要逻辑是:初始化串口和LCD屏幕,然后进入一个无限循环,不断接收串口数据并将其显示在LCD屏幕上。
需要注意的是,这段代码是基于reg52.h库和8051单片机的,如果你使用的是其他单片机或者开发板,可能需要做相应的修改。
相关问题
如果写#include <reg51.h>#include <stdio.h>#define LCD1602_DB P0sbit LCD1602_RS = P2^6;sbit LCD1602_RW = P2^5;sbit LCD1602_E = P2^7;#define LED P1#define BUZZER P3^7void 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); } } }}之前要怎样分析
作文时遇到了写不下去的情况,应该怎么办?
遇到写不下去的情况,可以先停下来,放松一下自己的身心,让自己的思维得到缓解。可以喝口水、走动一下,或者做一些简单的伸展运动,以缓解身体的疲劳感。然后可以重新审视自己的思路,重新整理自己的思路,找到写作的重点和思路,再开始写作。如果还是无法写出来,可以先写下自己的想法和思考,不必拘泥于语言和结构,只要把自己的思路表达出来即可。最后,可以请教老师或者同学,听取他们的意见和建议,以便更好地完善自己的作文。
使用STC89C52RC/LE52RC的LCD1602实现用按键或者按键矩阵输入一任意4位数(不为0),并通过LCD1602进行显示
好的,我来解答您的问题。
首先,您需要准备以下材料:
- STC89C52RC/LE52RC单片机
- LCD1602液晶显示屏
- 按键或按键矩阵
- 电容
- 电阻
- 杜邦线
- 电源
接下来,您可以按照以下步骤进行操作:
1. 连接电路
将LCD1602的引脚分别连接至STC89C52RC/LE52RC单片机的对应引脚。按键或按键矩阵的引脚连接至单片机的输入引脚。电容和电阻用于稳定电路。
2. 编写程序
下面是一个示例程序,可以实现您的要求:
```c
#include <reg52.h>
#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E = P2^2;
void delay(unsigned int t) {
while (t--);
}
void writeCommand(unsigned char command) {
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_E = 1;
delay(5);
LCD1602_DB = command;
delay(5);
LCD1602_E = 0;
}
void writeData(unsigned char data) {
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_E = 1;
delay(5);
LCD1602_DB = data;
delay(5);
LCD1602_E = 0;
}
void initLCD() {
writeCommand(0x38);
writeCommand(0x08);
writeCommand(0x01);
writeCommand(0x06);
writeCommand(0x0c);
}
void displayNumber(unsigned int num) {
unsigned char i;
unsigned char numChar[5];
if (num == 0) {
writeData('0');
return;
}
for (i = 0; num != 0; i++) {
numChar[i] = num % 10;
num /= 10;
}
for (i--; i >= 0; i--) {
writeData(numChar[i] + '0');
}
}
unsigned int getKey() {
unsigned int key;
P1 = 0xff;
while (P1 == 0xff);
delay(1000);
if (P1 != 0xff) {
P1 = 0xff;
switch (P1) {
case 0xfe: key = 1; break;
case 0xfd: key = 2; break;
case 0xfb: key = 3; break;
case 0xf7: key = 4; break;
default: break;
}
}
return key;
}
void main() {
unsigned int num = 0;
unsigned int key;
initLCD();
while (1) {
key = getKey();
if (key != 0 && num < 10000) {
num = num * 10 + key;
displayNumber(num);
}
}
}
```
3. 烧录程序
将编写好的程序烧录进单片机中。
4. 运行程序
按下按键或按键矩阵中的任意键,LCD1602屏幕上会显示输入的数字。可以继续输入数字,直到四位数为止。
希望这个回答对您有所帮助!
阅读全文