#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#define LCD1602_DB P0 //LCD1602数据线
sbit LCD1602_RS = P3^5; //LCD1602 RS引脚
sbit LCD1602_RW = P3^6; //LCD1602 RW引脚
sbit LCD1602_EN = P3^4; //LCD1602 EN引脚
void delay(unsigned int t) //延时函数
{
unsigned int i;
while(t--)
{
for(i=0;i<125;i++);
}
}
void lcd1602_write_cmd(unsigned char cmd) //LCD1602写命令
{
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_EN = 1;
_nop_();
_nop_();
_nop_();
LCD1602_EN = 0;
delay(5);
}
void lcd1602_write_data(unsigned char dat) //LCD1602写数据
{
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_EN = 1;
_nop_();
_nop_();
_nop_();
LCD1602_EN = 0;
delay(5);
}
void lcd1602_init() //LCD1602初始化
{
lcd1602_write_cmd(0x38); //设置16*2显示,5*7点阵,8位数据接口
lcd1602_write_cmd(0x38); //设置16*2显示,5*7点阵,8位数据接口
lcd1602_write_cmd(0x0c); //显示开,无光标,不闪烁
lcd1602_write_cmd(0x06); //光标右移,字符不移动
lcd1602_write_cmd(0x01); //清屏
}
void lcd1602_display(unsigned char x, unsigned char y, unsigned char *str) //LCD1602显示字符串
{
unsigned char addr;
if(y == 0)
{
addr = 0x80 + x;
}
else
{
addr = 0xc0 + x;
}
lcd1602_write_cmd(addr);
while(*str != '\0')
{
lcd1602_write_data(*str++);
}
}
void main()
{
unsigned int a=100;
unsigned char str[4];
lcd1602_init();
lcd1602_display(2, 0, "Hello, World!");
lcd1602_display(2, 1, "Good Moring!");
while(1);
}