LCD1602为5*8的黑白屏。
#include <reg52.h>
#define LCDPORT P0
#define LCD_WRITE_COM 0
#define LCD_WRITE_DATA 1
sbit RS = P2^4;
sbit RW = P2^5;
sbit E = P2^6;
//写入数据字符
void lcd_write(unsigned char byte, unsigned char flag)
{
if(flag)
{
RS = LCD_WRITE_DATA; //选择输入数据为数据
}
else
{
RS = LCD_WRITE_COM;
}
RW = 0; //写入数据
E = 1; //选中LCD
LCDPORT = byte;
delay_us(5);
E = 0; //失能LCD
}
//在屏上输出字符
void lcd_print(unsigned char x, unsigned char y, unsigned char byte)
{
if(x == 1)
{
lcd_write(0x80+y-1,LCD_WRITE_COM);
lcd_write(byte,LCD_WRITE_DATA);
}
else
{
lcd_write(0xc0+y-1,LCD_WRITE_COM);
lcd_write(byte,LCD_WRITE_DATA);
}
}
//输出字符串
void lcd_string(unsigned char x, unsigned char y, unsigned char *s)
{
while(*s != '\0')
{
lcd_print(x,y++,*s);
s++;
}
}
//初始化lcd
void lcd_init()
{
delay_ms(15);
lcd_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行显示;5*7点阵字符
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行显示;5*7点阵字符
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行显示;5*7点阵字符
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行显示;5*7点阵字符
delay_ms(5);
lcd_write(0x08,LCD_WRITE_COM); //关闭显示,关闭光标和闪烁
delay_ms(5);
lcd_write(0x01,LCD_WRITE_COM); //清屏
delay_ms(5);
lcd_write(0x06,LCD_WRITE_COM); //设置指针方式,画面不平移
delay_ms(5);
lcd_write(0x0c,LCD_WRITE_COM); //打开显示
delay_ms(5);
}
void main()
{
lcd_init();
lcd_string(1,4,"hello");
while(1);
}