CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Problem with structures and assigning strings

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Rohit de Sa



Joined: 09 Nov 2007
Posts: 282
Location: India

View user's profile Send private message Visit poster's website

Problem with structures and assigning strings
PostPosted: Fri Jul 17, 2009 9:08 am     Reply with quote

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! Confused )

I hope I've defined my problem clearly enough.

Rohit
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 17, 2009 10:39 am     Reply with quote

In CCS, if you declare a variable as 'const', then it will be put in ROM.

See my post in this thread which shows how to put a structure in ROM,
and also how to initialize it.
http://www.ccsinfo.com/forum/viewtopic.php?t=32604

This post shows how to put an array of structures into ROM:
http://www.ccsinfo.com/forum/viewtopic.php?t=30610
Guest








PostPosted: Fri Jul 17, 2009 12:50 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Fri Jul 17, 2009 10:00 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group