3. 51——LCD1602显示 字符 或 字符串
作者:互联网
51——LCD1602显示 字符 或 字符串
LCD1602简介
LCD1602相关引脚
LCD1602写命令、写数据相关操作图
写指令 代码
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS = 0; //指令
LCD_RW = 0; //写
LCD_DataPort = Command; //数据存放
LCD_E = 1; //使能
Delay1ms(); //最低要求延迟150纳秒 我们直接给1ms
LCD_E = 0; //失能
Delay1ms();
}
写数据 代码
void LCD_WriteData(unsigned char Data)
{
LCD_RS = 1; //数据
LCD_RW = 0; //写
LCD_DataPort = Data; //数据存放
LCD_E = 1; //使能
Delay1ms(); //最低要求延迟150纳秒 我们直接给1ms
LCD_E = 0; //失能
Delay1ms();
}
LCD1602显示程序步骤及如何初始化
- 显示开关控制指令(功能:控制显示器开/关 光标显示/关闭 是否闪烁)
- 进入模式设置指令(功能:每写入一位数据后光标的移动 左/右 显示屏不移动/整体右移 )
- 功能设定指令(功能:设定数据总线的位数 显示的行数 字型 )
- 清屏指令(功能:清屏显示空白 光标归位 地址计数器(AC)的值为0)
- 显示位置设置(功能:指定在1602上哪个位置上显示数据)
显示开关控制指令
LCD_WriteCommand(0x0c); // 0000 1100 显示关,游标不显示,不闪烁
进入模式设置指令
LCD_WriteCommand(0x06) // 0000 0110 写入数据后光标自动右移 整屏不移动
功能设定指令
LCD_WriteCommand(0x38); //0011 1000 数据总线8位 16X2显示 5*7点阵
清屏指令
LCD_WriteCommand(0x01); //0000 0001 清屏
设置从(x,y)开始显示
如果想在1602屏幕上第一行第一个开始显示 就是0x80
如果想在1602屏幕上第二行第一个开始显示 就是0xc0
void LCD_SetCursor(unsigned char Line,unsigned char Columu)
{
if(Line == 1)
{
LCD_WriteCommand( 0x80 | (Columu-1) );
}
else
{
LCD_WriteCommand( 0x80 | ( Columu-1 )+0x40 );
}
}
显示字符 或 字符串
显示字符
void LCD_ShowChar(unsigned char Line,unsigned char Columu,unsigned char Char)
{
LCD_SetCursor(Line,Columu);
LCD_WriteData(Char);
}
显示字符串
void LCD_ShowString(unsigned char Line,unsigned char Columu,unsigned char* String)
{
unsigned char i = 0;
LCD_SetCursor(Line,Columu);
for(i=0;String[i] != '\0';i++)
{
LCD_WriteData(String[i]);
}
}
接线图
代码整合
LCD1602.c
#include "regx52.h"
#include "LCD1602.h"
#include "intrins.h"
sbit LCD_RS = P2^5;
sbit LCD_RW = P2^6;
sbit LCD_E = P2^7;
sbit LED_V0 = P1^0;
#define LCD_DataPort P0
void Delay1ms() //@11.0592MHz 1ms
{
unsigned char i, j;
_nop_();
i = 2;
j = 199;
do
{
while (--j);
} while (--i);
}
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS = 0;
LCD_RW = 0;
LCD_DataPort = Command;
LCD_E = 1;
Delay1ms();
LCD_E = 0;
Delay1ms();
}
void LCD_WriteData(unsigned char Data)
{
LCD_RS = 1;
LCD_RW = 0;
LCD_DataPort = Data;
LCD_E = 1;
Delay1ms();
LCD_E = 0;
Delay1ms();
}
void LCD_Init()
{
LCD_WriteCommand(0x38);
LCD_WriteCommand(0x0c);
LCD_WriteCommand(0x06);
LCD_WriteCommand(0x01);
LED_V0 = 0; //本来接滑动变阻器的,因为缺乏,直接拉低
}
void LCD_SetCursor(unsigned char Line,unsigned char Columu)
{
if(Line == 1)
{
LCD_WriteCommand( 0x80 | (Columu-1) );
}
else
{
LCD_WriteCommand( 0x80 | ( Columu-1 )+0x40 );
}
}
void LCD_ShowChar(unsigned char Line , unsigned char Columu , unsigned char Char)
{
LCD_SetCursor(Line,Columu);
LCD_WriteData(Char);
}
void LCD_ShowString(unsigned char Line , unsigned char Columu , unsigned char* String)
{
unsigned char i = 0;
LCD_SetCursor(Line,Columu);
for(i=0;String[i] != '\0';i++)
{
LCD_WriteData(String[i]);
}
}
LCD1602.h
#ifndef _LCD1602_h_
#define _LCD1602_h_
void Delay1ms(void);
void LCD_WriteCommand(unsigned char Command);
void LCD_WriteData(unsigned char Data);
void LCD_Init(void);
void LCD_SetCursor(unsigned char Line,unsigned char Columu);
void LCD_ShowChar(unsigned char Line , unsigned char Columu , unsigned char Char);
void LCD_ShowString(unsigned char Line , unsigned char Columu , unsigned char* String);
#endif
main.c
#include "regx52.h"
#include "intrins.h"
#include "Timer0.h"
#include "uart.h"
#include "LCD1602.h"
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 4;
j = 129;
k = 119;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void main()
{
LCD_Init();
LCD_ShowChar(1,7,'Y');
LCD_ShowString(2 , 2 , "Hello!Yinyuer");
while(1)
{
}
}
void UART_Routine() interrupt 4
{
if( RI==1 )
{
P1=SBUF;
UART_SendByte(SBUF);
RI = 0;
}
}
效果展示
标签:void,51,unsigned,char,LCD,Columu,LCD1602,字符串 来源: https://blog.csdn.net/weixin_46105931/article/details/120633463