View previous topic :: View next topic |
Author |
Message |
future
Joined: 14 May 2004 Posts: 330
|
Aborting main loop? |
Posted: Tue Jun 22, 2004 4:59 pm |
|
|
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
|
|
Posted: Tue Jun 22, 2004 5:22 pm |
|
|
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
|
|
Posted: Tue Jun 22, 2004 5:52 pm |
|
|
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
|
|
Posted: Tue Jun 22, 2004 6:12 pm |
|
|
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
|
|
Posted: Tue Jun 22, 2004 6:21 pm |
|
|
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
|
|
Posted: Tue Jun 22, 2004 6:29 pm |
|
|
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
|
|
Posted: Tue Jun 22, 2004 7:34 pm |
|
|
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
|
|
Posted: Tue Jun 22, 2004 7:35 pm |
|
|
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();
}
}
}
|
|
|
|
|