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?
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,
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().
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.
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
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
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**
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.
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.