0

I have the following code:

#include <SPI.h>

void initChips(){

    SPI.begin();
    SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));

    pinMode(SS, OUTPUT); 
    digitalWrite(SS, LOW);

}

void setup() {

  Serial.begin(1000000);

  initChips();

  delay(1000);  
}

void loop() {

  //Perform read and write

  digitalWrite(SS, HIGH);
  SPI.endTransaction();
  SPI.end();
  pinMode(MOSI, OUTPUT); 
  digitalWrite(MOSI, LOW);


  Serial.println("Done\n");

}

The MOSI pin does not go low even when I turn off SPI and manually turn the pin low through digital write.

I saw a similar post Cannot set MOSI pin low even after ending SPI but the accepted solution (if I am understanding it correctly) is by changing the SPI library itself which I do not want to do.

What is correct sequence of SPI commands to turn the MOSI pin low after SPI has finished?

Solution: As one of the comments mentioned, the fix is to use SPCR &= ~_BV(SPE); after SPI.end();.

6
  • why not use SPI.transfer(byte)? – MichaelT Mar 21 '19 at 16:50
  • 1
    That post doesn't say anything about changing the library. Only that the library should already be doing this, but somehow doesn't. It suggest putting SPCR &= ~_BV(SPE); after SPI.end; – Gerben Mar 21 '19 at 16:55
  • 1
    @Gerben You are right. Just adding SPCR &= ~_BV(SPE); after SPI.end() fixes the issue. Thanks! – Harshavardhan Ramanna Mar 21 '19 at 17:49
  • @Juraj The MOSI pin is used to drive values into an SRAM IC's serial input pin. – Harshavardhan Ramanna Mar 21 '19 at 17:49
  • Might have been fixed by putting #include <SPI.h> at the top of the sketch also. – CrossRoads Mar 22 '19 at 1:13

Browse other questions tagged or ask your own question.