Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

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
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.
 
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.
 
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
 
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.

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.

Latest threads

New Articles From Microcontroller Tips

Back
Top