LCD1602驱动根据数据手册编写
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar num[] = "0123456789";
sbit RS = P1^7;
sbit RW = P1^6;
sbit E = P1^5;
void delay(uint x)
{
while(x --);
}
void write_command(uchar command)
{
RW = 0;
RS = 0;
P0 = command;
E = 1;
delay(100);
E = 0;
RW = 1;
}
void write_dat(uchar dat)
{
RW = 0;
RS = 1;
P0 = dat;
E = 1;
delay(100);
E = 0;
RW = 1;
}
void display_string(char *p)
{
while(*p)
{
write_dat(*p);
p++;
}
}
void display_num(uchar x)
{
if(x/10==0)
{
write_dat(num[x]);
}
if(x/100==0)
{
write_dat(num[x / 10]);
write_dat(num[x % 10]);
}
if(x/1000==0)
{
write_dat(num[x / 100]);
write_dat(num[x / 10 % 10]);
write_dat(num[x % 10]);
}
}
void goto_row_column(uchar row, uchar column)
{
if(row == 1)
{
write_command(0x80 + column);
delay(100);
}
else if(row == 2)
write_command( 0xc0 + column);
}
void FL1602_init(void)
{
write_command(0x38);
write_command(0x0f);
write_command(0x06);
}
void FL1602_clear(void)
{
write_command(0x01);
write_command(0x02);
}