液晶显示器lcd1602驱动程序
扫描二维码
随时随地手机看文章
#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 = 0x00;}//设置读
#define LCD_WR_CLR{ P1DIR |= BIT2 ; P1OUT &= ~BIT4; P2DIR = 0xff;}//设置写
#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//LCD_RS拉高 ,传输数据
if(rs == INSTRUCT)
{
LCD_RS_CLR//LCD_RS拉低//LCD_RS拉低,传输命令
}
LCD_WR_CLR//LCD_WR拉低为写
delay(1);
LCD_EN_SET//LCD_EN拉高
delay(1);
P2OUT = cmd;//数据
delay(1);
LCD_EN_CLR//LCD_EN拉低
delay(1);
}
//lcd1602a的初始化
void lcd1602_init(void)
{
P2DIR = 0xff;
P2SEL=0x00;
delay(1);
lcd1602_write(INSTRUCT,0x38);//显示模式设置
delay(1);
lcd1602_write(INSTRUCT,0x38);
delay(1);
lcd1602_write(INSTRUCT,0x38);
delay(1);
lcd1602_write(INSTRUCT,0x0c);//开显示,不显示光标,光标不闪烁
delay(1);
lcd1602_write(INSTRUCT,0x06);//读或写一个字符后地址指针和光标自动加1
delay(1);
lcd1602_write(INSTRUCT,0x01);
delay(1);
}
//lcd1602清除
void lcd1602_clear(void)
{
lcd1602_write(INSTRUCT,0x01);
delay(1);
}
//设置lcd1602的显示位置
void lcd1602_setPos(u8 ps)
{
lcd1602_write(INSTRUCT,(0x80 | ps));
}
void lcd1602_dispChar( u8 x,u8 y, u8 ch)
{
if(x != 0)
{
lcd1602_setPos(y | 0x40);
}
else
{
lcd1602_setPos(y);
}
lcd1602_write(NUMDATA,ch);
}
void lcd1602_disp_str( u8 x, u8 y, u8 *str)
{
if(x > 1 || y > 15)
{
return ;
}
for( ; *str != 0; )
{
lcd1602_dispChar( x, y,*str);
str++;
y++;
if(y > 15)
{
x++;
y = 0;
if( x > 1)
{
break;
}
}
}
}