Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to write string on specific line

Status
Not open for further replies.

Parth86

Member
I am trying to write message on specific line of LCD. I wrote following program but LCD doesn't show the message on screen
8051 and keil
C:
#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 DelayUs(unsigned int wait);
void DelayMs(unsigned int wait);
void LCD_Command(unsigned char cmd);
void LCD_Data(unsigned char Data);
void LCD_init();
void LCD_goto( int x, int y);
void LCD_print(char* str);

/* functions for delay */
 void DelayUs(unsigned int wait)
   {
     wait >>= 3;
      while(wait--);
   }

 void DelayMs(unsigned int wait)
   {
      while(wait--)
      DelayUs(1000);
       }

/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    port = cmd;
    RS=0;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
    port = Data;
    RS=1;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
 /* Function to prepare the LCD */
void LCD_init()
{
    LCD_Command(0x38);
    DelayMs(15);
    LCD_Command(0x0f);
    DelayMs(15);
    LCD_Command(0x01);
    DelayMs(15);
    LCD_Command(0x81);
    DelayMs(15);
}
/*Function to position cursor on the LCD */
void LCD_goto( int x, int y)
   {
     int addr = 0x80; 
     if(y==2) addr+=0x40;
     addr += x; 
     LCD_Command(addr);
   }
     
/* Function to print on the LCD */
void LCD_print(char* str)
     {
       while(*str!=0) 
       LCD_Data(*str++);
     }
     
void main(void)         
    {
        void LCD_init();   
    while(1)       
        {
          LCD_goto(1,1);
          LCD_print("Hello");
          LCD_goto(2,1);
          LCD_print("Forum");
       
        }
    }
Program output
compiling lcd.c...
lcd.c - 0 Error(s), 0 Warning(s).
 
The beginning of the second line is 0x94 (1,0).

However, if you "aren't seeing anything," that is probably not your problem. Can you print to (0,0) or 0x80? If there is gibberish, show us a photo. Do you have contrast turned up enough to see the faint outline of dark rectangles?
 
For a 16*2 the second line is address 0x40 = command 0xC0.
For a 20*4 display the second line is address 0x40 = command 0xC0.
For a 20*4 display the third line is address 0x14 = command 0x94.
For a 20*4 display the fourth line is address 0x54 = command 0xD4.

I suspect the contrast also.

Edit, as far as I can see, the code is correct.
Edit2, I take that back - your x and y seem to be in the wrong order in your goto routine.

Mike.
 
Pommie, I suspect your line values and mine differ by an offset for ascii characters (0x20 - 0x7F). Do we know whether the OP's has that offset?

I saw this " int addr = 0x80; " and assumed it did.
 
The 0x80 is the command to set the address. So writing 0x80 sets the LCD address to zero which is the first character of the top line. The second line is at address ox40 and so the OP needs to write oxCo as a command.

However, in the code above the x and y are the wrong way around and so "HForum" should be displayed on the top line.

Also, y is indexed from 1 and x from 0.

Mike.
 
Pommie & jpanhalt

Sorry I forgot to tell you that I am working on proteus simulator but I think there is no fault in design because I have been worked with this design before. there is nothing wrong in design. I suspect there is problem in program
 
If you remove the gotos do you then get HelloForum on the display?

Mike.
 
If you remove the gotos do you then get HelloForum on the display?

Mike.
I have done some changes in program and it work's. I can print message on first and second line

upload_2018-1-31_11-38-37.png


C:
#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 DelayUs(unsigned int wait);
void DelayMs(unsigned int wait);
void LCD_Command(unsigned char cmd);
void LCD_Data(unsigned char Data);
void LCD_init();
void LCD_goto( int x, int y);
void LCD_print(char* str);

/* functions for delay */
 void DelayUs(unsigned int wait)
   {
     wait >>= 3;
      while(wait--);
   }

 void DelayMs(unsigned int wait)
   {
      while(wait--)
      DelayUs(1000);
       }

/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    port = cmd;
    RS=0;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
    port = Data;
    RS=1;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
 /* Function to prepare the LCD */
void LCD_init()
{
    LCD_Command(0x38);
    DelayMs(15);
    LCD_Command(0x0f);
    DelayMs(15);
    LCD_Command(0x01);
    DelayMs(15);
    LCD_Command(0x81);
    DelayMs(15);
}
   
