标题:
51单片机LCD1602液晶显示器(LM016L)显示功能实现+Proteus仿真+常见问题原因
[打印本页]
作者:
Rremake
时间:
2022-5-9 21:14
标题:
51单片机LCD1602液晶显示器(LM016L)显示功能实现+Proteus仿真+常见问题原因
常见问题:
液晶显示器只有背光无法显示字符
将判忙函数替换成延时函数,延时5ms左右即可
proteus仿真图
1.png
(54.06 KB, 下载次数: 52)
下载附件
2022-5-9 21:57 上传
代码:
#include<reg51.h>
#include<intrins.h> //包含_nop_()函数定义的头文件
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit BF=P1^7;
uchar code dis1[]={"hellow world"};
uchar code dis2[]={"1 2 3 4 5 6"};
void delay(uchar ms){ //延时ms时间
uchar i;
while(ms--){
for(i=0;i<250;i++){
_nop_();_nop_();_nop_();_nop_(); //延时4个机器周期
}
}
}
bit busy(){ //检查是否忙碌
bit result;
RS=0;
RW=1;
E=1;
_nop_();_nop_();_nop_();_nop_()
result=BF;
E=0;
return result;
}
void wcmd(uchar cmd){ //写命令
//while(busy()==1); //判断LCD是否忙碌
delay(20); //将判忙函数换成延时函数(5毫秒左右)可以改变LM016L只有背光不显示的情况,还可以多次初始化
RS=0;
RW=0; //RS和RW同时为低电平时,可以写入命令
E=0; //为了使E正跳变,先置低电平
_nop_();_nop_();_nop_();_nop_();
P1=cmd; //将命令输入P1口
_nop_();_nop_();_nop_();_nop_();
E=1; //E置高电平,产生正跳变,将指令写入液晶模块
_nop_();_nop_();_nop_();_nop_();
E=0;
}
void pos(uchar y,uchar x){ //设置显示位置y行x列
y&=0x1; //最多两行,限制y的范围0~1
x&=0xf; //最多每行16个字符,限制x的范围0~15
if(y==1) x|=0xc0;
if(y==0) x|=0x80;
wcmd(x); //发送地址码
}
void wdat(uchar dat){ //写要显示的内容
//while(busy()==1);
delay(20);
RS=1;
RW=0;
E=0;
P1=dat;
_nop_();_nop_();_nop_();_nop_();
E=1;
_nop_();_nop_();_nop_();_nop_();
E=0;
}
void init(){ //初始化
delay(15);
wcmd(0x38);
delay(10);
wcmd(0x38);
delay(10);
wcmd(0x38);
delay(10);
wcmd(0x0c);
delay(10);
wcmd(0x06);
delay(10);
wcmd(0x01);
delay(10);
}
void main(void){
uchar i;
init();
delay(1);
pos(0,0);
i=0;
while(dis1[i]!='\0'){
wdat(dis1[i]);
i++;
}
pos(1,0);
i=0;
while(dis2[i]!='\0'){
wdat(dis2[i]);
i++;
}
while(1);
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1