Program to display message on LCD

Thread Starter

Parth786

Joined Jun 19, 2017
642
Hi
I want to write program to display message or text on LCD. I have searched on internet but stuck, I don't have clue how to draw flow chart for program.

1. LCD initialization
2. wait for some time
3. send command to LCD
4. send data to LCD

Please help me to understand working of LCD with Micro controller

.Here is 16x2 Lcd which has 16 pins and can be operated in 4-bit mode or 8-bit mode. Lcd has two types of registers namely Command Register and Data Register. When RS=0 or low,Command Register is selected and When RS=1 or high,Data Register is selected. When RW=1,data is read from Lcd and When RW=0,writes the data to Lcd. Data pins carries 8-bit data or command from an external unit such as micro controller.
.
upload_2017-7-5_19-8-7.png
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Your text has a large chapter on this subject. Also, search AAC for examples. Did you notice the Similar Threads section below?
This is a very common question and there is lots of info that can be had with just a little effort on your part. These displays use the 44780 controller:
https://www.sparkfun.com/datasheets/LCD/HD44780.pdf
I am just following your advice , you told me in my another thread to make flow chart first and then go for program. What I do , make flow chart or try to make program directly ?
Which programming language do you use.?
I write my program in C programming language and 8051
 

JohnInTX

Joined Jun 26, 2012
4,787
I am just following your advice , you told me in my another thread to make flow chart first and then go for program. What I do , make flow chart or try to make program directly ?
Fair enough.
You always plan first, code last.
Start by reading the 44780 datasheet to understand the internal registers and operation of the 44780 controller.
Know how to do the basic read/write to the controller.
Know the difference between writing to the control registers and the actual display memory that holds the characters using the RS pin.
Review the initialization sequence - flow charts are in the posts above, mine and @ericgibbs '
Draw a schematic showing how the display is hooked up so you know what I/O bits to use.
Decide what the program is to do with the now-functional display then generate a flow chart that shows all of the steps. Include the processor initialization, the initialization of the display, strings of characters to write to the display and routines to send characters to the display one by one.
Finally, code and test.
 
Last edited:

Thread Starter

Parth786

Joined Jun 19, 2017
642
I think its easy understand large program by divide into small pieces of code rather than drawing flow chart. I have studied some link and saw some sample code and Now I am trying to write my own program. so I have divide my program into many small pieces of code. I don't know weather doing wrong or right but I feel comfortable by dividing big program into small module. my leaning style is different but I feel comfortable with it. I always try to sort out small problems.

As per my thinking, These are some essential steps required to design program
1. define port and set bits
2. LCD initialization
3. wait for some time
4. send command to LCD
5. send data to LCD
6. message
7. main function
Step 1: connection of LCD to Micro controller
Code:
#include<reg51.h>
#define Data_port P1      /* Data pins connected to port P1 */
sbit RS = P2^0;           /* RS pin connected to pin 0 of port P2 */
sbit RW = P2^1;           /* RW pin connected to pin 1 of port P2 */
sbit EN = P2^2;           /* EN pin connected to pin 2 of port P2 */
Step 2: Function to send command instruction to LCD
Code:
/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    Data_port = cmd;
    RS= 0;
    RW=0;
    EN=1;
    Delay();
    EN=0;
}
Step 3: Function to send display data to LCD
Code:
/*Function to send display data to LCD */
void LCD_data(unsigned char data)
{
    Data_port = data;
    RS= 1;
    RW=0;
    EN=1;
    Delay(20);
    EN=0;
}
Step 4: Function to initialize the LCD
Code:
 void LCD_init() 
{
    LCD_Command(0x38);
    Delay(20);
    LCD_Command(0x0c);  /* display ON, cursor blinking */
    Delay(20);
    LCD_Command(0x01);  /* clear screen */
    Delay(20);
    LCD_Command(0x81);
    Delay(20);
}
step 5: Function for delay
Code:
void Delay(unsigned int i)
{
    for(i=0;i<20;i++)     
}
Step 6: function for message string
Code:
void Message()
{
       unsigned char name[15]="Parth_786";    /declearation and inilization of array with string.
}
step 7 main function
Code:
void main()
{
    LCD_init();
      Message();
    while(condition)
    {
     
    }
}
than I tried to assemble all modules in one program
Code:
#include<reg51.h>
#define Data_port P1      /* Data pins connected to port P1 */
sbit RS = P2^0;           /* RS pin connected to pin 0 of port P2 */
sbit RW = P2^1;           /* RW pin connected to pin 1 of port P2 */
sbit EN = P2^2;           /* EN pin connected to pin 2 of port P2 */

/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    Data_port = cmd;
    RS= 0;
    RW=0;
    EN=1;
    Delay();
    EN=0;
}

/*Function to send display data to LCD */
void LCD_data(unsigned char data)
{
    Data_port = data;
    RS= 1;
    RW=0;
    EN=1;
    Delay(20);
    EN=0;
}

/* Function to prepare the LCD */
void LCD_init()  
{
    LCD_Command(0x38);
    Delay(20);
    LCD_Command(0x0c);  /* display ON, cursor blinking */
    Delay(20);
    LCD_Command(0x01);  /* clear screen */
    Delay(20);
    LCD_Command(0x81);
    Delay(20);
}

/*Function for  delay */
void Delay(unsigned int i)
{
    for(i=0;i<20;i++)      
}

void Message()
{
       unsigned char name[15]="Parth_786";    .
}

