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 pass string as argument in function

Status
Not open for further replies.
I'm lost!!! Kiel uses C51 compiler.... MinGW32 is a windows compiler!! MinGW hasn't a port for Intel 51.

If you are using an ansi C compiler then you can use printf directly with the string..
Have you seen program and result in post #20 it has been written in keil

What's wrong do you see in program?
 
I can only assume the program compile is failing and you are using an old hex file... The messages from the compile should have an error.. The function String cannot have the same name as the variable String..

Show me the compiler error messages..
 
Show me the compiler error messages..

I compiled program with error

upload_2018-1-21_22-15-38.png
 
I have tried it but not getting what i want to display on screen
C:
 int main(void)
{
 
  LCD_init();
   string ();
      while (1)
      {
 
          }

}
what I didn't mention ? Have you read post #12 and #14. there is all information

I want write program for LCD on a 8051. I want to see the string "hello friend " on my LCD screen

this is program

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 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 string (void)
     {
     unsigned int i=0;
    unsigned char string[15]= "Hello Friend's";
     for(i=0; i<15; i++)
    {
            LCD_Data(string[i]);
    }

}
   int main(void)
{
 
  LCD_init();
   string ();
   while (1)
   {
 
   }

}

message on screen

99378-c10f204539f61a276951ec62118b3a57.jpg


message should be print on first row and it should be fixed

I've read the entire several times thread. You need to tell us EXACTLY what you want and exactly what you're getting when it's not what you want. More than once, you've provided code that you said didn't have the correct output but never told us what the code output was. That's not helpful.

Also remember that we don't have the code or LCD in front of us. We know NOTHING about the LCD. We don't know what commands are available or what those hex commands you are sending with LCD_Cmd() are supposed to do.

Also, giving just one input and output is not enough. We need more of them to figure out what the code and LCD are actually doing. We need multiple test cases.

Most of the time in this thread all you do is go "This is what I have. It's not working. What's wrong with it?" Then we provide a suggestion and all you do is come back and say "It doesn't work." You don't even tell us what it did most of the time. It feels like you're just sitting there waiting for us to reply without thinking or trying anything on your own You need to spend that time making little changes to your code and investigating what it does to try and give us new information to boil down the behaviour of the problem.

---
For example, look at the image of the LCD screen you provided. Did you even think about it very much?

Hello Friend's
lo Friend's Hell


What happened to the missing "Hel" at the beginning of the second line? I don't know because I don't have enough information. You provided a single image of what you said was moving text. Some text could be going off the edge of the display and getting cut off before moving to a new line or something else. I have no idea. If it's moving text, show more than one image or else it's like trying to understand a video from a single frame. Also, don't use "Hello World" or "Hello Friends". It's not a good test string. Use strings of non-repeating, easily countable characters "1234567890abcdefghik". Try strings that are short enough that they can be repeated on the screen as well as strings that are long enough that will run off the edge of the screen or wraparound to a newline. That will make it easy to identify different things like how many characters are being displayed, when they are repeating or moving to a new line, and which character positions always go missing.
---
Start like this: Feed many inputs that will provide unique results that will tell you about different things in the system and see how the system reacts. In this case, feed short strings and a very long strings into the LCD and see what happens. The string needs to be non-repeating characters so you won't get confused if something appears twice. Like "123" and "123\n". Then feed long strings that will have to run off the edge of the screen or wrap around like "1234567890abcdefghijk", and "1234567890abcdefghijk\n".

Does the short string move or repeat? How does it move or repeat? How does the \n change things?

Does the long version repeat? Does it run off the edge of the screen or move onto a new line? Or does it do both where it runs off the edge of the screen for a bit and then move onto a new line so characters in the middle are missing?

Do things like this to hunt down exactly how the LCD and code are behaving. It's not enough to just show us one input and one output and expect us to be very helpful. You have to do some investigative work to try and boil down what the program and LCD are actually doing.

Also change the 'i' in your for loop to different numbers and see how it behaves. How does do different values of i affect what is printed out?
 
Last edited:
I've read the entire several times thread. You need to tell us EXACTLY what you want and exactly what you're getting when it's not what you want. More than once, you've provided code that you said didn't have the correct output but never told us what the code output was. That's not helpful.
I don't know what you don't understand in above post. I can just show you some more output of LCD

upload_2018-1-22_1-4-19.png

