📄 lcd.c
字号:
#include "macro.h"
/******************************************************************************
**函数名称:Delay1ms
**函数功能:延时
**输入参数:时间t
**输出参数:无
*******************************************************************************/
void Delay1ms(uint32 t)
{
uint32 i,j;
for(i=0;i<t;i++)
for(j=0;j<100;j++);
}
/******************************************************************************
**函数名称:ReadStatusLCM
**函数功能:读状态
**输入参数:无
**输出参数:无
*******************************************************************************/
bool ReadStatusLCM(void)
{
bool busy;
RS=0;
RW=1;
EN=1;
_nop_();
_nop_();
_nop_();
_nop_();
EN=0;
return busy;
}
/******************************************************************************
**函数名称:WriteCommandLCD
**函数功能:LCD写指令
**输入参数:指令Cmd,BuysC为0时忽略忙检测
**输出参数:无
实现:RS=L,RW=L,D0~D7=指令码,E=高脉冲
*******************************************************************************/
void WriteCommandLCD(uint8 Cmd,uint8 BuysC )
{
if (BuysC)
while( ReadStatusLCM() ); //根据需要检测忙
RS = 0;
RW = 0;
EN = 0;
LCD_Data = Cmd;
Delay1ms(1); // 若晶振速度太高可以在这后加小的延时
EN=1;
Delay1ms(1);
EN=0;
}
/******************************************************************************
**函数名称:WriteDataLCD
**函数功能:LCD写数据
**输入参数:数据dat
**输出参数:无
实现:RS=H,RW=L,D0~D7=数据,E=高脉冲
*******************************************************************************/
void WriteDataLCD(uint8 dat)
{
while( ReadStatusLCM()); //根据需要检测忙
RS = 1;
RW = 0;
EN = 0;
LCD_Data = dat;
Delay1ms(1); // 若晶振速度太高可以在这后加小的延时
EN=1;
Delay1ms(1); //延时
EN=0;
}
/******************************************************************************
**函数名称:LCD_Init
**函数功能:LCD初始化设置
**输入参数:无
**输出参数:无
*******************************************************************************/
void LCD_Init(void)
{
WriteCommandLCD(0x38,0); // 显示模式设置(不检测忙信号)
Delay1ms(5);
WriteCommandLCD(0x38,0); //一共三次
Delay1ms(5);
WriteCommandLCD(0x38,0);
Delay1ms(5);
WriteCommandLCD(0x38,1); // 显示模式设置(以后均检测忙信号)
Delay1ms(5);
WriteCommandLCD(0x08,1); // 显示关闭
Delay1ms(5);
WriteCommandLCD(0x01,1); // 显示清屏
Delay1ms(5);
WriteCommandLCD(0x06,1); // 显示光标移动设置
Delay1ms(5);
WriteCommandLCD(0x0c,1); // 显示开及光标设置
Delay1ms(5);
}
/******************************************************************************
**函数名称:LocateXY
**函数功能:LCD显示光标定位
**输入参数:posX为X坐标,posY为Y坐标
**输出参数:无
*******************************************************************************/
void LocateXY( char posX,char posY)
{
uint8 temp;
temp = posX & 0xf;
posY &= 0x1;
if ( posY )
temp |= 0x40;
temp |= 0x80;
WriteCommandLCD(temp,0);
}
/*******************************************************************************
**函数名称:DispOneChar
**函数功能:LCD按指定位置显示数出一个字符
**输入参数:posX为X坐标,posY为Y坐标,Wdata要写的数据
**输出参数:无
*******************************************************************************/
void DispOneChar(uint8 posX,uint8 posY,uint8 Wdata)
{
LocateXY( posX, posY ); // 定位显示地址
WriteDataLCD( Wdata ); // 写字符
}
/********************************************************************************
显示字符串
**函数名称:DispOneChar
**函数功能:LCD按指定位置显示一串字符
**输入参数:X坐标,Y坐标,DData要写的数据
**输出参数:无
********************************************************************************/
void DisplayListChar(uint8 X, uint8 Y, uint8 *DData)
{
uint8 ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
while (DData[ListLength] > 0x20) //若到达字串尾则退出
{
if (X <= 0xF) //X坐标应小于0xF
{
DispOneChar(X, Y, DData[ListLength]); //显示单个字符
ListLength++;
X++;
}
}
}
#if 0
/******************************************************************************
**函数名称:ReadDataLCM
**函数功能:LCD读数据
**输入参数:无
**输出参数:读出的数据
*******************************************************************************/
uint8 ReadDataLCM(void)
{
RS = 1;
RW = 1;
EN= 0;
EN = 0;
EN = 1;
return(LCD_Data);
}
/*******************************************************************************
**函数名称:DisplayMoveStr
**函数功能:演示一行连续字符串,配合上位程序演示移动字串
**输入参数:无
**输出参数:读出的数据
*******************************************************************************/
void DisplayMoveStr( uint8 dd )
{
uint8 i;
for (i=0;i<16;i++)
{
DispOneChar(i,1,dd++);
dd &= 0x7f;
if (dd<32) dd=32;
}
}
#endif
/*******************************************************************************
**函数名称:CLS
**函数功能:清屏
**输入参数:无
**输出参数:无
*******************************************************************************/
void CLS(void)
{
WriteCommandLCD(0x01,1); // 显示清屏
Delay1ms(5);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -