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.
 
HI Mike

Thanks for this. I will give this a go and see what it spits up. Thanks for the headsup on the RAM useage too - I always wondered why I would run into problems writing text to arrays..
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…