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

function pointers and eeprom

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







function pointers and eeprom
PostPosted: Mon Apr 21, 2008 6:05 am     Reply with quote

Hello I have two questions.

Question 1.

I am creating a menu driven type system using a PIC18F8520. Instead of using many switch case statements trying to implement function pointers. However I get a weird error message of recursion. Can someone explain to me why this is? (I think I know what recursion is: “ function calling itself ” in simple terms). Below is a explanation.

Example

Code:

Void mainMenu();
Void  Menu1(){};
Void  Menu2(){};
Void  Menu3(){};
Void  Menu4(){};

Void  subMenu1_1(){};
Void  subMenu1_2){};
Void  subMenu1_3(){};
Void  subMenu1_4(){};


typedef void (*pt1Function)();      /* used for function pointer type */

Void MainMenu(){
char selection_off_choice =0;
pt1Function funcSelection[4] =
{& Menu1,& Menu1,&Menu1,& Menu1};       /* Function array */

while(button2Pressed == false){
if(buttonPressed) {// scroll along next function
   
   if(selection_off_choice < 4) selection_off_choice++;
   else selection_off_choice = 0;

(*funcSelection [selection_off_choice])();          /* only used for displaying highlighted options */      
}
}
}


Just say in my Menu1 function I have the same as MainMenu() it has sub menus therefore what to use function pointers. This is where the problem lays I get a recursion error for Menu1(). Why is this? (Please note to get round at the moment I will use a switch statement in all sub menus. )


Code:

Void Menu1(){

char selection_off_choice =0;
pt1Function funcSelection[4] =
{& subMenu1_1,& subMenu1_2,& subMenu1_3,& subMenu1_4};       /* Function array */

while(button2Pressed == false){
if(buttonPressed) {// scroll along next function
   
   if(selection_off_choice < 4) selection_off_choice++;
   else selection_off_choice = 0;

(*funcSelection[selection_off_choice])();          /* only used for displaying highlighted options */      
}
}

}



Question 2:

Inbuilt EEPROM, what is the best way of saving struct types; Do I have to go through each element in the struct one at a time and save or is there a easier way??


I am using CCS version 4.062 and MPLab 8V
Thanks
[/code]
Matro
Guest







PostPosted: Mon Apr 21, 2008 6:15 am     Reply with quote

Which function(s) are/is disable to prevent reentrancy?

Matro.
Matro
Guest







PostPosted: Mon Apr 21, 2008 6:17 am     Reply with quote

Reentrancy isn't a function that calls itself. This is recursivity.
Reentrancy is just the fact to call 2 or more times a function at the same time.
Typically when a function is called by the main() and also by an ISR. So the function can be entered 2 times at the same time.

Matro.
Ttelmah
Guest







PostPosted: Mon Apr 21, 2008 7:13 am     Reply with quote

Try changing the name of the selection array inside the subroutine.
'Recursion', is where a function ends up potentially calling itself. Because of the lack of a variable stack in the PIC, this will result destruction of the variables in the first copy.
What you post, shouldn't lead to recursion (it _would_ if the menu selections in the second menu, were the same as the first). However I'd suspect that function arrays are treated by the system as if they are 'global', when testing for this, so since the array name is the same in the subroutine, the compiler decides there is a potential for recursion.

Best Wishes
ssssssss
Guest







PostPosted: Mon Apr 21, 2008 7:48 am     Reply with quote

Hey Ttelmah, Matro

Thanks for the quick replies.

Matro I get a recursion error and not a reentrancy error.

*** Error 69 "main.c" Line 74(0,1): Recursion not permitted [Menu1]***


Ttelmah I tried your suggestion and still having the same problem the code is below. (please note the code is an example might have other errors but has been written to demonstrate the problem)

Code:

void mainMenu2();
void  Menu1();
void  Menu2();
void  Menu3();
void  Menu4();

void  subMenu1_1();
void  subMenu1_2();
void  subMenu1_3();
void  subMenu1_4();


typedef void (*pt1Function)();      /* used for function pointer type */
void MainMenu2(){
char selection_off_choice =0;
char button2Pressed =0;
char buttonPressed  = 0;

pt1Function funcSelection[4] = {& Menu1,& Menu2,&Menu3,& Menu4};       /* Function array */

while(button2Pressed == false){
   if(buttonPressed) {// scroll along next function
   
         if(selection_off_choice < 4) selection_off_choice++;
         else selection_off_choice = 0;

   (*funcSelection [selection_off_choice])();          /* only used for displaying highlighted options */       
   }
   }
}


typedef void (*pt2Function)();      /* used for function pointer type */
void Menu1(){
char selection_off_choice =0;
char button2Pressed =0;
char buttonPressed  = 0;

pt2Function functionArray[4] = {& subMenu1_1,& subMenu1_2,& subMenu1_3,& subMenu1_4};       /* Function array */

while(button2Pressed == false){
   if(buttonPressed) {// scroll along next function
   
         if(selection_off_choice < 4) selection_off_choice++;
         else selection_off_choice = 0;

   (*functionArray [selection_off_choice])();          /* only used for displaying highlighted options */       
   }
   }
}



void main(){
   MainMenu2();
}



I haven't a clue yet but if I find out will post back.


best regards
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 21, 2008 12:09 pm     Reply with quote

This thread might help. It has an example of using function pointers.
http://www.ccsinfo.com/forum/viewtopic.php?t=33458
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