51单片机—LCD1602显示

LCD1602显示由命令和数据共同控制;RS-数据/命令控制,0–数据,1–命令;RW-读/写控制,0–写,1–读;E-使能/失能控制,0-失能,1–使能。数据端口必须接上拉电阻(1K—10K),否则可能会出现乱码或不显示。

一、LCD程序

1、LCD.C程序

在程序并不复杂时判忙函数可以不使用

#include "lcd.h"
#include "intrins.h"

sbit RS = P2^0;
sbit RW = P2^1;
sbit E =  P1^2;

void Delay20us(void)   // 20us延时
{
	unsigned char i;

	_nop_();
	i = 6;
	while (--i);
}

void Delay5ms(void)  // 5ms延时
{
	unsigned char i, j;

	i = 9;
	j = 244;
	do
	{
		while (--j);
	} while (--i);
}

//unsigned char readcmd(void)  // LCD判忙
//{
//	unsigned char temp;
//	P0 = 0xFF;
//	RS = 0;
//	RW = 1;
//	E = 0;
//	Delay20us();
//	E = 1;
//	Delay20us();
//	temp = P0;
//	Delay20us();
//	E = 0;
//	return temp;
//}

void writecmd(unsigned char x)  // LCD写命令
{
	//while(readcmd() >= 0x80);
	RS = 0;
	RW = 0;
	E = 0;
	Delay20us();
	P0 = x;
	Delay20us();
	E = 1;
	Delay20us();
	E = 0;
}

void writedata(unsigned char y)  // LCD写数据
{
	//while(readcmd() >= 0x80);
	RS = 1;
	RW = 0;
	E = 0;
	Delay20us();
	P0 = y;
	Delay20us();
	E = 1;
	Delay20us();
	E = 0;
}

void Init_LCD1602(void)  // LCD初始化
{
	writecmd(0x38);
	writecmd(0x06);
	writecmd(0x0C); // writecmd(0x0F);
	writecmd(0x01);
	Delay5ms();
}

void write_cmd(unsigned char hang,unsigned char ge) //hang和ge表示在LCD的第几行第几个位置开始显示
{
	if(hang == 1) writecmd(0x80 + ge);
	else if(hang == 2) writecmd(0xC0 + ge);
	writedata(1 + 0x30);
	writedata(2 + 0x30);
	writedata(3 + 0x30);
	writedata(4 + 0x30);
	writedata(5 + 0x30);
}

void write_cmd_1(unsigned char hang,unsigned char ge)
{
	if(hang == 1) writecmd(0x80 + ge);
	else if(hang == 2) writecmd(0xC0 + ge);
	writedata('H');
	writedata('e');
	writedata('l');
	writedata('l');
	writedata('o');
}

2、LCD.h程序

#ifndef _LCD_H
#define _LCD_H
#include "reg52.h"

void Delay20us(void);
void Delay5ms(void);

//unsigned char readcmd(void);
void writecmd(unsigned char x);
void writedata(unsigned char y);
void Init_LCD1602(void);
void write_cmd(unsigned char hang,unsigned char ge);
void write_cmd_1(unsigned char hang,unsigned char ge);

#endif

二、main.c程序

#include "lcd.h"

