|
#include
#define uint unsigned int
#define uchar unsigned char
uchar string[]={"I am pengfa"};
uchar string1[]={"good men"};
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit BF=P0^7;
/*1ms延时函数*/
void delay1ms(void)
{
uchar i,j;
for(i=100;i>0;i--)
for(j=7;j>0;j--);
}
/* 1ms延时函数的调用*/
void delay(uchar i)
{
for(i;i>0;i--)
{
delay1ms();
}
}
/*判断忙信号*/
uchar zero(void)
{
uint i;
bit result;
RS=0;
RW=1;
E=1;
for( i=0;i<2;i++);
result=BF;
return result;
}
/*写命令函数*/
void sendaddress(uchar i)
{
uint j;
while(zero()==1);
RS=0;
RW=0;
E=0;
P0=i;
for( j=0;j<2;j++);
E=1;
for( j=0;j<2;j++);
E=0;
}
/*写数据函数*/
void senddate(uchar i)
{
uint j;
while(zero()==1) ;
RS=1;
RW=0;
E=0;
P0=i;
for( j=0;j<2;j++);
E=1;
for( j=0;j<2;j++);
E=0;
}
/*写地址函数*/
void writeaddress(uchar i)
{
sendaddress(i|0x80) ;
}
/*1602液晶初始状态设置*/
void setup1602(void)
{
delay(20);
sendaddress(0x38);
delay(3);
sendaddress(0x38);
delay(3);
sendaddress(0x0f);
delay(3);
sendaddress(0x06);
delay(3);
sendaddress(0x01);
delay(3);
}
void main()
{
uchar i;
setup1602();
while(1)
{
sendaddress(0x01);
writeaddress(0x00);
i=0;
if(string[i]!='\0')
{
senddate(string[i]);
i++;
}
delay(5);
i=0;
writeaddress(0x40);
if(string1[i]!='\0')
{
senddate(string1[i]);
i++;
}
delay(5000);
}
}
|
|