iNT: mov p0,#30h ;初始化
clr rs
clr rw
MOV R2,#03H ;循环量=3
inta: setb e
clr e
lcall delay
djnz r2,inta
mov p0,#38h
setb e
clr e
MOV a,#0fH ;设置显示方式
LCALL PR1
MOV a,#01H ;清屏
LCALL PR1
MOV a,#18H
LCALL PR1
MOV a,#06H ;设置输入方式
LCALL PR1
RET
;
;***** 写指令代码子程序
PR1: LCALL BUSY ;SEND OUT COMMAND
MOV P0,A
CLR rs
CLR rw
SETB e
CLR e
RET
;
;***** 写显示数据子程序
PR2: LCALL BUSY ;SEND OUT DATA
MOV P0,A
SETB rs
CLR rw
SETB e
CLR e
RET
;
BUSY: MOV P0,#0FFH ;READ BUSY
CLR rs
SETB rw
SETB e
MOV C,P0.7
CLR e
JC BUSY
RET
DELAY: MOV R6,#00H ;延时子程序
MOV R7,#00H
DELAY1: NOP
DJNZ R7,DELAY1
DJNZ R6,DELAY1
RET
;
sbit rs=P1^5; //指令和数据寄存器//
sbit rw=P1^6; //读写控制//
sbit e=P1^7; //片选//
int flag;
unsigned char busy(int); //判忙//
void w_dat(unsigned char); //写显示数据子程序//
void w_com(unsigned char); //写指令代码子程序//
void init(void); //初始化子程序//
void disp(unsigned char); //显示//
void wait(unsigned int ); //延时//
unsigned char busy(int flag)
{P0=0xff;
rs=0;
rw=1;
e=1;
flag=P0^7;
return(flag);
}
void wait(unsigned int w)
{ int time;
for(time=0;time <w;time++)
return;
}
void w_dat(unsigned char dat) //写数据//
{if(busy(flag )==0)
P0=dat;
rs=1;
rw=0;
e=1;
e=0;
}
void w_com(unsigned char com) //写指令//
{if(busy(flag)==0)
P0=com;
rs=0;
rw=0;
e=1;
e=0;
}
void init(void) {
unsigned char i;
P1=0;
for (i=0;i <2;i++) {
w_com(0x30);
wait(100);
}
w_com(0x38);
w_com(0x0f);
w_com(0x18);
w_com(0x06);
w_com(0x01);
w_com(0x80);
}
sbit rs=P1^5; //指令和数据寄存器//
sbit rw=P1^6; //读写控制//
sbit e=P1^7; //片选//
sbit flag=P0^7;
unsigned char busy(int); //判忙//
void w_dat(unsigned char); //写显示数据子程序//
void w_com(unsigned char); //写指令代码子程序//
void init(void); //初始化子程序//
void disp(unsigned char); //显示//
void wait(unsigned int ); //延时//
void busy()
{
while(1)
{
P0=0xff;
rs=0;
rw=1;
e=1;
if(!flag) break;
e=0;
}
}
void wait(unsigned int w)
{ int time;
for(time=0;time <w;time++);
return;
}
void w_dat(unsigned char dat) //写数据//
{busy( );
P0=dat;
rs=1;
rw=0;
e=1;
e=0;
return;
}
void w_com(unsigned char com) //写指令//
{busy( );
P0=com;
rs=0;
rw=0;
e=1;
e=0;
}
void init(void) {
unsigned char i;
P1=0;
for (i=0;i <2;i++) {
w_com(0x30);
wait(100);
}
w_com(0x38);
w_com(0x0f);
w_com(0x18);
w_com(0x06);
w_com(0x01);
w_com(0x80);
}
main( )
{
init( );
w_com(0x01);
w_com(0x80);
w_dat('f');
w_dat('e');
w_com(0xc0);
w_dat('e');
}
/*rs---------P0.0 经锁存器后
rw---------P0.1
E----------RD和WR与非后,再和P2.6与非后再反相
*/
#define LCDCOM XBYTE[0x4000] //命令寄存器地址cs=1,rs=0,rw=0
#define LCDDATA XBYTE[0x4001] //数据寄存器地址cs=1,rs=1,rw=0
#define LCDREAD XBYTE[0x4002] //读取数据 cs=1,rs=0,rw=1
byte bdata busy;
sbit busy_flag=busy^7; //忙标志
//测试lcd忙
void wait_lcd(void)
{ do
{ busy=LCDREAD;
}
while(busy_flag==1);
}
//送命令字
void I_SEND(byte y)
{
wait_lcd();
LCDCOM=y;
}
//送数据
void D_SEND(byte x)
{ wait_lcd();
LCDDATA=x;
}
//LCD初始化
void start_lcd()
{ delay_ms(50);
I_SEND(0x01); //清屏
I_SEND(0x38);
delay_ms(5);
I_SEND(0x38);
delay_ms(100);
I_SEND(0x38);
delay_100us(1);
I_SEND(0x38);
I_SEND(0x0C);
delay_ms(2);
I_SEND(0x06);
}