void main(void)
{
	Init_LCD1602();
	
	while(1)
	{
		write_cmd(1,3);   // 在第一行第三个位置开始显示write_cmd函数内的信息
		write_cmd_1(2,3); // 在第二行第三个位置开始显示write_cmd_1函数内的信息
	}
}
  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的51单片机LCD1602显示程序的实现。 首先,你需要包含头文件和定义相关的常量和函数,如下所示: ```c #include <reg52.h> // 51单片机头文件 #define LCD_RS P1_0 // LCD1602的RS引脚连接到P1.0 #define LCD_RW P1_1 // LCD1602的RW引脚连接到P1.1 #define LCD_EN P1_2 // LCD1602的EN引脚连接到P1.2 #define LCD_D4 P1_4 // LCD1602的D4引脚连接到P1.4 #define LCD_D5 P1_5 // LCD1602的D5引脚连接到P1.5 #define LCD_D6 P1_6 // LCD1602的D6引脚连接到P1.6 #define LCD_D7 P1_7 // LCD1602的D7引脚连接到P1.7 void LCD_Init(void); void LCD_WriteCommand(unsigned char command); void LCD_WriteData(unsigned char data); void LCD_WriteString(unsigned char *string); void LCD_SetCursor(unsigned char row, unsigned char col); void LCD_Clear(void); ``` 接下来,你需要编写相应的函数来实现LCD1602的初始化、写命令、写数据、写字符串、设置光标位置以及清除显示内容等功能。这里我提供一个参考实现,如下所示: ```c // 初始化LCD1602 void LCD_Init(void) { LCD_WriteCommand(0x02); // 4位总线,设置为4位模式 LCD_WriteCommand(0x28); // 4位总线,2行显示,5x8点阵 LCD_WriteCommand(0x0c); // 显示开,光标关闭,闪烁关闭 LCD_WriteCommand(0x06); // 光标移动,文字不动 LCD_Clear(); // 清除显示内容 } // 向LCD1602写入命令 void LCD_WriteCommand(unsigned char command) { LCD_RS = 0; // RS引脚为低电平,表示写入命令 LCD_RW = 0; // RW引脚为低电平,表示写入操作 LCD_EN = 1; // EN引脚为高电平,表示操作开始 LCD_D4 = command >> 4; // 高4位数据 LCD_D5 = command >> 5; LCD_D6 = command >> 6; LCD_D7 = command >> 7; LCD_EN = 0; // EN引脚为低电平,表示操作结束 LCD_D4 = command & 0x0f; // 低4位数据 LCD_D5 = (command & 0x1f) >> 1; LCD_D6 = (command & 0x3f) >> 2; LCD_D7 = (command & 0x7f) >> 3; LCD_EN = 1; // EN引脚为高电平,表示操作开始 LCD_EN = 0; // EN引脚为低电平,表示操作结束 } // 向LCD1602写入数据 void LCD_WriteData(unsigned char data) { LCD_RS = 1; // RS引脚为高电平,表示写入数据 LCD_RW = 0; // RW引脚为低电平,表示写入操作 LCD_EN = 1; // EN引脚为高电平,表示操作开始 LCD_D4 = data >> 4; // 高4位数据 LCD_D5 = data >> 5; LCD_D6 = data >> 6; LCD_D7 = data >> 7; LCD_EN = 0; // EN引脚为低电平,表示操作结束 LCD_D4 = data & 0x0f; // 低4位数据 LCD_D5 = (data & 0x1f) >> 1; LCD_D6 = (data & 0x3f) >> 2; LCD_D7 = (data & 0x7f) >> 3; LCD_EN = 1; // EN引脚为高电平,表示操作开始 LCD_EN = 0; // EN引脚为低电平,表示操作结束 } // 向LCD1602写入字符串 void LCD_WriteString(unsigned char *string) { while (*string != '\0') { LCD_WriteData(*string); string++; } } // 设置LCD1602的光标位置 void LCD_SetCursor(unsigned char row, unsigned char col) { unsigned char address; if (row == 0) { address = 0x00 + col; } else if (row == 1) { address = 0x40 + col; } else { return; } LCD_WriteCommand(0x80 | address); } // 清除LCD1602显示内容 void LCD_Clear(void) { LCD_WriteCommand(0x01); LCD_SetCursor(0, 0); } ``` 其中,LCD_Init函数用于初始化LCD1602,LCD_WriteCommand函数用于向LCD1602写入命令,LCD_WriteData函数用于向LCD1602写入数据,LCD_WriteString函数用于向LCD1602写入字符串,LCD_SetCursor函数用于设置LCD1602的光标位置,LCD_Clear函数用于清除LCD1602显示内容。 最后,你可以在主函数中调用相应的函数来实现LCD1602显示。例如,你可以编写一个简单的程序来显示"Hello, World!",如下所示: ```c void main(void) { LCD_Init(); // 初始化LCD1602 LCD_WriteString("Hello, World!"); // 在LCD1602显示"Hello, World!" while (1); // 程序运行结束 } ``` 希望这个程序能够对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值