View previous topic :: View next topic |
Author |
Message |
Predator_MF
Joined: 26 Mar 2005 Posts: 10
|
A simple question about strings in CCS! |
Posted: Tue Jun 14, 2005 5:29 pm |
|
|
I'm using CCS since an year and more...anyways, I've tryed many times to run a simple code like this:
Code: |
char* Hello()
{return "Hello"}
...
priintf("%S",Hello());
|
Never works for me! Why? Another test done by me:
Code: |
strcpy(s,Hello());
....
priintf("%S",Hello());
|
Also not working? I have so many programs behind my back that needed to be written in the way described above, but I just try to round writing that way. Can someone explain why is this code not working and how should I write it |
|
|
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Tue Jun 14, 2005 5:55 pm |
|
|
CCS does not support pointers to constant strings. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jun 15, 2005 2:00 am |
|
|
First some theory:
The processors that are used in a PC have a Von Neuman hardware architecture with a single memory space. The PIC processor has a Harvard hardware architecture with seperate data and instruction memories, this allows for faster program execution because instructions and data can be fetched simultaneously. Using Google you can find more details over the differences between Harvard and Von Neumann architectures.
Programs that use constant data, like fixed text strings, need a way to store this data permanently, i.e. in ROM instead of in RAM. This is often resolved by storing the constant data in program memory (flash). In order to get access to the stored data you need to execute some special instructions to transfer data from instruction memory to data memory. The need for these extra instructions is why a direct pointer to constant data in instruction memory is not working.
The workaround is to first copy contstant strings to a RAM buffer and then all pointer operations work as expected.
Code: | char SearchString[10]; // This must be big enough including the NULL terminator
strcpy(SearchString, "OK");
|
|
|
|
Predator_MF
Joined: 26 Mar 2005 Posts: 10
|
|
Posted: Wed Jun 15, 2005 2:13 am |
|
|
@ckielstra, thanks alot. I know what about PIC architecture and so on. I told erlier that I've written many programs working with PIC12, PIC16 and PIC18 written with CCS. My question is more than simple:
Code: | char* Hello();
{return "Hello"}; |
This code works on PC, not on PIC (CCS). How should I write my function to output a string? Using a global variable that is with defined length? (This is the way I was doing it for a long time) |
|
|
Ttelmah Guest
|
|
Posted: Wed Jun 15, 2005 3:05 am |
|
|
Code: |
char* Hello() {
//Assign RAM storage to hold the string
static char store[8];
//copy string from ROM to RAM
strcpy(store,"Hello");
return store;
}
...
priintf("%S",Hello());
|
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Jun 15, 2005 10:20 am |
|
|
The static variable in Ttelmah's example is better than using a global variable because now it is clear to which function the variable belongs, limiting the scope. Disadvantage is that this variable is permanently allocating valuable RAM space.
Another solution I sometimes use is to have the calling function allocate RAM on the stack.
Code: | void function1()
{
int8 Store[8];
Hello(Store);
printf("%S", Store);
}
void Hello(int8 *Buffer)
{
//copy string from ROM to RAM
strcpy(Buffer,"Hello");
} | Advantage of this solution is that you are not permanently allocating RAM. Disadvantage is that the pointer to the buffer is invalid as soon as you leave function1. |
|
|
|