51单片机—LCD1602显示

LCD1602显示由命令和数据共同控制;RS-数据/命令控制,0–数据,1–命令;RW-读/写控制,0–写,1–读;E-使能/失能控制,0-失能,1–使能。数据端口必须接上拉电阻(1K—10K),否则可能会出现乱码或不显示。

一、LCD程序

1、LCD.C程序

在程序并不复杂时判忙函数可以不使用

#include "lcd.h"
#include "intrins.h"

sbit RS = P2^0;
sbit RW = P2^1;
sbit E =  P1^2;

void Delay20us(void)   // 20us延时
{
	unsigned char i;

	_nop_();
	i = 6;
	while (--i);
}

void Delay5ms(void)  // 5ms延时
{
	unsigned char i, j;

	i = 9;
	j = 244;
	do
	{
		while (--j);
	} while (--i);
}

//unsigned char readcmd(void)  // LCD判忙
//{
//	unsigned char temp;
//	P0 = 0xFF;
//	RS = 0;
//	RW = 1;
//	E = 0;
//	Delay20us();
//	E = 1;
//	Delay20us();
//	temp = P0;
//	Delay20us();
//	E = 0;
//	return temp;
//}

void writecmd(unsigned char x)  // LCD写命令
{
	//while(readcmd() >= 0x80);
	RS = 0;
	RW = 0;
	E = 0;
	Delay20us();
	P0 = x;
	Delay20us();
	E = 1;
	Delay20us();
	E = 0;
}

void writedata(unsigned char y)  // LCD写数据
{
	//while(readcmd() >= 0x80);
	RS = 1;
	RW = 0;
	E = 0;
	Delay20us();
	P0 = y;
	Delay20us();
	E = 1;
	Delay20us();
	E = 0;
}

void Init_LCD1602(void)  // LCD初始化
{
	writecmd(0x38);
	writecmd(0x06);
	writecmd(0x0C); // writecmd(0x0F);
	writecmd(0x01);
	Delay5ms();
}

void write_cmd(unsigned char hang,unsigned char ge) //hang和ge表示在LCD的第几行第几个位置开始显示
{
	if(hang == 1) writecmd(0x80 + ge);
	else if(hang == 2) writecmd(0xC0 + ge);
	writedata(1 + 0x30);
	writedata(2 + 0x30);
	writedata(3 + 0x30);
	writedata(4 + 0x30);
	writedata(5 + 0x30);
}

void write_cmd_1(unsigned char hang,unsigned char ge)
{
	if(hang == 1) writecmd(0x80 + ge);
	else if(hang == 2) writecmd(0xC0 + ge);
	writedata('H');
	writedata('e');
	writedata('l');
	writedata('l');
	writedata('o');
}

2、LCD.h程序

#ifndef _LCD_H
#define _LCD_H
#include "reg52.h"

void Delay20us(void);
void Delay5ms(void);

//unsigned char readcmd(void);
void writecmd(unsigned char x);
void writedata(unsigned char y);
void Init_LCD1602(void);
void write_cmd(unsigned char hang,unsigned char ge);
void write_cmd_1(unsigned char hang,unsigned char ge);

#endif

二、main.c程序

#include "lcd.h"

void main(void)
{
	Init_LCD1602();
	
	while(1)
	{
		write_cmd(1,3);   // 在第一行第三个位置开始显示write_cmd函数内的信息
		write_cmd_1(2,3); // 在第二行第三个位置开始显示write_cmd_1函数内的信息
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值