文档库 最新最全的文档下载
当前位置:文档库 › pic lcd显示

pic lcd显示

//Display.c:
#include
#define _XTAL_FREQ 1000000UL
#include "Display.h"
//#include "main.h"

/*******************************************************************************
* 函 数 名: uchar Chk_1602_busy(void)
* 函数功能: 读液晶忙通道数据
* 入口参数: 无
* 返 回: 无
*******************************************************************************/
uchar Chk_1602_busy(void)
{
uint gR_data;
uint gwait_time=0xff; //设置忙超时数
LCD_RS=0; //表示状态
LCD_RW=1; //选择读
LCD_EN=1;
LCD_IOEN = 0xFF; //接收口设为输入口
__delay_us(30);
gR_data=LCD_IO;
while(TESTBIT(gR_data,7)) //表示busy
{
--gwait_time;
if(!gwait_time)
{ LCD_EN=0;LCD_IOEN = 0x00; return 0; }
}
LCD_EN=0;
LCD_IOEN = 0x00; //恢复为输出口
return 1;
}
/******************************************************************************
* 函 数 名: void Write_1602_command(uchar gcmd,uchar gvalue)
* 函数功能: 写指令
* 入口参数: gcmd--指令 gvalue--是否查忙
* 返 回: 无
*******************************************************************************/
void Write_1602_command(uchar gcmd,uchar gvalue)
{
if(gvalue) //写命令时大部分情况下是在LCD空闲模式下写
{
if(Chk_1602_busy())
{
LCD_RS=0; //选择指令
LCD_RW=0; //选择写
LCD_IO=gcmd; //赋指令
LCD_EN=1; //使能
__delay_us(30);
LCD_EN=0;
}
}
else
{
LCD_RS=0; //选择指令
LCD_RW=0; //选择写
LCD_IO=gcmd; //赋指令
LCD_EN=1; //使能
__delay_us(30);
LCD_EN=0;
}
}
/******************************************************************************
* 函 数 名: void Write_1602_data(uchar gdata)
* 函数功能: 写数据
* 入口参数: gdata--数据
* 返 回: 无
*******************************************************************************/
/*----------- 写数据 -------------*/
void Write_1602_data(uchar gdata)
{
if(Chk_1602_busy()) //写数据必须是在LCD空闲模式下才能写
{
LCD_RS=1; //选择数据
LCD_RW=0; //选择写
LCD_IO=gdata;
LCD_EN=1; //使能
__delay_us(30);
LCD_EN=0;
__delay_us(10);
}
}
/******************************************************************************
* 函 数 名: void INIT_1602(void)
* 函数功能: 初始化1602LCD
* 入口参数: 无
* 返 回: 无
*******************************************************************************/
/*-----------1602初始化函数-------------*/
void INIT_1602(void)
{
__delay_ms(15);
Write_1602_command(0x38,0); //设置16X2显示,5X7点阵,8位数据传送,不检测忙信号
__delay_ms(5);
Write_1602_command(0x38,0);
__delay_ms(5);
Write_1602_command(0x38,0); //设置16X2显示,5X7点阵,8位数据传送,不检测忙信号
Write_1602_command(0x38,1); //设置16X2显示,5X7点阵,8位数据传送,检测忙信号
Write_1602_command(0x08,1); //关闭显示、无光标、检测忙信号
Write_1602_command(0x01,1); //清屏、光标归位、AC=0、检测忙信号
Write_1602_command(0

x06,1); //显示光标右移位置、检测忙信号
Write_1602_command(0x0C,1); //显示功能开、无光标
}

/******************************************************************************
* 函 数 名: void Display_1602_string(uchar gadd_start,uchar gline,uchar glength,uchar *pdata)
* 函数功能: 显示字符串
* 入口参数: gadd_start-列号,uchar gline-行号,glength-数据长度,pdata-数组元素
* 返 回: 无
*******************************************************************************/
/*-----------显示字符串-------------*/
// 列号 行号 数据长度 数组元素
void Display_1602_string(uchar gadd_start,uchar gline,uchar glength,uchar *pdata)
{
uchar gaddress;
uchar *pdat;
uchar gcount=0;
if(!gline) //第0行
{ gaddress=0x80+gadd_start; } //地址对应
else
{ gaddress=0xc0+gadd_start; } //第一行
for(;gcount{
Write_1602_command(gaddress,1); //设定数据地址
Write_1602_data(*pdata); //取设定地址里的数据
gaddress++;
pdata++;
}
}
/******************************************************************************
* 函 数 名: void Display_1602(uchar x,uchar y,uchar gdata)
* 函数功能: 指定位置显示字符
* 入口参数: x-起始地址横坐标,y-起始地址纵坐标,gdata-要显示的字符
* 返 回: 无
*******************************************************************************/
/*-----------指定位置显示字符-------------*/
void DispChar_XY_1602(uchar x,uchar y,uchar gdata)
{
uchar gaddress;
if(!y)
{ gaddress=0x80+x; }
else
{ gaddress=0xc0+x; }
Write_1602_command(gaddress,1); //设定数据地址
Write_1602_data(gdata);
}
/*-----------指定位置显示数字-------------*/
void DispNum_XY_1602(uchar x,uchar y,uchar gdata)
{
uchar gaddress;
if(!y)
{ gaddress=0x80+x; }
else
{ gaddress=0xc0+x; }
Write_1602_command(gaddress,1); //设定数据地址
Write_1602_data(gdata+0x30);
}
/*-----------清屏-------------*/
void Clear_Display(void)
{
Write_1602_command(0x01,1);
__delay_ms(5);
}
void Open_Disp(void)
{
Write_1602_command(0x0c,1);
}
void Close_Disp(void)
{
Write_1602_command(0x08,1);
}
/*-----------显示光标-------------*/
void Display_Cursor(void)
{
Write_1602_command(0x06,1); //显示光标右移位置、检测忙信号
Write_1602_command(0x0D,1); //显示功能开、无光标
}
/*-----------1602端口初始化-------------*/
void Port_1602_Init(void)
{
ADCON1 = 0x87;//设置PORTA为普通IO口
TRISA0 = 0;
TRISA1 = 0;
TRISA2 = 0;
LCD_IOEN = 0x00;//设置LCD_IO为输出
}

相关文档