View previous topic :: View next topic |
Author |
Message |
septillion
Joined: 21 Jan 2010 Posts: 13
|
Preform an action on a string |
Posted: Thu Jan 21, 2010 6:25 am |
|
|
16F628A
PCM 4.057
Hi,
I try to kind of port a AVR GCC pice of code to CCS but i can't get it to work.
The pice of GCC code:
Code: | void display_str(char *s) {
uint8_t i;
// don't use the lefthand dot/slash digit
display[0] = 0;
// up to 8 characters
for (i=1; i<9; i++) {
// check for null-termination
if (s[i-1] == 0)
return;
// Numbers and leters are looked up in the font table!
if ((s[i-1] >= 'a') && (s[i-1] <= 'z')) {
display[i] = pgm_read_byte(alphatable_p + s[i-1] - 'a');
}
else if ((s[i-1] >= '0') && (s[i-1] <= '9')) {
display[i] = pgm_read_byte(numbertable_p + s[i-1] - '0');
} else {
display[i] = 0; // spaces and other stuff are ignored :(
}
}
} |
After that it's called like:
display_str("text")
Where in CCS I ported the functions
pgm_read_byte(alphatable_p + var - 'a') -> alpha(var)
pgm_read_byte(numbertable_p + s[i-1] - '0') -> number(var)
It has to perform a action on every char in a string, max 8 char + terminator, and store it in an array starting @ [1] every time the function is called.
But it does not work. I end up with "Attempt to create a pointer to a constant" I can understand that part because of CCS makes the piece of text a const.
So i tried to call it like:
char test[9];
test = "text";
printStr(test);
But line 2 gives 'Expecting LVALUE such as a variable name or * expression"
So i'm kind of lost how to make this work in CCS. Mayby i don't think the right way but i can't get around it. I hope someone can help. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Jan 21, 2010 9:06 am |
|
|
You can't transfer a string, using '='. This is standard in C. Look at 'strcpy'.
If you look at the first example, this shows exactly how to do what you want...
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Sat Jan 23, 2010 1:01 pm |
|
|
Thnx! I get it to work now. Forgot about strings. I kept the function but call it like:
Code: |
txtarray[10];
....
strcpy(txtarray, "message");
display_str(txtarray);
|
And to make it less code make a define
Code: |
#DEFINE printStr(var) strcpy(txtarray, var); display_str(txtarray);
|
All I need to do is call
printStr("message")
Or is there a better way so I can call just one function like
printStr("message") ? |
|
![](templates/subSilver/images/spacer.gif) |
septillion
Joined: 21 Jan 2010 Posts: 13
|
|
Posted: Sat Jan 23, 2010 1:02 pm |
|
|
That was me, not knowing I was posting as a guest... |
|
![](templates/subSilver/images/spacer.gif) |
|