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.

writing strings to arrays

Status
Not open for further replies.

Electronworks

New Member
I am using the HI Tech C compiler with MPLAB. I am trying to write a character string to a 16 digit LCD display. I want to set up an array of 16 characters then write different messages to it throughout the program (warning messages, status info etc)

The string will obviously be a maximum of 16 characters, so I have set up an array of 16 digits using the following:

Code:
unsigned char lcd_row[] = "                ";

I have also tried

Code:
unsigned char lcd_row[16];

I then try and fill it using

Code:
lcd_row[] = "Hello World";

I have also tried using pointers to do the same thing, all without success. I get a variety of compiler errors.

What am I doing wrong? I have also read some of the LCD tutorials on this site, but they either load in individual characters (I want to load the whole string), or use the sprintf() function which is not documented in any of the code books I have. There must be a simpler way.

Please let me know

Thanks
 
You can use,
Code:
char test[17]="Hello World12345";

Note that the array has to be 1 longer than the string to contain the ending zero.

However, you will rapidly run out of ram doing it that way. I normally use,
Code:
//Write a string to the LCD
void PutMessage(const char *Message){
    while(*Message!=0)
        WriteChar(*Message++);
}
And then in the code just do,
Code:
    PutMessage((const char*) "Hello World!");

Doing it this way stores the strings in ROM and so you can fit a lot more.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top