void sendbyte(BYTE outData);
void lcd_init(void)
{
delayms(40); //大于40MS 的延时程序
CLRBIT(lcdport,PSB); //设置为串口工作模式
delayus(1); //延时
CLRBIT(lcdport,RES); //复位
delayms(2); //延时
SETBIT(lcdport,RES); //复位置高
CLRBIT(lcdport,CLK); //Clock Set High
CLRBIT(lcdport,SDA); //Data Set High
CLRBIT(lcdport,CS); //CS set low to disable communication
delayus(BYTE_INTERVAL);
senddata(0x30,TRUE); //Extended Function Set :8BIT 设置,RE=0: basic instruction set, G=0 :graphic display OFF
delayus(CMD_INTERVAL); //大于 100uS 的延时程序
senddata(0x03,TRUE);
delayus(CMD_INTERVAL); //大于 100uS 的延时程序
senddata(0x0C,TRUE); //Display Control,D=1,显示开
delayus(CMD_INTERVAL); //大于 100uS 的延时程序
senddata(0x01,TRUE); //Display Clear
delayms(4); //大于 10mS 的延时程序
senddata(0x06,TRUE); //Enry Mode Set,光标从右向左加 1 位移动
delayus(CMD_INTERVAL); //大于 100us 的延时程序
}
BYTE ReceiveByte(void)
{
BYTE i,temp1,temp2;
temp1=temp2=0;
CLRBIT(lcdddr,SDA);
for(i=0;i<8;i++)
{
SETBIT(lcdport,CLK);
delayus(BIT_DELAY);
CLRBIT(lcdport,CLK);
delayus(BIT_DELAY);
if(GETBIT(lcdpin,SDA)!=0) temp1 |= (1<<(8-i));
}
for(i=0;i<8;i++)
{
SETBIT(lcdport,CLK);
delayus(BIT_DELAY);
CLRBIT(lcdport,CLK);
delayus(BIT_DELAY);
if(GETBIT(lcdpin,SDA)!=0) temp2 |= (1<<(8-i));
}
i = (temp1&0xf0) + ((temp2>>4)&0x0f);
SETBIT(lcdddr,SDA);
//FormTXDMessage(RETURN_STATUS,i);
//delayms(100);
return i;
}
void CheckBusy( void )
{
do sendbyte(0xfc); //11111,RW(1),RS(0),0
while(0x80&ReceiveByte()); //BF(.7)=1 Busy
}
void sendbyte(BYTE outData)
{
BYTE i = 0;
for(i=0;i<8;i++)
{
CLRBIT(lcdport,CLK);
if((outData & 0x80) != 0)
SETBIT(lcdport,SDA);
else
CLRBIT(lcdport,SDA);
delayus(BIT_DELAY);
SETBIT(lcdport,CLK);
delayus(BIT_DELAY);
CLRBIT(lcdport,CLK);
outData <<= 1;
}
}
void senddata(BYTE data,BYTE isCmd)
{
SETBIT(lcdport,CS);
CheckBusy();
if(isCmd)
sendbyte(0xf8); //11111,RW(0),RS(1),0
else
sendbyte(0xfa);
//delayus(BYTE_INTERVAL);
sendbyte(data & 0xf0); //Send high 4 bits
//delayus(BYTE_INTERVAL);
sendbyte((data<<4) & 0xf0); //Send low 4 bits
//delayus(BYTE_INTERVAL);
CLRBIT(lcdport,CS);
}
void refresh(BYTE linenum, BYTE FirstTime)
{
BYTE i;
static BYTE xx = 0;
if(FirstTime)
{
startpos[0] = 0;
startpos[1] = 0;
}
senddata(0x30,TRUE);
switch(linenum)
{
case REFRESH_ALL:
default:
senddata(0x80+xx,TRUE); //Set line 1 DD RAM address
for(i = 0;i < FIXED_MAX;i++)
senddata(/*LineFour[i]*/0x30,FALSE);
xx += 8;
xx &= 0x1f;
if(xx==0)
senddata(0x01,TRUE);
/*
senddata(0x88,TRUE);
delayus(CMD_INTERVAL);
sendlinedata(num_two,linenum - 1,LineTwo);
senddata(0x90,TRUE);
delayus(CMD_INTERVAL);
sendlinedata(num_three,linenum - 1,LineThree);
senddata(0x98,TRUE); //Set line 1 DD RAM address
delayus(CMD_INTERVAL);
for(i = 0;i < FIXED_MAX;i++)
senddata(LineFour[i],FALSE);
*/
break;
case LINE_TWO:
startpos[0] += 2;
senddata(0x88,TRUE);
sendlinedata(num_two,startpos[0],LineTwo);
break;
case LINE_THREE:
startpos[1] += 2;
senddata(0x98,TRUE);
sendlinedata(num_three,startpos[1],LineThree);
break;
}
}
上面是代码,有点多,大家包涵点,目前只是运行了REFRESH_ALL这个case里的代码,在主while里定时300ms调用一次 |