void LCD_goto( char x, char y)
   {
   int addr = 0x80;   // start address
   if(y==2) addr+=0x40;  // are we on second row
   addr += x;    // add in the columns
   LCD_Command(addr);   // send to display.
   }

void LCD_Print(unsigned char *string)  //Function to send string to LCD
{
int index=0;
while(string[index]!='\0')
{
  LCD_Data(string[index]);
  index++;
  DelayMs(10);
}
return;
}


void main()
{
unsigned char Messsage1[] ="Hello";
unsigned char Messsage2[] ="Forum";

lcd_init();
   
   while(1)
       {
       LCD_goto(0,0);
       LCD_Print(Messsage1);
       DelayMs(50);
       LCD_goto(0,2);
       LCD_Print(Messsage2);
       DelayMs(50);
           
    }
}
 
It makes no sense that you saw nothing with the code in the first post. What did you change?

Also, having the top line as line 0 and the second line as line 2 makes no sense. Change it to 0 and 1.

Mike.
 
I wrote following program but LCD doesn't show the message on screen
Loosley worded.. Looks very much like the code I gave you..


char message[] = "Hello";

Creates a message in rom... Therefore the LCD_print statement I posted will not work..

char message[8];
printf(message,"Hello");

will create the message in ram and will work... Remember a Rom pointer is different to a Ram pointer..
 
char message[8];
printf(message,"Hello");

will create the message in ram and will work... Remember a Rom pointer is different to a Ram pointer..
Sorry but I didn't understand your point. I think I am doing mistake here
C:
/* Function to print on the LCD */
void LCD_print(char* str)
     {
       while(*str!=0)
       LCD_Data(*str++);
     }
   
void main(void)       
    {
        char message[8];
        void LCD_init(); 
    while(1)     
        {
          LCD_goto(1,1);
          LCD_print("Hello");
          LCD_goto(2,1);
          LCD_print("Forum");
     
        }
    }
 
Sorry but I didn't understand your point.
Okay... As simple as I can:-

8051 has a data space and a code space... Therefore in C we have two pointers.. One for code and one for data.

char message[] = "Hello world"; is static text... Therefore the compiler puts it in code space..

char message[8]; is non static and will be placed in data space..

This puts a burden on C programmers as this line:-

void LCD_print (char * message) will only work for data pointers..

void LCD_print(rom char* message) will only work with code pointers..
LCD_goto(1,1);
LCD_print("Hello");
LCD_goto(2,1);
LCD_print("Forum");

This is also wrong as x is columns and y is rows... Line 1 = (0 ,1) and Line 2 = (0,2)..
 
Okay... As simple as I can:-
void LCD_print(rom char* message) will only work with code pointers...
I tried it but program showing error
C:
#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 DelayUs(unsigned int wait);
void DelayMs(unsigned int wait);
void LCD_Command(unsigned char cmd);
void LCD_Data(unsigned char Data);
void LCD_init();
void PutMessage(rom char *Message);

/* functions for delay */
 void DelayUs(unsigned int wait)
   {
     wait >>= 3;
      while(wait--);
   }

 void DelayMs(unsigned int wait)
   {
      while(wait--)
      DelayUs(1000);
       }

/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    port = cmd;
    RS=0;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
    port = Data;
    RS=1;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
 /* Function to prepare the LCD */
void LCD_init()
{
    LCD_Command(0x38);
    DelayMs(15);
    LCD_Command(0x0c);
    DelayMs(15);
    LCD_Command(0x01);
    DelayMs(15);
    LCD_Command(0x81);
    DelayMs(15);
}
void PutMessage(rom char *Message){
char i=0;
    while(Message[i]!=0)
        PutChar(Message[i++]);
}

  void main(void)       
    {
            LCD_init();
       PutMessage("Hello");       
           while(1)
           {
               }
    }
compiling lcd.c...
lcd.c(13): error C141: syntax error near 'char', expected ')'
lcd.c(60): error C141: syntax error near 'char', expected ')'
lcd.c(62): error C202: 'Message': undefined identifier
lcd.c(63): error C202: 'Message': undefined identifier
lcd.c - 4 Error(s), 0 Warning(s).
 
Okay... You need to read your compilers help document..
Does it make any sense

