View previous topic :: View next topic |
Author |
Message |
strasserh
Joined: 21 Jan 2007 Posts: 5
|
How can I use const data commands with printf |
Posted: Sun Jan 21, 2007 9:06 am |
|
|
I have a problem to access data with the new const data commands:
1. I declare constant data in programm memory and want to put it out with printf.
const char Menu1[][*]={"Pretrigger","Posttrigger","Sequence","One shoot"};
How can I access data to put it out to an LCD Display.
printf(lcd_put,"%s", Menu1[1]);
this doesn't work )
2. The same problem with:
char ROM commands[] = {"Pin|Port|RS232|V24"};
printf (lcd_put,"%s",commands[1]);
this doesn't work.
Has anybody used these commands? _________________ Helmut Strasser |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Sun Jan 21, 2007 9:27 am |
|
|
If you are using version 3.x and below, it doesn't support pointers to constants. |
|
|
strasserh
Joined: 21 Jan 2007 Posts: 5
|
constant data |
Posted: Sun Jan 21, 2007 11:46 am |
|
|
I use Version 4.021, and it is no problem to compile, but it doesn't work.
thx _________________ Helmut Strasser |
|
|
ferrumvir
Joined: 01 Feb 2006 Posts: 64 Location: England
|
|
Posted: Sun Jan 21, 2007 4:03 pm |
|
|
Hi,
I've only got versoin 3.xxx so I can't comment, if there are any changes in v4 that make what I'm about to say in-correct.
I understand (could be wrong) that PICs can't reference the ROM as pointers, you have to copy the constant character strings to RAM. I don't know if this is a CCS thing or a PIC thing.
I use something like this in all my "human interface/menu" programs.
Code: |
//==========================================================================
//==========================================================================
// STRING CONSTANTS
//==========================================================================
//==========================================================================
char STRstart[6];
char STRstop[5];
char STRshelp[6];
char STRset_[5];
char STRpwm_[5];
void str_init()
{
strcpy(STRstart,"start");
strcpy(STRstop,"stop");
strcpy(STRshelp,"shelp");
strcpy(STRset_,"set ");
strcpy(STRpwm_,"pwm ");
}
//==========================================================================
//==========================================================================
// END - STRING CONSTANTS
//==========================================================================
//==========================================================================
|
These variables are now stored in RAM, and can then be used as pointers in functions like printf.
Have a check of the manual for a fuller explanation.
Cheers Scott |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jan 21, 2007 5:06 pm |
|
|
If you initialize the RAM arrays like this, it will use far less ROM space.
Code: |
char STRstart[6]= {"start"};
char STRstop[5] = {"stop"};
char STRshelp[6] = {"shelp"};
char STRset_[5] = {"set"};
char STRpwm_[5] = {"pwm"};
|
|
|
|
ferrumvir
Joined: 01 Feb 2006 Posts: 64 Location: England
|
|
Posted: Mon Jan 22, 2007 1:58 am |
|
|
Thanks PMC_Programmer,
That's very useful to know, as I've only ever got close to the ROM limit when programming menus for hyperterminal.
Cheers Scott |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
Re: How can I use const data commands with printf |
Posted: Mon Jan 22, 2007 6:02 am |
|
|
strasserh wrote: | I have a problem to access data with the new const data commands:
1. I declare constant data in programm memory and want to put it out with printf.
const char Menu1[][*]={"Pretrigger","Posttrigger","Sequence","One shoot"};
How can I access data to put it out to an LCD Display.
printf(lcd_put,"%s", Menu1[1]);
this doesn't work )
2. The same problem with:
char ROM commands[] = {"Pin|Port|RS232|V24"};
printf (lcd_put,"%s",commands[1]);
this doesn't work.
Has anybody used these commands? |
Normally you would declare like this:
const char* Menu1[]={"Pretrigger","Posttrigger","Sequence","One shoot"}; |
|
|
strasserh
Joined: 21 Jan 2007 Posts: 5
|
is it a CCS bug or not, thats the question |
Posted: Mon Jan 22, 2007 3:08 pm |
|
|
Hi Mark!
Yes I thought like you did, but the CCS compiler didn't accept this syntax.
And if you use the original syntax it doesn't work, or I am too dumb to use it. _________________ Helmut Strasser |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 22, 2007 3:25 pm |
|
|
Mark is thinking of the C18 compiler. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jan 22, 2007 3:42 pm |
|
|
PCM programmer wrote: | Mark is thinking of the C18 compiler. |
Nope, this works for GNU for ARM as well as CodeWarrior for Freescale MCU's. I am currently developing code for both and this is how I would declare them. C18 is a little different in that you specify the rom qualifier. I don't know how CCS's version 4 of the compiler works, but if it allows pointers to constants, it should be something like this. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jan 23, 2007 8:59 am |
|
|
I do something like this.
This is only 1 line, 2,3,4 is just the same.
Its a 2d array, so all lines must be the same length.(16that is 15 and null termination)
int8 ERRORS; //is a global bit map, set bit 0 and it will show buffer overflow.
Code: | const char error_txt[1][16] =
{
"buffer overflow" // -0-
//"buffer overflow", // -0-
//" ",// -1-
//" ",// -2-
//" ",// -3-
//" ",// -4-
//" ",// -5-
//" ",// -6-
//" "
};
//========================== err_chk ===============================
void ChkErr(void) //print out any errors that occured
//**verifly that error text is applicable
{
int8 chk=0;
if (ERRORS==0){return;} //if no errors jump right back out
for(chk=0;chk<8;chk++)
{
if (bit_test(ERRORS,chk))
{
bit_clear(ERRORS,chk);
fprintf(DEBUG,"ERROR(%U): %s.\r\n",chk,error_txt[chk]);
rx_indx_i=rx_indx_o=0;
}
}
}
|
|
|
|
|