?? lcd1602.h
字號:
/*********************************************************************
微 雪 電 子 WaveShare http://www.waveShare.net
目 的: 建立LCD1602操作庫(偏考慮效率的寫法)
(LCD1602ForTran.H:偏考慮移植的寫法)
目標系統: 基于任何兼容C51的微處理器
應用軟件: Keil C
版 本: Version 1.0
圓版時間: 2004-08-25
開發人員: SEE
說 明: 若用于商業用途,請保留此段文字或注明代碼來源
深 圳 微 雪 電 子 保 留 所 有 的 版 權
*********************************************************************/
/*01010101010101010101010101010101010101010101010101010101010101010101
----------------------------------------------------------------------
版本更新記錄:
版 本: Version 1.1
圓版時間: 2005-03-25
----------------------------------------------------------------------
入口參數說明:
sbit LCD1602_RS = P0^5;
sbit LCD1602_RW = P0^6;
sbit LCD1602_E = P0^7;
sbit LCD1602_D4 = P2^4;
sbit LCD1602_D5 = P2^5;
sbit LCD1602_D6 = P2^6;
sbit LCD1602_D7 = P2^7;
sbit LCD1602_BUSY = P2^7;
----------------------------------------------------------------------
待定參數說明:
#define DELAY() {NOP();NOP();NOP();}
----------------------------------------------------------------------
對外變量說明:
----------------------------------------------------------------------
對外函數說明:
----------------------------------------------------------------------
10101010101010101010101010101010101010101010101010101010101010101010*/
#ifndef LCD1602_H
#define LCD1602_H
#include "D:\C51_H\CmmC51.H"
sbit LCD1602_RS = P0^5;
sbit LCD1602_RW = P0^6;
sbit LCD1602_E = P0^7;
sbit LCD1602_D4 = P2^4;
sbit LCD1602_D5 = P2^5;
sbit LCD1602_D6 = P2^6;
sbit LCD1602_D7 = P2^7;
sbit LCD1602_BUSY = P2^7;
/* 待定參數 */
#define DELAY() {NOP();NOP();NOP();}
/* 不考慮移植性的寫法,KEIL專用,少占用RAM啊 */
uint8 bdata bdat;
sbit bdat0=bdat^0;
sbit bdat1=bdat^1;
sbit bdat2=bdat^2;
sbit bdat3=bdat^3;
sbit bdat4=bdat^4;
sbit bdat5=bdat^5;
sbit bdat6=bdat^6;
sbit bdat7=bdat^7;
/* 考慮移植性的寫法 */
//uint8 bdat;
//#define bdat0 (bdat&0x01)
//#define bdat1 (bdat&0x02)
//#define bdat2 (bdat&0x04)
//#define bdat3 (bdat&0x08)
//#define bdat4 (bdat&0x10)
//#define bdat5 (bdat&0x20)
//#define bdat6 (bdat&0x40)
//#define bdat7 (bdat&0x80)
#define CGRAM0 0x00
#define CGRAM1 0x01
#define CGRAM2 0x02
#define CGRAM3 0x03
#define CGRAM4 0x04
#define CGRAM5 0x05
#define CGRAM6 0x06
#define CGRAM7 0x07
#define TRUE 1
#define FALSE 0
bool LCD1602Err = FALSE;
/*--------------------------------------------------------------------
函數名稱:LCD1602讀讀讀讀讀忙~
函數功能:都說是讀讀讀讀讀忙咯~
注意事項:對于高速CPU,應加延時,好像是廢話~
提示說明:
輸 入:無
返 回:無
--------------------------------------------------------------------*/
void busy(void)
{
uint8 busyCounter=0;
bool busySta; //用于探測 lcd busy status
LCD1602_D4=1;
LCD1602_D5=1;
LCD1602_D6=1;
LCD1602_D7=1;
//DELAY();
LCD1602_RS=0;
//DELAY();
LCD1602_RW=1;
//DELAY();
do
{
LCD1602_E=1;
//DELAY();
/* 這里讀取AC4-AC6位及BF的值,程序不需記錄AC4-AC6的值,所以不存儲 */
busySta=LCD1602_BUSY;
LCD1602_E=0;
//DELAY();
/* 讀取 "BUSY"時,"D4-D7"狀態可能已經改變,必須再次設為輸出"1" */
LCD1602_D4=1;
LCD1602_D5=1;
LCD1602_D6=1;
LCD1602_D7=1;
//DELAY();
LCD1602_E=1;
//DELAY();
/* 這里讀取AC0-AC3位的值,程序不需記錄AC0-AC3的值,所以不存儲 */
LCD1602_E=0;
//DELAY();
if(busyCounter==1000)
{
LCD1602Err=TRUE; //標識LCD1602錯誤,方便上繳系統報錯
return ; //避免由于LCD1602錯誤而導致程序阻塞
}
busyCounter++;
}
while(busySta);
LCD1602Err=FALSE;
LCD1602_E=0;
}
/*--------------------------------------------------------------------
函數名稱:LCD1602寫操作
函數功能:
注意事項:對于高速CPU,應加延時,好像是廢話~
提示說明:
輸 入:
返 回:無
--------------------------------------------------------------------*/
void write(bool flag,uint8 dat) //flag=0:command,flag=1:data
{
bdat=dat;
busy();
LCD1602_RS=flag;
//DELAY();
LCD1602_RW=0;
//DELAY();
LCD1602_D4=(bool)bdat4; //可以不加(bool)類型轉換
LCD1602_D5=(bool)bdat5; //加上(bool)則可以提高程序健壯性、可讀性等
LCD1602_D6=(bool)bdat6;
LCD1602_D7=(bool)bdat7;
//DELAY();
LCD1602_E=1;
//DELAY();
LCD1602_E=0;
//DELAY();
LCD1602_D4=(bool)bdat0;
LCD1602_D5=(bool)bdat1;
LCD1602_D6=(bool)bdat2;
LCD1602_D7=(bool)bdat3;
//DELAY();
LCD1602_E=1;
//DELAY();
LCD1602_E=0;
//DELAY();
}
/*--------------------------------------------------------------------
函數名稱:LCD1602讀操作
函數功能:
注意事項:對于高速CPU,應加延時,好像是廢話~
提示說明:
輸 入:
返 回:無
--------------------------------------------------------------------*/
//void read(uint8 adr)
//{
//}
/*--------------------------------------------------------------------
函數名稱:LCD1602設置CGRAM內容
函數功能:
注意事項:對于高速CPU,應加延時,好像是廢話~
提示說明:調用LCD1602_setCG(0,userCh),則寫入用戶定義的字符"userCh"
輸 入:"adr"數據范圍:0-8,"buf"為用戶需要寫入的字符"userCh"
返 回:無
--------------------------------------------------------------------*/
//void LCD1602_setCGRAM(uint8 adr,uint8 buf[8])
//{
// write(0,0x40+adr*8);
// while(*buf)
// write(1,*buf++);
//}
/*--------------------------------------------------------------------
函數名稱:LCD1602命令設置
函數功能:
注意事項:對于高速CPU,應加延時,好像是廢話~
提示說明:
輸 入:"CLR_SCR"/"GO_HOME"/"AC_INC"/"AC_DEC"...
返 回:無
--------------------------------------------------------------------*/
//---- function ------ 1 -------- 0 ----LcdWordPos--
// dispEn | Enable | Disable | bit2
// cursorEn | Enable | Disable | bit1
// blinkEn | Enable | Disable | bit0
//------------------------------------------------------
// isACinc | INC_AC | DEC_AC | bit1
// shiftEn | Enable | Disable | bit0
//------------------------------------------------------
void LCD1602_setCmd(uint8 *str)
{
static bool dispEn =0;
static bool cursorEn=0;
static bool blinkEn =0;
static bool shiftEn =0;
static bool isACinc =0;
if(!strcmp(str,"CLR_SCR")) //clear screen
write(0,0x01);
else if(!strcmp(str,"GO_HOME")) //set AC go home
write(0,0x02);
/*--------------------------------------------------
isACinc & shiftEn 共用一個命令設置
--------------------------------------------------*/
else if(!strcmp(str,"INC_AC")) //set AC as inc mode
{
isACinc=1;
if(shiftEn)
write(0,0x07);
else
write(0,0x06);
}
else if(!strcmp(str,"DEC_AC")) //set AC as dec mode
{
isACinc=0;
if(shiftEn)
write(0,0x05);
else
write(0,0x04);
}
else if(!strcmp(str,"EN_SHIFT")) //enable shift
{
shiftEn=1;
if(isACinc)
write(0,0x07);
else
write(0,0x06);
}
else if(!strcmp(str,"DIS_SHIFT")) //disable shift
{
shiftEn=0;
if(isACinc)
write(0,0x05);
else
write(0,0x04);
}
/*--------------------------------------------------
dispEn & cursorEn & blinkEn共用一個命令設置
--------------------------------------------------*/
else if(!strcmp(str,"OPEN_LCD")) //open lcd
{
dispEn=1;
if(cursorEn)
if(blinkEn)
write(0,0x0F);
else
write(0,0x0E);
else
if(blinkEn)
write(0,0x0D);
else
write(0,0x0C);
}
else if(!strcmp(str,"CLOSE_LCD")) //close lcd
{
dispEn=0;
if(cursorEn)
if(blinkEn)
write(0,0x0B);
else
write(0,0x0A);
else
if(blinkEn)
write(0,0x09);
else
write(0,0x08);
}
else if(!strcmp(str,"OPEN_CURS")) //open cursor
{
cursorEn=1;
if(dispEn)
if(blinkEn)
write(0,0x0F);
else
write(0,0x0E);
else
if(blinkEn)
write(0,0x0B);
else
write(0,0x0A);
}
else if(!strcmp(str,"CLOSE_CURS")) //close cursor
{
cursorEn=0;
if(dispEn)
if(blinkEn)
write(0,0x0D);
else
write(0,0x0C);
else
if(blinkEn)
write(0,0x09);
else
write(0,0x08);
}
else if(!strcmp(str,"EN_BLINK")) //enable blink cursor
{
blinkEn=1;
if(dispEn)
if(cursorEn)
write(0,0x0F);
else
write(0,0x0D);
else
if(cursorEn)
write(0,0x0B);
else
write(0,0x09);
}
else if(!strcmp(str,"DIS_BLINK")) //disable blink cursor
{
blinkEn=0;
if(dispEn)
if(cursorEn)
write(0,0x0E);
else
write(0,0x0C);
else
if(cursorEn)
write(0,0x0A);
else
write(0,0x08);
}
/*--------------------------------------------------
dispEn & cursorEn & blinkEn共用一個命令設置
--------------------------------------------------*/
else if(!strcmp(str,"RIGHT_SCR")) //right shift screen
write(0,0x1c);
else if(!strcmp(str,"LEFT_SCR")) //left shift screen
write(0,0x18);
else if(!strcmp(str,"RIGHT_CURS")) //right shift cursor
write(0,0x14);
else if(!strcmp(str,"LEFT_CURS")) //left shift cursor
write(0,0x10);
}
/*--------------------------------------------------------------------
函數名稱:LCD1602初始化
函數功能:
注意事項:
提示說明:
輸 入:無
返 回:無
--------------------------------------------------------------------*/
void LCD1602_init(void)
{
delay50ms(1);
//LCD1602_RS=0;
//LCD1602_RW=0;
//LCD1602_E=0;
LCD1602_D7=0;
LCD1602_D6=0;
LCD1602_D5=1;
LCD1602_D4=1;
//DELAY();
LCD1602_RS=0;
//DELAY();
LCD1602_RW=0;
//DELAY();
LCD1602_E=1; //first pulse
DELAY();
LCD1602_E=0;
delay50us(200);
LCD1602_E=1; //second pulse
DELAY();
LCD1602_E=0;
delay50us(200);
LCD1602_E=1; //third pulse
DELAY();
LCD1602_E=0;
delay50us(200);
//LCD1602_RS=0;
//LCD1602_RW=0;
//LCD1602_E=0;
LCD1602_D7=0;
LCD1602_D6=0;
LCD1602_D5=1;
LCD1602_D4=0;
//DELAY();
LCD1602_E=1;
//DELAY();
LCD1602_E=0;
//DELAY();
LCD1602_setCmd("OPEN_LCD");
LCD1602_setCmd("CLR_SCR");
LCD1602_setCmd("INC_AC");
//LCD1602_setCmd("OPEN_CURS");
//LCD1602_setCmd("GO_HOME");
}
/*--------------------------------------------------------------------
宏名稱:設置AC值
宏功能:設置AC值啦
注意事項:
提示說明:
輸 入:
返 回:無
--------------------------------------------------------------------*/
#define LCD1602_setAC(adr) write(0,adr)
/*--------------------------------------------------------------------
函數名稱:輸出一個字符
函數功能:
注意事項:對于高速CPU,應加延時,好像是廢話~
提示說明:調用LCD1602_putc(0x80,'A'),則在第一行第一個字符處輸出'A'
輸 入:
返 回:無
--------------------------------------------------------------------*/
void LCD1602_putc(uint X,uint8 Y,uint8 ch)
{
uchar addr;
if(Y)
{
addr=0xc0;
}
else
{
addr=0x80;
}
write(0,addr+X);
write(1,ch);
}
/*--------------------------------------------------------------------
函數名稱:輸出一個字符串
函數功能:
注意事項:無
提示說明:調用LCD1602_puts(0x80,"IloveU "),則從第一行第一個位置開始輸出"IloveU"
輸 入:
返 回:無
--------------------------------------------------------------------*/
void LCD1602_puts(uint8 X,uint Y,uint8 *str)
{
/*
while(*str)
{
LCD1602_putc(startAdr++,*str++);
}
*/
//LCD1602_setCmd("INC_AC");
// write(0,startAdr);
while(*str)
// write(1,*str++);
{
LCD1602_putc(X,Y,*str++);
X++;
}
}
/*--------------------------------------------------------------------
函數名稱:輸出一個數值(帶0)
函數功能:有時候你可能不是需要"123",而是需要"00123"吧
注意事項:無
提示說明:調用LCD1602_putd0(0x8F,123,5),則從0x8B開始到0X8F輸出"00123"
輸 入:
返 回:無
--------------------------------------------------------------------*/
//for example:dat=123,length=6,output 000123
//void LCD1602_putd0(uint8 endAdr,uint32 dat,uint8 length)
//{
// sint8 i;
// speaData(dat,length);
// //LCD1602_setCmd("INC_AC");
// write(0,endAdr-length+1);
// for(i=length-1;i>=0;i--)
// write(1,dataElem[i]+0x30);
//}
/*--------------------------------------------------------------------
函數名稱:輸出一個數值(不帶0)
函數功能:
注意事項:無
提示說明:調用LCD1602_putd(0x8F,123,5),則從0x8B開始到0X8F輸出" 123"
輸 入:
返 回:無
--------------------------------------------------------------------*/
//void LCD1602_putd(uint8 endAdr,uint32 dat,uint8 length)
//{
// sint8 i;
// sint8 effectLen;
// if(dat>999999)
// effectLen=7;
// else if(dat>99999)
// effectLen=6;
// else if(dat>9999)
// effectLen=5;
// else if(dat>999)
// effectLen=4;
// else if(dat>99)
// effectLen=3;
// else if(dat>9)
// effectLen=2;
// else
// effectLen=1;
// speaData(dat,effectLen);
// //LCD1602_setCmd("INC_AC");
// if(length>effectLen)
// {
// write(0,endAdr-length+1);
// for(i=length-effectLen-1;i>=0;i--)
// write(1,' ');
// }
// for(i=effectLen-1;i>=0;i--)
// {
// if(i==0||dataElem[i])
// {
// write(0,endAdr-i);
// for(;i>=0;i--)
// write(1,dataElem[i]+0x30);
// }
// }
//}
/*--------------------------------------------------------------------
函數名稱:輸出一個混合串
函數功能:
注意事項:最好不要加載這個函數,因為它將占用將近1K空間
提示說明:調用LCD1602_sprintf(0x8F,12AB,4),則從0x8B開始到0X8F輸出"12ABok"
輸 入:
返 回:無
--------------------------------------------------------------------*/
//void LCD1602_sprintf(uint8 startAdr,uint32 dat,uint8 length)
//{
// /* clear the display area first here! */
// //LCD1602_puts(addr," ");
// sprintf(t,"%luok",dat);
// //LCD1602_setCmd("INC_AC");
// LCD1602_puts(addr,t);
//}
#endif
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -