液晶顯示器lcd1602驅(qū)動(dòng)程序
掃描二維碼
隨時(shí)隨地手機(jī)看文章
#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 = 0x00;}//設(shè)置讀
#define LCD_WR_CLR{ P1DIR |= BIT2 ; P1OUT &= ~BIT4; P2DIR = 0xff;}//設(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//LCD_RS拉高 ,傳輸數(shù)據(jù)
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;//數(shù)據(jù)
delay(1);
LCD_EN_CLR//LCD_EN拉低
delay(1);
}
//lcd1602a的初始化
void lcd1602_init(void)
{
P2DIR = 0xff;
P2SEL=0x00;
delay(1);
lcd1602_write(INSTRUCT,0x38);//顯示模式設(shè)置
delay(1);
lcd1602_write(INSTRUCT,0x38);
delay(1);
lcd1602_write(INSTRUCT,0x38);
delay(1);
lcd1602_write(INSTRUCT,0x0c);//開顯示,不顯示光標(biāo),光標(biāo)不閃爍
delay(1);
lcd1602_write(INSTRUCT,0x06);//讀或?qū)懸粋€(gè)字符后地址指針和光標(biāo)自動(dòng)加1
delay(1);
lcd1602_write(INSTRUCT,0x01);
delay(1);
}
//lcd1602清除
void lcd1602_clear(void)
{
lcd1602_write(INSTRUCT,0x01);
delay(1);
}
//設(shè)置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;
}
}
}
}