51单片机实验17:LCD1602液晶

开发板液晶模块电路图如下:

51单片机实验17:LCD1602液晶_第1张图片

#include
#include

uc dig1[]=" Happy New Year ";
uc dig2[]=" Ha  Ha  Ha  Ha ";

void main()
{
	uc i;
	lcdinit();
	lcdwritecom(0x80);
	for(i=1;i<=16;i++)
	{
		lcdwritedata(dig1[i-1]);
	}
    lcdwritecom(0xc0);
	for(i=1;i<=16;i++)
	{
		lcdwritedata(dig2[i-1]);
	}
	while(1);
}
#include

void delay()   //延时500微秒
{
    uc a,b;
    for(b=99;b>0;b--)
        for(a=1;a>0;a--);
}

#ifndef lcd4pins
void lcdwritecom(uc com)
{
	lcd_e=0;
	lcd_rs=0;
	lcd_rw=0;
	lcd_data=com;
	delay();
	lcd_e=1;
	delay();
	lcd_e=0;
}
#else
void lcdwritecom(uc com)
{
	lcd_e=0;
	lcd_rs=0;
	lcd_rw=0;
	lcd_data=com;
	delay();
	lcd_e=1;
	delay();
	lcd_e=0;
	lcd_data=com<<4;
	delay();
	lcd_e=1;
	delay();
	lcd_e=0;
}
#endif

#ifndef lcd4pins
void lcdwritedata(uc dat)
{
	lcd_e=0;
	lcd_rs=1;
	lcd_rw=0;
	lcd_data=dat;
	delay();
	lcd_e=1;
	delay();
	lcd_e=0;
}
#else
void lcdwritedata(uc dat)
{
	lcd_e=0;
	lcd_rs=1;
	lcd_rw=0;
	lcd_data=dat;
	delay();
	lcd_e=1;
	delay();
	lcd_e=0;
	lcd_data=dat<<4;
	delay();
	lcd_e=1;
	delay();
	lcd_e=0;
}
#endif

#ifndef lcd4pins
void lcdinit()
{
	lcdwritecom(0x01);
	lcdwritecom(0x06);
	lcdwritecom(0x0c);
	lcdwritecom(0x38);
}
#else
void lcdinit()
{
	lcdwritecom(0x32);
	lcdwritecom(0x28);
	lcdwritecom(0x01);
	lcdwritecom(0x06);
	lcdwritecom(0x0c);
}
#endif
#ifndef _LCD_H_
#define _LCD_H_

#include

#ifndef uc
#define uc unsigned char
#endif
#ifndef ui
#define ui unsigned int
#endif

//#define lcd4pins   //4PIN输入时打开这个

#define lcd_data P0
sbit lcd_e=P2^7;
sbit lcd_rw=P2^5;
sbit lcd_rs=P2^6;

void delay();
void lcdwritecom(uc com);
void lcdwritedata(uc dat);
void lcdinit();


#endif

 

你可能感兴趣的:(51单片机,进阶之路——单片机的学习)