📄 lcd1602.h
字号:
#include <reg51.h>
//*******************LCD模块
//*******************LCD模块
#define LCD_DATA P1 //LCD的数据口
sbit LCD_BUSY=LCD_DATA^7; //LCD忙信号位
sbit LCD_RW=P3^3; //LCD读写控制
sbit LCD_RS=P3^2; //LCD寄存器选择
sbit LCD_EN=P3^4; //LCD使能信号
void LCD_check_busy(void) //检测LCD状态,看它是不是还在忙呢
{
while(1)
{
LCD_EN=0;
LCD_RS=0; //指令寄存器通信
LCD_RW=1; //read data
LCD_DATA=0xff;
LCD_EN=1;
if(!LCD_BUSY)break;
}
LCD_EN=0;
}
void LCD_cls(void) //LCD清屏
{
LCD_check_busy();
LCD_RS=0;
LCD_RW=0;
LCD_DATA=1;
LCD_EN=1;
LCD_EN=0;
}
void LCD_write_instruction(unsigned char LCD_instruction) //写指令到LCD
{
LCD_check_busy();
LCD_RS=0;
LCD_RW=0; //写数据
LCD_DATA=LCD_instruction;
LCD_EN=1;
LCD_EN=0;
}
void LCD_write_data(unsigned char LCD_data) //输出一个字节数据到LCD
{
LCD_check_busy();
LCD_RS=1;
LCD_RW=0;
LCD_DATA=LCD_data;
LCD_EN=1;
LCD_EN=0;
}
void LCD_set_position(unsigned char x) //LCD光标定位第一行到x处
{
LCD_write_instruction(0x80+x);
}
void LCD_set_yposition(unsigned char y)
{
LCD_write_instruction(0xc0+y); //LCD光标定位到第二行y处
}
void LCD_printc(unsigned char lcd_data) //输出一个字符到LCD
{
LCD_write_data(lcd_data);
}
void LCD_prints(unsigned char *lcd_string) //输出一个字符串到LCD
{
unsigned char i=0;
while(lcd_string[i]!=0x00)
{
LCD_write_data(lcd_string[i]);
i++;
}
}
//=========================================================================
// function : display one char in one position
// input : chr --> display data
// posi --> display position
// row --> display row
// output : void
// return : void
// created date 2008-10-05
//=========================================================================
void DispChrPosi(unsigned char chr, unsigned char posi,unsigned char row)
{
unsigned char ch;
if(row > 1)
{
LCD_prints("Error");
return;
}
if(row == 1)
ch = 0xc0+posi;
else
ch = 0x80+posi;
LCD_write_instruction(ch);
LCD_write_data(chr);
}
//========================================================================
// function : display string
// input : str --> display string
// posi--> first position
// output : void
// return : void
// created date 2008-10-05
//========================================================================
void DispStrPosi(unsigned char *str, unsigned char posi, unsigned char row)
{
unsigned char ch;
unsigned char i = 0;
if(row > 1)
return;
if(row == 1)
ch = 0xc0+posi;
else
ch = 0x80+posi;
while(str[i] != 0x00)
{
LCD_write_instruction(ch + i);
LCD_write_data(str[i]);
i ++;
}
}
void LCD_initial(void) //初始化LCD
{
LCD_write_instruction(0x3c);
LCD_write_instruction(0x0c);
LCD_write_instruction(0x06);//显示屏一定要不移动。
LCD_cls();
}
//*************************LCD模块结束
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -