5 years, 6 months ago.

how to disable and enable interrupts

How can I disable the interrupt for (0.25) and enable it again in the following code:

#include "mbed.h"
InterruptIn squarewave(p5);     //Connect input square wave here
DigitalOut led(p6);
DigitalOut flash(LED4);

void pulse() {                 //ISR sets external led high for fixed duration
  led = 1;
  wait(0.01);
  led = 0;
}
 
int main() {
  squarewave.rise(&pulse);  // attach the address of the pulse function to
                                                        // the rising edge
  while(1) {              // interrupt will occur within this endless loop
    flash = !flash;
    wait(0.25);
  }
}

1 Answer

5 years, 6 months ago.

Something like?

squarewave.rise(NULL);
wait(0.25);
squarewave.rise(&pulse);

Also use

<<code>>
your code
<</code>>

and it will format things correctly.

Thanks for the answer, but I want to know how this can be done using these two functions : disable_irq(); enable_irq();

posted by Ebrahim Atya 18 Jun 2019

Short answer:

You don't do that.

Wait uses an internal timer interrupt. If you disable interrupts the timer won't run and so the wait will never end.

Disabling all interrupts should only every be done for the shortest possible time if you have some task that needs to take place uninterrupted. e.g. if you have a serial receive buffer that you want to copy you don't want more data arriving half way through the copy. So you disable interrupts, copy the contents and then re-enable them.

You don't disable all interrupts for a length of time just to stop something from happening, in that situation you disable the specific interrupt you want to stop. You can either use the mbed way above to remove the interrupt and then put it back or you can poke the interrupt handler directly and disable it using NVIC_DisableIRQ(device_IRQn) but you would need to look up the appropriate IRQ number for your board, it'll be in the device specific header files in the mbed library.

posted by Andy A 18 Jun 2019