|
#include "1602.h"
#include "delay.h"
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define _NOP() _nop_()
sbit RS = P2^4;
sbit RW = P2^5;
sbit EN = P2^6;
#define DataPort P0
#define DataPIN P0
#define CLR_RS (RS=0)
#define SET_RS (RS=1)
#define CLR_RW (RW=0)
#define SET_RW (RW=1)
#define CLR_EN (EN=0)
#define SET_EN (EN=1)
/*
LcdReset();
DelayMs(10);
sprintf(temp,"1111111111111111");//???????
DispStr(0,0,(unsigned char *)temp);//??????
sprintf(temp,"1111111111111111");//???????
DispStr(0,1,(unsigned char *)temp);//??????
*/
/***********************************************
?????????DispStr
?? ?????????????λ???????????????????
?? ????x--λ?????????
y--λ?????????
ptr--???????????λ??????
????? ????
***********************************************/
void DispStr(uchar x,uchar y,uchar *ptr)
{
uchar *temp;
uchar i,n = 0;
temp = ptr;
while(*ptr++ != '\0') n++; //???????????Ч????????
for (i=0;i<n;i++)
{
Disp1Char(x++,y,temp);
if (x == 0x10)
{
break;
}
}
}
/*******************************************
?????????DispNchar
?? ?????????????λ???????????N?????
?? ????x--λ?????????
y--λ?????????
n--???????
ptr--?????????λ??????
????? ????
*******************************************
void DispNChar(uchar x,uchar y, uchar n,uchar *ptr)
{
uchar i;
for (i=0;i<n;i++)
{
Disp1Char(x++,y,ptr);
if (x == 0x10)
{
x = 0;
y ^= 1;
}
}
}
*/
/*******************************************
?????????LocateXY
?? ??????????????????λ??????????
?? ????x--λ?????????
y--λ?????????
????? ????
********************************************/
void LocateXY(uchar x,uchar y)
{
uchar temp;
temp = x&0x0f;
y &= 0x01;
if(y) temp |= 0x40; //??????2??
temp |= 0x80;
LcdWriteCommand(temp,1);
}
/*******************************************
?????????Disp1Char
?? ????????λ???????????
?? ????x--λ?????????
y--λ?????????
data--????????????
????? ????
********************************************/
void Disp1Char(uchar x,uchar y,uchar data1)
{
LocateXY( x, y );
LcdWriteData( data1 );
}
/*******************************************
?????????LcdReset
?? ?????1602????????и?λ????
?? ??????
????? ????
********************************************/
void LcdReset(void)
{
// DataDir = 0xFF; //??????????????
LcdWriteCommand(0x38, 0); //?涨???λ????
DelayMs(5);
LcdWriteCommand(0x38, 0);
DelayMs(5);
LcdWriteCommand(0x38, 0);
DelayMs(5);
LcdWriteCommand(0x38, 1); //?????????
LcdWriteCommand(0x08, 1); //??????
LcdWriteCommand(0x01, 1); //???????
LcdWriteCommand(0x06, 1); //д???????岻???
LcdWriteCommand(0x0c, 1); //????????????α??????
}
/*------------------------------------------------
????????
------------------------------------------------*/
void LcdClear(void)
{
LcdWriteCommand(0x01,1);
DelayMs(5);
}
/*******************************************
?????????LcdWriteCommand
?? ???????????д??????
?? ????cmd--????
chk--????????????1???????0??????
????? ????
********************************************/
void LcdWriteCommand(uchar cmd,uchar chk)
{
if (chk) WaitForEnable(); // ????????
CLR_RS;
CLR_RW;
_NOP();
DataPort = cmd; //????????д????????
_NOP();
SET_EN; //??????????????
_NOP();
_NOP();
CLR_EN;
}
/*******************************************
?????????LcdWriteData
?? ??????????????????д?????????
?? ????data--??????????
????? ????
********************************************/
void LcdWriteData( uchar data1 )
{
WaitForEnable(); //?????????
SET_RS;
CLR_RW;
SET_EN;
_NOP();
DataPort = data1; //?????????д????????
_NOP();
//??????????????
_NOP();
_NOP();
CLR_EN;
}
/*******************************************
?????????WaitForEnable
?? ??????1602?????????????
?? ??????
????? ????
********************************************/
void WaitForEnable(void)
{
unsigned int later=0;
DataPort=0xff;
CLR_RS;
SET_RW;
_NOP();
SET_EN;
_NOP();
_NOP();
// while((DataPIN&Busy)!=0);
while(((DataPIN&0x80)!=0)&&(later<1000))
{
DelayUs2x(2);
later++;
}
CLR_EN;
}
|
|