#include <reg52.h>
#include<intrins.h>
typedef unsigned char uchar;
typedef unsigned int uint;
sbit RS = P3^5;
sbit RW = P3^6;
sbit EN = P3^4;
#define LCD1602_A P0
void delay_ms(uint n)
{
uint i,y;
for(i=n;i>0;i--)
{
for(y=114;y>0;y--);
}
}
// º¯ÊýÃû£ºÅÐ溯Êý
// º¯Êý¹¦ÄÜ£ºÅжÏÒº¾§ÊÇ·ñ·±Ã¦
void Read_busy()
{
uchar busy;
LCD1602_A = 0xff;
RS = 0;
RW = 1;
do{
EN = 1;
busy = LCD1602_A;
EN = 0;//ÀµÍʹÄÜ¶Ë ±ãÓÚÏ´ÎʹÓÃ
}while(busy & 0x80);//²»ÎªÁã¾ÍÒ»Ö±Ö´ÐÐ
}
//º¯ÊýÃû³Æ£ºÐ´ÃüÁî
//º¯Êý¹¦ÄÜ£ºÏòLCDÖÐдÈëÃüÁî
void Write_cmd(uchar cmd)
{
Read_busy();
RS = 0;
RW = 0;
LCD1602_A = cmd;
EN = 1;
EN = 0;
}
//º¯ÊýÃû³Æ£ºÐ´Êý¾Ý
//º¯Êý¹¦ÄÜ£ºÏòLCDÖÐдÈëÊý¾Ý
void Write_dat(uchar dat)
{
Read_busy();
RS = 1;
RW = 0;
LCD1602_A = dat;
EN = 1;
EN = 0;
}
//º¯ÊýÃû³Æ£ºÏÔʾһ¸ö×Ö·û
//º¯Êý¹¦ÄÜ£ºÔÚÖ¸¶¨µÄλÖÃÏÔʾһ¸ö×Ö·û
void Dis_onechar(uchar x, uchar y, uchar dat)
{
if(y) x |= 0x40;
x |= 0x80;
Write_cmd(x);
Write_dat(dat);
}
//º¯ÊýÃû³Æ£ºÏÔʾһ¸ö×Ö·û´®
//º¯Êý¹¦ÄÜ£ºÁ¬ÐøµÄÏÔʾ×Ö·û
void Dis_str(uchar x, uchar y, uchar *str)//strÊÇÖ¸Õë±äÁ¿(µØÖ·)*strÊÇÖ¸Õë»òµØÖ·ÀïµÄÄÚÈÝ
{
if(y) x |= 0x40;
x |= 0x80;
Write_cmd(x);
while(*str != '\0')
{
Write_dat(*str++);//Ö¸ÏòÏÂÒ»¸öµØÖ· µÄÄÚÈÝ
}
}
//º¯ÊýÃû³Æ£ºLCD³õʼ»¯
//º¯Êý¹¦ÄÜ£º³õʼ»¯LCD
void Init_LCD1602()
{
Write_cmd(0x38);//ÉèÖÃÏÔʾģʽ
Write_cmd(0x0c);// ¿ªÏÔʾ ÏÔʾ¹â±ê ¹â±ê²»ÉÁ
Write_cmd(0x06);//µØÖ·Ö¸Õë¼ÓÒ» ¹â±ê¼ÓÒ»
Write_cmd(0x01);//ÇåÆÁ
}
void main()
{
uchar i = 1;
uchar *p;
p = "Just do it ! ";
Init_LCD1602();
while(*p)
{
Dis_str(i,0,*p);
i++;
P++;
delay_ms(1000);
}
}
|