upload_2018-2-1_15-52-35.png



C:
#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 DelayUs(unsigned int wait);
void DelayMs(unsigned int wait);
void LCD_Command(unsigned char cmd);
void LCD_Data(unsigned char Data);
void LCD_init();
void LCD_goto( char x, char y);

/* functions for delay */
 void DelayUs(unsigned int wait)
   {
     wait >>= 3;
      while(wait--);
   }

 void DelayMs(unsigned int wait)
   {
      while(wait--)
      DelayUs(1000);
       }

/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    port = cmd;
    RS=0;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
    port = Data;
    RS=1;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
 /* Function to prepare the LCD */
void LCD_init()
{
    LCD_Command(0x38);
    DelayMs(15);
    LCD_Command(0x0c);
    DelayMs(15);
    LCD_Command(0x01);
    DelayMs(15);
    LCD_Command(0x81);
    DelayMs(15);
}

void LCD_goto( char x, char y)
   {
   int addr = 0x80;
   if(y==2) addr+=0x40;
   addr += x;
   LCD_Command(addr);
   }

//Function to send string to LCD
void PutMessage(unsigned char *string)
{
   int index=0;
  while(string[index]!='\0')
 {
  LCD_Data(string[index]);
  index++;
  DelayMs(10);
 }

return;
}
 void main()
{
   unsigned char string1[] ="Hello";
   unsigned char string2[] ="Forum";
   unsigned char string3[] ="It's work";
 
lcd_init();
 
   while(1)
       {
       LCD_goto(1,1);
       PutMessage(string1);
       DelayMs(50);
       LCD_goto(7,1);
       PutMessage(string2);
       DelayMs(50);
       LCD_goto(1,2);
       PutMessage(string3);
       DelayMs(50);
        
    }
}
 
Last edited:
void PutMessage(const char *Message)
{
char i=0;
while(Message[i]!=0)
PutMessage(Message[i++]);
}

Just look at your code.... If you sent that code to the LCD instead of itself, it will work..
 
Just look at your code.... If you sent that code to the LCD instead of itself, it will work..
I tried this before but it show error
C:
#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 DelayUs(unsigned int wait);
void DelayMs(unsigned int wait);
void LCD_Command(unsigned char cmd);
void LCD_Data(unsigned char Data);
void LCD_init();
void LCD_goto( char x, char y);

/* functions for delay */
 void DelayUs(unsigned int wait)
   {
     wait >>= 3;
      while(wait--);
   }

 void DelayMs(unsigned int wait)
   {
      while(wait--)
      DelayUs(1000);
       }

/* Function to send command instruction to LCD */
void LCD_Command(unsigned char cmd)
{
    port = cmd;
    RS=0;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
/*Function to send display dato LCD */
void LCD_Data(unsigned char Data)
{
    port = Data;
    RS=1;
    RW=0;
    EN=1;
    DelayMs(5);
    EN=0;
}
 /* Function to prepare the LCD */
void LCD_init()
{
    LCD_Command(0x38);
    DelayMs(15);
    LCD_Command(0x0c);
    DelayMs(15);
    LCD_Command(0x01);
    DelayMs(15);
    LCD_Command(0x81);
    DelayMs(15);
}

void LCD_goto( char x, char y)
   {
   int addr = 0x80;  
   if(y==2) addr+=0x40;
   addr += x;  
   LCD_Command(addr); 
   }

//Function to send string to LCD

void PutMessage(const char *Message)
{
  char i=0;
  while(Message[i]!=0)
  PutMessage(Message[i++]);
}
 void main()
{
   unsigned char string1[] ="Hello";
   unsigned char string2[] ="Forum";
    unsigned char string3[] ="It's work";
  
lcd_init();
  
   while(1)
       {
       LCD_goto(1,1);
       PutMessage(string1);
       DelayMs(50);
       LCD_goto(7,1);
       PutMessage(string2);
       DelayMs(50);
       LCD_goto(1,2);
       PutMessage(string3);
       DelayMs(50);
          
    }
}
compiling lcd.c...
lcd.c(76): warning C265: '_PutMessage': recursive call to non-reentrant function
lcd.c(76): error C214: illegal pointer conversion
lcd.c - 1 Error(s), 1 Warning(s).
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top