31. LCD1602
-
31.1. LCD1602介绍
•LCD1602(Liquid Crystal Display)液晶显示屏是一种字符型液晶显示模块,可以显示ASCII码的标准字符和其它的一些内置特殊字符,还可以有8个自定义字符
•显示容量:16×2个字符,每个字符为5*7点阵 -
31.2. 引脚及应用电路
接线D0~D7与单片机P口高低位对齐
-
31.3. 内部结构框图
CGRAM CGROM类似段码表,RAM可写,ROM不可写;
DDRAM用户可写,DDRAM40x2,比屏幕大,但是前16映射屏幕;多出来的可以移屏显示;
AC光标位置,地址计数器,指令确定AC的位置
- 31.4. 存储器结构
举例A的索引码:0100 0001 B, 0x41,上方的索引码填入到左侧的xxxx中
索引码同ASIC码
-
31.5. 时序结构(写的时序结构)
RS=1(数据);RS=0(指令)
RW=0;
DB0~DB7放好数据;
E=1写入数据;
E=0完成写入;
-
31.6. LCD1602指令集
下表是指令的格式,指令集;
原LCD1602程序中显示(行,列,xxx)就是在设置DDRAM地址,光标位置;
-
31.7. LCD1602操作流程
•初始化:
发送指令0x38 //八位数据接口,两行显示,5*7点阵
发送指令0x0C //显示开,光标关,闪烁关
发送指令0x06 //数据读写操作后,光标自动加一,画面不动
发送指令0x01 //清屏•显示字符:
发送指令0x80|AC //设置光标位置//实际地址最高为必须置1代表是地址设置的指令
发送数据 //发送要显示的字符数据
发送数据 //发送要显示的字符数据
…… -
31.8. 字符、字符串
•字符:根据一定规则建立的数字到字符的映射(ASCII码表)
例如:0x21=’!’,0x41=’A’,0x00=’\0’
定义方法:char x=‘A’;(等效于char x=0x41;)
•字符数组:存储字符变量的一个数组
定义方法:char y[]={’A’, ’B’, ’C’};
(等效于char y[]={0x41,0x42,0x43}; )
•字符串:在字符数组后加一个字符串结束标志,本质上是字符数组
定义方法:char z[]=”ABC”;(等效于char z[]={’A’, ’B’, ’C’, ’\0’};)
32. LCD1602显示模块编程
-
32.1. 编程思路:
- 需要编写LCD写数据模块和写命令模块
- 编写LCD初始化模块
- 编写LCD光标位设置模块
- 编写LCD_ShowChar显示单字节模块
-
32.2. LCD1602.c和LCD1602.h程序如下:
#include <REGX52.H>
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_E=P2^7;
#define LCD_DataPort P0
/*定义一个1ms延时函数*/
void LCD_Delay(void) //@12.000MHz, 1ms延时
{
unsigned char data i, j;
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
/*LCD写命令函数*/
void LCD_WriteCommand(unsigned char Command)
{
LCD_RS=0;
LCD_RW=0;
LCD_DataPort=Command;
LCD_E=1;
LCD_Delay(); //如果不延时会出错
LCD_E=0;
LCD_Delay(); //如果不延时会出错
}
/*LCD写数据函数*/
void LCD_WriteData(unsigned char Data)
{
LCD_RS=1;
LCD_RW=0;
LCD_DataPort=Data;
LCD_E=1;
LCD_Delay(); //如果不延时会出错
LCD_E=0;
LCD_Delay(); //如果不延时会出错
}
/*LCD初始化函数*/
void LCD_Init(void)
{
LCD_WriteCommand(0x38);
LCD_WriteCommand(0x0c);
LCD_WriteCommand(0x06);
LCD_WriteCommand(0x01);
}
/*LCD光标位置设置函数*/
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
if(Line==2)
{
LCD_WriteCommand(0x80|(Column-1)+0x40); //0x40是根据表格进行的移位
}
}
/*LCD显示一个字符函数*/
void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char)
{
LCD_SetCursor(Line,Column); //光标位置
LCD_WriteData(Char); //写入数据
}
/*LCD显示字符串函数*/
void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char String[]) //String[]传递的是首地址和指针一致
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
/*定义一个次方函数*/
int LCD_Power(int X, int Y)
{
unsigned char i;
int Result=1;
for(i=0;i<Y;i++)
{
Result*=X;
}
return Result;
}
/*LCD显示无符号数函数*/
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData('0'+Number/LCD_Power(10,i-1)%10); //加'0'等于加0x30,对应码表,否则显示空
}
}
/*LCD显示有符号数函数*/
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1; //借用Number1,防止-32768无法显示
LCD_SetCursor(Line,Column);
if(Number>=0) //判断数字大于0加+号
{
LCD_WriteData('+');
Number1=Number;
}
else //判断数字小于0加-号
{
LCD_WriteData('-');
Number1=-Number; //如果没有Number,char -32768会超出
}
for(i=Length;i>0;i--)
{
LCD_WriteData('0'+Number1/LCD_Power(10,i-1)%10); //加'0'等于加0x30,对应码表,否则显示空
}
}
/*LCD显示有16进制数函数*/
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
unsigned char SingleNumber;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
SingleNumber=Number/LCD_Power(16,i-1)%16;
if(SingleNumber<10)
{
LCD_WriteData('0'+SingleNumber);
}
else
{
LCD_WriteData('A'+SingleNumber-10); //A的偏移对应10~16, A~F,-10对齐表格
}
}
}
/*LCD显示有2进制数函数*/
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=Length;i>0;i--)
{
LCD_WriteData('0'+Number/LCD_Power(2,i-1)%2);
}
}
#ifndef _LCD1602_H_
#define _LCD1602_H_
void LCD_Init(void);
void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char);
void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char String[]);
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_WriteCommand(unsigned char Command);
#endif
- main主程序为:
#include <REGX52.h>
#include "LCD1602.h"
#include "delay_xms.h"
void main()
{
LCD_Init();
LCD_ShowChar(1,1,'A'); //索引码A=0x41; \反斜杠用于转义,如'\'';'\\'标准ASIC是反斜杠,中文是钱币符号
LCD_ShowString(1,3,"Hello");
LCD_ShowNum(1,9,66,2);
LCD_ShowSignedNum(1,12,-88,2);
LCD_ShowHexNum(2,1,0xA5,2);
LCD_ShowBinNum(2,4,0xA5,8);
LCD_ShowChar(2,13,0xdf); //直接输入码表地址0xdf,0xe0=@
LCD_ShowChar(2,14,'C'); //直接输入码表地址0xdf,0xe0=@
LCD_ShowString(1,16,"Welcome to China!");
while(1)
{
LCD_WriteCommand(0x18); //流动字幕效果的移屏命令地址
delay_xms(500);
}
}
Proteus测试无误