lcd1602的4线数据传输驱动程序
扫描二维码
随时随地手机看文章
#include "lcd1602_driver.h"
#include
#include "typedef.h"
#ifndef _LCD_1602_
#define _LCD_1602_
//lcd1602的io
#define LCD1602_WR_DATA_PORTP2OUT//数据输出端口
#define LCD1602_RD_DATA_PORTP2IN//数据输入端口
#define LCD_EN_SET{ P1DIR |= BIT5 ; P1OUT |=BIT5;}//lcd使能设置
#define LCD_EN_CLR{ P1DIR |= BIT5;P1OUT &= ~BIT5; }//lcd使能清除
#define LCD_RS_SET{ P1DIR |= BIT3;P1OUT |= BIT3;}//传输数据
#define LCD_RS_CLR{ P1DIR |= BIT3;P1OUT &= ~BIT3; }//传输指令
#define LCD_RD_SET{ P1DIR |= BIT4 ; P1OUT |= BIT4;P2DIR &= 0x0f;}//设置读
#define LCD_WR_CLR{ P1DIR |= BIT4 ; P1OUT &= ~BIT4; P2DIR |= 0xf0;}//设置写
#define INSTRUCT0
#define NUMDATA1
#endif
//延迟
void delay(u8 time)
{
u8 i;
while(time--)
{
for(i = 100; i-- ; )
{
;
}
}
}
//判断busy
void lcd1602_busy(void)
{
LCD_EN_CLR//LCD_EN拉低
LCD_RS_CLR//LCD_RS拉低,传输指令
LCD_RD_SET//LCD_WR拉高为读
delay(1);
LCD_EN_SET//LCD_EN拉高
delay(1);
while(P2IN&0x80);//等待可读
LCD_EN_CLR//LCD_EN拉低
}
//写lcd1602
void lcd1602_write(u8 rs,u8 cmd)
{
lcd1602_busy();
LCD_EN_CLR//LCD_EN拉低
LCD_RS_SET//默认输入数据
if(rs == INSTRUCT)
{
LCD_RS_CLR//输入命令
}
LCD_WR_CLR//写信号
delay(1);
LCD_EN_SET
delay(1);
P2OUT &= 0x0f;
P2OUT |= (cmd & 0xf0);//传输高四位数据
delay(1);
LCD_EN_CLR
delay(1);
LCD_EN_SET//LCD_EN拉高
delay(1);
cmd = cmd & 0x0f;
cmd = cmd << 4;
P2OUT &= 0x0f;//传输第四位数据
P2OUT |= (cmd & 0xf0);
delay(1);
LCD_EN_CLR//LCD_EN拉低
delay(1);
}
//lcd1602a的初始化
void lcd1602_init(void)
{
delay(200);
P2DIR |= 0xf0;
P2SEL &=0x0f;
delay(1);
lcd1602_write(INSTRUCT,0x28);//显示模式设置
delay(1);
lcd1602_write(INSTRUCT,0x28);
delay(1);
lcd1602_write(INSTRUCT,0x28);
delay(1);
lcd1602_write(INSTRUCT,0x0c);//开显示,不显示光标,光标不闪烁
delay(1);
lcd1602_write(INSTRUCT,0x06);//读或写一个字符后地址指针和光标自动加1
delay(1);
lcd1602_write(INSTRUCT,0x01);
delay(1);
lcd1602_clear();
}
//lcd1602清除
void lcd1602_clear(void)
{
lcd1602_write(INSTRUCT,0x01);
delay(1);
}
//设置lcd1602的显示位置
void lcd1602_setPos(u8 x, u8 y)
{
u8 address;
if (x == 0)
{
address = 0x80 + y;
}
else
{
address = 0xc0 + y;
}
lcd1602_write(INSTRUCT,address);
}
void lcd1602_dispChar( u8 x,u8 y, u8 ch)
{
lcd1602_setPos(x,y);
lcd1602_write(NUMDATA,ch);
}
void lcd1602_disp_str( u8 x, u8 y, u8 *str)
{
if(x > 1 || y > 15)
{
return ;
}
lcd1602_setPos(x,y);
for( ; *str != 0; )
{
lcd1602_write(NUMDATA,*str);
str++;
}
}