detachInterrupt(pin_num)

OS: Windows 10
Board: Nano 33 BLE
IDE: Arduino IDE Version: 1.8.19
Mbed OS: Version 2.6.1

every time I add a "deattachInterrupt(digitalPinToInterrupt(PIN);"
instruction, the serial communication is halted.
This is happens with all codes.
Is there any trouble with compiler?

Thanks

No.
But you are missing a bracket in that instruction you posted )

You should not detach an interrupt unless you previously attached one.
However, it is an odd thing to do detaching an interrupt anyway.

Can you post all your code

attachInterrupt() disconnects an IO line from a port and connects it with the interrupt logic; therefore, at the end of interrupt process, the detachInterrupt() should be excuted to return the IO line back to the respective port.

I know what it does for goodness sake. It is just that beginners tend to think you need to do this. It is quire a odd thing to want to do. What sort of problem needs this?
Normally you have an interrupt for the duration of your code,

Please show me in the 328P datasheet where that happens?

And explain why I can still read the pin?

Thanks a lot for answers, yes I a forgot a bracket but the mine is a conceptual question, because I've the issue only with Nano 33 BLE and not with AVR boards.
My question is related using the deattachInterrupt(digitalPinToInterrupt(7)) command in loop() after an attachInterrupt(digitalPinToInterrupt(7) in setup().
The serial is hang just like all the interruts were blocked and not only that of the pin 7.
Surely is a my bug, but I'd like to use the interrupts instead of the PulseInLong instruction and I cannot find an example of source code running for Nano 33 BLE to measure a low frequency signal (<50Hz) without influence of the rest of program with millis() and delay().

I cannot confirm what you are saying. When I run the simple test code posted below I see

starting
interrupt attached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
const uint8_t buttonPin = 2;
volatile boolean newPress = false;
volatile byte readPin;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  delay(500);
  Serial.println("starting");
  pinMode(buttonPin, INPUT);
  delay(500);

  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, CHANGE);
  Serial.println("interrupt attached");;
}

void loop() {
  delay(1000);
  detachInterrupt(digitalPinToInterrupt(buttonPin));
  Serial.println("interrupt detached");

}


void buttonISR(void) {
  newPress = true;
}

1. I got the following conceptual idea (Fig-1) of port disconnection during interrupt based on this code bitRead(EIFR, INTF0) which I use to read interrupt status. Now, I see that digitalRead(2) is also working while attachInterrupt() instruction is active.
intTrigger

2. Thank you to raise flag which has helped me converting my ignorance into knowledge.

3. The following test program shows that detachInterrupt() instruction disables the "Local Interrupt enable bit (INT0))" of INT0-interrupt.

void setup()
{
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), ISRZ, FALLING);
  Serial.println("IRQ Trigger Level: FALLING");
  Serial.println("No IRQ signal is injected.");
  Serial.println();
  
  Serial.println("== Statuses after attcahInterrupt().== ");
  Serial.print(digitalRead(2)); //DPin-2 = 1
  Serial.println(" Status of DPin-2 as input port-pin");

  Serial.print(bitRead(EIFR, INTF0));  //INTF0 = 0
  Serial.println(" Status of INTF0 flag.");

  Serial.print(bitRead(EIMSK, 0)); //INT0 = 1
  Serial.println(" Status of Local Interrupt Enable Bit.");

  Serial.print(bitRead(SREG, 7));   //I-bit = 1
  Serial.println(" Status of Global Interrupt Enable Bit.");
  Serial.println();

  Serial.println("== Stauses of the above events after detachInterrupt()==");
  detachInterrupt(digitalPinToInterrupt(2));
  Serial.println(digitalRead(2));   //DPin-2 = 1
  Serial.println(bitRead(EIFR, INTF0));  //INTF0 = 0
  Serial.println(bitRead(EIMSK, 0));//INT0 = 0
  Serial.println(bitRead(SREG, 7)); //I-bit = 1
}

void loop()
{

}

void ISRZ()
{

}

Output:

IRQ Trigger Level: FALLING
No IRQ signal is injected.

== Statuses after attcahInterrupt().== 
1 Status of DPin-2 as input port-pin
0 Status of INTF0 flag.
1 Status of Local Interrupt Enable Bit.
1 Status of Global Interrupt Enable Bit.

== Stauses of the above events after detachInterrupt()==
1
0
0
1

