LCD的读写操作
| RS | R/W | E | D0~D7 |
---|
读状态 | 0 | 1 | 1 | 输出状态字 |
写命令 | 0 | 0 | 正脉冲 | 输出无 |
读数据 | 1 | 1 | 1 | 输出数据 |
写数据 | 1 | 0 | 正脉冲 | 输出无 |
LCD1602的控制命令表
指令码 | 描述 |
---|
01H | 清屏,向DDRAM写入空格码,光标返回显示屏左上角 |
02H | 光标归位,光标返回显示屏左上角 |
06H | 读或写一个字符后,光标右移,屏幕不动 |
04H | 读或写一个字符后,光标左移,屏幕不动 |
07H | 读或写一个字符后,光标和屏幕向右移动一个字符 |
05H | 读或写一个字符后,光标和屏幕向左移动一个字符 |
08H | 关显示,关光标,关闪烁 |
0EH | 开显示,开光标,关闪烁 |
0DH | 开显示,关光标,开闪烁 |
0CH | 开显示,关光标,关闪烁 |
0FH | 开显示,开光标,开闪烁 |
10H | 每输入该指令,光标左移一个字符 |
14H | 每输入该指令,光标右移一个字符 |
18H | 每输入该指令,画面左移一个字符 |
1CH | 每输入该指令,画面右移一个字符 |
30H | 单行显示,5X8点阵,8位总线 |
34H | 单行显示,5X11点阵,8位总线 |
38H | 两行显示,5X8点阵,8位总线 |
3CH | 两行显示,5X11点阵,8位总线 |
命令 7:CGRAM地址设置
命令格式:04H+地址码,该命令决定自定义字符的存储地址。
命令 8:DDRAM地址设置
命令格式:08H+地址码,该命令决定字符在LCD上的显示位置。
命令 9:读“忙”或状态字
BF = 1:LCD忙,不能接收命令或数据
BF = 0:LCD不忙,可以接收命令或数据
main.c
#include "lcd1602.h"unsigned char code temp1[] = {0x01,0x1F,0x11,0x1F,0x11,0x1F,0x01,0x01,0x00,0x1C,0x04,0x1C,0x04,0x1D,0x01,0x1E, 0x07,0x04,0x04,0x04,0x05,0x04,0x08,0x13,0x1e,0x00,0x10,0x10,0x1C,0x10,0x14,0x1F}; unsigned char Thousand;
unsigned char Hundred;
unsigned char Ten;
unsigned char One;
unsigned int temp = 8421;void main(void)
{Thousand = temp/1000; Hundred = temp/100%10; Ten = temp/10%10; One = temp%10;
EA = 1;ET0 = 1;LCD1602_Init();LCD1602_WriteChar(1,0x00,'A'); LCD1602_WriteChar(1,0x01,0x30|Thousand); LCD1602_WriteChar(1,0x02,0x30|Hundred); LCD1602_WriteChar(1,0x03,0x30|Ten); LCD1602_WriteChar(1,0x04,0x30|One); LCD1602_WriteString(1,0x05,"Hello Word");
LCD1602_W(0x00,4,temp1);
LCD1602_WriteChar(2,0x00,0x00); LCD1602_WriteChar(2,0x01,0x01); LCD1602_WriteChar(2,0x02,0x02); LCD1602_WriteChar(2,0x03,0x03); while(1);
}
lcd1602.h
#ifndef LCD1602_H
#define LCD1602_H#include <STC8H.H>
#include "delay.h"#define unchar unsigned char
#define unint unsigned int#define BF 0x80 #define OUT P0 sbit RS = P2^6;
sbit R_W = P2^5;
sbit E = P2^7; void GPIO_Init(void);
void LCD1602_Init(void); void LCD1602_WriteCommand(unchar command);
void LCD1602_WriteData(unchar Data); void LCD1602_WriteAddr(unchar x,unchar y);
void LCD1602_WriteString(unchar x,unchar y,unchar *s);
void LCD1602_WriteChar(unchar x,unchar y,unchar c); void LCD1602_CheckBusy(void);
void LCD1602_Write_0x38(void);
void LCD1602_W(unchar addr,unchar n,unchar *s);#endif
lcd1602.c
#include "lcd1602.h"void GPIO_Init(void)
{
P0M0 = 0x00;P0M1 = 0x00;P2M0 = 0x00;P2M1 = 0x00;
}void LCD1602_Init(void)
{GPIO_Init();LCD1602_Write_0x38(); delay_ms(5); LCD1602_Write_0x38(); delay_ms(5); LCD1602_Write_0x38(); delay_ms(5); LCD1602_WriteCommand(0x38); LCD1602_WriteCommand(0x01); delay_ms(5);LCD1602_WriteCommand(0x06); LCD1602_WriteCommand(0x0c);
}void LCD1602_WriteCommand(unchar command)
{LCD1602_CheckBusy();RS = 0; R_W = 0; OUT = command;E = 1;delay_us(1);E = 0;
}void LCD1602_WriteData(unchar Data)
{LCD1602_CheckBusy();RS = 1; R_W = 0; OUT = Data; E = 1;delay_us(1);E = 0;
}void LCD1602_WriteAddr(unchar x,unchar y)
{if(x==1){LCD1602_WriteCommand(y|0x80);}else if(x==2){LCD1602_WriteCommand(y|0x80|0x40);}
}void LCD1602_WriteChar(unchar x,unchar y,unchar c)
{LCD1602_WriteAddr(x, y);LCD1602_WriteData(c);
}void LCD1602_WriteString(unchar x,unchar y,unchar *s)
{LCD1602_WriteAddr(x, y);while(*s != '\0'){LCD1602_WriteData(*s);s++;}
}void LCD1602_CheckBusy(void)
{unchar temp;OUT = 0xff;RS = 0; R_W = 1; do{ E = 1; temp = OUT; }while(temp&BF); E = 0;
}
void LCD1602_Write_0x38(void)
{ E = 0;RS = 0; R_W = 0; OUT = 0x38;E = 1;delay_us(1);E = 0;
}void LCD1602_W(unchar addr,unchar n,unchar *s)
{unsigned char i; LCD1602_WriteCommand(0x40|addr);for(i=0;i<(8*n);i++){LCD1602_WriteData(*s);s++; }
}