1
votes

First of all, I apologize if this question is repeated.

I am using two DWM1000 modules (datasheet: Decawave DWM1000) in an Arduino Mega and SPI to communicate with them. I have no problems communicating with only one module. But I need to use at least two modules as one will be the transmitter and another as receiver.

Is it possible to assign another GPIO pin other than pin 53 (default chip select pin) as the second module's SS pin?

void setup() {
  pinMode(53, OUTPUT);
  pinMode(45, OUTPUT);
  SPI.begin();

  digitalWrite(53, LOW);
  // communicating first module here using SPI.transfer()
  // MOSI and MISO data transfer have to go between a LOW digitalWrite and a HIGH digitalWrite
  digitalWrite(53, HIGH);

  digitalWrite(45, LOW);
  // communicating second module here using SPI.transfer()
  digitalWrite(45, HIGH);

  SPI.end();
}

Is this attempt correct?

1

1 Answers

2
votes

Yes. Note the SS as other pins are multifunction. In that it is normal GPIO and a Slave Select to the SPI. Where your application is to use the SPI as a Master, freeing the SS up. It has the one constraint that when the SPI is master the SS must be an output. If it is an input changes the SPI from being a master.

So in Master mode one is free to use any GPIO pin (minding their constraints) as slave selects to other devices. As in your above code example is basically correct.

For future reference I you may need to set the other SPI parameter each time you use it. If you are mingling different types of devices. Say an SdCard, Temp Sense, Audio etc... As they will set their SPI parameters each time and may interfere with your. Don't necessarily assume the SPI is the way you left it.

Also the SPI.end() is not necessarily needed. It only clears the SPI ENable bit.

pinMode(53, OUTPUT);
pinMode(45, OUTPUT);
SPI.begin();

digitalWrite(53, LOW);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(spi_Write_Rate);      

SPI.transfer(0x02); //send your stuff
//...
digitalWrite(53, HIGH);

digitalWrite(45, LOW);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(spi_Write_Rate);      

SPI.transfer(0x02); //send your stuff
//...
digitalWrte(45, HIGH);