#include
/**************************************************/
sbit RS=P2^4;
sbit RW=P2^3;
sbit E=P2^2;
sbit P36=P3^6;
sbit P20=P2^0;
sbit P21=P2^1;
unsigned char n,k,h,l,m;
unsigned char d[14]={'Z','H', 'I','X','I','N','G','X','U','E','Y','U','A','N'}; //第二行要输入的字符
/****************************延时 z ms***************************/
void delay(unsigned int z) //延时 z ms
{
unsigned int i;
unsigned char j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
/****************************写命令******************************/
void zhiling(unsigned char a) //写命令
{
E=0;RS=0;RW=0;
E=1;
P1=a;
delay(5); //延时 5ms
E=0;
delay(5); //延时 5ms
}
/*****************************写数据*****************************/
void shuju(unsigned char b) //写数据
{
E=0;RS=1;RW=0;
E=1;
P1=b;
delay(5); //延时 5ms
E=0;
delay(5); //延时 5ms
}
/****************************初始化******************************/
void init() //初始化
{
zhiling(0x01); //清屏
zhiling(0x30); //功能设置
zhiling(0x0e); //开/关显示设置
zhiling(0x06); //输入方式设置
zhiling(0x14); //光标右移
}
/*****************************主程序*****************************/
void main()
{
unsigned char i;
delay(500);
init();
zhiling(0x80); //第一行
for(i=0;i<14;i++)
shuju(d[i]);
zhiling(0x9f);
shuju('!');
while(1)
{
zhiling(0x87);
}
}
/****************************************************************/
/****************************************************************/
/******************************************************************************
LCD1602显示程序,调用函数为:lcd_display(uchar *lp,uchar address);仅第一次调用时
初始化lcd。
******************************************************************************/
#include
#include
#define lcd_data P2 //数据端口
#define busy 0x80 //用于检测LCD状态字中的Busy标识
#define uchar unsigned char
#define uint unsigned int
sbit lcd_RS=P1^0; //数据/命令端
sbit lcd_RW=P1^1; //读/写端
sbit lcd_EN=P1^2; //全使能端
uchar flag=1;
void delay_ms(uint ms);
uchar lcd_testbusy();
void lcd_write_data(uchar dat,uchar Busy);
void lcd_write_com(uchar com,uchar Busy);
void lcd_init();
void lcd_display(uchar *lp,uchar address);
void lcd_display(uchar *lp,uchar address)
{
uchar i,j;
j=strlen(lp);
if(flag)
lcd_init(); //初始化lcd
lcd_write_com(0x80+address,1); //初始化写数据指针
for(i=0;i
lcd_write_data(lp[i],1); //写数据
}
flag=0;
}
uchar lcd_testbusy()
{
lcd_data=0xff; //读入前先写1
lcd_RS=0;
lcd_RW=1;
lcd_EN=0;
delay_ms(1);
lcd_EN=1;
delay_ms(1);
while(lcd_data & busy); //等待lcd空闲
return(1);
}
void lcd_write_data(uchar dat,uchar Busy)
{
if(Busy) //判断是否需要检测忙
lcd_testbusy();
lcd_RW=0;
lcd_RS=1;
lcd_data=dat;
delay_ms(2);
lcd_EN=1;
delay_ms(5);
lcd_EN=0;
}
void lcd_write_com(uchar com,uchar Busy)
{
if(Busy) //判断是否需要检测忙
lcd_testbusy();
lcd_RW=0;
lcd_RS=0;
lcd_data=com;
delay_ms(2);
lcd_EN=1;
delay_ms(5);
lcd_EN=0;
}
void lcd_init()
{
delay_ms(20); //等待启动
lcd_write_com(0x38,0); //显示模式设置,不检测忙
delay_ms(5);
lcd_write_com(0x38,0); //显示模式设置,不检测忙
delay_ms(5);
lcd_write_com(0x38,0); //显示模式设置,不检测忙
lcd_write_com(0x38,1); //显示模式设置,检测忙
lcd_write_com(0x08,1); //关显示
lcd_write_com(0x01,1); //清屏
lcd_write_com(0x06,1); //显示光标移动设置
lcd_write_com(0x0c,1); //显示光标设置
}
void delay_ms(uint ms)
{
uint i,j;
for(i=0;i
}