📄 lcd1602.h
字号:
#ifndef _LCD1602_H_
#define _LCD1602_H_
/********************************************************************/
#include "reg51.h"
#include "stdio.h"
//lcd part
#define LINE1 0
#define LINE2 1
#define LINE1_HEAD 0x80
#define LINE2_HEAD 0xC0
#define LCD_DELAY_TIME 40
#define SET_DATA_MODE LCD_en_command(0x38)
#define LCD_SHOW LCD_en_command(0x0C) //显示
#define LCD_HIDE LCD_en_command(0x00) //隐藏
//模式设定 移位 输入方式 一般不用
#define CURSOR_L_SHIFT LCD_en_command(0x04) // 输入1bit数据,光标左移
#define CURSOR_R_SHIFT LCD_en_command(0x06) // 光标右移
#define CHAR_L_SHIFT LCD_en_command(0x05) //光标不动,字符左移
#define CHAR_R_SHIFT LCD_en_command(0x07) //光标不动,字符右移
////////////////////////
#define SET_DISPLAY_ADDRESS LCD_en_command(0x80)
#define CLEAR_SCREEN LCD_en_command(0x01) //清屏
#define CURSOR_ON LCD_en_command(0x0e) //光标开
#define CURSOR_OFF LCD_en_command(0x0c) //光标关
#define CURSOR_FLASH LCD_en_command(0x0f) //光标闪烁
#define CURSOR_NO_FLASH LCD_en_command(0x0e)//光标不闪烁
#define CURSOR_TO_HOME LCD_en_command(0x02) //光标复位
//使动操作 移位
#define CURSOR_L_MOVE LCD_en_command(0x10) // 光标左移
#define CURSOR_R_MOVE LCD_en_command(0x14) // 光标右移
#define CHAR_L_MOVE LCD_en_command(0x18) //光标不动,字符左移
#define CHAR_R_MOVE LCD_en_command(0x1C) //光标不动,字符右移
///////////////////////////////
//common part
#define HIGH 1
#define LOW 0
#define TRUE 1
#define ZERO 0
/*******************************************************************/
//不同的电路修改为相应引脚即可
#define LCDIO P3
sbit LCD1602_RS=P2^2; //data command select 1 data 0 command pin 4
sbit LCD1602_RW=P2^1; //read write select 1 read 0 write pin 5
sbit LCD1602_EN=P2^0; //LCD enable signal pin 6
/********************************************************************/
void LCD_delay(void);//lcd delay
void LCD_en_command(unsigned char command);//write command
void LCD_en_dat(unsigned char temp);//write data
void LCD_set_xy( unsigned char x, unsigned char y );//set display address
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string
void LCD_init(void);//lcd initize
void LCD_write_number(unsigned char X,unsigned char Y,double number);//write a number
/********************************************************************/
void delay_nms(unsigned int n);//delay
/********************************************************************/
char buffer[8]={0};
/******************** LCD PART *************************************/
void LCD_delay(void)
{
unsigned char i;
for(i=LCD_DELAY_TIME;i>ZERO;i--) //be sure lcd reset
;
}
/********************************************************************写指令*/
void LCD_en_command(unsigned char command)
{
LCDIO=command;
LCD1602_RS=LOW;
LCD1602_RW=LOW;
LCD1602_EN=LOW;
LCD_delay();
LCD1602_EN=HIGH;
}
/********************************************************************写数据*/
void LCD_en_dat(unsigned char dat)
{
LCDIO=dat;
LCD1602_RS=HIGH;
LCD1602_RW=LOW;
LCD1602_EN=LOW;
LCD_delay();
LCD1602_EN=HIGH;
}
/********************************************************************设置显示位置x列y行*/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == LINE1)
address = LINE1_HEAD + x;
else
address = LINE2_HEAD + x;
LCD_en_command(address);
}
/*******************************************************************写字符*/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy( x, y );
LCD_en_dat(dat);
}
/*******************************************************************写字符串*/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
LCD_set_xy( X, Y ); //set address
while (*s) // write character
{
LCDIO=*s;
LCD_en_dat(*s);
s ++;
}
}
/*******************************************************************初始化LCD显示器,设置工作方式*/
void LCD_init(void)
{
CLEAR_SCREEN;//clear screen
SET_DATA_MODE;//set 8 bit data transmission mode
CURSOR_FLASH;
//CURSOR_L_SHIFT;
SET_DISPLAY_ADDRESS;//set lcd first display address
CLEAR_SCREEN;//clear screen
}
/********************************************************************/
/*********************** OTHER PART ********************************延时*/
void delay_nms(unsigned int n)
{
unsigned int i=0,j=0;
for (i=n;i>0;i--)
for (j=0;j<1140;j++);
}
///////////////////////////////////////////////////////////////////////////////写数字(慎用,占用较多内存)
/*void LCD_write_number(unsigned char X,unsigned char Y,double number)
{
sprintf(buffer,"%6.3f",number);
LCD_write_string(X,Y,buffer);
} */
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -