lcd.c
#include "lcd.h"
#include "reg52.h"void lcd1602_write_cmd(uchar cmd)
{LCD1602_RS=0; //写命令LCD1602_RW=0;//写LCD1602_E=0;LCD1602_DATEPORT=cmd;delay_ms(1);//1msLCD1602_E=1;delay_ms(1);LCD1602_E=0;
}
void lcd1602_write_data(uchar dat)
{LCD1602_RS=1; //写数据LCD1602_RW=0;//写LCD1602_E=0;LCD1602_DATEPORT=dat;delay_ms(1);//1msLCD1602_E=1;delay_ms(1);LCD1602_E=0;
}
void lcd1602_init(void)
{lcd1602_write_cmd(0x38);lcd1602_write_cmd(0x0c);lcd1602_write_cmd(0x06);//模式lcd1602_write_cmd(0x01);
}
void lcd1602_clear(void)
{lcd1602_write_cmd(0x01);
}
//x:0-15 y:0-1
void lcd1602_show_string(uchar x,uchar y,uchar *str)//lcd1602_show_string(0,0,"123456789")
{uchar i=0;if((y>1)|(x>15))return;if(y<1) //从第一行开始显示{while(*str!='\0'){if(i<16-x)lcd1602_write_cmd(0x80+x+i);//第一行else lcd1602_write_cmd(0x80+0x40+x+i-16);//第二行lcd1602_write_data(*str);str++;i++;}}else //从第二行开始显示{while(*str!='\0'){if(i<16-x)lcd1602_write_cmd(0x80+0x40+x+i);//第一行else lcd1602_write_cmd(0x80+x+i-16);//第二行lcd1602_write_data(*str);str++;i++;}}
}
void lcd1602_show_data(uchar x,uchar y,uchar dat)//lcd1602_show_data(0,0,1);dat+0x30
{if((y>1)|(x>15))return;if(y<1)//第一行{lcd1602_write_cmd(0x80+x);}else //第二行{lcd1602_write_cmd(0x80+0x40+x);}lcd1602_write_data(dat+0x30);
}
lcd.h
#ifndef __lcd_H_
#ifndef __lcd_H_#include "reg52.h"#define uchar unsigned char
#define uint unsigned int#define LCD1602_DATEPORT P2sbit LCD1602_RS=P0^7;
sbit LCD1602_RW=P0^6;
sbit LCD1602_E=P0^5;void delay_ms(uint ms);
void lcd1602_write_cmd(uchar cmd);
void lcd1602_write_data(uchar dat);
void lcd1602_init(void);
void lcd1602_clear(void);
void lcd1602_show_string(uchar x,uchar y,uchar *str);
void lcd1602_show_data(uchar x,uchar y,uchar dat);#endif