LCD1602.h
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
/*--------------------------------------------------------------*/
//File: LCD1602_8A.H
//Time: 20:10
//Modi: 09-5-17
/*--------------------------------------------------------------*/
//防止被重复定义
#ifndef __LCD1602_8A_H__
#define __LCD1602_8A_H__
/*--------------------------------------------------------------*/
//模式选择(条件编译)
#define Port_Type_Select 1 //=1, 选择八位数据模式
//=0, 选择四位数据模式, LCD高四位接MCU端口高四位
#define Int_Transfer 1 //=1, 允许中断服务函数调用
//=0, 不允许中断服务函数调用
/*--------------------------------------------------------------*/
//LCD1602与单片机接口定义
sfr LCD_IO = 0x80; //P0-0x80,P1-0x90,P2-0xA0,P3-0xB0;
sbit LCD_RS = P2^0; //LCD数据/命令选择端(H/L)
sbit LCD_RW = P2^1; //LCD 读/写 选择端(H/L)
sbit LCD_EN = P2^2; //LCD使能控制端
/*--------------------------------------------------------------*/
//工作方式设置
#define LCD_DISPLAY8_DOUBLE_LINE LCD_cmd(0x38); //两行显示8-bits
#define LCD_DISPLAY8_SINGLE_LINE LCD_cmd(0x30); //单行显示8-bits
#define LCD_DISPLAY4_DOUBLE_LINE LCD_cmd(0x28); //两行显示4-bits
#define LCD_DISPLAY4_SINGLE_LINE LCD_cmd(0x20); //单行显示4-bits
/*--------------------------------------------------------------*/
//输入方式设置
#define LCD_AC_AUTO_INCREMENT LCD_cmd(0x06); //数据读、写操作后,AC自动加 1
#define LCD_AC_AUTO_DECREASE LCD_cmd(0x04); //数据读、写操作后,AC自动减 1
#define LCD_MOVE_ENABLE LCD_cmd(0x07); //数据读、写操作,画面平移
#define LCD_MOVE_DISENABLE LCD_cmd(0x06); //数据读、写操作,画面不动
/*--------------------------------------------------------------*/
//设置显示、光标及闪烁开、关
#define LCD_DISPLAY_ON LCD_cmd(0x0c); //LCD开显示
#define LCD_DISPLAY_OFF LCD_cmd(0x08); //LCD关显示
#define LCD_CURSOR_ON LCD_cmd(0x0e); //光标显示
#define LCD_CURSOR_OFF LCD_cmd(0x0c); //光标不显示
#define LCD_CURSOR_BLINK_ON LCD_cmd(0x0f); //光标闪烁
#define LCD_CURSOR_BLINK_OFF LCD_cmd(0x0e); //光标不闪烁
#define LCD_GO_HOME LCD_cmd(0x02); //AC=0,光标、画面回HOME位, DDRAM内容不变
#define LCD_CLR LCD_cmd(0x01); //LCD清屏, 清除DDRAM, 清除屏幕, 置AC为0, 光标回位?
/*--------------------------------------------------------------*/
//光标、画面移动,不影响DDRAM
#define LCD_LEFT_MOVE LCD_cmd(0x18); //LCD显示左移一位
#define LCD_RIGHT_MOVE LCD_cmd(0x1c); //LCD显示右移一位
#define LCD_CURSOR_LEFT_MOVE LCD_cmd(0x10); //光标左移一位
#define LCD_CURSOR_RIGHT_MOVE LCD_cmd(0x14); //光标右移一位
/*--------------------------------------------------------------*/
//LCD1602地址相关
/*
#define LINE1_HEAD 0x80 //第一行DDRAM起始地址
#define LINE2_HEAD 0xc0 //第二行DDRAM起始地址
#define LINE1 0 //第一行
#define LINE2 1 //第二行
#define LINE_LENGTH 16 //每行的最大字符长度(40-DDRAM)
/*--------------------------------------------------------------*/
//函数声明
void LCD_busy (void); //检测LCD是否忙
void LCD_init (void); //LCD初始化
void LCD_cmd (unsigned char cmd); //写入指令
void LCD_dat (unsigned char dat); //写入数据
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_printn(unsigned char x, unsigned char y, unsigned int num); //定位输出16位二进制数字
unsigned char LCD_current_addr(void); //读出AC当前地址(DB6~DB0)或忙标志位DB7
unsigned char LCD_current_addr_dat(void); //读出AC当前地址的数据
unsigned char LCD_addr_dat(unsigned char x, unsigned char y); //读出AC指定地址的数据
void LCD_pos_CG(unsigned char x, unsigned char *CGRAM_dat); //指定地址(x: 0-7)写入8bytes数据CGRAM
/*--------------------------------------------------------------*/
//读出AC当前地址(DB6~DB0)或忙标志位DB7
#if Port_Type_Select //选择八位数据模式
unsigned char LCD_current_addr(void)
{
unsigned char ac_addr;
LCD_EN = 0;
LCD_RS = 0; //指令
LCD_RW = 1; //读出
LCD_IO = 0xff; //端口置为输入方式(P0)
LCD_EN = 1;
ac_addr = LCD_IO;
LCD_EN = 0;
return (ac_addr);
}
#else //选择四位数据模式
unsigned char LCD_current_addr(void)
{
unsigned char ac_addr;
LCD_EN = 0;
LCD_RS = 0; //指令
LCD_RW = 1; //读出
LCD_IO |= 0xf0; //端口置为输入方式(P0)
LCD_EN = 1;
ac_addr = LCD_IO & 0xf0; //高四位
LCD_EN = 0;
LCD_EN = 1;
ac_addr |= LCD_IO >> 4; //低四位
LCD_EN = 0;
return (ac_addr);
}
#endif
/*--------------------------------------------------------------*/
//检测LCD忙状态
#if Int_Transfer //允许中断服务函数调用
void LCD_busy(void)
{
unsigned char ac_dat;
unsigned char busy_flag;
do
{
ac_dat = LCD_current_addr();
busy_flag = ac_dat & 0x80;
}while(busy_flag); //=1表示忙, =0表示空闲
}
#else //不允许中断服务函数调用
void LCD_busy(void)
{
unsigned char ac_dat;
bit busy_flag;
do
{
ac_dat = LCD_current_addr();
busy_flag = (bit)(ac_dat & 0x80);
}while(busy_flag); //=1表示忙, =0表示空闲
}
#endif
/*--------------------------------------------------------------*/
//读出AC当前地址的数据
#if Port_Type_Select //选择八位数据模式
unsigned char LCD_current_addr_dat(void)
{
unsigned char addr_dat;
// unsigned char i;
LCD_busy(); //或者检查忙最可靠
// for(i = 0; i < 3; i++) //要连续读出三次才会有效
{
LCD_EN = 0;
LCD_RS = 1; //数据
LCD_RW = 1; //读出
LCD_IO = 0xff; //端口置为输入方式(P0)
LCD_EN = 1;
addr_dat = LCD_IO;
LCD_EN = 0;
}
return (addr_dat);
}
#else //选择四位数据模式
unsign
ed char LCD_current_addr_dat(void)
{
unsigned char addr_dat;
// unsigned char i;
LCD_busy(); //或者检查忙最可靠
// for(i = 0; i < 3; i++) //要连续读出三次才会有效
{
LCD_EN = 0;
LCD_RS = 1; //数据
LCD_RW = 1; //读出
LCD_IO |= 0xf0; //端口置为输入方式(P0)
LCD_EN = 1;
addr_dat = LCD_IO & 0xf0; //高四位
LCD_EN = 0;
LCD_EN = 1;
addr_dat |= LCD_IO >> 4; //低四位
LCD_EN = 0;
}
return (addr_dat);
}
#endif
/*--------------------------------------------------------------*/
//写入指令
#if Port_Type_Select //选择八位数据模式
void LCD_cmd(unsigned char cmd)
{
LCD_busy(); //检测忙
LCD_RS = 0; //指令
LCD_RW = 0; //写入
LCD_EN = 1;
LCD_IO = cmd; //传指令
LCD_EN = 0; //下降沿有效
}
#else //选择四位数据模式
void LCD_cmd(unsigned char cmd)
{
LCD_busy(); //检测忙
LCD_IO &= 0x0f; //清高四位
LCD_RS = 0; //指令
LCD_RW = 0; //写入
LCD_EN = 1;
LCD_IO |= cmd & 0xf0; //写高四位
LCD_EN = 0; //下降沿有效
cmd <<= 4; //低四位移到高四位
LCD_IO &= 0x0f; //清高四位
LCD_EN = 1;
LCD_IO |= cmd & 0xf0; //写高四位
LCD_EN = 0; //下降沿有效
}
#endif
/*--------------------------------------------------------------*/
//写入数据
#if Port_Type_Select //选择八位数据模式
void LCD_dat(unsigned char dat)
{
LCD_busy(); //检测忙
LCD_RS = 1; //数据
LCD_RW = 0; //写入
LCD_EN = 1;
LCD_IO = dat; //传数据
LCD_EN = 0; //下降沿有效
}
#else //选择四位数据模式
void LCD_dat(unsigned char dat)
{
LCD_busy(); //检测忙
LCD_IO &= 0x0f; //清高四位
LCD_RS = 1; //数据
LCD_RW = 0; //写入
LCD_EN = 1;
LCD_IO |= dat & 0xf0; //写高四位
LCD_EN = 0; //下降沿有效
dat <<= 4; //低四位移到高四位
LCD_IO &= 0x0f; //清高四位
LCD_EN = 1;
LCD_IO |= dat & 0xf0; //写高四位
LCD_EN = 0; //下降沿有效
}
#endif
/*--------------------------------------------------------------*/
//显示定位DDRAM
void LCD_pos(unsigned char x, unsigned char y)
{
if(y) LCD_cmd(x|0xc0);//y=1第二行显示,y=0第一行显示 0<=x<16(40-DDRAM)
else LCD_cmd(x|0x80);//数据指针=80+地址码(00H~27H,40H~67H)
}
/*--------------------------------------------------------------*/
//指定地址(x: 0-7)写入8bytes数据CGRAM
void LCD_pos_CG(unsigned char x, unsigned char *CGRAM_dat)
{
unsigned char i;
x <<= 3; //转化为6位数据的高三位,就是x*8;
x |= 0x40;
LCD_cmd(x); //写入地址
for(i = 0; i < 8; i++)
LCD_dat(CGRAM_dat[i]); //写入数据
}
/*--------------------------------------------------------------*/
//读出AC指定地址的数据
unsigned char LCD_addr_dat(unsigned char x, unsigned char y)
{
unsigned char addr_dat;
LCD_pos(x, y); //定位
addr_dat = LCD_current_addr_dat(); //读出数据
return (addr_dat);
}
/*--------------------------------------------------------------*/
//定位输出
字符
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++;
}
}
/*--------------------------------------------------------------*/
//定位输出16位二进制数字
// 0<= num <65536
void LCD_printn(unsigned char x, unsigned char y, unsigned int num)
{
char i;
unsigned char ii;
unsigned char dat[6];
for(i = 0; i < 6; i++) dat[i] = 0; i = 0; //初始化数据
while(num / 10) //拆位
{
dat[i] = num % 10; //最低位
num /= 10; i++;
}
dat[i] = num; //最高位
ii = i; //保存dat的位数
for(; i >= 0; i--) dat[i] += 48; //转化成ASCII
LCD_prints(x, y, " "); //清显示区域
LCD_pos(x, y);
for(i = ii; i >= 0; i--) LCD_dat(dat[i]); //输出数字字符
}
/*--------------------------------------------------------------*/
//LCD初始化
void LCD_init(void)
{
#if Port_Type_Select //选择八位数据模式
LCD_DISPLAY8_DOUBLE_LINE //设置LCD为16X2显示,5X7点阵,八位数据接口
#else
LCD_DISPLAY4_DOUBLE_LINE //设置LCD为16X2显示,5X7点阵,四位数据接口
#endif
LCD_AC_AUTO_INCREMENT //LCD显示光标移动设置(光标地址指针加1,整屏显示不移动)
LCD_DISPLAY_ON //LCD开显示及光标设置(光标不闪烁,不显示"_")
LCD_CLR //清屏
}
/*--------------------------------------------------------------*/
#endif