LCD1602和LCD12864液晶四位显示.docx
《LCD1602和LCD12864液晶四位显示.docx》由会员分享,可在线阅读,更多相关《LCD1602和LCD12864液晶四位显示.docx(16页珍藏版)》请在冰豆网上搜索。
![LCD1602和LCD12864液晶四位显示.docx](https://file1.bdocx.com/fileroot1/2023-1/29/0a39ff97-e541-434f-8c72-cfec9deacf67/0a39ff97-e541-434f-8c72-cfec9deacf671.gif)
LCD1602和LCD12864液晶四位显示
LCD1602和LCD12864液晶四位显示
LCD1602和LCD12864是常用的液晶显示器件,与单片机连接有8位、4位和串口方式。
8位显示时,首先要选择单片机的一个8位端口与液晶的数据引脚(D7-D0)相连,还需另选一个端口与RS、RW和E三个控制引脚相连,多余5个这个端口引脚与RS、RW和E三个控制引脚构成互斥关系,造成程序编程设计上的难度,如果舍弃这五个引脚,则需占用单片机的两个端口引脚才能完成液晶显示。
在复杂电路设计中由于端口受限所以造成设计无法完成,须采用更多端口引脚的单片机才能完成设计。
本文利用LCD1602和LCD12864均具有的四位显示方式,将8位数据分成高四位和低四位分时传输,将单片机的低四位与液晶屏的高四位连接,将单片机的高四位与RS、RW和E三个控制引脚连接,完成了只用单片机一个端口即可完成显示任务,给设计带来极大的方便。
1、LCD1602四位显示例
下面的一个例子是使用P0口的低4位连接到LCD1602的高4位数据线。
RS、RW、E则分别连接到P0.4、P0.5、P0.6。
只用一个端口即完成了与LCD1602的数据传输。
如图2。
这里要特别指出LCD1602的低四位虽然不用,但也要如图中所示那样通过1K电阻接高电平,否则LCD1602是无法工作的。
图21602四位连接图
源代码:
/*
*1602.h
*Author:
程静涛
*/
#ifndef_u1602_H_
#define_u1602_H_
#defineucharunsignedchar
#defineuintunsignedint
sbitRS=P0^4;
sbitRW=P0^5;
sbitEN=P0^6;
//延时程序
voiddelay1(uinti)
{
uintj;
while(--i!
=0)
{
for(j=0;j<255;j++);
}
}
//写指令
VoidW_com(ucharcom)
{
EN=0;
RS=0;
RW=0;
P0=0x80|((com&0xf0)>>4);//送高四位数据到P0口,同时保持P0高四位。
EN=1;
delay1(5);
EN=0;
P0=0x80|(com&0x0f);//送低四位数据到P0口,同时保持P0高四位。
EN=1;
delay1(5);
EN=0;
}
//写数据
voidW_datd(uchardat)
{
EN=0;
RS=1;
RW=0;
P0=0x90|((dat&0xf0)>>4);//送高四位数据到P0口,同时保持P0高四位。
EN=1;
delay1(5);
EN=0;
P0=0x90|(dat&0x0f);//送低四位数据到P0口,同时保持P0高四位。
EN=1;
delay1(5);
EN_0;
}
//lcd初始化程序
voidlcdinit()
{delay1(30);
W_com(0x02);//光标归位
W_com(0x28);//四位传输
W_com(0x0c);//显示功能开,无光标、无闪烁。
W_com(0x06);//写入新数据后光标右移
W_com(0x01);//清屏
delay1(10);
}
//在指定位置显示一个字符,x为行,Y为列;
voiddisplaydat(ucharx,uchary,ucharone)
{
ucharadd;
if(x==0)add=0x80+y;
elseadd=0xc0+y;
W_com(add);
W_datd(one);
}
//显示字符串
voiddisplaystr(ucharx,uchary,uchar*s)
{
y=y&0x0f;
while(*s)
{
displaydat(x,y,*s);
y++;
s++;
}
}
#endif/*1602_H_*/
主程序
/*Processor:
AT89C52
*Compiler:
Keilfor8051
*/
#include
#include
#include"u1602.h"
#defineuintunsignedint
#defineucharunsignedchar
uchartable[]="LCD1602Line4";
uchartable1[]="Welcometostudy";
voidmain(void)
{lcdinit();
displaystr(0,0,table);
delay1(5);
displaystr(1,0,table1);
//Writeyourcodehere
while
(1)
;
}
2、LCD12864四位显示例
下面的一个例子是使用P0口的低4位连接到LCD12864的高4位数据线。
RS、RW、E则分别连接到P0.4、P0.5、P0.6。
只用一个端口即完成了与LCD12864的数据传输。
如图3。
这里要特别指出LCD12864的低四位虽然不用,但也要如图中所示那样通过1K电阻接高电平,否则LCD12864是无法工作的。
如图3
图3LCD12864四位显示
#include
#include
#include
#definedely();{_nop_();_nop_();_nop_();_nop_();};
#defineucharunsignedchar
#defineuintunsignedint
/*12864端口定义*/
#defineLCD_dataP0//数据口
sbitRS=P0^4;//寄存器选择输入
sbitRW=P0^5;//液晶读/写控制
sbitE=P0^6;//液晶使能控制
ucharcodestr1[]={"欢迎学习单片机"};
ucharcodestr2[]={"字符显示模式"};
ucharcodestr3[]={"LCD12864四位"};
ucharcodedis4[]={"交流QQ:
812549630"};
ucharcodetable1[]={//自定义字库
0x02,0x00,0x03,0x80,0x03,0x83,0xFF,0xFF,0xFF,0xFF,0xC3,0x83,0xFF,0xFF,0xFF,0xFF,
0xC3,0x83,0xFF,0xFF,0xFF,0xFF,0x03,0x80,0x03,0x82,0x03,0xFE,0x01,0xFC,0x00,0xF8,//电
0x00,0x0C,0x7F,0xFE,0x7F,0xFF,0x00,0x1C,0x00,0x78,0x01,0xE0,0x01,0xC0,0x00,0xC0,
0xFF,0xFF,0x7F,0xFF,0x00,0xC0,0x00,0xC0,0x00,0xC0,0x1F,0xC0,0x0F,0xC0,0x03,0x80,//子
0x03,0x18,0x33,0x1C,0x33,0x1C,0x33,0x18,0x33,0x18,0xFF,0xFF,0xFF,0xFF,0x33,0x18,
0x33,0x18,0x33,0x18,0x33,0xF8,0x33,0xF8,0x30,0x01,0x30,0x03,0x3F,0xFF,0x1F,0xFE,//世
0x30,0x0C,0x3F,0xFE,0x3F,0xFC,0x33,0xCC,0x3F,0xFC,0x3F,0xFC,0x33,0xCC,0x3F,0xFC,
0x3F,0xFC,0x0F,0x60,0x1E,0x30,0x3E,0x3C,0x76,0x3E,0xE6,0x37,0x0E,0x30,0x3C,0x30};//界
unsignedcharcodePhoto1[]=//图形
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x80,0x00,
0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE0,0x18,
0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x14,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF2,0xFE,
0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xF2,0x88,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xF7,0xE8,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF2,0xA8,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF3,0xAA,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF6,0xEE,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xAC,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF3,0x8C,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x13,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x20,
0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0xF0,0x40,
0xF0,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0xF0,0x30,
0xF0,0x00,0x00,0x00,0x00,0x0F,0xFE,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0xF0,0x00,
0xF0,0x00,0x00,0x00,0x00,0x3F,0xFF,0x80,0x00,0x00,0x3C,0x00,0x00,0x00,0xF7,0xFF,
0xF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FF,0xFF,0xFF,0xFF,0xBF,0xFF,0xFE,0xF0,0x00,
0xF7,0xFF,0xFF,0xBB,0xF7,0x6F,0xFB,0xFF,0xFF,0xFF,0xFF,0xBF,0xFF,0xFE,0xF0,0x00,
0xF7,0xFF,0xFD,0xBB,0xB7,0x6F,0xFB,0xFF,0xFF,0xBF,0x7F,0xBF,0xFF,0xFE,0xF0,0x00,
0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xF0,0x00,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x00,
0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE0,0x00,
0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xC0,0x00
};
voidDisply(ucharX,ucharY);//确定显示位置
/*延时子程序*/
voidDelay1ms(unsignedintcount)
{
unsignedinti,j;
for(i=0;ifor(j=0;j<120;j++);
}
/*******************************************************************/
/*写指令数据到LCD/*RS=L,RW=L,E=高脉冲,D0-D7=指令码*/
/****************************************************************/
voidLcd_com1(ucharcmd)
{
Delay1ms(10);
RS=0;
RW=0;
E=0;
_nop_();
_nop_();
LCD_data=0x80|((cmd&0xf0)>>4);
dely();
E=1;
dely();
E=0;
//LCD_data=(LCD_data&0xf0)|(cmd&0x0f);
//dely();
//E=1;
//dely();
//E=0;
}
/****************************************************************/
写指令数据到LCD*RS=L,RW=L,E=高脉冲,D0-D7=指令码/****************************************************************/
voidLcd_com(ucharcmd)
{
Delay1ms(10);
RS=0;
RW=0;
E=0;
_nop_();
_nop_();
LCD_data=0x80|((cmd&0xf0)>>4);
dely();
E=1;
dely();
E=0;
LCD_data=0x80|(cmd&0x0f);
dely();
E=1;
dely();
E=0;
}
/****************************************************************/*写显示数据到LCD*RS=H,RW=L,E=高脉冲,D0-D7=数据/*******************************************************************/
voidLCD_Write(u