📄 lcd 1602.h
字号:
#ifndef _lcd_1602_h
#define _lcd_1602_h
#include<reg52.h>
#include<intrins.h>
#include"delay.h"
#define uchar unsigned char
#define uint unsigned int
#define lint long int
#define busy 0x80
#define HIGH 1
#define LOW 0
sbit LCD1602_RS=P0^0; //RS为寄存器选择,高电平时选择数据寄存器、低电平时选择指令寄存器。
sbit LCD1602_RW=P0^1; //RW为读写信号线,高电平时进行读操作,低电平时进行写操作。
sbit LCD1602_EN=P0^2; //E端为使能端,当E端由高电平跳变成低电平时,液晶模块执行命令。
//***********//LCD1602的接线//*****************************
#define LCD_IO P0 //LCD1602的高四位接在P0口的高四位
//*****************************************
#define DATA_MODE 0x28 //LCD显示模式设置,0x28表示“四位数据线”模式
#define OPEN_SCREEN 0x0f //打开LCD显示,无光标且显示双行
#define SET_ADDRESS 0x80 //写地址指令
#define CLEARSCREEN LCD_en_com(0x01) //清屏
//**********************************************************************
void LCD_en_write(void); //EN控制端产生一高电平,使能写LCD
void LCD_en_com(uchar command); //写命令指令
void LCD_en_dat(uchar dat); //写数据指令
void LCD_read_dat(uchar dat); //读数据指令
void LCD_set_xy(uchar x,uchar y); //设置显示地址:X为横轴(0-15),Y为纵轴(0-1).
void LCD_write_char(uchar x,uchar y,uchar dat); //写一个字符
void LCD_write_string(uchar x,uchar y,uchar *s); //写一个字符串
void LCD_write_int(uchar x,uchar y,uint intdat); //写无字符型整形数
void LCD_init(void); //lcd初始化
void LCD_Read_BF(void); //LCD读忙信号
void movesreen_rl_1602(uint time,uchar direction); //整频左移、右移
void LCD_write_float(unsigned char row,unsigned char col,float n); //显示单精度浮点数n
void LCD_write_defchar(void); //显示自定义字符
void num(uchar x,uchar y,uint n);
//**********************************************************************
void LCD_write_defchar(void) //???怎么写自定义的字符到CGRAM中去??*/*
{
LCD_en_com(0x40+0x00);
LCD_en_dat(0x0f);
LCD_en_com(0x40+0x01);
LCD_en_dat(0x09);
LCD_en_com(0x40+0x02);
LCD_en_dat(0x0f);
LCD_en_com(0x40+0x03);
LCD_en_dat(0x09);
LCD_en_com(0x40+0x04);
LCD_en_dat(0x0f);
LCD_en_com(0x40+0x05);
LCD_en_dat(0x09);
LCD_en_com(0x40+0x06);
LCD_en_dat(0x03);
LCD_en_com(0x40+0x07);
LCD_en_dat(0x00);
}
void LCD_en_write(void)
{
LCD1602_EN=HIGH;
_nop_();
LCD1602_EN=LOW;
}
void LCD_Read_BF(void)
{
LCD1602_RS=LOW;
LCD1602_RW=HIGH;
LCD1602_EN=HIGH;
LCD_IO=LCD_IO&0x0f|0xf0; //将P0的高四位置0,保留低四位的原数据
while(LCD_IO&busy);
LCD1602_EN=LOW;
}
void LCD_en_com(uchar command)
{
LCD_Read_BF();
LCD1602_RS=LOW;
LCD1602_RW=LOW;
LCD_IO=LCD_IO&0x0f;
LCD_IO=command&0xf0|LCD_IO&0x0f;
LCD_en_write();
command=command<<4;
LCD_IO=LCD_IO&0x0f; //从P0写入command高四位数据后,要给P0清零,否则之后写入的command的低四位错误
LCD_IO=command&0xf0|LCD_IO&0x0f;
LCD_en_write();
}
void LCD_en_dat(uchar dat)
{
LCD_Read_BF();
LCD1602_RS=HIGH;
LCD1602_RW=LOW;
LCD_IO=LCD_IO&0x0f;
LCD_IO=dat&0xf0|LCD_IO&0x0f;
LCD_en_write();
dat=dat<<4;
LCD_IO=LCD_IO&0x0f; //从P0写入dat高四位数据后,要给P0清零,否则之后写入的dat的低四位错误
LCD_IO=dat&0xf0|LCD_IO&0x0f;
LCD_en_write();
}
//*****************从CGRAM或DDRAM中读数*****************//
void LCD_read_dat(uchar dat)
{
LCD_Read_BF();
LCD1602_RS=HIGH;
LCD1602_RW=HIGH;
LCD_IO=LCD_IO&0x0f;
LCD_IO=dat&0xf0|LCD_IO&0x0f;
LCD_en_write();
dat=dat<<4;
LCD_IO=LCD_IO&0x0f; //从P0读出dat高四位数据后,要给P0清零,否则之后写入的dat的低四位错误
LCD_IO=dat&0xf0|LCD_IO&0x0f;
LCD_en_write();
}
//**************设置显示地址:X为横轴(0-15),Y为纵轴(0-1)********************
void LCD_set_xy(uchar x, uchar y)
{
uchar address;
if(!y)
address=0x80+x;
else
address=0xc0+x;
LCD_en_com(address);
}
//***************写一个字符:X为横轴(0-15),Y为纵轴(0-1)**********
void LCD_write_char(uchar x,uchar y,uchar dat)
{
LCD_set_xy(x,y);
LCD_en_dat(dat);
}
//****************写一个字符串***************************************
void LCD_write_string(uchar x,uchar y,uchar *s)
{
LCD_set_xy(x,y);
while(*s)
{
LCD_en_dat(*s);
s++;
}
}
//****************写一个无字符整数***************************************
void LCD_write_int(uchar x,uchar y,uint intdat)
{
uchar i,temp[5];
while(intdat/10||intdat%10)
{
temp[i]=intdat%10;
intdat/=10;
i++;
}
LCD_set_xy(x,y);
while(i--)
LCD_en_dat(temp[i]+0x30);
}
//*******time为每移动一格时所需要的时间,direction为移动方向:0为左移,1为右移***//
//*******函数功能:循环左(右)移动屏幕上的所有字符*******//
void movesreen_rl_1602(uint time,uchar direction)
{
uint i=0,j=0;
for(i=0;i<16;i++)
{
if(direction==0)
LCD_en_com(0x18);
else if(direction==1)
LCD_en_com(0x1c);
for(j=0;j<time;j++)
delayms(10);
}
}
//*********写一个小数*******************//
void LCD_write_float(uchar row,uchar col,float n)
{
uchar i=0,numlength=0,xlength=0,zlength=0,a[9]={0,0,0,0,0,0,0,0}; //numlength是数长,xlength是小数长度,zlength是整数长度
float ftemp;
lint ltemp=0;
ftemp=n;
for(i=0;i<6;i++) //该循环计算出小数长度
{
ltemp=ftemp; //去掉小数
if(ftemp>ltemp)
{
ftemp=ftemp*10;
ltemp=ftemp;
xlength++;
}
}
ltemp=n; //去掉小数
for(i=0;i<6;i++) //该循环计算出整数长zlength
{
if(ltemp>=1)
{
ltemp=ltemp/10;
zlength++;
}
}
if(n<1)
zlength=1;
numlength=zlength+xlength; //计算原数字及小数点的长度
ftemp=n; //将n去掉小数点放进ftemp
for(i=xlength;i>0;i--)
ftemp=ftemp*10;
ltemp=ftemp; //将ftemp变为长整型放进字符数组a[]
for(i=numlength;i>zlength;i--)
{
a[i]=ltemp%10+0x30;
ltemp/=10;
}
for(i=zlength;i>0;i--)
{
a[i-1]=ltemp%10+0x30;
ltemp/=10;
}
if(xlength==0) //小数部分为0是,去掉小数点
{
a[zlength]=' ';
a[numlength]='%';
}
if(xlength!=0)
{
a[zlength]='.';
a[numlength+1]='%';
} //项目所用,数组a只应该有8哥元素,而不是9个元素,%在头文件中应该舍去
LCD_write_string(row,col,a);
}
void num(uchar x,uchar y,uint n)
{
unsigned char i,length,a[6]={0,0,0,0,0,0};
uint nx=n;
if(n==0){LCD_write_char(x,y,'0');return;}
for(i=0;i<6;i++)
{
if(nx>=1)length++;
nx/=10;
}
nx=n;
for(;length>0;length--)
{
a[length-1]=nx%10+48;
nx/=10;
}
LCD_write_string(x,y,a);
}
void LCD_init(void)
{
LCD_en_com(DATA_MODE);
LCD_en_com(DATA_MODE);
LCD_en_com(OPEN_SCREEN);
LCD_en_com(SET_ADDRESS);
CLEARSCREEN;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -