View previous topic :: View next topic |
Author |
Message |
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
Problem with structures and assigning strings |
Posted: Fri Jul 17, 2009 9:08 am |
|
|
Hey all,
I'm trying to create a UI using a 16x1 character LCD. I'm using structures for this. My code's working, but I need to optimize it and iron out the chinks. I've only included the relevant stuff below.
My structure definition:
Code: | struct menu_type
{
char string[8];
int8 back;
int8 top;
int8 bottom;
int8 forward;
}menu[6];
int8 new_menu, current_menu, var; |
I define items of the structure like this:
Code: | //level 1
menu[0].string[0]="1";
menu[0].string[1]=" ";
menu[0].string[2]=" ";
menu[0].string[3]=" ";
menu[0].string[4]=" ";
menu[0].string[5]=" ";
menu[0].string[6]=" ";
menu[0].string[7]=0x00;
menu[1].string[0]="2";
menu[1].string[1]=" ";
menu[1].string[2]=" ";
menu[1].string[3]=" ";
menu[1].string[4]=" ";
menu[1].string[5]=" ";
menu[1].string[6]=">";
menu[1].string[7]=0x00;
//level 2
menu[2].string[0]="2";
menu[2].string[1]=".";
menu[2].string[2]="1";
menu[2].string[3]=" ";
menu[2].string[4]=" ";
menu[2].string[5]="<";
menu[2].string[6]=" ";
menu[2].string[7]=0x00;
menu[3].string[0]="2";
menu[3].string[1]=".";
menu[3].string[2]="2";
menu[3].string[3]=" ";
menu[3].string[4]=" ";
menu[3].string[5]="<";
menu[3].string[6]=">";
menu[3].string[7]=0x00;
menu[4].string[0]="2";
menu[4].string[1]=".";
menu[4].string[2]="3";
menu[4].string[3]=" ";
menu[4].string[4]=" ";
menu[4].string[5]="<";
menu[4].string[6]=" ";
menu[4].string[7]=0x00;
//level 3
menu[5].string[0]="2";
menu[5].string[1]=".";
menu[5].string[2]="2";
menu[5].string[3]=".";
menu[5].string[4]="1";
menu[5].string[5]="<";
menu[5].string[6]=" ";
menu[5].string[7]=0x00;
//////////////////////////level 1
menu[0].back=0;
menu[1].back=1;
menu[0].top=1;
menu[1].top=0;
menu[0].bottom=1;
menu[1].bottom=0;
menu[0].forward=0;
menu[1].forward=2;
///////////////////////////level 2
menu[2].back=1;
menu[3].back=1;
menu[4].back=1;
menu[2].top=4;
menu[3].top=2;
menu[4].top=3;
menu[2].bottom=3;
menu[3].bottom=4;
menu[4].bottom=2;
menu[2].forward=2;
menu[3].forward=5;
menu[4].forward=4;
///////////////////////////level 3
menu[5].back=3;
menu[5].top=5;
menu[5].bottom=5;
menu[5].forward=5; |
For some reason I can't define the string like this: Code: | menu[0].string[6]="hello" | Only the first letter of the string is stored starting from (address of) string[6]. I have verified this from the disassembly listing. It's funny because this works fine: Code: | char str[6]="hello" |
Another problem is memory. As far as I understand, a PIC16 only allows me to store data in 128 contiguous RAM locations. So I can't create an array greater than 6, of the type *menu_type*. I get a "Data too big" error.
This code is just for testing, but ultimately each item of the structure *menu_type* will be 21 bytes big, and I'd like to have at least 25 such items, ie menu[25]. I don't mind dumping this all into program memory, but a ROM directive doesn't work. Will I need to abandon my structure and resort to a different approach?
Using v4.084 on a 16F876A for testing. Ultimately I'd like to put this onto a 16F628A (yes, I know I'm running close to the limits here! )
I hope I've defined my problem clearly enough.
Rohit |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Guest
|
|
Posted: Fri Jul 17, 2009 12:50 pm |
|
|
hmmm.
String in ccs can't be directly assigned a "string".
ex:
Code: | char s[8]="1234567"; //init and assign
s[0]='p';//assigning a char to the first location in s
printf("%s\n",s); //p234567
|
you can use(but your version is faster):
Code: | strcpy(menu[0].string,"1234567");
printf("Out:%s\n",menu[0].string); |
|
|
|
Rohit de Sa
Joined: 09 Nov 2007 Posts: 282 Location: India
|
|
Posted: Fri Jul 17, 2009 10:00 pm |
|
|
Thanks for the replies.
PCM, I've implemented what I read in the links and it works fine. For a block of data greater than 256 words I get a "Data too big" restriction. I'm sticking with a PIC16, so I'm using multiple structures with a linked-list kind of approach.
Rohit |
|
|
|