Is it possible to break or stop a while clasue because of an interrupt?

Status
Not open for further replies.

JavierMA

New Member
Hi all,

I have a project in Code Compose Studio coded in C language that, among other things, triggers an interrupt when a switch is released. The interrupts are configured and work properly except in one case, when the code is inside a while loop that would end when a button is released after presssing it and, therefore, the interrupt is triggered.

At this moment the only way it works is by polling the state of the switch inside the while loop, but I would like to be able to exit the loop with the interrupt. Is that possible?


Thanks,
Javier
 
Thanks Nigel, though I am afraid I already tried that option before asking in the forum and it does not work.


Regards,
Javier
 
Thanks Nigel, though I am afraid I already tried that option before asking in the forum and it does not work.


Regards,
Javier

As Pommie said, you're doing it wrong then. Make sure you're not mixing up '==' and '=' - my favourite error
 
As Pommie said, you're doing it wrong then. Make sure you're not mixing up '==' and '=' - my favourite error
Still gets me too, so I try to do if (6==var) as this will give an error if single equals is used.

Mike.
 
Anyway - back to the OP - can you post the code you tried?. Use the code tags to maintain the formatting.
 
Thanks Nigel, though I am afraid I already tried that option before asking in the forum and it does not work.


Regards,
Javier
note... if a variable is used in two functions, it must be global to those function. BUT a flag is read and not used, so an optimiser will bin it. so it must be declared volatile..

eg.. volatile char mFlag;

Then it isn't destroyed outside the functions.
 

I declare mine as static:

C:
static bit POWER_ON=false;                  // flag for power on or off

Presumably that does the same, and all bit variables in XC8 have to be global anyway, but that's what you need.
 
Last time I tried to use bit as boolean it gave an error in xc8. Will try again tomorrow, nearly 1am here.

Mike.
 
Last time I tried to use bit as boolean it gave an error in xc8. Will try again tomorrow, nearly 1am here.

Mike.

Did you try it as a global variable? - it gives an error otherwise.

Anyway, get to bed - it's too late.
 
If checking a flag doesn't work then you're doing it wrong.

Mike.

Many thanks Pommie. I already use a global variable to keep the code inside the while clause running unless the switch is pressed, and by checking the flag again I realised that part was correctly done, but a few lines later I could see that my error was that I entered the while clause without actually exiting the interrrupt handler function, so it could not be called again after the next press and release of the button. By letting the handler function finish and changing the place in code where the function is called now the while clause is stopped after pressing the button as expected.


Thanks,
Javier
 

You should keep the ISR as short as possible, do everything you can outside the ISR - just set the flag in there.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…