LCD1602显示总结
2021-11-24 来源:eefocus
仿真原理图如下:
1.LCD1602正常显示两行数据
/*------------------------
功能:LCD1602显示两行数据
Author: Zhang Kaizhou
Date: 2019-6-3 12:42:22
------------------------*/
#include #define uchar unsigned char #define uint unsigned int uchar code table0[] = {'0123456789ABCDEF'}; // 每行的字符数据 uchar code table1[] = {'0123456789abcdef'}; sbit lcdrs = P1^2; // LCD数据/命令选择端 sbit lcdrw = P1^3; // LCD读写选择端 sbit lcden = P1^4; // LCD使能端,高脉冲有效 uchar num; void init(); void writeCommand(uchar command); void delay(uchar xms); void writeData(uchar dat); void main() { init(); writeCommand(0x80); // 将数据指针定位到第一行的首地址 for(num = 0; num < 16; num++) { writeData(table0[num]); delay(5); } writeCommand(0x80 + 0x40); // 将数据指针定位到第二行的首地址 for(num = 0; num < 16; num++) { writeData(table1[num]); delay(5); } while(1); } /*初始化LCD1602的设置*/ void init() { lcden = 0; // 拉低使能端,准备产生使能高脉冲信号 writeCommand(0x38); // 显示模式设置(16x2, 5x7点阵,8位数据接口) writeCommand(0x0c); // 开显示,不显示光标 writeCommand(0x06); // 写一个字符后地址指针自动加1 writeCommand(0x01); // 显示清零,数据指针清零 } /*写指令函数*/ void writeCommand(uchar command) { lcdrs = 0; // 命令选择 lcdrw = 0; P0 = command; delay(5); lcden = 1; // 产生一个正脉冲使能信号 delay(5); lcden = 0; } /*写数据函数*/ void writeData(uchar dat) { lcdrs = 1; // 数据选择 lcdrw = 0; P0 = dat; delay(5); lcden = 1; delay(5); lcden = 0; } /*延时函数*/ void delay(uchar xms) { uint i, j; for(i = xms; i > 0; i--) for(j = 110; j > 0; j--); } 2.整屏左移显示 /*------------------------ 功能:LCD1602整屏左移显示 Author: Zhang Kaizhou Date: 2019-6-3 12:35:23 ------------------------*/ #include #define uchar unsigned char #define uint unsigned int uchar code table0[] = {'0123456789ABCDEF'}; // 每行的字符数据 uchar code table1[] = {'0123456789abcdef'}; sbit lcdrs = P1^2; // LCD数据/命令选择端 sbit lcdrw = P1^3; // LCD读写选择端 sbit lcden = P1^4; // LCD使能端,高脉冲有效 uchar num; void init(); void writeCommand(uchar command); void delay(uchar xms); void writeData(uchar dat); void main() { init(); writeCommand(0x80 + 0x10); // 将数据指针定位到第一行的非显示存储区 for(num = 0; num < 16; num++) { writeData(table0[num]); delay(5); } writeCommand(0x80 + 0x50); // 将数据指针定位到第二行的非显示存储区 for(num = 0; num < 16; num++) { writeData(table1[num]); delay(5); } for(num = 0; num < 16; num++) // 循环整屏左移 { writeCommand(0x1c); // 整屏左移 delay(100); } while(1); } /*初始化LCD1602的设置*/ void init() { lcden = 0; // 拉低使能端,准备产生使能高脉冲信号 writeCommand(0x38); // 显示模式设置(16x2, 5x7点阵,8位数据接口) writeCommand(0x0c); // 开显示,不显示光标 writeCommand(0x06); // 写一个字符后地址指针自动加1 writeCommand(0x01); // 显示清零,数据指针清零 } /*写指令函数*/ void writeCommand(uchar command) { lcdrs = 0; // 命令选择 lcdrw = 0; P0 = command; delay(5); lcden = 1; // 产生一个正脉冲使能信号 delay(5); lcden = 0; } /*写数据函数*/ void writeData(uchar dat) { lcdrs = 1; // 数据选择 lcdrw = 0; P0 = dat; delay(5); lcden = 1; delay(5); lcden = 0; } /*延时函数*/ void delay(uchar xms) { uint i, j; for(i = xms; i > 0; i--) for(j = 110; j > 0; j--); } 3.刷屏显示多行字符 /*---------------------------- 功能:LCD1602刷屏显示多行字符 Author: Zhang Kaizhou Date: 2019-6-4 16:15:43 -----------------------------*/ #include #include #define uchar unsigned char #define uint unsigned int uchar code table0[] = {'0123456789ABCDEF'}; // 每行的字符数据 uchar code table1[] = {'0123456789abcdef'}; uchar code table2[] = {'Welcome to here!'}; uchar code table3[] = {'I'm ZKZ!'}; uchar code table4[] = {'Life is nice!'}; uchar code table5[] = {'Tomorrow is OK!'}; sbit lcdrs = P1^2; // LCD数据/命令选择端 sbit lcdrw = P1^3; // LCD读写选择端 sbit lcden = P1^4; // LCD使能端,高脉冲有效 uchar num; void init(); void writeCommand(uchar command); void delay(uint xms); void writeData(uchar dat); void main() { init(); /*第一屏*/ writeCommand(0x80); // 将数据指针定位到第一行首 for(num = 0; num < strlen(table0); num++) { writeData(table0[num]); delay(5); } writeCommand(0x80 + 0x40); // 将数据指针定位到第二行首 for(num = 0; num < strlen(table1); num++) { writeData(table1[num]); delay(5); } delay(2000); writeCommand(0x01); /*第二屏*/ writeCommand(0x80); // 将数据指针定位到第一行首 for(num = 0; num < strlen(table2); num++) { writeData(table2[num]); delay(5); } writeCommand(0x80 + 0x40); // 将数据指针定位到第二行首 for(num = 0; num < strlen(table3); num++) { writeData(table3[num]); delay(5); } delay(2000); writeCommand(0x01); /*第三屏*/ writeCommand(0x80); // 将数据指针定位到第一行首 for(num = 0; num < strlen(table4); num++) { writeData(table4[num]); delay(5); } writeCommand(0x80 + 0x40); // 将数据指针定位到第二行首 for(num = 0; num < strlen(table5); num++) { writeData(table5[num]); delay(5); } while(1); } /*初始化LCD1602的设置*/ void init() { lcden = 0; // 拉低使能端,准备产生使能高脉冲信号 writeCommand(0x38); // 显示模式设置(16x2, 5x7点阵,8位数据接口) writeCommand(0x0c); // 开显示,不显示光标 writeCommand(0x06); // 写一个字符后地址指针自动加1 writeCommand(0x01); // 显示清零,数据指针清零 } /*写指令函数*/ void writeCommand(uchar command) { lcdrs = 0; // 命令选择 lcdrw = 0; P0 = command; delay(5); lcden = 1; // 产生一个正脉冲使能信号 delay(5); lcden = 0; } /*写数据函数*/ void writeData(uchar dat) { lcdrs = 1; // 数据选择 lcdrw = 0; P0 = dat; delay(5); lcden = 1; delay(5); lcden = 0; } /*延时函数*/ void delay(uint xms) { uint i, j; for(i = xms; i > 0; i--) for(j = 110; j > 0; j--); }
上一篇:C51键盘检测相关问题总结
下一篇:C51软件精确延时(us级)