I have already posted code and I just want to see only message "Hello Friends " on the LCD

Is it clear?
 
I don't know what you don't understand in above post. I can just show you some more output of LCD

I have already posted code and I just want to see only message "Hello Friends " on the LCD
That's because you aren't thinking thoroughly enough about all the things that could be going wrong. Then you would see it's not so obvious.

Code is often not enough because we don't have your equipment in front of us and we don't have simulators in our brain. We need you to provide with how code reacts to little changes.

I can just show you some more output of LCD

View attachment 110426


Is it clear?

But good right there. What I guessed from your one image seems to be confirmed from your multiple images on the LCD. You can see in the LCD images that characters are running off the screen. The LCD screen is able to accept characters that do not display. Add \n to the end of your string and see if it displays two complete lines properly. Fix one problem at a time.
 
Last edited:
Add \n to the end of your string and see if it displays two complete lines properly.
If I want to print something in new line then I write like below in c programming
printf("Hello Friend's" \n");

How to start new line in embedded program
C:
void string (void)
     {
     unsigned int i=0;
    unsigned char string[15]= "Hello Friend's";
     for(i=0; i<15; i++)
    {
            LCD_Data(string[i]);
    }

}
 
I don't know what you don't understand in above post. I can just show you some more output of LCD

View attachment 110426
I have already posted code and I just want to see only message "Hello Friends " on the LCD

Is it clear?
If I want to print something in new line then I write like below in c programming
printf("Hello Friend's" \n");

How to start new line in embedded program
C:
void string (void)
     {
     unsigned int i=0;
    unsigned char string[15]= "Hello Friend's";
     for(i=0; i<15; i++)
    {
            LCD_Data(string[i]);
    }

}

'\n' is a character all on it's own like 'a' or 'b' so you can do any of these:
Code:
printf("Hello Friend's", "\n"); //method 1
printf("Hello Friend's\n"); //method 2

printf("Hello Friend's"); //--method 3 uses these two lines together
printf("\n");

If you want to use your LCD_Data() function, then you can build the newline into your string itself:
Code:
MyString[] = "HelloWorld!\n";

You should also be able to go:
Code:
LCD_Data("\n");
if you use malloc and copy the literal into that memory inside lcd_data(). Don't forget to free(). strdup() will do the entire malloc and copy process for you but uses malloc internally it so you need to free() when you are done. You need to do this in order to feed literal string arguments into a fuction since strings are arrays in memory and a literal argument does not exist in memory in the same way.
 
Last edited:
Oh one more thing. Make your for loop

C:
void string (void)
     {
     unsigned int i=0;
    unsigned char string[15]= "Hello Friend's";
     for(i=0; string[i] != '\0'; i++) //CHANGE THE LOOP TERMINATION CONDITION
    {
            LCD_Data(string[i]);
    }

}

That way, you don't have to constantly adjust your for loop based on string length. That's not going to work when you want to pass strings of different lengths into LCD_Data();

You could also use a while(string != '\0') loop instead like you had at the very beginning. I assume you already know about how strings in C are terminated since you defined [15] for the array size even though "Hello Friend's" is only 14 characters long.

And change your string() and char string[] to be different names.
 
Last edited:
Do you have a manual for your LCD commands? It looks like your LCD automatically does a newline after it has received enough characters, but this number of characters is more than what the LCD can display on one line, so all those extra characters get cut off at the edge of the screen. It might be adjustable with a command. If it's not, then you'll just have to manage it manually in your code.

I bet there's something important about your LCD commands that you're not aware of.
 
Last edited:
so why was it moving and repeating? you were only sending the string once right? what did cursor off do?
 
Parth could you post the working code plz I'd like to see it Thanks
I tried your code changed the port stuff so it match a pic here what you get cool .


lcd1.jpg
 
Last edited:
Parth could you post the working code plz I'd like to see it Thanks
I tried your code changed the port stuff so it match a pic here what you get cool .
I lost my that code while practicing. I wrote new code

upload_2018-2-1_8-6-26.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 message(void);

/* 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 message(void)
     {
       unsigned int i=0;
       unsigned char string[15]= "Hello Friend's";
       for(i=0; i<15; i++)
       {
            LCD_Data(string[i]);
       }

}

  void main(void)   
    {
           LCD_init();
           message();       
           while(1)
           {
              /*do nothing */
            }
    }
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top