|
用P2口作读写数据口,P3.0--P3.2连接RS,RW,E.未使用查忙,而使用延时足够长的时间来满足写指令和数据.
#i nclude <reg52.h>
#i nclude "intrins.h"
#define nop _nop_()
#define PtData P2
#define uint unsigned int
#define uchar unsigned char
sbit RS=P3^0;
sbit RW=P3^1;
sbit E=P3^2;
void WriteInstr(uint);
void WriteData(uint);
void Delay(uint);
main()
{
nop;
P0=0;//调试时候用于观察,P0口共阳极LED亮
WriteInstr(0x01);//初始化
WriteInstr(0x02);
WriteInstr(0x06);
WriteInstr(0x0c);
WriteInstr(0x38);
WriteInstr(0x01);
Delay(30000);
P0^=0x01;//调试时候用于观察,P0.0口LED状态取反
while(1)
{
WriteInstr(0x81);//写第一行DDRAM
WriteData(0x65);
WriteData(0x6c);
WriteData(0x65);
WriteData(0x63);
WriteData(0x6c);
WriteData(0x69);
WriteData(0x6b);
WriteData(0x65);
WriteInstr(0xc1);//写第二行DDRAM
WriteData(0x31);
WriteData(0x36);
WriteData(0x30);
WriteData(0x32);
Delay(10000);
P0^=0x02;//调试时候用于观察一次循环,P0.1口LED状态取反
}
}
//---写指令
void WriteInstr(uint Temp)
{
Delay(10);//晶振为6M时,延时0.272ms
RS=0;
RW=0;
E=0;
nop;
nop;
nop;
E=1;
nop;
PtData=Temp;
nop;
E=0;
}
//---写数据
void WriteData(uint Temp)
{
Delay(10);
RS=1;
RW=0;
E=0;
nop;
nop;
nop;
E=1;
nop;
PtData=Temp;
nop;
E=0;
}
//---延时
void Delay(uint N)
{uint i;
for(i=0;i<N;i++){}
}
|
|