|
|
View previous topic :: View next topic |
Author |
Message |
adesport
Joined: 03 Apr 2008 Posts: 18 Location: France
|
String manipulation with CCS |
Posted: Thu Apr 03, 2008 11:41 pm |
|
|
Hi all,
For my following projects I really need to manipulate strings. I used to work with CCS but, unfortunately I never tried to work around with string (like we may do in Java) so far.
My first question is about constant string in function parameters. How can I simply do a string comparing from a buffer and a string constant (by using stdlib strcmp).
I tried this but it doesn't work since const doesn't seem to be accepted:
Code: |
signed int8 strcmp2(char *s1, const char * s2)
{
for (; *s1 == *s2; s1++, s2++)
if (*s1 == '\0')
return(0);
return((*s1 < *s2) ? -1: 1);
}
//....
char buffer[MAX_CHAR];
strcmp(buffer,"TEST");
|
I understood while reading CCS user guide, we may use a function with a char in parameter and the compiler will call this function for each character of the string:
Code: |
void lcd_putc(char c ) {
...
}
lcd_putc ("Hello");
|
This is great but it doesn't work with functions like strcomp(...)
If you guys have any tips to work around strings, I will be very glad.
Cheers, |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 04, 2008 12:14 am |
|
|
Use the CCS strcpy() function to copy a constant string into a RAM array.
Now you can pass the ram array to the strcmp() function.
Code: | char temp[20];
strcpy(temp, "Hello");
|
|
|
|
Matro Guest
|
|
Posted: Fri Apr 04, 2008 1:36 am |
|
|
I think it should work even with string constants. In CCS help some examples are used with them. What is your CCS version?
Matro. |
|
|
adesport
Joined: 03 Apr 2008 Posts: 18 Location: France
|
|
Posted: Fri Apr 04, 2008 1:48 am |
|
|
PCM programmer wrote: | Use the CCS strcpy() function to copy a constant string into a RAM array.
|
That's what I did but I thought there would be a better way...
I've got CCS PCH C Compiler, Version 4.068 |
|
|
Matro Guest
|
|
Posted: Fri Apr 04, 2008 1:55 am |
|
|
If you have a lcd on your project that is working fine, do some tests and display results on your lcd.
Code: |
lcd_putc(strcmp("tes","test")+0x35);
lcd_putc(strcmp("test","test")+0x35);
lcd_putc(strcmp("teste","test")+0x35);
|
Display should be respectively "4", "5" and "6"
Matro |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 04, 2008 11:37 am |
|
|
CCS doesn't allow pointers to constants. If your code is compiled
as shown in the test program below, you will get these error messages:
Quote: |
*** Error 90 Line 14(22,23): Attempt to create a pointer to a constant
*** Error 90 Line 15(23,24): Attempt to create a pointer to a constant
*** Error 90 Line 16(24,25): Attempt to create a pointer to a constant |
Test program:
Code: |
#include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include "flex_lcd.c"
#include <string.h>
//====================================
void main()
{
lcd_init();
lcd_putc(strcmp("tes","test")+0x35);
lcd_putc(strcmp("test","test")+0x35);
lcd_putc(strcmp("teste","test")+0x35);
while(1);
} |
However, if you add the statement shown in bold, then it will compile:
Quote: | #include <18F452.h>
#device PASS_STRINGS = IN_RAM
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
|
The #device statement forces the compiler to copy the constant strings
into an internal RAM buffer. So when the strings are passed to strcmp(),
they are now in RAM, not ROM. This effectively does the same thing
as my example code, though it may be better because it does the
copying for you, automatically. You don't have to think about it.
Here's the .LST file. You can see that it's copying the constant data to
a RAM buffer called "STRPARAM".
Code: |
........ lcd_putc(strcmp("tes","test")+0x35);
0212: MOVLW 74
0214: MOVWF @STRPARAM
0216: MOVLW 65
0218: MOVWF @STRPARAM+1
021A: MOVLW 73
021C: MOVWF @STRPARAM+2
021E: CLRF @STRPARAM+3
0220: MOVLW 74
0222: MOVWF @STRPARAM+4
0224: MOVLW 65
0226: MOVWF @STRPARAM+5
0228: MOVLW 73
022A: MOVWF @STRPARAM+6
022C: MOVLW 74
022E: MOVWF @STRPARAM+7
0230: CLRF @STRPARAM+8
0232: CLRF s1+1
0234: MOVLW @STRPARAM
0236: MOVWF s1
0238: CLRF s2+1
023A: MOVLW @STRPARAM+4
023C: MOVWF s2
023E: RCALL 0114 // call strcmp()
0240: MOVLW 35
0242: ADDWF @01,W
0244: MOVWF @@17
0246: MOVWF c
0248: RCALL 01AC // call lcd_putc()
|
|
|
|
adesport
Joined: 03 Apr 2008 Posts: 18 Location: France
|
|
Posted: Fri Apr 04, 2008 11:41 am |
|
|
Thank, that exactly what I was waiting for... |
|
|
Ralf2
Joined: 05 Feb 2009 Posts: 10
|
|
Posted: Mon Jul 14, 2014 11:09 am |
|
|
Why this coded not work???
Code: | /*
A more complex example that creates an array of pointers to constant strings:
*/
const char *strings[] =
{
"HELLO",
"WORLD",
"CONST",
"STRINGS"
};
/*
Access the above const pointers
*/
const char *ptr; // ERROR?
while (i = 0; i < (sizeof(strings) / sizeof(const char *)); i++)
{
ptr = strings[i];
printf("%s", ptr);
} |
The program is page official ccs compiler
https://www.ccsinfo.com/content.php?page=compiler-features
compiler 5.026
Any idea how to fix it?
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Jul 14, 2014 11:51 am |
|
|
A search here will find lots of answers.
Obvious first problem you have is trying to make the pointer a constant.
Big problem is that CCS has three/four different ways of handling things, and the behaviour of some commands changes according to the configuration:
There are two different ways of declaring constants.
The first uses the 'const' keyword.
The second uses the 'rom' keyword.
With the 'rom' keyword used for the constant, you can declare a pointer to an element in rom, using
rom char *ptr;
The compiler understands this is a pointer to ROM, rather than being stored in ROM itself.
Then as PCM_programmer points out, you can add the declaration:
PASS_STRINGS=IN_RAM
With this the ROM constants are accessed as if they are in RAM, and the pointer must be to the RAM, not the ROM. Only works for strings though, not general data.
Then it gets more complex, since the declaration 'CONST=READ_ONLY', makes all constants copy to RAM automatically (uses a lot of space...), but be 'read only'. This is the ANSi form.
Then there is another problem. You are trying to declare the constant pointers to constant strings at compile time. CCS doesn't 'know' where constants are held, till after they are all placed.
Your syntax is wrong in the statements inside the while loop declaration. Use for, or rethink your syntax. The calculation also won't work. |
|
|
Ralf2
Joined: 05 Feb 2009 Posts: 10
|
|
Posted: Mon Jul 14, 2014 2:24 pm |
|
|
Hi Ttelmah
So I can not store data type int, float in rom? and create a pointer to these?
only strings |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Mon Jul 14, 2014 2:42 pm |
|
|
No.
Using the 'rom' keyword, you can create pointers to anything in rom. But they have to be rom pointers, and need to be initialised in the code, not at compile time.
So:
Code: |
rom int16 vals[] = {1,2,3,4,5,6,7,8,9};
rom * int16 fred;
fred = &vals[5];
//initialises a rom pointer to the value 6.
|
The core 'reason' is that the PIC has twin memory spaces, not a single linear space (Harvard architecture). It has been discussed here many times. |
|
|
Ralf2
Joined: 05 Feb 2009 Posts: 10
|
|
Posted: Mon Jul 14, 2014 3:45 pm |
|
|
Code: | rom int16 vals[] = {1,2,3,4,5,6,7,8,9};
rom * int16 fred;
fred = &vals[5];
//initialises a rom pointer to the value 6.
|
not work the coded? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 14, 2014 4:08 pm |
|
|
The rom pointer line had a slightly incorrect syntax. I've corrected it and
posted the test program below. IF you run it in MPLAB simulator, you
will get this output, which is correct:
Test program:
Code: | #include <18F4520.h>
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
//===================================
void main()
{
rom int16 vals[] = {1,2,3,4,5,6,7,8,9};
rom int16 *fred;
fred = &vals[5]; //initialises a rom pointer to the value 6.
printf("%ld \r", *fred);
while(1);
} |
|
|
|
Ralf2
Joined: 05 Feb 2009 Posts: 10
|
|
Posted: Mon Jul 14, 2014 4:29 pm |
|
|
Thanks PCM
And as I walk the array with pointer?
Because these not work
Code: | for(int i=0; i<10; i++)
printf("%ld \r", *(fred+i));
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jul 14, 2014 7:07 pm |
|
|
There appears to be a problem in which CCS doesn't like pointer
arithmetic with rom pointers. There might be some trick to make it work.
Instead, I want to ask, what do you really want to do ? Forget rom
pointers for the moment. Just tell me what you want to do. There
will probably be some easier way to do it, if I know what you want. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|