代码:
#include <reg51.h>
#include <intrins.h>
sbit RS = P3^0;
sbit RW = P3^1;
sbit E = P3^2;
void check_busy();
void write_command(unsigned char command);
void write_data(unsigned char dat);
void lcd_init();
void show_string(unsigned char ad, unsigned char *str);
void delay(unsigned int n);
void main(){
lcd_init();
while(1){
show_string(0x85, "Welcome"); // 第一个参数是写入的位置,第二个是待写入的字符串
show_string(0xC2, "Harbin");
delay(100);
write_command(0x01);
delay(100);
}
}
void check_busy(){
unsigned char dat;
do{
dat = 0xff;
E = 0;
RS = 0;
RW = 1;
E = 1;
dat = P2;
}while(dat & 0x80);
E = 0;
}
void write_command(unsigned char command){
check_busy();
E = 0;
RS = 0;
RW = 0;
P2 = command;
E = 1;
_nop_();
E = 0;
delay(1);
}
void write_data(unsigned char dat){
check_busy();
E = 0;
RS = 1;
RW = 0;
P2 = dat;
E = 1;
_nop_();
E = 0;
delay(1);
}
void lcd_init(){
write_command(0x38);
write_command(0x0c);
write_command(0x60);
write_command(0x01);
}
void show_string(unsigned char ad, unsigned char *str){
write_command(ad);
while (*str != '\0'){
write_data(*str++);
delay(50000);
}
}
void delay(unsigned int n){ // 延时 n ms
while(n--);
}