登录  | 立即注册

游客您好!登录后享受更多精彩

扫一扫,访问微社区

QQ登录

只需一步,快速开始

开启左侧

[寒假笔记] 浅尝1602显示屏

[复制链接]
发表于 2022-2-6 20:56:09 | 显示全部楼层 |阅读模式
学习笔记
学习科目: 51单片机
学习安排: 学习1602的一些皮毛
开始时间: 2022-02-05
结束时间: 2022-02-06
学习1602显示屏的一些基础知识编写程序,编写好程序后发现不能用,翻看大佬的贴子发现要在LCD1602_E的前后用延时函数隔开,也就是将高脉冲隔开,涉及到了下一章的内容。还有就是显示屏显示的字符是倒过来的看着很难受,希望学到后面能解决这个问题。


程序:
让显示屏显示字符


#include<reg52.h>


#define LCD1602_DB P0
sbit LCD1602_RS = P2^0;
sbit LCD1602_RW = P2^1;
sbit LCD1602_E = P1^2;


void InitLcd1602();
void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str);
void delay()
{
unsigned int i;
i=5;
while(--i);
}


void main()
{
unsigned char str[] = "Xin Bao";


InitLcd1602();
LcdShowStr(2,0,str);
LcdShowStr(0,1,"I Love You");
while(1);
}


void LcdWaitReady()
{
unsigned char sta;


LCD1602_DB = 0xFF;
LCD1602_RS = 0;
LCD1602_RW = 1;
do{
LCD1602_E = 1;
sta = LCD1602_DB;
LCD1602_E = 0;
}while(sta&0x80);
}


void LcdWriteCmd(unsigned char cmd)
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
delay();
LCD1602_E = 1;
delay();
LCD1602_E = 0;
}


void LcdWriteDat(unsigned char dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
delay();
LCD1602_E = 1;
delay();
LCD1602_E = 0;
}


void LcdSetCursor(unsigned char x,unsigned char y)
{
unsigned char addr;


if(y == 0)
addr = 0x00+x;
else
addr = 0x40+x;
LcdWriteCmd(addr | 0x80);
}


void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str)
{
LcdSetCursor(x,y);
while(*str != '\0')
{
LcdWriteDat(*str++);
}
}


void InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);
}

好懒~~不想说~~~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表