LCD1602
14.1LCD1602的基础知识
1.LCD1602介绍
LCD1602(Liquid Crystal Display)液晶显示屏是一种字符型液晶显示模块,可以显示ASCII码的标准字符和其它的一些内置特殊字符,还可以有8个自定义字符
显示容量:16×2个字符,每个字符为5*7点阵
2.引脚及应用电路
3.内部结构框图
4.时序结构
5.LCD1602指令集
6.字符值
7.LCD1602操作流程
14.2LCD1602功能函数代码
1.显示一个字符
(1)工程目录
(2)main.c函数
c
#include <REGX52.H>
#include "LCD1602.h"
void main()
{
LCD_Init();
LCD_ShowChar(2,3,'A');
while(1)
{
}
}
(3)LCD1602.c函数
c
#include <REGX52.H>
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_E=P2^7;
#define LCD_DataPort P0
void LCD_Delay() //@11.0592MHz 1ms
{
unsigned char data i, j;
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;
LCD_Delay();
LCD_E=0;
LCD_Delay();
}
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();
}
void LCD_Init(void)
{
LCD_WriteCommand(0x38);
LCD_WriteCommand(0x0C);
LCD_WriteCommand(0x06);
LCD_WriteCommand(0x01);
}
void LCD_ShowChar(unsigned char Line,unsigned char Column,unsigned char Char)
{
if(Line==1)
{
LCD_WriteCommand(0x80|(Column-1));
}
else
{
LCD_WriteCommand(0x80|(Column-1)+0x40);
}
LCD_WriteData(Char);
}
补充知识点:字符和字符串
字符:根据一定规则建立的数字到字符的映射(ASCII码表)
例如:0x21='!'.0x41='A'.0x00=\0'
定义方法:char x='A':(等效干char x=0x41:)
字符数组:存储字符变量的一个数组
定义方法:char y[]=f'A','B','C';(等效于char y[]={0x41,0×42,0×43;)
字符串:在字符数组后加一个字符串结束标志,本质上是字符数组定义方法:char z[]="ABC";(等效于char z[]=f'A','B','C',\O;)
2.显示一组字符串
(1)代码
c
void LCD_ShowString(unsigned char Line,unsigned char Column,unsigned char String[])
{
unsigned char i;
LCD_SetCursor(Line,Column);
for(i=0;String[i]!='\0';i++)
{
LCD_WriteData(String[i]);
}
}
2.显示数字
(1)无符号代码
c
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_Pow(10,i-1)%10);
}
}
(2)有符号代码
c
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
unsigned char i;
unsigned int Number1;
LCD_SetCursor(Line,Column);
if(Number>=0)
{
LCD_WriteData('+');
Number1=Number;
}
else
{
LCD_WriteData('-');
Number1=-Number;
}
for(i=Length;i>0;i--)
{
LCD_WriteData('0'+Number1/LCD_Pow(10,i-1)%10);
}
}
2.显示进制
(1)十六进制
c
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_Pow(16,i-1)%16;
if(SingleNumber<10)
{
LCD_WriteData('0'+SingleNumber);
}
else
{
LCD_WriteData('A'+SingleNumber-10);
}
}
}
(1)二进制
c
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_Pow(2,i-1)%2);
}
}