#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//ÕâÈý¸öÒý½Å²Î¿¼×ÊÁÏ
sbit E=P3^4;//1602ʹÄÜÒý½Å
sbit RW=P3^6; //1602¶ÁдÒý½Å
sbit RS=P3^5; //1602Êý¾Ý/ÃüÁîÑ¡ÔñÒý½Å
/********************************************************************
* Ãû³Æ : delay()
* ¹¦ÄÜ : ÑÓʱ,ÑÓʱʱ¼ä´ó¸ÅΪ5US¡£
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
***********************************************************************/
void delay()
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
/********************************************************************
* Ãû³Æ : bit Busy(void)
* ¹¦ÄÜ : Õâ¸öÊÇÒ»¸ö¶Á״̬º¯Êý£¬¶Á³öº¯ÊýÊÇ·ñ´¦ÔÚæ״̬
* ÊäÈë : ÊäÈëµÄÃüÁîÖµ
* Êä³ö : ÎÞ
***********************************************************************/
bit Busy(void)
{
bit busy_flag = 0;
RS = 0;
RW = 1;
E = 1;
delay();
busy_flag = (bit)(P0 & 0x80);
E = 0;
return busy_flag;
}
/********************************************************************
* Ãû³Æ : wcmd(uchar del)
* ¹¦ÄÜ : 1602ÃüÁÊý
* ÊäÈë : ÊäÈëµÄÃüÁîÖµ
* Êä³ö : ÎÞ
***********************************************************************/
void wcmd(uchar del)
{
while(Busy());
RS = 0;
RW = 0;
E = 0;
delay();
P0 = del;
delay();
E = 1;
delay();
E = 0;
}
/********************************************************************
* Ãû³Æ : wdata(uchar del)
* ¹¦ÄÜ : 1602дÊý¾Ýº¯Êý
* ÊäÈë : ÐèҪдÈë1602µÄÊý¾Ý
* Êä³ö : ÎÞ
***********************************************************************/
void wdata(uchar del)
{
while(Busy());
RS = 1;
RW = 0;
E = 0;
delay();
P0 = del;
delay();
E = 1;
delay();
E = 0;
}
/********************************************************************
* Ãû³Æ : L1602_init()
* ¹¦ÄÜ : 1602³õʼ»¯£¬Çë²Î¿¼1602µÄ×ÊÁÏ
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
***********************************************************************/
void L1602_init(void)
{
wcmd(0x38);
wcmd(0x0c);
wcmd(0x06);
wcmd(0x01);
}
/********************************************************************
* Ãû³Æ : L1602_char(uchar hang,uchar lie,char sign)
* ¹¦ÄÜ : ¸Ä±äÒº¾§ÖÐijλµÄÖµ£¬Èç¹ûÒªÈõÚÒ»ÐУ¬µÚÎå¸ö×Ö·ûÏÔʾ"b" £¬µ÷Óøú¯ÊýÈçÏÂ
L1602_char(1,5,'b')
* ÊäÈë : ÐУ¬ÁУ¬ÐèÒªÊäÈë1602µÄÊý¾Ý
* Êä³ö : ÎÞ
***********************************************************************/
void L1602_char(uchar hang,uchar lie,char sign)
{
uchar a;
if(hang == 1) a = 0x80;
if(hang == 2) a = 0xc0;
a = a + lie - 1;
wcmd(a);
wdata(sign);
}
/********************************************************************
* Ãû³Æ : L1602_string(uchar hang,uchar lie,uchar *p)
* ¹¦ÄÜ : ¸Ä±äÒº¾§ÖÐijλµÄÖµ£¬Èç¹ûÒªÈõÚÒ»ÐУ¬µÚÎå¸ö×Ö·û¿ªÊ¼ÏÔʾ"ab cd ef" £¬µ÷Óøú¯ÊýÈçÏÂ
L1602_string(1,5,"ab cd ef;")
* ÊäÈë : ÐУ¬ÁУ¬ÐèÒªÊäÈë1602µÄÊý¾Ý
* Êä³ö : ÎÞ
***********************************************************************/
void L1602_string(uchar hang,uchar lie,uchar *p)
{
uchar a,b=0;
if(hang == 1) a = 0x80;
if(hang == 2) a = 0xc0;
a = a + lie - 1;
while(1)
{
wcmd(a++);
b++;
if((*p == '\0')||(b==16)) break;
wdata(*p);
p++;
}
}
/********************************************************************
* Ãû³Æ : Main()
* ¹¦ÄÜ : Ö÷º¯Êý
* ÊäÈë : ÎÞ
* Êä³ö : ÎÞ
***********************************************************************/
void Main()
{
L1602_init();
L1602_string(1,1," 0123456789 ");
L1602_string(2,1," ABCDEFGHIJ ");
L1602_char(2,15,'O');
L1602_char(2,16,'K');
while(1);
}