8051 LCD programming help

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Hello,
I have a sample program that is written in micro-c and shows examples
of how to write data to an LCD connected to the 8051 micro-controller.
Could someone please explain what the following function is doing?

void WriteCommon( unsigned char value )
{
clrbit(READ);
value = value & 0x0F;
value = value << 4;
P0 = P0 & 0x0F;
P0 = P0 | value;
setbit(STROBE);
delay(1);
clrbit(STROBE);
setbit(READ);
delay(1);
}


Here is the full program where this function came from:

/**********************************************************************
*****
;
; This module is presented here only to serve as a sample for
; 8051 program development. BiPOM Electronics provides this module
;as is and
; does not guarantee its functionality or suitability for a
;particular purpose.
; Please e-mail any questions and suggestions to snipped-for-privacy@bipom.com;
;
; Module: LCD.C
;
; Revision: 1.01
;
; Description: Interface routines for driving a alphanumeric
; Liquid Crystal Display module using 8051
;micro-controller.
; LCD is driven by only 4 data lines ( 4-bit
;mode )
;
; This program is written using Micro-C Compiler from
; Dunfield Development Systems.
;
*/
#include <8051io.h>
#include <8051bit.h> /* Bit set/clear macros */
#include <8051reg.h>
#define READ P0.1 /*READ=SET & WRITE=RESET*/
#define STROBE P0.2 /*DATA STROBE (ENABLE)*/
#define CTRL P0.0 /*CONTROL BIT (CONTROL=RESET &
DATA=SET)*/
void InitLCD();
void WriteCtrl( unsigned char value );
void WriteData( unsigned char value );
void WriteCommon( unsigned char value );
void WriteLCD( unsigned char* message );
void CursorHome();
void CursorSet( unsigned char pos );
main()
{
unsigned char i;
serinit(9600);
/* Initialize the LCD as the very first thing */
InitLCD();
/* Write a simple message to the LCD */
CursorHome();
WriteLCD( "Hello" );
/* Keep the message for 2 seconds */
/* ( 1 second on MINI-MAX/51-C because of 22MHz clock instead of
standard 11MHz ) */
delay(2000);
/* Clear the LC display */
CursorHome();
WriteLCD( " " );
/* Example of writing to anywhere on the display */
for( i=0; i<128; i++ )
{
CursorSet(i);
WriteData( '*' );
}
delay(4000);
}
/**********************************************************************
*****
; Function: InitLCD
;
; Description: Initializes the LCD
;
; Inputs: none
;
; Returns: nothing
;**********************************************************************
****/
void InitLCD()
{
/* Wait a bit after power-up */
delay(200);
/* Initialize the LCD to 4-bit mode */
WriteCtrl(3);
delay(50);
WriteCtrl(3);
delay(10);
WriteCtrl(3);
delay(10);
WriteCtrl(2);
delay(10);
/* Function Set */
WriteCtrl(2);
delay(10);
WriteCtrl(8);
delay(10);
/* Display OFF */
WriteCtrl(0);
delay(10);
WriteCtrl(8);
delay(10);
/* Display ON */
WriteCtrl(0);
delay(10);
WriteCtrl(0x0F);
delay(10);
/* Entry mode */
WriteCtrl(0);
delay(10);
WriteCtrl(6);
delay(10);
/* Clear Screen */
WriteCtrl(0);
delay(10);
WriteCtrl(1);
delay(100);
/* Cursor home */
WriteCtrl(0);
delay(10);
WriteCtrl(2);
delay(100);
}
void CursorSet( unsigned char pos )
{
WriteCtrl(8 | ( (pos>>4) & 7 ) );
delay(1);
WriteCtrl(pos & 0x0F);
delay(1);
}
void CursorHome()
{
/* Cursor home */
WriteCtrl(0);
delay(1);
WriteCtrl(2);
delay(1);
}
void WriteLCD( unsigned char* message )
{
unsigned char i;
for( i=0; i<20; i++ )
{
if( !message[i] )
break;
WriteData(message[i]);
}
}
void WriteCtrl( unsigned char value )
{
clrbit(CTRL);
WriteCommon( value );
}
void WriteData( unsigned char value )
{
setbit(CTRL);
WriteCommon( value >> 4 );
WriteCommon( value );
}
void WriteCommon( unsigned char value )
{
clrbit(READ);
value = value & 0x0F;
value = value << 4;
P0 = P0 & 0x0F;
P0 = P0 | value;
setbit(STROBE);
delay(1);
clrbit(STROBE);
setbit(READ);
delay(1);
}


Re: 8051 LCD programming help
Hi Steve,

Take a peek at http://members.iinet.net.au/~vanluynm/Down/LCD.zip ~6KB
There are some comments that may clear up LCD programming for you. Also this
method implements low level C functions so you can write to the LCD with
printf().

Julyan Ilett's excellent articles "How to use intelligent LCD's" are also
very useful.
http://www.epemag.wimborne.co.uk/lcd1.pdf
http://www.epemag.wimborne.co.uk/lcd2.pdf

First 2 lines shrink value down to a 4 bit number in the upper 4 bits of
value. Next 2 clear the upper 4 bits of port 0, and send value out on these.
Then the LCD is strobed to read the 4 bit message. Its just a routine to
write 4 bit values to the LCD when using it in 4 line mode.

Dave Dunfield is a clever sod. I like his filesystem especially.

Regards,
Murray R. Van Luyn.


Quoted text here. Click to load it



Re: 8051 LCD programming help
Hi,
Thanks for the explanation and the links you provided. It helped allot.
Thanks again


Re: 8051 LCD programming help
void WriteCommon( unsigned char value )
{
// look like the chip is in 4 bit mode
clrbit(READ);                 // assert read
value = value & 0x0F;    // get low nibble (4bits)
value = value << 4;        // move low nibble to high nibble
P0 = P0 & 0x0F;          // clear high nibble of port 0
P0 = P0 | value;            // move data to port 0 (leave low nibble as is)

setbit(STROBE);            // assert strobe
delay(1);                        // wait for write (LCD is slow)
clrbit(STROBE);            // strobe off
setbit(READ);                // read off
delay(1);                        // wait again
}




Re: 8051 LCD programming help
Quoted text here. Click to load it
... snip ...

You should indent your program properly.  If your newsreader is
removing leading spaces and tabs get a better newsreader.

That routine is wrapping three actions together.  At the innermost
level it turns on a line named STROBE, pauses, and turns it off.
Outside that it takes the lower 4 bits of the input value and puts
them on the higher 4 bits of a port named P0 without disturbing the
lower 4 bits.  It wraps that whole process in setting a line called
READ, and then resetting it.  

Quoted text here. Click to load it
actually implemented in the lower 4 bits of that port.  Waveforms
are approximately:

READ         ___________----------------______________

STROBE       __________________-______________________

bits set up  ______________---------------------------

in function  _______-----------------------___________

--
Chuck F ( snipped-for-privacy@yahoo.com) ( snipped-for-privacy@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
We've slightly trimmed the long signature. Click to see the full one.
Re: 8051 LCD programming help

Quoted text here. Click to load it

It is writing the bottom four bits of a byte to the LCD.
This is because the LCD can be used in byte or nybble mode.
Your example is using the latter (fewer I/O pins, but slower)

In this mode, you have to specify "command" or "data" and send
the two (one?) nybbles corresponding to whatever you want
to send.

This function is called by several other "wrapper" functions
to perform send-data, send-command and probably a few others.

I think commands can sometimes be sent in only 4 bits (especially
for initialisation)

Richard [in PE12]




Site Timeline