51单片机——LCD1602

51单片机------LCD1602

1.LCD1602显示一个字符C

c 复制代码
#include "reg52.h"
#include "intrins.h"
/*
RS  -- P1.0
RW  -- P1.1 
E   -- P1.4 */
#define databuffer  P0 //定义8位数据线,Po端口组
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;

void check_busy()
{
	char tmp = 0x80;
	databuffer = 0x80;
	
	while(tmp & 0x80){//1000 0000
		RS = 0;
		RW = 1;
		EN = 0;
		_nop_();
		EN = 1;
		_nop_();
		_nop_();
		tmp = databuffer;
		EN = 0;
		_nop_();
	}
}

void Write_Cmd_Func(char cmd)
{
	check_busy();
	RS = 0;
	RW = 0;
	
	EN = 0;
	_nop_();
	databuffer = cmd;
	_nop_();
	EN = 1;
	_nop_();
	_nop_();
	EN = 0;
	_nop_();	
}

void Write_Data_Func(char dataShow)
{
	check_busy();
	RS = 1;
	RW = 0;
	
	EN = 0;
	_nop_();
	databuffer = dataShow;
	_nop_();
	EN = 1;
	_nop_();
	_nop_();
	EN = 0;
	_nop_();	
}

void Delay15ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 27;
	j = 226;
	do
	{
		while (--j);
	} while (--i);
}
void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

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


void LCD1602_INIT()
{
	//(1)延时 15ms
	Delay15ms();
//(2)写指令 38H(不检测忙信号) 
	Write_Cmd_Func(0x38);
//(3)延时 5ms
	Delay5ms();
//(4)以后每次写指令,读/写数据操作均需要检测忙信号
//(5)写指令 38H:显示模式设置
	Write_Cmd_Func(0x38);
//(6)写指令 08H:显示关闭
	Write_Cmd_Func(0x08);
//(7)写指令 01H:显示清屏
	Write_Cmd_Func(0x01);
//(8)写指令 06H:显示光标移动设置
	Write_Cmd_Func(0x06);
//(9)写指令 0CH:显示开及光标设置}
	Write_Cmd_Func(0x0c);
}

void main()
{
	char position = 0x80 + 0x05;
	char dataShow = 'C';
	LCD1602_INIT();
	Write_Cmd_Func(position);//选择要显示的地址
	Write_Data_Func(dataShow);//发送要显示的字符

}

2.LCD1602显示一行

c 复制代码
#include "reg52.h"
#include "intrins.h"
/*
RS  -- P1.0
RW  -- P1.1 
E   -- P1.4 */
#define databuffer  P0 //定义8位数据线,Po端口组
sbit RS = P1^0;
sbit RW = P1^1;
sbit EN = P1^4;

void check_busy()
{
	char tmp = 0x80;
	databuffer = 0x80;
	
	while(tmp & 0x80){//1000 0000
		RS = 0;
		RW = 1;
		EN = 0;
		_nop_();
		EN = 1;
		_nop_();
		_nop_();
		tmp = databuffer;
		EN = 0;
		_nop_();
	}
}

void Write_Cmd_Func(char cmd)
{
	check_busy();
	RS = 0;
	RW = 0;
	
	EN = 0;
	_nop_();
	databuffer = cmd;
	_nop_();
	EN = 1;
	_nop_();
	_nop_();
	EN = 0;
	_nop_();	
}

void Write_Data_Func(char dataShow)
{
	check_busy();
	RS = 1;
	RW = 0;
	
	EN = 0;
	_nop_();
	databuffer = dataShow;
	_nop_();
	EN = 1;
	_nop_();
	_nop_();
	EN = 0;
	_nop_();	
}

void Delay15ms()		//@11.0592MHz
{
	unsigned char i, j;

	i = 27;
	j = 226;
	do
	{
		while (--j);
	} while (--i);
}
void Delay5ms()		//@11.0592MHz
{
	unsigned char i, j;

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


void LCD1602_INIT()
{
	//(1)延时 15ms
	Delay15ms();
//(2)写指令 38H(不检测忙信号) 
	Write_Cmd_Func(0x38);
//(3)延时 5ms
	Delay5ms();
//(4)以后每次写指令,读/写数据操作均需要检测忙信号
//(5)写指令 38H:显示模式设置
	Write_Cmd_Func(0x38);
//(6)写指令 08H:显示关闭
	Write_Cmd_Func(0x08);
//(7)写指令 01H:显示清屏
	Write_Cmd_Func(0x01);
//(8)写指令 06H:显示光标移动设置
	Write_Cmd_Func(0x06);
//(9)写指令 0CH:显示开及光标设置}
	Write_Cmd_Func(0x0c);
}

void LCD1602_showLine(char row, char col, char *string)
{
	
	switch(row){

		case 1:
				Write_Cmd_Func(0x80+col);
				while(*string){
					Write_Data_Func(*string);
					string++;
				}
				break;
		
		case 2:
				Write_Cmd_Func(0x80+0x40+col);
				while(*string){
					Write_Data_Func(*string);
					string++;
				}
				break;
	
	}
}

void main()
{
	char position = 0x80 + 0x05;
	//char dataShow = 'C';
	LCD1602_INIT();
	LCD1602_showLine(1,5,"NO.1");
	LCD1602_showLine(2,0,"clc handsome");

}
相关推荐
赋能大师兄2 小时前
单片机3.3V和5V电压等级
单片机·3.3v·5v
BreezeJuvenile2 小时前
外设模块学习(6)——DHT11温湿度传感器(STM32)
stm32·单片机·dht11·温湿度传感器
桃里桑3 小时前
【嵌入式软件】STM32 UART转485通信问题
stm32·单片机·嵌入式硬件
萧技电创EIIA4 小时前
基于wifi摄像头与单片机的串口透传及二次开发
单片机·51单片机·硬件工程·个人开发
三佛科技-134163842124 小时前
迷你加湿器方案开发,加湿器/香薰机MCU控制方案开发设计
单片机·嵌入式硬件·智能家居·pcb工艺
hazy1k4 小时前
51单片机基础-矩阵按键
嵌入式硬件·矩阵·51单片机
一支闲人5 小时前
带你了解STM32:SPI通信(硬件部分)
stm32·单片机·嵌入式硬件·基础知识
我先去打把游戏先8 小时前
VSCode通过SSH连接到Ubuntu虚拟机失败“找不到ssh安装”问题解决
笔记·vscode·单片机·嵌入式硬件·学习·ubuntu·ssh
学工科的皮皮志^_^10 小时前
电压源和电流源学习理解
单片机·嵌入式硬件·学习
linweidong12 小时前
跨平台驱动开发:打造兼容多款MCU的硬核方案
驱动开发·单片机·嵌入式硬件·bsp·rtos·spi驱动·hal设计