一、LCD1602介绍:
LCD1602是一种字符型液晶显示屏,可以显示ASCII码的标准字符和其他的内置特殊字符。
显示容量:16*2个字符,每个字符为5*7点阵。
二、引脚及应用电路:
其中:D0-7这8位数据是接在P0引脚上的,且高低位对应,即P0^7 - D7。
三、内部结构:
DDRAM储存ASCII码值,随后在CGRAM字模库中找到对应的应亮像素点,然后显示出来。
其中DDRAM的容量大于屏幕大小,所以后面会学指令,使屏显内容移动,做到流动屏。
DDRAM地址:
四、时序结构:
先将RS置1,随后R/W置0,再将E置1,随后修改D0-7的数据,再将E置0。
五、指令集:
本节所用指令:
清屏:0x01
输入方式:0x06 数据读写后,光标加一,画面不动
显示开关控制:0x0c 显示开,光标关,显示关
功能设置:0x38 8位数据接口,2行显示,5*7点阵
DDRAM地址设置:选择在DDRAM中存储数据的地址。最高位置1,后面为地址。
(例:第二行第一位:40即 0100 0000,最高位置1,得1100 0000,即为地址)
可用:0x80 | ()
总集:
六、集成LCD1602:
sbit LCD_RS = P2 ^ 6;
sbit LCD_RW = P2 ^ 5;
sbit LCD_E = P2 ^ 7;
#define LCD_DataPort P0
void LCD_Delay() //@12.000MHz 1ms
{
unsigned char i, j;
i = 2;
j = 239;
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()
{
LCD_WriteCommand(0x38); //8位数据接口,2行显示,5*7点阵
LCD_WriteCommand(0x0C); //显示开,光标关,显示关
LCD_WriteCommand(0x06); //数据读写后,光标加一,画面不动
LCD_WriteCommand(0x01); //清屏
}
//设置光标位置
void LCD_SetCursor(unsigned char Line, unsigned char Column)
{
if (Line == 1)
{
LCD_WriteCommand(0x80 | (Column - 1));
}
else
{
LCD_WriteCommand(0x80 | (Column - 1) + 0x40);
}
}
void LCD_ShowChar(unsigned char Line, unsigned char Column, unsigned char Char)
{
LCD_SetCursor(Line, Column);
LCD_WriteData(Char);
}
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]);
}
}
int LCD_Pow(int x, int y)
{
unsigned char i;
int Result = 1;
for (i = 0; i < y; i++)
{
Result *= x;
}
return Result;
}
//10进制
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);
}
}
//有符号10进制
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);
}
}
//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_Pow(16, i - 1) % 16;
if (SingleNumber < 10)
{
LCD_WriteData('0' + SingleNumber);
}
else
{
LCD_WriteData('A' + SingleNumber - 10);
}
}
}
//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_Pow(2, i - 1) % 2);
}
}