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

Status
Not open for further replies.

Aaron2

Junior Member level 3
Joined
Nov 20, 2013
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,471
I'm trying to learn what I'm doing wrong with 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. 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 a Chinese STC 8051 dev board & examples).

Code below:
Code:
/********************** 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--);
 	}
}

Any assistance appreciated.
Please try and be specific on recommended code changes.
 


If you understand this section then you can slove the problem you having.In this function your X is incremented which is your column address and Y is fixed which is row address for example For type data at row 1 its starting address is 0x80 and continue till 16 char so your row 2 starting address would be 0xC0 and continue

Code C - [expand]
1
2
DisplayListChar(0, 1, "Line One"); // print text from row 1 column 1
    DisplayListChar(1, 1, "Line Two");  //print text from row2 column 1

 

Re: 1602 LCD Row &amp; Col Question, 8051 LCD Demo test in C.

I thought I understood the DisplayListChar function.

I found this format (Col-X, Row-Y) in the original demo code, matching function, not working.
Code:
DisplayListChar(4, 0, "Line 1");  // original code
	DisplayListChar(4, 1  "Line 2"); // Both items printed on same row ????

But both items printed on the same row.
 
Last edited:

which demo code you are talking about? Are you sure that it is working and just for curiosity why using demo code? Make it your own code
 

My initial post displays the Demo Code I referred to (that you commented on previously). My question is also given. I'm Sure the example is not working properly.

I'm trying to learn C, and basic demo code that was included with my 8051 development board is helpful in learning by example.

A bit of advice to understand and correct small aspects of the example code would be helpful.
 

Try my LCD code. If you use PIC18F devices then remember to replace PORTxbits.Rxy with LATXbits.LATxy in LCD defines. If you use my LCD code you can use any pin combination for LCD control and data.

The library functions are used like this.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
LCD_Init();
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Cmd(_LCD_CLEAR);
LCD_Out(1,1,"Line 1");
LCD_Out(2,1,"Line 2");
LCD_Out(3,1,"Line 3");
LCD_Out(4,1,"Line 4");
 
LCD_Out_Cp("20x4");
LCD_Chr(1,10,'K');
LCD_Chr_Cp('J');

 

Attachments

  • LCD_4bit.txt
    3.9 KB · Views: 68

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…