I learned, that SPI-code uses interrupts. So is it true then, that I can't use SPI related code inside an ISR?

Background: I want to capture one or more revolution speeds with an MCP23S17. I rewired the interrupt lines to INT1 and INT2 accordingly to get the interrupts at the Arduino.

I thought it was good to fetch the port registers from the MCP23S17 inside the ISR, but this apparently doesn't work.

Is the only thing I can do in my ISR set a flag for another test in the main loop? And let a routine called from lood() then poll the registers from the port expander? This opposes the idea to fetch the relevant data which issued the interrupt ASAP.

  • 1
    This is why you don't use port expanders for timing-sensitive applications. Aug 20, 2015 at 9:39
  • And instead of port expanders i shall use what?
    – Ariser
    Aug 20, 2015 at 9:42
  • Real IO pins on the microcontroller.
    – Majenko
    Aug 20, 2015 at 9:45
  • 1
    Please post your code. Majenko is perfectly correct. SPI does not use interrupts, at least during SPI.transfer(). I learned, that SPI-code uses interrupts. - where did you learn that?
    – Nick Gammon
    Aug 20, 2015 at 10:23
  • 2
    apparently doesn't work. - I hate that expression. What happened? What did you expect to happen? In what way did they differ?
    – Nick Gammon
    Aug 20, 2015 at 10:25

1 Answer 1


SPI doesn't use interrupts. It references the interrupt flag to know if a transfer has been completed, but it doesn't actually have interrupts enabled:

SPDR = data;
asm volatile("nop");
while (!(SPSR & _BV(SPIF))) ; // wait
return SPDR;

It should be perfectly possible to use SPI within the interrupt - indeed there is portions of the SPI API that deal specifically with this - see https://www.arduino.cc/en/Reference/SPIusingInterrupt for instance.

  • Ok, I missed that. Then there must be another thing I apparently have botched...
    – Ariser
    Aug 20, 2015 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.