1602 LCD Row & Col Question, 8051 LCD Demo test in C.

Thread Starter

Ashton

Joined Jun 10, 2013
3
I'm trying to determine what's wrong with a simple demo C code to display two lines of text on a 1602 LCD display.

Whatever (Col, Row) I select everything stays on Row 1 or 2. See section 'Display Text'.

This is the ONLY project I can get to compile/build in Keil successfully without errors. Everything else I have using LCD.h has countless errors, and produces no output files. (strange characters? is STC 8051 dev board examples).

Code below:
Code:
/********************** 8 bit    LCD1602 ***************************************
//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[] = {"[URL='http://www.taobao.com']www.taobao.com[/URL]"};

/********º¯ÊýÉùÃ÷*************************************************************/
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(1, 0, "Line One");  // was (4, 0, "TaoBaoID"); (Row,Col ??)
    DisplayListChar(1, 1, "Line Two");  // was (4, 1  "Line two"); 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);
}

/*********** Iterate through each X char of Y row *****************************************/ 
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--);
    }
}
Any advice appreciated.
Please try and be specific on recommended code changes.

Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,377
Toss your DisplayOneChar() routine and WriteCommandLCD() directly until you understand how the addressing works.
 
Top