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");

}
相关推荐
bai5459363 小时前
STM32 硬件I2C读写MPU6050
stm32·单片机·嵌入式硬件
LS_learner3 小时前
6020角度双环控制一种用于电机控制的策略
嵌入式硬件
IT.小航4 小时前
STM32F103RC的USB上拉电阻1.5K
stm32·单片机·嵌入式硬件
m0_555762904 小时前
MCU 开发工具汇总
单片机·嵌入式硬件
伴杯猫5 小时前
【ESP32-IDF】高级外设开发3:I2S
c语言·单片机·嵌入式硬件·mcu·物联网·esp32·esp-idf
飞凌嵌入式7 小时前
高性能、高实时、高安全:如何在飞凌嵌入式i.MX95xx核心板上同时实现?
嵌入式硬件·安全·嵌入式·飞凌嵌入式
智能物联实验室8 小时前
如何低门槛自制Zigbee 3.0温湿度计?涂鸦上新开发包,开箱即用、完全开源
嵌入式硬件·开源·硬件工程
淘晶驰AK8 小时前
51单片机与stm32单片机,先学习哪一个?
stm32·单片机·51单片机
躺不平的小刘9 小时前
从YOLOv5到RKNN:零冲突转换YOLOv5模型至RK3588 NPU全指南
linux·python·嵌入式硬件·yolo·conda·pyqt·pip
Moonnnn.9 小时前
【51单片机学习】AT24C02(I2C)、DS18B20(单总线)、LCD1602(液晶显示屏)
笔记·单片机·学习·51单片机