通用型51單片機(jī)lcd1602驅(qū)動
文件:lcd_1602.h
#ifndef _LCD1602_TRAVELER_H_
#define _LCD1602_TRAVELER_H_
/*************************待配置項(xiàng)*******************************/
#define DATA_LCD P0 //液晶與MCU 數(shù)據(jù)口
sbit RS_LCD = P2^5; // 液晶的RS、RW、E 端在 MCU端的連線
sbit RW_LCD = P2^6;
sbit E_LCD = P2^7;
/****************************************************************/
void lcdInit(void);
//lcdInit() 液晶初始化、所有對液晶的操作之前均需要調(diào)用此函數(shù)
void lcdDelayMs(const unsigned char ms);
// lcdDelayMs(ms) 液晶內(nèi)部使用的毫秒級延時函數(shù) 如不需要請自行刪除
// 該驅(qū)動函數(shù)須在不超過12MHz的晶振頻率下才能正常工作
void lcdDelayUs(const unsigned char us);
// lcdDelayMs(ms) 液晶內(nèi)部使用的微秒級延時函數(shù) 如不需要請自行刪除
// 該驅(qū)動函數(shù)須在不超過12MHz的晶振頻率下才能正常工作
void lcdWriteCmd(const unsigned char cmd);
// lcdWriteCmd(cmd) 向液晶寫命令字函數(shù)
void lcdWriteChar(const unsigned char chr);
// lcdWriteChar(chr) 向液晶寫字符函數(shù)
void lcdWriteData(const unsigned char pos,const unsigned char dat);
//lcdWriteData(pos,dat)向液晶板上指定位置寫字符函數(shù)
void lcdWriteString(const unsigned char line,unsigned char *str,const unsigned char offset);
//lcdWriteString(line,*str,offset) 向液晶面板指定行、寫字符串 可指定首字符偏移量 所顯示的區(qū)域限制在當(dāng)前行內(nèi)
bit lcdReadBusy();
//讀lcd的忙標(biāo)志 忙 返回1 不忙 返回0
void lcdWriteUint(unsigned char line,unsigned int c,unsigned char offset);
//lcdWriteUint(unsigned char line,unsigned int c,unsigned char offset)寫無符號整形數(shù) 到lcd (占位 5 字符)
//line 指明所在行、c要寫的數(shù)據(jù)、offset 距首列偏移量
#endif
——————————————————
文件:lcd_1602.c
#include
#include "lcd_1602.h"
/*************************************************************************/
/***********************12MHz晶振下微秒級延時*****************************/
void lcdDelayUs(const unsigned char us){
unsigned char cin = us>>1; //prepare for clause while calculating time
cin++;
while(--cin);// 'dec cin' and 'jnz cin' 2 circles
}
/*************************************************************************/
/***********************12MHz晶振下毫秒級延時*****************************/
void lcdDelayMs(const unsigned char ms){
unsigned char cin = ms+1;
while(--cin){
lcdDelayUs(250);
lcdDelayUs(250);
lcdDelayUs(250);
lcdDelayUs(250);
}
}
/*************************************************************************/
/**************************寫命令字***************************************/
void lcdWriteCmd(const unsigned char cmd){
while(lcdReadBusy());
E_LCD = 0;
RS_LCD = 0;
RW_LCD = 0;
DATA_LCD = cmd;
lcdDelayUs(40);
E_LCD = 1;
lcdDelayUs(250);
E_LCD = 0;
lcdDelayUs(250);
}
/*************************************************************************/
/************************寫字符********************************************/
void lcdWriteChar(const unsigned char chr){
while(lcdReadBusy());
E_LCD = 0;
RS_LCD = 1;
RW_LCD = 0 ;
DATA_LCD = chr;
lcdDelayUs(40);
E_LCD = 1;
lcdDelayUs(250);
E_LCD = 0;
lcdDelayUs(250);
}
/*************************************************************************/
/*******************向指定位置寫字符**************************************/
void lcdWriteData(const unsigned char pos,const unsigned char dat){
unsigned char p;
if(pos>=16) p = 0xb0 + pos ;
else p = 0x80 + pos ;
lcdWriteCmd(p);
lcdWriteChar(dat);
}
/*************************************************************************/
/**************************寫字符串***************************************/
void lcdWriteString(const unsigned char line,unsigned char *str,const unsigned char offset){
unsigned char p;
unsigned char curlineend = (line)?32:16; //每次寫字符串最多允許寫完單行,不允許跨行寫
unsigned char i=0;
if(line) p = 16;else p = 0;
p = p + offset;
while(p!=curlineend){
if(!str[i])break;//如果到字符尾則退出
lcdWriteData(p++,str[i++]);
}
}
/*************************************************************************/
/************************初始化液晶***************************************/
void lcdInit(void){
lcdDelayMs(15);
lcdWriteCmd(0x38);
lcdWriteCmd(0x38);
lcdWriteCmd(0x38);[!--empirenews.page--]
lcdWriteCmd(0x06);
lcdWriteCmd(0x0c);
lcdWriteCmd(0x01);
lcdWriteData(1,' ');//此為驅(qū)動的bug,未消除bug之前請勿刪除此行及下行兩行
lcdWriteCmd(0x0c);
}
/*************************************************************************/
bit lcdReadBusy(){
bit i = 1;
E_LCD = 0;
RS_LCD = 0;
RW_LCD = 1;
E_LCD = 1;
i = DATA_LCD & 0x80;
return i;
}
//lcdWriteUint(unsigned char line,unsigned int c,unsigned char offset)寫無符號整形數(shù) 到lcd (占位 5 字符)
//line 指明所在行、c要寫的數(shù)據(jù)、offset 距首列偏移量
void lcdWriteUint(unsigned char line,unsigned int c,unsigned char offset){
unsigned char i,j;
unsigned char arrTmp[6] = {" "};
j = 0;
do{
i = c % 10;
arrTmp[4 - j] = i + '0';
j ++ ;
c /= 10;
}while(c);
lcdWriteString(line,arrTmp,offset);
}