/********************** 8 bit LCD1602 Test ***********************
//Connects
//1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//vss vcc vee rs rw e d0 d1 d2 d3 d4 d5 d6 d7 led+ led-
//gnd 5v gnd p10 p11 p12 p00 p01 p02 p03 p04 p05 p06 p07 5v gnd
*******************************************************************/
#include "reg51.h"
sbit LCD_RS=P1^0;
sbit LCD_RW=P1^1;
sbit LCD_E=P1^2;
#define LCD_Data P0
#define Busy 0x80
/********Êý¾Ý¶¨Òå******************************************************/
//unsigned char code user_string[] = {"Happy every day"};
//unsigned char code net[] = {"www.taobao.com"};
/********º¯ÊýÉùÃ÷*******************************************************/
void WriteDataLCD(unsigned char WDLCD);
void WriteCommandLCD(unsigned char WCLCD,BuysC);
unsigned char ReadDataLCD(void);
unsigned char ReadStatusLCD(void);
void LCDInit(void);
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);
void Delay5Ms(void);
void Delay400Ms(void);
/*********** Display Text **********************************************/
void main(void)
{
Delay400Ms();
LCDInit();
Delay5Ms();
DisplayListChar(0, 1, "Line One"); // was (4, 0, "TaoBaoID") (Col, Row ??)
DisplayListChar(0, 2, "Line Two"); // was (4, 1 Both items print on same row ????
ReadDataLCD();
}
/***********дÊý¾Ý****************************************************/
void WriteDataLCD(unsigned char WDLCD)
{
ReadStatusLCD(); //
LCD_Data = WDLCD;
LCD_RS = 1;
LCD_RW = 0;
LCD_E = 0; //
LCD_E = 0; //
LCD_E = 1;
}
/*********** Write LCD ***************************************************/
void WriteCommandLCD(unsigned char WCLCD,BuysC)
{
if (BuysC) ReadStatusLCD();
LCD_Data = WCLCD;
LCD_RS = 0;
LCD_RW = 0;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
}
/*********** Read LCD ****************************************************/
unsigned char ReadDataLCD(void)
{
LCD_RS = 1;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
return(LCD_Data);
}
/*********** Status *******************************************************/
unsigned char ReadStatusLCD(void)
{
LCD_Data = 0xFF;
LCD_RS = 0;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
while (LCD_Data & Busy); //¼ì²âæÐźÅ
return(LCD_Data);
}
/*********** Initialize LCD ***************************************************/
void LCDInit(void)
{
LCD_Data = 0;
WriteCommandLCD(0x38,0); //
Delay5Ms();
WriteCommandLCD(0x38,0);
Delay5Ms();
WriteCommandLCD(0x38,0);
Delay5Ms();
WriteCommandLCD(0x38,1); //
WriteCommandLCD(0x08,1); //¹Ø±ÕÏÔʾ
WriteCommandLCD(0x01,1); //ÏÔʾÇåÆÁ
WriteCommandLCD(0x06,1); //ÏÔʾ¹â±êÒƶ¯ÉèÖÃ
WriteCommandLCD(0x0C,1); //ÏÔʾ¿ª¼°¹â±êÉèÖÃ
}
/***********°´Ö¸¶¨Î»ÖÃÏÔʾһ¸ö×Ö·û*******************************************/
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //
if (Y) X |= 0x40; //
X |= 0x80; //
WriteCommandLCD(X, 0); //
WriteDataLCD(DData);
}
/***********°´Ö¸¶¨Î»ÖÃÏÔʾһ´®×Ö·û*****************************************/
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF;
while (DData[ListLength]>=0x20){ //
if (X <= 0xF){ //
DisplayOneChar(X, Y, DData[ListLength]);
ListLength++;
X++;
}
}
}
/***********¶ÌÑÓʱ********************************************************/
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
/***********³¤ÑÓʱ********************************************************/
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--){
TempCycB=7269;
while(TempCycB--);
}
}