void main()
{
    LCD_init();
      Message();
    while(condition)
    {
      
    }
}
I know there may be many errors but first I want to understand what's the problem and than I will try to solve problem. does it make any sense?
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
It is ALWAYS correct to write a large program in small pieces.

Offhand I see a few things in your code. I can't be too exact as all I have with me here sitting on the beach in the Caribbean is my Fire tablet but here goes...

The display needs time from power on before it is ready to accept any commands or data, so look that up in the data sheet.

Next, your LCD_data() routine looks ok, but your Message() routine doesn't use it. Message() is best when you pass it a pointer to a character array (string) and send each character to the LCD_data() routine.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Refer to the attached pdfs below for an excellent tutorial on communicating with an Hitachi HD44780 compatible LCD.
I looked that two pdf files, there are the good description but I am looking help on C program. I tried to explain what I understand to make program
The display needs time from power on before it is ready to accept any commands or data, so look that up in the data sheet.

Next, your LCD_data() routine looks ok, but your Message() routine doesn't use it. Message() is best when you pass it a pointer to a character array (string) and send each character to the LCD_data() routine.
I think 10ms to 20ms is ok for LCD to accept any commands or data
 

ErnieM

Joined Apr 24, 2011
8,377
According to the outline posted above everything completes in less time than that so it is ok... Even too long but it should work.

Sorry I cannot format code here but a string epinephrine looks like this in its simplest form:

void PrintString(char * buffer)
{
while (*buffer)
{ LCD_data(*buffer++) }
}

You pass a pointer to a string array, and this sends each character to the display until the trailing zero character is found.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
According to the outline posted above everything completes in less time than that so it is ok... Even too long but it should work.

Sorry I cannot format code here but a string epinephrine looks like this in its simplest form:

void PrintString(char * buffer)
{
while (*buffer)
{ LCD_data(*buffer++) }
}

You pass a pointer to a string array, and this sends each character to the display until the trailing zero character is found.
I looked some example where we can use string with array or string with pointer. still I am looking some examples . In your code you made function where you declare pointer but you have not initialized pointer
does it make any sense. I have declared and initialize the pointer
Code:
void PrintString(char * buffer)
    char * buffer = "Parth786"
{
while (*buffer)
{ LCD_data(*buffer++) }
}
 

ErnieM

Joined Apr 24, 2011
8,377
I did not declare a pointer in the routine because it is the job of the code that calls the routine to "pass in" a pointer to a string to be printed. Otherwise you would need a different routine for every string!

char* string1[] = "Hello";
chaf * string2[] = "World";
...
...
PrintString(string1);
PrintString(string2);

Note string1 and string2 are arrays, and the array name by itself is a pointer to the first character.

You will get used to this notation as you get more familiar with C.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I did not declare a pointer in the routine because it is the job of the code that calls the routine to "pass in" a pointer to a string to be printed. Otherwise you would need a different routine for every string!
.
I wrote this program to understand how the Array are relates with pointer
Code:
void main()
{
  unsigned char string[15]="Parth786";
  char *buffer = string;
  LCD_init();
  while(*buffer)
        {
    LCD_data(*buffer++);
    Delay(60);
        }
}
char *buffer = string;
Every pointer has the data types and name followed by an asterisk (*) operator. here data type is char and buffer is name of pointer
can you explain how does this program work with taking some numeric value just like pseudo code for understanding of basic?
 

ErnieM

Joined Apr 24, 2011
8,377
Correction to post 14:
Should be

char string1[] = "Hello";
char string2[] = "World";

These declare two string arrays. The type
char * string[]
would be an array of pointers to strings.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Correction to post 14:
Should be

char string1[] = "Hello";
char string2[] = "World";

These declare two string arrays. The type
char * string[]
would be an array of pointers to strings.
Can you provide some detail like as I tried in my post 14. by definition A pointer is a variable which contains the address in memory of another variable. I think after the deceleration of pointer, compiler assign a address to the variable and attached this unique address with the variable name. There are lot of confusion. can you please explain by assuming some variable data and the address in memory. I want to understand what's happen when this code run on controller.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
finally I did it but Now I want to do some changes. I want to display some more message. first display message " This is Parth" after that " I Love you siya" and later "will you marry me" so I think I need three array and than I have to pass each array in pointer. how to do it ?
Code:
#include<reg51.h>
#define port P1           /* Data pins connected to port P1 */
sbit RS = P2^0;           /* RS pin connected to pin 0 of port P2 */
sbit RW = P2^1;           /* RW pin connected to pin 1 of port P2 */
sbit EN = P2^2;           /* EN pin connected to pin 2 of port P2 */

void Delay(unsigned int wait)
{
    unsigned i,j ;
    for(i=0;i<wait;i++)  
    for(j=0;j<1200;j++);
}
/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    port = cmd;
    RS=0;
    RW=0;
    EN=1;
    Delay(2);
    EN=0;
}
/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
    port = Data;
    RS=1;
    RW=0;
    EN=1;
    Delay(2);
    EN=0;
}

/* function for delay */

/* Function to prepare the LCD */
void LCD_init()
{
    LCD_Command(0x38);
    Delay(20);
    LCD_Command(0x0f);
    Delay(20);
    LCD_Command(0x01);
    Delay(20);
    LCD_Command(0x81);
    Delay(20);
}
void main()
{
  unsigned char string[15]="This is Parth";
  char *buffer = string;
  LCD_init();
  while(*buffer)
        {
         LCD_Data(*buffer++);
         Delay(60);
        }
}
 
Top