📄 rd_lcd1602b.h
字号:
#ifndef _Use_LCD1602B
#define _Use_LCD1602B
/********************************************************
* 函数库说明:LCD1602B基本驱动函数库 *
* 版本: v2.0 *
* 作者: 傻孩子 *
* 日期: 2005年9月6日 *
* 修改: 傻孩子 *
* 修改日期: 2006年1月20日 *
* *
* 说明: *
* 1、需要底层硬件驱动函数支持 *
* 基本的宏定义: *
* LCD_RS LCD_RW LCD_E *
* LCD_SetWriteData LCD_SetReadData *
* LCD_SendHalfCharHigh(a) *
* LCD_SendHalfCharLow(a) *
* 当处于串行状态下时,只定义LCD_RS *
* LCD_E LCD_LCD_SendHalfCharHigh(a) *
* LCD_SendHalfCharLow(a) *
* 2、自带基本的延时函数。需要外部提供毫秒 *
* 单向增加的两个计数器用于特效显示: *
* FlashTimeCounter RunTimeCounter *
* 3、需要修改引用宏定义来指定底层硬件驱动 *
* 函数库。 *
* 4、本库驱动下的LCD显示支持1602的各种连 *
* 接方法。当使用只写模式是请在引用该头 *
* 文件之前加上宏定义: *
* # define _Use_LCDOnlyWrite *
* 5、在调用本库之前,定义RunStringSpeed可 *
* 以设置滚屏的速度。通过在引用前定义 *
* FlashTimeOut 和 FlashGIFTimeOut 可以 *
* 改变字符闪烁的频率。 *
* 6、增加一个GIF字幕特效函数。 *
* 7、确定没有LCD复位问题。 *
* 8、增加对595串行转并行连接的支持。 *
* 需要头文件SerialToCollateral.h *
********************************************************/
/***********************
* 系 统 宏 定 义 *
***********************/
/*---------------------*
* 常 数 宏 定 义 *
*---------------------*/
#ifndef True
# define True 0x01
#endif
#ifndef False
# define False 0x00
#endif
#ifndef High
# define High 0x01
#endif
#ifndef Low
# define Low 0x00
#endif
#ifndef RunStringSpeed
# define RunStringSpeed 100
#endif
#ifndef FlashTimeOut
# define FlashTimeOut 500
#endif
#ifndef FlashGIFTimeOut
# define FlashGIFTimeOut 1000
#endif
# define LCD_Write 0x00
# define LCD_Read 0x01
# define LCD_Command 0x00
# define LCD_Data 0x01
# define LCD_CMD_Init 0x28
# define LCD_CMD_DispCtr 0x0c
# define LCD_CMD_CLS 0x01
# define LCD_CMD_EnterSet 0x06
# define LCD_CMD_IconShow 0x0f
# define LCD_CMD_IconHide 0x0c
# define LCD_CMD_NotMove 0b00010100
/*---------------------*
* 动 作 宏 定 义 *
*---------------------*/
#ifndef LCD_SetWriteData
# define LCD_SetWriteData
#endif
#ifndef LCD_SetReadData
# define LCD_SetReadData
#endif
#ifndef LCD_RW
# define LCD_RW LCD_Temp
#endif
#ifndef LCD_BF
# define LCD_BF LCD_Temp
#endif
#ifndef _Use_LCD1602B_Serial
# define SetReadState LCD_SetReadData;LCD_RS = LCD_Command;LCD_RW = LCD_Read;
# define SetRead LCD_SetReadData;LCD_RW = LCD_Read;
# define SetWrite LCD_SetWriteData;LCD_RW = LCD_Write;
# define SetCommand LCD_RS = LCD_Command;
# define SetData LCD_RS = LCD_Data;
#else
# define _Use_LCDOnlyWrite
# define SetReadState
# define SetRead
# define SetWrite
# define SetCommand LCD_RS = LCD_Command;refreshVirtualPORT();
# define SetData LCD_RS = LCD_Data;refreshVirtualPORT();
# define SetEnable LCD_E = High;refreshVirtualPORT();
# define SetDisable LCD_E = Low;refreshVirtualPORT();
#endif
# define Print(a) LCDDisplayString(a);
# define Locate(x,y) LCDSetXY(x-1,y-1);
# define CLS LCDWaitForReady();LCDSendCommand(LCD_CMD_CLS);LCDWaitForReady();
# define PrintN(a,b) LCDDisplayNum((unsigned long)a,b);
# define ShowIcon LCDWaitForReady();LCDSendCommand(LCD_CMD_IconShow);LCDWaitForReady();
# define HideIcon LCDWaitForReady();LCDSendCommand(LCD_CMD_IconHide);LCDWaitForReady();
/***********************
* 全局变量声明区 *
***********************/
const char CHR[16] = {'0','1','2','3','4','5','6','7','8'
,'9','a','b','c','d','e','f'};
extern unsigned int FlashTimeCounter;
extern unsigned int RunTimeCounter;
extern unsigned int FlashGIFStringCounter;
char LCD_Temp = 0;
/***********************
* 系统函数声明区 *
***********************/
void LCDInit(void);
void LCDSendCommand(char Command);
void LCDSendData(char Data);
void LCDWaitForReady(void);
void LCDSetXY(char X,char Y);
void LCDDisplayString(char *String);
void LCDDisplayNum(unsigned long Num,char BitCount);
void LCDDelay(unsigned int Time);
void LCDDelayUs(unsigned int Time);
void RunString(char *String,char Direction,char Y,char StartX,char EndX);
void Flash(char *String,char Icon,char X,char Y);
char StringLength(char *String);
void FlashStringGroup(char String[][17],char StringCounter,char X,char Y);
#ifdef _Use_LCD1602B_Serial
extern void refreshVirtualPORT(void);
#endif
/********************************************************
* 函数说明:LCD驱动类毫秒延时函数 *
* 输入: 需要延时的大体毫秒数 *
********************************************************/
void LCDDelay(unsigned int Time)
{
unsigned int TimeCounter = 0;
for (TimeCounter = 0;TimeCounter < Time;TimeCounter ++)
{
LCDDelayUs(255);
}
}
/********************************************************
* 函数说明:LCD驱动指令周期延时函数 *
* 输入: 需要大体延时Us数 *
********************************************************/
void LCDDelayUs(unsigned int Time)
{
unsigned int TimeCounter = 0;
for (TimeCounter = 0;TimeCounter < Time;TimeCounter ++)
{
asm("nop");
}
}
/********************************************************
* 函数说明:LCD初始化函数 *
********************************************************/
void LCDInit(void)
{
LCDDelay(15);
LCDWaitForReady();
LCDSendCommand(LCD_CMD_Init);
LCDWaitForReady();
LCDSendCommand(LCD_CMD_DispCtr);
LCDWaitForReady();
LCDSendCommand(LCD_CMD_CLS);
LCDDelay(2);
LCDSendCommand(LCD_CMD_EnterSet);
}
#ifndef _Use_LCD1602B_Serial
/********************************************************
* 函数说明:向LCD发送指令函数 *
* 输入: 需要发送的指令 *
********************************************************/
void LCDSendCommand(char Command)
{
SetWrite;
SetCommand;
{
LCD_E = High;
LCD_SendHalfCharHigh(Command);
LCD_E = Low;
}
{
LCD_E = High;
LCD_SendHalfCharLow(Command);
LCD_E = Low;
}
SetRead;
SetCommand;
}
/********************************************************
* 函数说明:向LCD发送数据函数 *
********************************************************/
void LCDSendData(char Data)
{
SetWrite;
SetData;
{
LCD_E = High;
LCD_SendHalfCharHigh(Data);
LCD_E = Low;
}
{
LCD_E = High;
LCD_SendHalfCharLow(Data);
LCD_E = Low;
}
SetRead;
SetCommand;
}
/********************************************************
* 函数说明:等待LCD空闲状态函数 *
********************************************************/
void LCDWaitForReady(void)
{
#ifdef _Use_LCDOnlyWrite
LCDDelayUs(30);
#else
SetRead;
SetCommand;
LCD_E = High;
while (LCD_BF == Enable); //RW=1,读PD7,为0表示空闲;
LCD_E = Low;
#endif
}
#else
/********************************************************
* 函数说明:向LCD发送指令函数 *
* 输入: 需要发送的指令 *
********************************************************/
void LCDSendCommand(char Command)
{
SetCommand;
{
SetEnable;
LCD_SendHalfCharHigh(Command);
SetDisable;
}
{
SetEnable;
LCD_SendHalfCharLow(Command);
SetDisable;
}
SetCommand;
}
/********************************************************
* 函数说明:向LCD发送数据函数 *
********************************************************/
void LCDSendData(char Data)
{
SetData;
{
SetEnable;
LCD_SendHalfCharHigh(Data);
SetDisable;
}
{
SetEnable;
LCD_SendHalfCharLow(Data);
SetDisable;
}
SetCommand;
}
/********************************************************
* 函数说明:等待LCD空闲状态函数 *
********************************************************/
void LCDWaitForReady(void)
{
LCDDelayUs(30);
}
#endif
/********************************************************
* 函数说明:设置显示坐标函数 *
********************************************************/
void LCDSetXY(char X,char Y)
{
char Address;
if (Y == 0)
{
Address = 0x80 + X;
}
else
{
Address = 0xc0 + X;
}
LCDWaitForReady();
LCDSendCommand(Address);
}
/********************************************************
* 函数说明:LCD字符串显示函数 *
********************************************************/
void LCDDisplayString(char *String)
{
while(*String)
{
LCDWaitForReady();
LCDSendData(*String);
String++;
}
}
/********************************************************
* 函数说明:数值显示函数(HEX) *
* 输入: 需要显示的数字(无符号长整形) *
********************************************************/
void LCDDisplayNum(unsigned long Num,char BitCount)
{
char a = 0;
for (a = 8-BitCount ;a<8;a++)
{
LCDSendData(CHR[(Num<<(a<<2))>>28]);
}
}
/********************************************************
* 函数说明:滚屏字幕效果 *
* 输入: 需要滚屏的字符串 长度 位置 *
********************************************************/
void RunString(char *String,char Direction,char Y,char StartX,char EndX)
{
static char StringHead = 0;
char SCREEN[17];
char a = 0;
char Point = StringHead;
char StringLong = StringLength(String);
static unsigned int RunTimeCounter = 0;
for (a = 0;a<EndX - StartX + 1;a++)
{
SCREEN[a] = String[Point];
Point ++;
if (Point == StringLong)
{
Point = 0;
}
}
for (;a < 17;a++)
{
SCREEN[a] =' ';
}
RunTimeCounter ++;
if (RunTimeCounter >RunStringSpeed)
{
StringHead ++;
RunTimeCounter = 0;
if (StringHead == StringLong)
{
StringHead = 0;
}
}
Locate(StartX,Y)
Print(SCREEN)
}
/********************************************************
* 函数说明:字符串长度测试函数 *
********************************************************/
char StringLength(char *String)
{
char n = 0;
while (*String)
{
n++;
String ++;
}
return n;
}
/********************************************************
* 函数说明:闪烁显示函数 *
********************************************************/
void Flash(char *String,char Icon,char X,char Y)
{
char a = 0;
char StringLong = StringLength(String);
if (FlashTimeCounter % FlashTimeOut > (FlashTimeOut >> 1))
{
Locate(X,Y)
Print(String)
}
else
{
for (a = X ;a < X+StringLong;a++)
{
Locate(a,Y)
LCDWaitForReady();
LCDSendData(Icon);
}
}
}
/********************************************************
* 函数说明:字幕GIF函数 *
********************************************************/
void FlashStringGroup(char String[][17],char StringCounter,char X,char Y)
{
static char Pictures = 0;
static char DispState = 0;
if (FlashGIFStringCounter % FlashGIFTimeOut > (FlashGIFTimeOut >> 1))
{
if (DispState == 0)
{
Pictures ++;
if (Pictures == StringCounter)
{
Pictures = 0;
}
DispState = 1;
}
}
else
{
if (DispState == 1)
{
Pictures ++;
if (Pictures == StringCounter)
{
Pictures = 0;
}
DispState = 0;
}
}
Locate(X,Y);
Print(String[Pictures]);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -