第一次调4线的LCD1602,比较困难,或者因为延迟不对,或者因为命令错误,我足足用了一天时间,搞定了4线的1602编程实现。这里提供51单片机和LPC23XX系列单片机的程序。51的程序是我上网找的,可以参考里面的实现步骤,可以直接仿真,但是不知道烧到板子里是否可以。LPC23XX系列单片机的程序可以直接烧写板子里运行,注意里面的管脚定义,因为在proteus里没找到对应的芯片,这里不知是否可以仿真。
51程序
#include
sbit LCM_RW=P2^0; //定义引脚
sbit LCM_RS=P2^1;
sbit LCM_E =P2^2;
#define LCM_Data P1
#define Busy 0x80 //用于检测 LCM 状态字中的 Busy 标识
void WritEDAtaLCM(unsigned char Data);
void WriteCommandLCM(unsigned char Command);
void LCMInit(void);
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);
void Delayms(unsigned int n);
void dellay(unsigned int h);
unsigned char code blog_adr[] = {"EDNchina"};
unsigned char code email[] = {"tengjingshu@126.com"};
void main(void)
{
//Delay400Ms(); //启动等待,等 LCM 讲入工作状态
LCMInit(); //LCM 初始化
DisplayListChar(3, 0, blog_adr);
DisplayListChar(0, 1, email);
while(1);
}
//写数据 RS=H,RW=L,D0~D7=数据,E=高脉冲
void WritEDAtaLCM(unsigned char Data)
{
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 0;
LCM_Data =(Data & 0xF0);
LCM_E = 1;
dellay(100); //短暂延时,代替检测忙状态
LCM_E = 0;
LCM_Data =(Data & 0x0F)<<4;
LCM_E = 1;
dellay(100);
LCM_E = 0;
}
//写指令 RS=L,RW=L,D0~D7=指令码,E=高脉冲
void WriteCommandLCM(unsigned char Command)
{
dellay(100); //短暂延时,代替检测忙状态
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 0;
LCM_Data =( Command & 0xF0);
LCM_E = 1;
dellay(100);
LCM_E = 0;
LCM_Data =( Command & 0x0F )<<4;
LCM_E = 1;
dellay(100);
LCM_E = 0;
}
//读数据 RS=H,RW=H,E=H
//读状态 RS=L,RW=H,E=H
//由于不要检测忙,所以读数据和读状态两个函数省略
void LCMInit(void) //LCM 初始化
{
LCM_Data = 0;
Delayms(15);
WriteCommandLCM(0x03); //三次显示模式设置,不检测忙信号
Delayms(5);
WriteCommandLCM(0x03);
Delayms(5);
WriteCommandLCM(0x03);
Delayms(5);
WriteCommandLCM(0x02);
Delayms(5);
WriteCommandLCM(0x28); //显示模式设置,开始要求每次检测忙信号
WriteCommandLCM(0x06); // 显示光标移动设置
WriteCommandLCM(0x0C);
WriteCommandLCM(0x01);
Delayms(5);
}
//按指定位置显示一个字符
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制 X 不能大于 15,Y 不能大于 1
if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
X |= 0x80; //算出指令码
WriteCommandLCM(X); //这里不检测忙信号,发送地址码
WritEDAtaLCM(DData);
}
//按指定位置显示一串字符
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF; //限制 X 不能大于 15,Y 不能大于 1
while (DData[ListLength]>0x1f) //若到达字串尾则退出
{
if (X <= 0xF) //X 坐标应小于 0xF
{
DisplayOneChar(X, Y, DData[ListLength]); //显示单个字符
ListLength++; X++;
}
}
}
void Delayms(unsigned int n)
{
unsigned int i,j;
for(j=n;j>0;j--)
for(i=112;i>0;i--);
}
void dellay(unsigned int h)
{
while(h--); //0.01MS
}
上一篇:LCD1602程序设计4线并口通信源程序
下一篇:STC单片机AD和EEPROM的程序设计C代码
推荐阅读最新更新时间:2024-03-16 15:51