📄 lcd1602_4.h
字号:
/*--------------------------------------------------------------*/
//LCD1602四线驱动程序
//File: LCD1602_4.H
//Date: 08-8-7
//Time: 9:21
//Ver: 0.01
/*--------------------------------------------------------------*/
#ifndef __LCD1602_4_H__ //防止被重复定义
#define __LCD1602_4_H__
/*--------------------------------------------------------------*/
//LCD1602接口定义
sfr LCD_DATA = 0xa0; //P2口高四位(P2^4~P2^7)与LCD的高四位(D4~D7)一一对应相接
sbit LCD_RS = P2^2; //数据指令
sbit LCD_EN = P2^3; //使能端
//sbit LCD_RW = GND; //读写控制端接地 ^_^哈哈,又少了一根线咯!
/*--------------------------------------------------------------*/
//函数声明
void LCD_init (void); //液晶初始化
void LCD_en (void); //输入使能
void LCD_clr (void); //液晶清屏
void LCD_cmd (unsigned char cmd); //指令输入
void LCD_dat (unsigned char dat); //数据输入
void LCD_delay (unsigned char m, unsigned char n); //延迟时间
void LCD_pos (unsigned char x, unsigned char y); //液晶定位
void LCD_printc (unsigned char x, unsigned char y, unsigned char c); //字符定位输出
void LCD_prints (unsigned char x, unsigned char y, unsigned char *s); //字符串定位输出
/*
void LCD_fprints(unsigned char *s,signed char pos); //字符串输出
*/
/*--------------------------------------------------------------*/
//延迟时间12us→130 000us
void LCD_delay(unsigned char m, unsigned char n)
{
unsigned char tempm,tempn=n;
do{tempm=m;while(--tempm);}while(--tempn);
}
/*--------------------------------------------------------------*/
//液晶初始化
void LCD_init(void)
{
LCD_cmd(0x28);
LCD_en();
LCD_cmd(0x28); //四线显示
LCD_cmd(0x0c); //显示打开
LCD_cmd(0x01); //显示清屏
LCD_delay(2, 249); //2000-2 us
}
/*--------------------------------------------------------------*/
//输入使能
void LCD_en(void)
{
LCD_EN = 1;
LCD_delay(1,1); //12us
LCD_EN = 0;
}
/*--------------------------------------------------------------*/
//指令输入
void LCD_cmd(unsigned char cmd)
{
LCD_delay(15,1); //40us
LCD_RS = 0; //指令
LCD_DATA &= 0x0f; //清高四位
LCD_DATA |= cmd&0xf0; //写高四位
LCD_en();
cmd <<= 4; //低四位移到高四位
LCD_DATA &= 0x0f; //清高四位
LCD_DATA |= cmd&0xf0; //写高四位
LCD_en();
}
/*--------------------------------------------------------------*/
//数据输入
void LCD_dat(unsigned char dat)
{
LCD_delay(15,1); //40us
LCD_RS = 1; //数据
LCD_DATA &= 0x0f; //清高四位
LCD_DATA |= dat&0xf0; //写高四位
LCD_en();
dat <<= 4; //低四位移到高四位
LCD_DATA &= 0x0f; //清高四位
LCD_DATA |= dat&0xf0; //写高四位
LCD_en();
}
/*--------------------------------------------------------------*/
//液晶清屏
void LCD_clr(void)
{
LCD_cmd(0x01);
LCD_delay(2, 249); //2000-2 us
}
/*--------------------------------------------------------------*/
//液晶定位
void LCD_pos(unsigned char x, unsigned char y)
{
if(y) LCD_cmd(x|0xc0);
else LCD_cmd(x|0x80);
}
/*--------------------------------------------------------------*/
//字符定位输出
void LCD_printc(unsigned char x, unsigned char y, unsigned char c)
{
LCD_pos(x, y);
LCD_dat(c);
}
/*--------------------------------------------------------------*/
//字符串定位输出
void LCD_prints(unsigned char x, unsigned char y, unsigned char *s)
{
LCD_pos(x, y);
while(*s)
{
LCD_dat(*s);
s++;
}
}
/*--------------------------------------------------------------*/
//字符串输出 //pos=-1 清屏 *s='\n' 换行 *s='\b' 退格
/*void LCD_fprints(unsigned char *s, signed char pos)
{
unsigned char i;
if(pos==-1)
{
LCD_cmd(0x01); //清屏
LCD_delay(2, 249); //2000-2 us
pos = 0;
}
while(*s)
{
switch(*s)
{
case '\n': //换行
{
if(pos < 17)
{
for(i = pos; i < 16; i++)
LCD_printc(i%16, i/16, ' ');
pos = 16;
}
else
{
for(i = pos; i < 32; i++)
LCD_printc(i%16, i/16, ' ');
pos = 32;
}
break;
}
case '\b': //退格
{
if(pos > 0) pos--;
LCD_printc(pos%16, pos/16, ' ');
break;
}
default:
{
if(*s < 0x20)
*s=' ';
LCD_printc(pos%16, pos/16, *s);
pos++;
break;
}
}
s++;
}
}
*/
/*--------------------------------------------------------------*/
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -