Exit outside from a loop.

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
vibodha
Posts: 7
Joined: 19 Jun 2013 08:13

Exit outside from a loop.

#1 Post by vibodha » 08 Apr 2014 06:24

Hello I'm new to mikroC.
I'm stuck with a simple program..
This Is what I want to do.

Blinking LEDs of PortD in every time. (infinite loop) ---Working without a problem
Stop Blinking when Interrupt Received on INT0.
-Interrupt received correctly. But I cannot exit from the while loop. I know This can be done by setting a flag in the interrupt routine and checking the while condition while(flag). But I want to do an intimidate exit from the loop without going to the while loop condition.

Here is the chord.... Please Help me :?: :?: :?: :?: :?:

Code: Select all

void interrupt(){          // Interrupt rutine
  if(INT0IF_bit==1 ){
    INT0IF_bit=0;
    PORTD=0;
  }
}

  Blinking(){
          while(1){
          PORTD = 15;
          Delay_ms(50);
          PORTD = 0;
          Delay_ms(50);
           //Some more chords comes here
             ///////////////////////
          }
   }


void main() {              // Main program


     ADCON0 = 0;
      ADCON1 = 15;
      TRISB=1;            // Set PB7 as input
      TRISD =0;            // Set PortD as output
      TRISC=0;

 // RBIE_bit = 1;
 // RBIP_bit=0;
    INT0IE_bit=1;
   INTEDG0_bit=1;
   GIE_bit   = 1;   // Global Interrupt Enable

 Blinking();
  
}

vibodha
Posts: 7
Joined: 19 Jun 2013 08:13

Re: Exit outside from a loop.

#2 Post by vibodha » 08 Apr 2014 07:52

:? :? no replies

Acetronics
Posts: 715
Joined: 27 Dec 2006 14:33
Location: Le Tréport , FRANCE

Re: Exit outside from a loop.

#3 Post by Acetronics » 08 Apr 2014 12:17

Hi,

an interrupt is supposed to go back where it came from, once done ... soooo, nothing strange in what you see :roll:

if there is no stack issue to take care of, ... you simply can place a GOTO just after GIE = 1 ...

but it would be better to use a different program flow ( and good programming uses ... :mrgreen: )

Alain

vibodha
Posts: 7
Joined: 19 Jun 2013 08:13

Re: Exit outside from a loop.

#4 Post by vibodha » 08 Apr 2014 13:43

Thnxx Alain...

can I call "goto XXXX" in the interrupt routine???

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Exit outside from a loop.

#5 Post by Sparky1039 » 08 Apr 2014 17:12

can I call "goto XXXX" in the interrupt routine???
Very bad idea. It's not recommended because after a number of interrupts you will overrun the stack pointer and your PIC will crash. Doing so is very poor programming practice. The use of an interrupt flag as you proposed in conjunction with an "if" and "break" statement in the while loop is the best way.

vibodha
Posts: 7
Joined: 19 Jun 2013 08:13

Re: Exit outside from a loop.

#6 Post by vibodha » 08 Apr 2014 17:56

Thank you very much Sparky1039 ..
I'm new to programming and thnx for your advice..
Please give me a small example about your advice. this will be very usefull for me..

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Exit outside from a loop.

#7 Post by Sparky1039 » 08 Apr 2014 18:09

Something like this.

Code: Select all

unsigned char exitFlag = 0;

void interrupt()
{          // Interrupt routine
  if(INT0IF_bit==1)
    {
      INT0IF_bit=0;
      PORTD=0;
      exitFlag = 1;
    }
}

Blinking()
{
  while(1)
  {
    if (exitFlag == 1)
      {
        exitFlag = 0; // reset exitFlag
        break; // exit from while loop
      }
    PORTD = 15;
    Delay_ms(50);
    PORTD = 0;
    Delay_ms(50);
   //Some more chords comes here
     ///////////////////////
  }
//    >> more code here to manage the break from while loop <<
...
...
}

vibodha
Posts: 7
Joined: 19 Jun 2013 08:13

Re: Exit outside from a loop.

#8 Post by vibodha » 09 Apr 2014 16:09

Ahhh Got it....
Thank you Sparky1039... Ill try it... :D

android
Posts: 345
Joined: 12 May 2010 10:35
Location: Sinny, Straya

Re: Exit outside from a loop.

#9 Post by android » 09 Apr 2014 23:20

Or even (same result ...just rearranged slightly)...

Code: Select all

Blinking()
{
  while (!exitFlag)
  {
    PORTD = 15;
    Delay_ms(50);
    PORTD = 0;
    Delay_ms(50);
   //Some more chords comes here
     ///////////////////////
  }
  exitFlag = 0; // reset exitFlag
//    >> more code here to manage the break from while loop <<
...
...
}
Regards, android
(User of MikroC Pro + mikroProg)

Post Reply

Return to “mikroC PRO for PIC General”