Thanks Cattledog and GolamMostafa, I programmed a new Nano 33 BLE board with the suggested test code:

const uint8_t buttonPin = 2;
volatile boolean newPress = false;
volatile byte readPin;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  delay(500);
  Serial.println("starting");
  pinMode(buttonPin, INPUT);
  delay(500);

  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, CHANGE);

  Serial.println("interrupt attached");;
}

void loop() {
  delay(1000);

  detachInterrupt(digitalPinToInterrupt(buttonPin));

  Serial.println("interrupt detached");
}

void buttonISR(void) {
  newPress = true;
}

and the result in serial monitor is:

starting
interrupt attached
interrupt detached

( :sleeping:serial hanged)

then I reprogrammed same board with the code:

const uint8_t buttonPin = 2;
volatile boolean newPress = false;
volatile byte readPin;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  delay(500);
  Serial.println("starting");
  pinMode(buttonPin, INPUT);
  delay(500);

  attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, CHANGE);

  Serial.println("interrupt attached");;
}

void loop() {
  delay(1000);

**//  detachInterrupt(digitalPinToInterrupt(buttonPin));**

Serial.println("interrupt detached");
}

void buttonISR(void) {
  newPress = true;
}

with detach instruction commented and the result is as expected :

starting
interrupt attached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached
interrupt detached

........ for ever

I verified it several times. Is there any other checks in my tool chain to do?

Since I could not confirm your issue on a Nano 33 BLE I don't really know where to point you.

What platform is running the monitor?
Can you try a different terminal program instead of the Serial monitor?

Are you reselecting the port and reopening the monitor after the program loads?

What happens if you manually go into boot mode, load the program, and then select the communication port? Some times the ide does not manage the port switching correctly.

Thanks for your patience, I will try to be as precise as possible, in the meantime I changed my PC with another one :
Windows 10 Pro
Arduino IDE 1.8.19
Arduino MBed OS Nano Boards 2.6.1
info board: BN: Arduino Nano 33 BLE
VID: 2341
PID: 805a
SN: CFEADBB0D6B10330

code (a little changed from your):


volatile boolean newPress = false;

void setup() {
Serial.begin(9600);
while (!Serial);
delay(500);
Serial.println("starting");
pinMode(7, INPUT);
delay(500);
attachInterrupt(digitalPinToInterrupt(7), buttonISR, RISING);
Serial.println("Interrupt attached");;
}

void loop() {
delay(1000);
detachInterrupt(digitalPinToInterrupt(7));
Serial.println("interrupt detached");
}

void buttonISR(void) {
newPress = true;
}



With the Arduino serial monitor,  the communication is halted after the "detachInterrupt(digitalPinToInterrupt(7));" instruction but run correctly without it.

**Answers:**
What platform is running the monitor? **Only a Nano 33 BLE board connected to USB**
Can you try a different terminal program instead of the Serial monitor? Tried with a free serial monitor https://www.serial-port-monitor.org/, the result is the same, 
after ..("interrupt detached"); the communication from board is stopped instead with the commented line runs indefinetly.
Are you reselecting the port and reopening the monitor after the program loads? **Yes , I reselect the port and reopen serial monitor every time**
What happens if you manually go into boot mode, load the program, and then select the communication port? **After double reset no behaviour changes, the commented version runs, the original no**

@mcr68, can you please edit your post #11, select all code and click the </> button to apply code tags and next save your post.

I am also using MBed OS Nano Boards 2.6.1. Differences are that I have a home version of Windows 10 and my ide version is 1.8.15.

I've the issue with two PC with Win 10 Pro e Enterprise and I don't think this means.
Neither the IDE version.
Maybe the board library MBed OS Nano Boards 2.6.1 but is the same your.
Is there anybody that can test the code behaviour and confirm?
Thanks

@mcr68
I think the problem here is that you are trying to detach an interrupt pin that is not actually attached to anything. That is why it works the first time but not the second time.

Write your code so that after detaching the interrupt pin, have a small delay and then attach it again. Then your loop will not cause a hang up.

The sort of computer you have is irrelevant to your problem.

Thanks Mike, I'll test it and report,
anyway I epected the same behaviour between Arduino Nano AVR and Nano MBed OS 33 BLE .

I wouldn't because the interrupt structures will be different so I would expect different behaviour from the AVR processors to the BLE 33, especially when you are doing something daft with the interrupt vectors.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.