http://hi.baidu.com/haihuawu/blog/item/f990493968e0212a97ddd836.html
/************************************************************
作者:吴海华
1602显示头文件
************************************************************/
/************************************************************
说明:
还需要有51_delay.h头文件
一共有_start_1602()、_display_1602_single(unsigned char address,unsigned char world)、_display_1602(void)三个接口函数
显示前应先用_start_1602()初始化1602模块
_display_1602_single(unsigned char address,unsigned char world)有两个参数,一次只能显示一个字符
address为地址,第一行从0x00起,第二行从0x40起
world为字符,可以是变量,也可以是常量
_display_1602()一次显示16*2个字符,字符在char a[2][16]中修改,为常量。内存使用较大,建议不使用此函数。
************************************************************/
#define __en_display_1602__ //控制,如未用到_display_1602()函数则删除此句
#define __en_display_1602_single__ //控制,如未用到_display_1602_single()函数则删除此句
#ifndef __51_1602_h__
#define __51_1602_h__
#ifndef __51_reaty_h__
#include<51_delay.h>
#endif
#ifndef __intrins_h__
#include
#endif
/************************************************************************
此处定义io口
************************************************************************/
#define io P1
sbit rs=P2^0;
sbit rw=P2^1;
sbit e=P2^2;
sbit bf=P1^7;//就是D7,检测忙标志用的
void testbusy(void);
void writecommand(unsigned char command);
void writedata(unsigned char wdata);
void _start_1602(void);//接口函数,初始化液晶
void _display_1602_single(unsigned char address,unsigned char world);//接口函数,显示单个字符
void _display_1602(void);//接口函数,显示全部字符
void _start_1602(void)//初始化1602函数
{
_delay_ms(15);
writecommand(0x38);
_delay_ms(5);
writecommand(0x38);
_delay_ms(5);
writecommand(0x38);
testbusy();
writecommand(0x38);
testbusy();
writecommand(0x08);
testbusy();
writecommand(0x01);
testbusy();
writecommand(0x06);
testbusy();
writecommand(0x0c);
}
#ifdef __en_display_1602_single__
void _display_1602_single(unsigned char address,unsigned char word)//显示单个字符
{
address = address | 0x80;
testbusy();
writecommand(address);
testbusy();
writedata(word);
}
#endif
#ifdef __en_display_1602__
void _display_1602(void)
{
unsigned char i=0;
char a[2][16] =
{{'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'},
{'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'}};
testbusy();
writecommand(0x80);
while(i<=16)
{
testbusy();
writedata(a[0][i]);
i++;
}
testbusy();
writecommand(0xc0);
i = 0;
while(i<=16)
{
testbusy();
writedata(a[1][i]);
i++;
}
}
#endif
void testbusy(void)//检测忙的函数
{
loop_busy:
e = 1;
rs = 0;
rw = 1;
io = 0xff;
_nop_();
_nop_();
e = 0;
_nop_();
_nop_();
e = 1;
if(bf) goto loop_busy;
}
void writecommand(unsigned char command)//写指令的函数
{
rs = 0;
rw = 0;
e = 1;
io = command;
_nop_();
_nop_();
e = 0;
_nop_();
_nop_();
e = 1;
}
void writedata(unsigned char wdata)//写数据的函数
{
rs = 1;
rw = 0;
e = 1;
io = wdata;
_nop_();
_nop_();
e = 0;
_nop_();
_nop_();
e = 1;
}
#endif