View previous topic :: View next topic |
Author |
Message |
LTS
Joined: 08 Dec 2005 Posts: 8
|
Creating a string table... |
Posted: Wed Dec 21, 2005 1:59 pm |
|
|
Hello
I want to store several strings in a table. This is for displaying "info" screens on a graphic lcd.
I have no problem displaying
char info[]="Hello, this is the info screen";
glcd_putc(info);
I want to declare something like:
char info[][x]="skjdkhfs","sijdfhskd","jskdhfk";
And be able to to do f.ex
glcd_putc(info[1]);
glcd_putc(info[2]);
and so on..
Tried alot of things, but get tons of errors..
Is it possible to create a table with lets say 10 elements of 50 chars?
Thanks in advance
LT |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
|
LTS
Joined: 08 Dec 2005 Posts: 8
|
|
Posted: Wed Dec 21, 2005 2:42 pm |
|
|
Hmm, ok, i guess i could use that method, but is there no way to create a simple table of strings, and then use simple indexes "info[x]" to reach? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 21, 2005 2:46 pm |
|
|
Quote: | but is there no way to create a simple table of strings, and then use simple indexes "info[x]" to reach? |
Here is one example. It displays:
Quote: |
ABCD
Hello
Hi there
123456789
|
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define NUM_STRINGS 4
#define STRING_BUF_SIZE 10
const char array[NUM_STRINGS][STRING_BUF_SIZE] =
{
{"ABCD"},
{"Hello"},
{"Hi there"},
{"123456789"}
};
void main()
{
int8 i;
int8 j;
int8 c;
for(i = 0; i < NUM_STRINGS; i++)
{
for(j = 0; j < STRING_BUF_SIZE; j++)
{
c = array[i][j];
if(c == 0) // If reached end of string
{
putc(10); // then display CRLF and break
putc(13);
break;
}
else
putc(c);
}
}
while(1);
} |
|
|
|
LTS
Joined: 08 Dec 2005 Posts: 8
|
|
Posted: Wed Dec 21, 2005 2:54 pm |
|
|
Great, thats exactly what i meant.. but.. how many elements and how many chars per element can i use? I really need each element to contain between 50-100 chars.. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 21, 2005 3:13 pm |
|
|
Unfortunately, you can't do it. CCS says this in the manual:
Quote: | ROM arrays may not occupy more than 256 locations. |
So you can't do it with one large two-dimensional const array.
Example:
This compiles OK:
Code: | const char array[2][128] =
{
{"ABCD"},
{"Hello"},
}; |
But this gives an error because 2 x 129 = 258, and that's greater than 256.
Code: | const char array[2][129] =
{
{"ABCD"},
{"Hello"},
}; |
|
|
|
LTS
Joined: 08 Dec 2005 Posts: 8
|
|
Posted: Wed Dec 21, 2005 3:20 pm |
|
|
Damn... I guess I could use several arrays, but then again I get an indexing problem. What is the best way to store several strings around 100 chars, and easily use an integer to reach the correct one? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 21, 2005 3:38 pm |
|
|
Here is one way. The code below displays this:
Quote: | ABCD
Hello World
The quick brown fox jumps over the lazy dogs back |
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
const char msg1[] ={"ABCD"};
const char msg2[] ={"Hello World"};
const char msg3[] ={"The quick brown fox jumps over the lazy dogs back"};
void display_msg(int8 index)
{
switch(index)
{
case 1:
printf(msg1);
break;
case 2:
printf(msg2);
break;
case 3:
printf(msg3);
break;
}
printf("\n\r");
}
//=====================================
void main()
{
display_msg(1);
display_msg(2);
display_msg(3);
while(1);
} |
|
|
|
LTS
Joined: 08 Dec 2005 Posts: 8
|
|
Posted: Wed Dec 21, 2005 3:49 pm |
|
|
Yeah, that would work. Thanks! |
|
|
|