lcd1602的4線數(shù)據(jù)傳輸驅(qū)動程序
#include "lcd1602_driver.h"
#include
#include "typedef.h"
#ifndef _LCD_1602_
#define _LCD_1602_
//lcd1602的io
#define LCD1602_WR_DATA_PORTP2OUT//數(shù)據(jù)輸出端口
#define LCD1602_RD_DATA_PORTP2IN//數(shù)據(jù)輸入端口
#define LCD_EN_SET{ P1DIR |= BIT5 ; P1OUT |=BIT5;}//lcd使能設(shè)置
#define LCD_EN_CLR{ P1DIR |= BIT5;P1OUT &= ~BIT5; }//lcd使能清除
#define LCD_RS_SET{ P1DIR |= BIT3;P1OUT |= BIT3;}//傳輸數(shù)據(jù)
#define LCD_RS_CLR{ P1DIR |= BIT3;P1OUT &= ~BIT3; }//傳輸指令
#define LCD_RD_SET{ P1DIR |= BIT4 ; P1OUT |= BIT4;P2DIR &= 0x0f;}//設(shè)置讀
#define LCD_WR_CLR{ P1DIR |= BIT4 ; P1OUT &= ~BIT4; P2DIR |= 0xf0;}//設(shè)置寫
#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//默認輸入數(shù)據(jù)
if(rs == INSTRUCT)
{
LCD_RS_CLR//輸入命令
}
LCD_WR_CLR//寫信號
delay(1);
LCD_EN_SET
delay(1);
P2OUT &= 0x0f;
P2OUT |= (cmd & 0xf0);//傳輸高四位數(shù)據(jù)
delay(1);
LCD_EN_CLR
delay(1);
LCD_EN_SET//LCD_EN拉高
delay(1);
cmd = cmd & 0x0f;
cmd = cmd << 4;
P2OUT &= 0x0f;//傳輸?shù)谒奈粩?shù)據(jù)
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);//顯示模式設(shè)置
delay(1);
lcd1602_write(INSTRUCT,0x28);
delay(1);
lcd1602_write(INSTRUCT,0x28);
delay(1);
lcd1602_write(INSTRUCT,0x0c);//開顯示,不顯示光標,光標不閃爍
delay(1);
lcd1602_write(INSTRUCT,0x06);//讀或?qū)懸粋€字符后地址指針和光標自動加1
delay(1);
lcd1602_write(INSTRUCT,0x01);
delay(1);
lcd1602_clear();
}
//lcd1602清除
void lcd1602_clear(void)
{
lcd1602_write(INSTRUCT,0x01);
delay(1);
}
//設(shè)置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++;
}
}