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 support@ccsinfo.com

Putting stuff in ROM

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



Joined: 09 Oct 2008
Posts: 16

View user's profile Send private message

Putting stuff in ROM
PostPosted: Sat Jan 31, 2009 5:38 pm     Reply with quote

Hey all,

I'm trying to make a little menu system for my LCD project is there a way I can get this into ROM seeing as none of it will change, so I can save a bit of space?
Code:

void Test(void)
{
}

typedef void (*_fptr)(void);

typedef struct MENU_ITEM_STRUCT {
   int8 Type;
   char Text[10];
   _fptr FunctionPntr;
} MENU_ITEM;

MENU_ITEM MainMenu[] = {
   {MENU_ITEM_TYPE_MENU_HEADER, "Main menu", Test},
   {MENU_ITEM_TYPE_SUB_MENU, "Show", 0},
   {MENU_ITEM_TYPE_COMMAND, "About", 0},
   {MENU_ITEM_TYPE_END_OF_MENU, "", 0}};

I kept running into problems with "must evaluate to constant" etc.
I am using a recent compiler.

Edit:- It would be nice if this menu system had sub menus, I was thinking make a link to the parent menu from the submenu, but that would require some forward declarations, and am I right in thinking CCS doesn't like that trickery with structs?
Anyway around it? - I guess I could use a stack of pointers to these tables.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 01, 2009 1:21 pm     Reply with quote

You need to post your code as part of a test program. See this example:
http://www.ccsinfo.com/forum/viewtopic.php?t=33458
I pasted your code into a framework for 18F452, with #fuses, etc., and
a main(), and I got this error:
Quote:

Undefined identifier MENU_ITEM_TYPE_MENU_HEADER

If you can post a test program that cleans up this kind of error, then
I can work on the function pointer issue in your code, which is what
you really want to get working.
Wipster



Joined: 09 Oct 2008
Posts: 16

View user's profile Send private message

PostPosted: Mon Feb 02, 2009 1:22 pm     Reply with quote

Oh woops sorry, ok here it is in a example - I blind programmed this as my board doesn't have an RS232 but it should illustrate what I want to do (and work).

Code:
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define MENU_ITEM_TYPE_MENU_HEADER      0
#define MENU_ITEM_TYPE_SUB_MENU         1
#define MENU_ITEM_TYPE_COMMAND         2
#define MENU_ITEM_TYPE_END_OF_MENU      3

void Test(void);

typedef void (*_fptr)(void);

typedef struct MENU_ITEM_STRUCT {
   int8 Type;
   char Text[10];
   _fptr FunctionPntr;
} MENU_ITEM;

MENU_ITEM MainMenu[4] = {
   {MENU_ITEM_TYPE_MENU_HEADER, "Main menu", 0},
   {MENU_ITEM_TYPE_COMMAND, "Show", Test},
   {MENU_ITEM_TYPE_COMMAND, "About", 0},
   {MENU_ITEM_TYPE_END_OF_MENU, "", 0}};

void main() {
   _fptr temp;   

   temp = MainMenu[1].FunctionPntr;

   (*temp)();
}

void Test(void)   {
   printf("Hello World\n\r");
   printf("%s",MainMenu[1].Text);
}


Oh and by the way thanks PCM Programmer for that example on how to use function pointers, was a massive help.
That bit should be ok but seeing as none of table entrys change it would be nice to have it in ROM.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 02, 2009 1:31 pm     Reply with quote

Quote:
void main() {
_fptr temp;

temp = MainMenu[1].FunctionPntr;

(*temp)();

while(1);
}

I added the while(1) statement at the end of main() so it didn't cut off
part of the "Show" text. Then it displayed:
Quote:
Hello World
Show

So do you still have a problem, or are you done ? (i.e. it's working OK)
Guest








PostPosted: Wed Feb 04, 2009 8:33 am     Reply with quote

Well there is a small problem with this, if the user selects an entry without a pointer to a function (its supposed to be 0) it crashes.

I have the entry set to 0 so I can check "if (temp != 0) (*temp)();" but it doesn't seem to work correctly as temp gets filled with 0xFF but if I try to check against 0xFF it still crashes.

Change main() to:
Code:
void main() {
   _fptr temp;   

   temp = MainMenu[2].FunctionPntr;

   if (temp != 0) (*temp)();

   while(1);
}


That should illustrate the problem I face now.
Wipster



Joined: 09 Oct 2008
Posts: 16

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 8:34 am     Reply with quote

Woops forgot to sign in, sorry.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 04, 2009 1:18 pm     Reply with quote

I couldn't duplicate your problem. I took your existing code, as posted
earlier, and changed the main() to match your latest post. Then I added
a printf statement to look at the value of the temp pointer. I got 0.
Example:
Quote:
temp = 0000

Code:
void main()
 {
   _fptr temp;   

   temp = MainMenu[2].FunctionPntr;

   printf("temp = %LX", temp);  // *** For Testing ***

//   if (temp != 0) (*temp)();

while(1);
}

Can you post a test program that shows the problem consistently ?
Also post your compiler version. If you do those two things I can work on it.
Wipster



Joined: 09 Oct 2008
Posts: 16

View user's profile Send private message

PostPosted: Fri Feb 06, 2009 2:50 pm     Reply with quote

Sorry I can't seem to duplicate my problem, I don't know why it's happening - it's pretty much a non issue because all of the entrys in the menu will be linked with something, but in this testing phase I have pointed them to a NULLFunction which does nothing.
Thanks anyway.
future_
Guest







PostPosted: Mon Feb 09, 2009 4:49 pm     Reply with quote

Hi,

Does this code compile/work on ccs version 3.249?

This version could not find the function addresses last time I tried to initialize structs with pointers to function.

Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 09, 2009 5:01 pm     Reply with quote

The code posted in this thread won't compile in vs. 3.249. The code
in my link compiles, but it gives an error in the MPLAB simulator.
So either vs. 3.249 is too early for function pointers to work properly,
or it would take some research to find work-arounds.
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