LCD1602
liquid crystal display 液晶显示屏,一种字符型液晶显示模块,可以显示 16*2 个字符,每个字符是 5*7 点阵。
P0 P2 会和数码管、LED 一定程度上冲突。
-
地。
-
Vcc。
-
调对比度的。
-
RS:数据指令端。1代表 DB 是数据,0代表是指令。
-
RW:1读0写。
-
E:类似时钟的使能。高电平有效,下降沿执行。
-
DB:并行输入。一个字节长。
-
BG:背光灯电源。
内部结构图
类似 SMG,想显示1并不是直接输入1,而是操纵数码管右侧一竖被点亮。字模库起的就是这个作用,里面有固定的 ROM 和用户课自定义的 RAM。
DDRAM 长于屏幕,可以通过移平实现滚动效果。
AC address controller,可以自动移位写入数据。
字模库中的数据大多数和 ASCII 码是一样的。
指令
初始化指令
初始化要做哪些操作?
规定显示区域(如8位数据接口,2行显示,5*7点阵,即为0011 10xx,如果取0即为 0x38.)。
显示的模式设置(如开启显示,关闭光标,关闭光标闪烁:0000 1101,即 0x0D)。
进入模式设置(如读写后光标++,屏幕不动:0000 0110,0x06。如果是滚动屏幕则为)。
清屏(0x01)。
显示指令
先设置 DDRAM 初始地址,0x80|AC(开头的8是 DDRAM 固定指令信息不能改。后面的全是0,与 AC 光标位置做与,AC 不同位置的值见 DDRAM 地址表).
然后发送数据。
时序
模块化
第一阶段我们先编写 LCD1602 模块化编程代码,这一部分主要显示静态内容,用于程序编写过程中显示变量进行调试。
目标模块化函数:
//LCD1602.h
#ifndef __LCD1602_H__
#define __LCD1602_H__
#include "reg52.h"
#include "intrins.h"
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_E=P2^7;
#define LCD_DATAPORT P0
void LCD_Delay1ms(); //@11.0592MHz
void LCD_WriteCommand(unsigned char Command);
void LCD_WriteData(unsigned char Data);
void LCD_Init();
void LCD_SetCursor(unsigned char line, unsigned char column);
unsigned int LCD_Pow(unsigned char x, unsigned char y);
void LCD_ShowChar(unsigned char line, unsigned char column, unsigned char c);
void LCD_ShowString(unsigned char line, unsigned char column, unsigned char str[]);
void LCD_ShowNum(unsigned char line, unsigned char column, unsigned int num, unsigned char length);
void LCD_ShowSignedNum(unsigned char line, unsigned char column, int num, unsigned char length);
void LCD_ShowHexNum(unsigned char line, unsigned char column, unsigned int num, unsigned char length);
void LCD_ShowBinNum(unsigned char line, unsigned char column, unsigned int num, unsigned char length);
#endif
//LCD1602.c
#include "LCD1602.H"
void LCD_Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void LCD_WriteCommand(unsigned char Command){
LCD_RS=0;
LCD_RW=0;
LCD_E=0;
LCD_DATAPORT=Command;
LCD_Delay1ms();
LCD_E=1;
LCD_Delay1ms();
LCD_E=0;
}
void LCD_WriteData(unsigned char Data){
LCD_RS=1;
LCD_RW=0;
LCD_E=0;
LCD_DATAPORT=Data;
LCD_Delay1ms();
LCD_E=1;
LCD_Delay1ms();
LCD_E=0;
}
void LCD_Init(){
LCD_WriteCommand(0x38);
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);
}
}
unsigned int LCD_Pow(unsigned char x, unsigned char y){
unsigned char i=y;
unsigned int res=1;
for(;i>0;i--)res*=x;
return res;
}
void LCD_ShowChar(unsigned char line, unsigned char column, unsigned char c){
LCD_SetCursor(line, column);
LCD_WriteData(c);
}
void LCD_ShowString(unsigned char line, unsigned char column, unsigned char str[]){
unsigned int i=0;
LCD_SetCursor(line, column);
while(str[i]!='\0'){
LCD_WriteData(str[i]);
i++;
}
}
void LCD_ShowNum(unsigned char line, unsigned char column, unsigned int num, unsigned char length){
unsigned char i;
unsigned base;
LCD_SetCursor(line, column);
for(i=length;i>0;i--){
base=LCD_Pow(10, i-1);
LCD_WriteData(num/base%10+'0');
}
}
void LCD_ShowBinNum(unsigned char line, unsigned char column, unsigned int num, unsigned char length){
unsigned char i;
unsigned char base;
unsigned char single_num;
LCD_SetCursor(line, column);
for(i=length;i>0;i--){
base=LCD_Pow(2, i-1);
single_num=num/base%2+'0';
LCD_WriteData(single_num);
}
}
void LCD_ShowHexNum(unsigned char line, unsigned char column, unsigned int num, unsigned char length){
unsigned char i;
unsigned char base;
unsigned char single_num;
LCD_SetCursor(line, column);
for(i=length;i>0;i--){
base=LCD_Pow(16, i-1);
single_num=num/base%16;
if(single_num<=9)single_num+='0';
else single_num+='A'-10;
LCD_WriteData(single_num);
}
}
void LCD_ShowSignedNum(unsigned char line, unsigned char column, int num, unsigned char length){
unsigned char i;
unsigned base;
LCD_SetCursor(line, column);
if(num>0)LCD_WriteData('+');
else {LCD_WriteData('-');num=-num;}
for(i=length;i>0;i--){
base=LCD_Pow(10, i-1);
LCD_WriteData(num/base%10+'0');
}
}
如果希望屏幕滚动,一定时间执行一次LCD_WriteCommand(0x18);
屏幕左移指令。