View previous topic :: View next topic |
Author |
Message |
edi
Joined: 22 Dec 2003 Posts: 82
|
Define a string |
Posted: Sun Aug 01, 2004 11:43 am |
|
|
I'm trying to define 4 different strings options.
Then send to RS232 one of the option as a result of parameter.
I want to make a switch..case syntax and
printf string1 in case of 1, string 2 in case of 2 ...
Can someone help me with the correct definitions.
something like this (this one of course does not work)
...
#define OPTION1 0123text1
#define OPTION2 text2XYZ
#define OPTION3 axcdsw
#define OPTION4 stringnum4
....
switch (result)
{
case (1): printf (OPTION1);
case (2): printf (OPTION2);
case (3): printf (OPTION3);
case (4): printf (OPTION4);
} |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Sun Aug 01, 2004 3:18 pm |
|
|
It could be as simple as this.
switch (result)
{
case (1): printf ("0123text1");
case (2): printf ("text2XYZ");
case (3): printf ("axcdsw");
case (4): printf ("stringnum4");
} |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Mon Aug 02, 2004 6:31 am |
|
|
yes, but if I want to change the string I need to look into the program.
It is much more elegant to change it just in the definitions.
The strings may appeare few times in the program.
Any more suggestions, please? |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Aug 02, 2004 6:53 am |
|
|
Code: |
#define OPTION1 "0123text1"
#define OPTION2 "text2XYZ"
#define OPTION3 "axcdsw"
#define OPTION4 "stringnum4"
|
|
|
![](templates/subSilver/images/spacer.gif) |
T800 Guest
|
Re: Define a string |
Posted: Mon Aug 02, 2004 7:24 am |
|
|
maybe something like this ?!
const char OPT1[10]="foo";
const char OPT2[10]="bar";
[...]
printf("%S",OPT1);
rgds
T800 |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Aug 02, 2004 7:32 am |
|
|
Seems like a big waste of RAM unless CCS added support pointers to ROM strings. |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Mon Aug 02, 2004 8:57 am |
|
|
Mark wrote: | Seems like a big waste of RAM unless CCS added support pointers to ROM strings. |
Worth saying, that for the example given, where there are only constant strings, the 'better' solution, is:
#define OPTION1 "0123text1 "
#define OPTION2 "text2XYZ "
#define OPTION3 "axcdsw "
#define OPTION4 "stringnum4 "
....
switch (result)
{
case (1): putc(OPTION1);
case (2): putc(OPTION2);
case (3): putc(OPTION3);
case (4): putc(OPTION4);
}
This takes advantage of the CCS 'shortcut', where if a routine that accepts a single integer (like putc), is called with a constant string, the routine will be called repeatedly, with each character in turn from the constant string. This is smaller, and more efficient on RAM useage, than using the printf version, or converting the ROM strings to RAM.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Mon Aug 02, 2004 8:58 am |
|
|
Anonymous wrote: | Mark wrote: | Seems like a big waste of RAM unless CCS added support pointers to ROM strings. |
Worth saying, that for the example given, where there are only constant strings, the 'better' solution, is:
#define OPTION1 "0123text1 "
#define OPTION2 "text2XYZ "
#define OPTION3 "axcdsw "
#define OPTION4 "stringnum4 "
....
switch (result)
{
case (1): putc(OPTION1);
case (2): putc(OPTION2);
case (3): putc(OPTION3);
case (4): putc(OPTION4);
Remember also you need a 'break' after eack case, unless you _want_ it to print all four strings for the first option.
Best Wishes
}
This takes advantage of the CCS 'shortcut', where if a routine that accepts a single integer (like putc), is called with a constant string, the routine will be called repeatedly, with each character in turn from the constant string. This is smaller, and more efficient on RAM useage, than using the printf version, or converting the ROM strings to RAM.
Best Wishes |
|
|
![](templates/subSilver/images/spacer.gif) |
gunking88
Joined: 18 Jun 2004 Posts: 11 Location: WV
|
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Aug 02, 2004 10:48 am |
|
|
What does that have to do with strings? |
|
![](templates/subSilver/images/spacer.gif) |
edi
Joined: 22 Dec 2003 Posts: 82
|
|
Posted: Wed Aug 04, 2004 2:15 pm |
|
|
Ttelmah,
Thanks for your help, but your sample gives an error during the compilation:
"A numeric expression must appear here"
the error is for the line : " case (1): putc(OPTION1); "
(at the beginning I put the line : #define OPTION1 "0123text1 " )
Any suggestion please?
Edi |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Aug 04, 2004 2:21 pm |
|
|
This compiles just fine:
Code: |
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=57600, xmit=PIN_C6, rcv=PIN_C7)
#define OPTION1 "0123text1"
#define OPTION2 "text2XYZ"
#define OPTION3 "axcdsw"
#define OPTION4 "stringnum4"
void main()
{
int result;
switch (result)
{
case (1): printf (OPTION1);
case (2): printf (OPTION2);
case (3): printf (OPTION3);
case (4): printf (OPTION4);
}
while(TRUE)
{
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Aug 05, 2004 7:47 am |
|
|
for my 2 cents worth,... this is how I code something like that...
I think you could also use a switch.
Code: |
const char error_txt[16][16] =
{
"USART overflow", // -0-
"TXBuffer ovrflw", // -1-
"RXBuffer ovrflw", // -2-
"Bad Checksum", // -3-
"LCD busy", // -4-
"5", // -5-
"6", // -6-
"7",
"8",
"9",
"10",
"11",
"12",
"13",
"14",
"NO ERRORS"
};
//somewhere else in code
bit_set(ERRORS,1);//set error flag bit 1 for tx overflow
unsigned int16 ERRORS;
void ChkErr(void) //print out any errors that occured
{
int8 chk=0;
if (ERRORS==0){return;} //if no errors jump right back out
for(chk=0;chk<16;chk++)
{
if (bit_test(ERRORS,chk))
{
bit_clear(ERRORS,chk);
fprintf(STDERR,"ERROR(%U): %s.\r\n",chk,error_txt[chk]);
tx_buffer(NACK,sizeof(NACK));
rx_indx_i=rx_indx_o=0;
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Thu Aug 05, 2004 8:28 am |
|
|
edi wrote: | Ttelmah,
Thanks for your help, but your sample gives an error during the compilation:
"A numeric expression must appear here"
the error is for the line : " case (1): putc(OPTION1); "
(at the beginning I put the line : #define OPTION1 "0123text1 " )
Any suggestion please?
Edi |
Brilliant! (screams gently).
CCS's own 'shortcut', refuses to work with their own putc function...
This works:
Code: |
#define OPTION1 "TestString1"
#define OPTION2 "TestString2"
#define OPTION3 "AnotherTest"
#define OPTION4 "FinalTest"
#inline
void myputc(int val) {
putc(val);
}
|
Then in main:
Code: |
switch (sval) {
case 0:
myputc(OPTION1);
break;
case 1:
myputc(OPTION2);
break;
case 3:
myputc(OPTION3);
break;
case 4:
myputc(OPTION4);
break;
}
|
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
edi
Joined: 22 Dec 2003 Posts: 82
|
|
Posted: Thu Aug 05, 2004 3:18 pm |
|
|
Thanks a lot to all. |
|
![](templates/subSilver/images/spacer.gif) |
|