#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define out P0
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit S1=P2^3;
sbit S2=P2^4;
sbit S3=P2^5;
sbit S4=P2^6;
sbit S5=P2^7;
unsigned char keyval;
void forward();
void backward();
void ALter();
void blink();
void key_scan();
void led_delay();
void delay10ms();
void check_busy(void);
void write_command(uchar com);
void write_data(uchar dat);
void string(uchar ad ,uchar *s);
void lcd_test(void);
void LCD_initial( );
void delay(uint);
void main(void)
{
LCD_initial( );
while(1)
{
key_scan();
switch(keyval)
{
case 1:forward();
break;
case 2:backward();
break;
case 3:ALter();
break;
case 4:blink();
break;
}
string(0x85,"I LOVE YOU!");
string(0xC2,"Fan Li Sheng");
delay(100);
write_command(0x01);
delay(100);
}
}
void delay(uint j)
{
uchar i=250;
for(;j>0;j--)
{
while(--i);
i=249;
while(--i);
i=250;
}
}
void check_busy(void)
{
uchar dt;
do
{
dt=0xff;
E=0;
RS=0;
RW=1;
E=1;
dt=out;
}while(dt&0x80);
E=0;
}
void write_command(uchar com)
{
check_busy();
E=0;
RS=0;
RW=0;
out=com;
E=1;
_nop_( );
E=0;
delay(1);
}
void write_data(uchar dat)
{
check_busy();
E=0;
RS=1;
RW=0;
out=dat;
E=1;
_nop_();
E=0;
delay(1);
}
void LCD_initial(void)
{
write_command(0x38);
write_command(0x0C);
write_command(0x06);
write_command(0x01);
delay(1);
}
void string(uchar ad,uchar *s)
{
write_command(ad);
while(*s>0)
{
write_data(*s++);
delay(100);
}
}
void key_scan(void)
{
P2=0xff;
if((P2&0xf8)!=0xf8)
{
delay10ms();
if(S1==0)
keyval=1;
if(S2==0)
keyval=2;
if(S3==0)
keyval=3;
if(S4==0)
keyval=4;
if(S5==0)
keyval=5;
}
}
void forward(void)
{
P3=0xfb;
led_delay();
P3=0xf7;
led_delay();
P3=0xef;
led_delay();
P3=0xdf;
led_delay();
P3=0xbf;
led_delay();
P3=0x7f;
led_delay();
}
void backward(void)
{
P3=0x7f;
led_delay();
P3=0xbf;
led_delay();
P3=0xdf;
led_delay();
P3=0xef;
led_delay();
P3=0xf7;
led_delay();
P3=0xfb;
led_delay();
}
void Alter(void)
{
P3=0x1f;
led_delay();
P3=0xe3;
led_delay();
}
void blink(void)
{
P3=0xff;
led_delay();
P3=0x00;
led_delay();
}
void led_delay(void)
{
unsigned char i,j;
for(i=0;i<220;i++)
for(j=0;j<220;j++);
}
void delay10ms(void)
{
unsigned char i,j;
for(i=0;i<100;i++)
for(j=0;j<100;j++);
} |