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

Aborting main loop?

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



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

Aborting main loop?
PostPosted: Tue Jun 22, 2004 4:59 pm     Reply with quote

My program flow need to abort/restart main() if a condition occurs.

Code:
#inline
void function1(void);
#inline
void function2(void);
#inline
void function3(void);

main() {
   for(;;) {
      function1();
      function2();
      function3();
   }
}
#inline
void function1(void) {
      do something;
}
#inline
void function2(void) {
      if(something>otherthing) RESTART MAIN LOOP;
}
#inline // this funcition will not be called if condition in function2 is true
void function3(void) {
      do something;
}


The code is more complex than this, but basically what I need.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 5:22 pm     Reply with quote

Have function2 return TRUE if you want the main loop to restart.
Otherwise, it should return FALSE.

Then in the main loop, check the return value of function2.
If it's True, then do a 'break', which will break you out of the for(;;) loop.
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 5:52 pm     Reply with quote

But if I break the program will end.

I need to restart it. I tried with labels and goto's but it does not accept a goto in a function pointing to a label in main.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 6:12 pm     Reply with quote

When you say "restart", do you want to restart at the beginning
of the for(;;) statement, or do you want to restart at the very
beginning of main (as if the PIC was reset) ?
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 6:21 pm     Reply with quote

Restart the FOR loop.

Before the loop there is a hardware init routine, error checks...

I dont need to "Restart" it, I need a way to stop the program flow and jump to the first instruction in the loop.

If I start to test what functions returns I think it will be a mess.

I'm thinking about coding all inline like BASIC.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 6:29 pm     Reply with quote

OK. Instead of executing a 'break' statement, you should execute
a 'continue' statement. This is a rarely used control-flow statement
(I probably have used it only a few times in my life), but it should
do what you need. For reference, here is the MSDN article on it:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/statem_17.asp

I read your post further and you seem discouraged, so I have
written a demo program below, which shows how to use 'continue'.

This program displays this on the Terminal window:
line 1
line 2
line 1
line 2
etc.

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

char my_function(void);

main()
{

 for(;;)
    {
     printf("line 1\n\r");
     delay_ms(500);
   
     printf("line 2\n\r");
     delay_ms(500);
     
     if(my_function() == TRUE)
        continue;
 
     printf("line 3\n\r");
     delay_ms(500);

    }
}
//-------------------------------------------------------------
char my_function(void)
{
// Your actual function will do a test, and return True or False
// based on the results of the test.   For this demo program,
// I will always return True.
return(TRUE);   
}
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 7:34 pm     Reply with quote

This is EXACTLY what I was looking for!

Thank you very much.

Thank God I moved from BASIC to C too!
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Tue Jun 22, 2004 7:35 pm     Reply with quote

I commonly use a variable to pull this off. It's not a real restart but does allow re-configuration of hardware.

Code:

void main()
{  While(1)
   {  Initialize_Comm_Port;
      Initialize_PWM;
      Initialized=1;
      While(Initialized)
      {  COMM1_Service();
         Analog_Service();
         Clock_Service();
      }
   }
}
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