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.

String question for C18

Status
Not open for further replies.

superbrew

Member
Hi, I am trying to understand how to assign strings in C18. I have built a three wire LCD interface and would like to write some messages on it. I can get the ADC results on the screen if I use the itoa function like this:
Code:
                itoa(result,output);//Convert ADC result to string
  		while(output[i]) nybbleOut(output[i++]);
		i = 0;

However, I can not assign a literal value to the string like:
Code:
output = "Hello, world";

I have the output assigned as a character array. Why can't I just assign the string like above? What does "const rom char*" mean? I tried looking inot the user's guide, but it does not explain it well enough for me to understand. Thanks.
 
Hi, I am trying to understand how to assign strings in C18. I have built a three wire LCD interface and would like to write some messages on it. I can get the ADC results on the screen if I use the itoa function like this:
Code:
                itoa(result,output);//Convert ADC result to string
  		while(output[i]) nybbleOut(output[i++]);
		i = 0;

However, I can not assign a literal value to the string like:
Code:
output = "Hello, world";

I have the output assigned as a character array. Why can't I just assign the string like above?
For LCDs I usually do something like this:
This is a global
Code:
char string[] = "                ";

Here I stuff the array with the string I want displayed
Code:
		sprintf(string,"Time: %02u:",hours);
		lcd_string(string);

and display it
Code:
void lcd_string(char *senpoint){
	while(*senpoint != '\0'){
		lcd_char(*senpoint);
		senpoint++;
	}
}
I don't mean to say that this is the only way to do it of course. There's lots of different ways to do these things.

If you want to see the above in context, **broken link removed**.


What does "const rom char*" mean? I tried looking inot the user's guide, but it does not explain it well enough for me to understand. Thanks.
Means it stores that char array in program memory (flash) instead of RAM. This means it is static (you can't change it), but it doesn't eat up your precious RAM which you need for other things.
 
However, I can not assign a literal value to the string like:
Code:
output = "Hello, world";

I have the output assigned as a character array. Why can't I just assign the string like above? What does "const rom char*" mean? I tried looking inot the user's guide, but it does not explain it well enough for me to understand. Thanks.

const rom char is stored in program memory space to save RAM. Use strcpy() ( find in string.h) to copy your string to a buffer in RAM sort of like:
Code:
#define MAX_CHAR 14 (the number of chars in your string + one for null
output = "Hello, world";
char s[MAX_CHAR];
strcpy(s, output);

Never mind; use futz's way. (God that guy is fast! )
 
Last edited:
Thank you very much for the replies. However, I don't think that I was clear in my original question on what I wanted. I tried sprintf(), but that seems more like just adding an integer to a string with specific formatting. I use the itoa() function now, as I have a function to format the number the way I want it. I would like to have a string that can be assisgned different values in various sections of the program.

For example:
*pseudo code*
output = "hello";
LCD_out(output);
output = "world";
LCD_out(output);

Please forgive my ignorance in this, I am more used to the way strings are dealt with in C#.
*Note: I have a function that outputs strings onto my display, but only if the strings are initialized in the declaration.
 
I have used something like this (using BoostC compiler) which I think I may have copied from Pommie;

Code:
PutLCD("K8LH Clock");
PutLCD("HH:MM:SS");



void PutLCD(rom char *pdata)
{
  char n = 0;                    //
  char temp;                     //
  while(temp = pdata[n++])       //
    PutLCD(dat,temp);            //
}
 
Last edited:
Thanks again all, I have got it to work, although I am sure that is not the most efficient way.
Code:
        sprintf (str, "Hello", j);
	while(str[i]) nybbleOut(str[i++]);
	i = 0;
	sprintf (str, " World", j);
	while(str[i]) nybbleOut(str[i++]);

I just initialized j to 0, and it does not change the strings.
 
The problem with strings, C and Pic Micros is the Harvard Architecture which separates program space from data space. String constants are automatically put into ROM (program space) but most functions want the data in the RAM (data space).
You need to include the <string.h> header and then use the strcpypgm2ram function to copy the string constants into RAM first:

Code:
char strbuffer[20];
strcpypgm2ram( strbuffer, "hello");
LCD_out(strbuffer);
strcpypgm2ram( strbuffer, "world");
LCD_out(strbuffer);

Or you can use Mike's method and write a wrapper so that PutLCD takes ROM data:

Code:
// Rewritten to avoid recursion ;)
PutLCD_rom("K8LH Clock");
PutLCD_rom("HH:MM:SS");

void PutLCD_rom(rom char *pdata)
{
  char n = 0;                    //
  char temp;                     //
  while(temp = pdata[n++])       //
    PutLCD(dat,temp);            //
}
 
Last edited:
kchriste, thanks, the strcpy method uses much less program memory. Using sprintf, my program was ~3000 words, now it is ~1150 words.
 
Another solutuion for const char * s

Hey I had the same problem, and found this solution:
Changing the declaration of the string from
"constant char * s" to "rom char * s"

Now it works !:)
 
...
output = "hello";
...

I am more used to the way strings are dealt with in C#.

You already got the answer "how to do it" .. just to add a comment on this quote to possible help you understand "why" instead of just telling you "how"

in C# which is OO compared to C which is procedural language, the variable "output" would be Object:
Code:
String output;

output = "hello";
output = output + " World";

in C this is not object but simple array:
Code:
char output[30];
output[0]='h';
output[1]='e';
...

output = "this will NOT work"; //this you cannot do as types are different
strcpy(output, "this WILL work"); //this will copy static string into output array byte by byte
sprintf(output ....); //this will copy "formed" string into array byte by byte

the object does know how to handle both c_string and string object, how to perform different operations (addition, assignment ..) hence that works in C# or C++ or any other OO language but in C it cannot so you have to know what is the pointer, what is the array and how array can be accessed. The string in C is just array of char's terminated by "special" char ('\0').

There is a number of functions that deal with strings, the functions are usually grouped in "strings" library and headers are in "strings.h" or "string.h".
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top