HD4478U顯示LCD與51單片機驅動程序
#include
#include
sbit LCDRSPIN = P3^7;
sbit LCDRWPIN = P3^6;
sbit LCDEPIN = P2^6;
#define UCHAR unsigned char
void delay(unsigned int i)
{
UCHAR j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}
///////////////////////////////
//控制LCD
unsigned char XPOS,YPOS;
//測忙
void LcdWaitIdle(void)
{ P0=0xff;
LCDRSPIN=0;
LCDRWPIN=1;
LCDEPIN=1;
while((P0&0x80)==0x80);
LCDEPIN=0;
}
//寫指令代碼
void LcdWCN(unsigned char c)
{ LCDRSPIN=0;
LCDRWPIN=0;
P0=c;
LCDEPIN=1;
_nop_();
LCDEPIN=0;
}
//寫指令代碼(測忙)
void LcdWC(unsigned char c)
{ LcdWaitIdle();
LcdWCN(c);
}
//寫數據
void LcdWD(unsigned char d)
{ LcdWaitIdle();
LCDRSPIN=1;
LCDRWPIN=0;
P0=d;
LCDEPIN=1;
_nop_();
LCDEPIN=0;
}
//內部數據地址指針定位
void LcdPos(void)
{ XPOS&=0x0f;
YPOS&=0x03;
if(YPOS==0x00)
LcdWC(XPOS|0x80);
else if(YPOS==0x01)
LcdWC((XPOS+0x40)|0x80);
else if(YPOS==0x02)
LcdWC((XPOS+0x10)|0x80);
}
//初始化
void InitLcd()
{
LCDEPIN=0;
delay(150); LcdWCN(0x38); /*設置8位格式,2行,2*16*/
LcdWC(0x08); /*清除顯示*/
LcdWC(0x06); /*設定輸入方式,增量不移位*/
LcdWC(0x0c); /*整體顯示,關光標,不閃爍*/
}
void LcdWDA(unsigned char x,unsigned char y,unsigned char *s)
{
YPOS=y;
for(XPOS=x;XPOS<16 && *s != 0;XPOS++)
{ LcdPos();
LcdWD(*s);
s++;
}
}
char LcdStr[13]={L,A,N,D, ,C,5,1,-,P,R,O,0x0};
char * str1=LcdStr;
char * str2="WWW.LDMCU.COM";
char CharPos=-11;
char CharLen = 12; //滾動的字符的個數
bit LcdPause = 0; //是否停止?jié)L動
以下為字符顯示位置與主程序,可相應修改。
//設置str1從左邊進入
void LcdLeftIn()
{
delay(1500);
LCDEPIN=0;
LcdWDA(2,1,str2);
if(CharPos > 16)
{
CharPos = (CharLen - 1)* -1;
}
if(CharPos > 0)
{
LcdWDA(CharPos-1,0," "); //前進一格清除前面的字符
LcdWDA(CharPos,0,str1);
}
else
{
LcdWDA(0,0,str1-CharPos);
}
CharPos++;
// P3=P3B;
}
main()
{
InitLcd();
while(1)
{
LcdLeftIn();
}
}
評論