recursive function call to "_main"

This is a discussion on recursive function call to "_main" within the C Programming forums, part of the General Programming Boards category; Hello, i'm new here, using c language for a PIC16f877A and MPLABS IDE(any better free IDE's out there?) i'm pretty ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    2

    recursive function call to "_main"

    Hello, i'm new here,
    using c language for a PIC16f877A and MPLABS IDE(any better free IDE's out there?)

    i'm pretty new the c language!


    i have written some code that simply controls 3 buttons that when pushed, a counter counts up or down by 1, when held down counter++ or -- until it reachs 0. My results are beening displayed on an LCD.

    athough my code wrks perfectly i'm not sure i am going about this the right way.. take the (RB0 / minus) return to main function

    here's where the simple line i dont know should go!
    Code:
    void rtn_main (void)
    {
    	while (minus != 0)
    	{
    		if (plus == 0)
    		{
    			main(); /*<--------------is this right, it compiles and works fine*/
    		}
    	}
    }
    i am neck deep in a switch case statment when i call this so break and return do not seem to work.

    any ideas????


    code posted below... the lcdres.c not inclued

    Code:
    #include <pic.h>
    #include <stdio.h>
    #include "..\lcdres.c"
    
    
    __CONFIG (0x3739);
    
    #define plus RB1
    #define minus RB2
    
    const char wel[] = "----WELCOME-----";
    const char numr[] = "Number = ";
    const char full[] = "MaxLimit ";
    const char pak[] = "--Press A Key---";
    
    unsigned char readkeys = 0x00;
    unsigned char keypressed = 0x00;
    unsigned char num1 = 0x7f;
    
    unsigned char check_keys (void);
    void init (void);
    void key_holdup (void);
    void key_holddown (void);
    void delay (int t);
    void print_num (void);
    void print_full (void);
    void rtn_main (void);
    
    void main (void)
    {
    	init();
    	init_lcd();
    	cls();
    	printf("%s",wel);
    	nline();
    	printf("Enter Press 1&2");
    	while((RB0 == 1) || (RB1 == 1));
    	cls();
    	printf("!-Release Keys-!");
    	while((RB0 != 1), (RB1 != 1));
    	do
    	{ 
    		switch(check_keys())
    		{
    			case 0x00: {move_lcd(0x00); printf("%s",pak); print_num();break;}
    
    			case 0x01: {cls(); printf("Exit, Really??? "); nline(); printf("K2= yes / K0= no"); while(RB0 == 0); rtn_main(); cls(); break;}
    
    			case 0x02: {if (num1 != 255){(num1++; print_num(); key_holdup();}else{move_lcd(0x00); print_full(); print_num();} break;}
    
    			case 0x03: {if (num1 != 0){(num1--; print_num(); key_holddown();}else{move_lcd(0x00); print_full(); print_num();} break;}
    
    			default:   {move_lcd(0x00);printf
    ("Careful...");print_num();break;}
    		}
    	}
    	while(1);
    }
    void rtn_main (void)
    {
    	while (minus != 0)
    	{
    		if (plus == 0)
    		{
    			main();
    		}
    	}
    }
    void print_full (void)
    {
    	printf("%s",full);
    }
    	
    void print_num (void)
    {
    	nline();
    	printf("%s%d  ",numr, num1);
    }
    void delay (int t)
    {
    	int n;
    	n=0x0000;
    	while(n++ < t);
    }
    void key_holdup (void)
    {
    	unsigned int WT = 0x0000;
    	while (plus == 0) 
    	{
    		WT++;
    		if (WT >26000)
    		{
    			if (num1!=255) 
    			{
    				num1++;
    				print_num();
    			}
    			else
    			{
    				nline();
    				print_full();
    			}
    		}
    	}
    }	
    void key_holddown (void)
    {
    	unsigned int WT = 0x0000;
    	while (minus == 0)
    	{
    		WT++;
    		if (WT >26000)
    		{
    			if (num1!=0)
    			{
    				num1--;
    				print_num();
    		
    			}
    			else
    			{
    				nline();
    				print_full();
    			}
    		}
    	}
    }
    void init (void)
    {
    	TRISB = 0xFF;
    	OPTION = 0X00;
    	PORTB = 0x00;
    }
    unsigned char check_keys (void)
    {
    	readkeys = (PORTB & 0x07);  // & inclue only these pins
    	switch (readkeys)
    	{
    		case 0x07: {keypressed = 0x00; break;} //no keys pressed
    		case 0x06: {keypressed = 0x01; break;} //key 1 not in use
    		case 0x05: {keypressed = 0x02; break;} //key 2
    		case 0x03: {keypressed = 0x03; break;} //key 3
    		default: {keypressed = 0xFF; break;} // if portb doesnt match the hex value
    	}
    	return keypressed;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I wouldn't recommend calling main recursively, even though it's legal. You can get by with a loop (which you already have) and encapsulating the initialization code into a function. Then it's simply a matter of calling the initialization function and continuing the loop:
    Code:
    void main(void)
    {
        initialize();
    
        while (1) {
            switch(check_keys()) {
            /* ... */
            case 0x01:
                /* ... */
                initialize();
                continue;
            /* ... */
            }
        }
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    2
    Hey thanks for the quick reply, of course i can,, thats really usefull and probably the easiest way..., although from what you have put in ur code example makes me think that it would be better to hit that delete key and write higher and lower levels to my switch statment...

    YOUR WAY FIRST THO

    thankyou

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 08:42 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. how to get function call stack
    By George2 in forum C Programming
    Replies: 18
    Last Post: 11-11-2006, 07:51 AM
  5. Problem with Template Function and overloaded equality operator
    By silk.odyssey in forum C++ Programming
    Replies: 7
    Last Post: 06-08-2004, 05:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21