Having issues getting a microSD card breakout module and seeedstudio v1.2 CAN shield to play nice on an Uno. On their own, I have them working, but not at the same time.
The seeedstudio v1.2 CAN shield has the hardware solder tabs set to use the default D9 CS pin. Then the microSD breakout module has its CS pin on D10. Both devices are connected to D11-13 for MOSI, MISO, and SCK.
Here's how I understand it, the default SPI.begin() will use D10 as SS. In setup(), I should set arduino as the Master for SPI mode with:
void setup()
{
pinMode(10, OUTPUT);
digitalWrite(10, HIGH);
SPI.begin();
....
}
After it is set to master mode, pin 10 can now act as the CS for the SD card and pin 9 acts as the CS for the CAN shield. Now to initialize the shields. So I should disable SD CS/enable CAN CS, initialize CAN shield, then enable SD CS/disable CAN CS and initialize SD card.
void setup()
{
....
digitalWrite(10, HIGH); // disable SD
digitalWrite(9, LOW); // enable CAN
// initialize CAN shield
while (CAN_OK != CAN.begin(CAN_500KBPS)) // init can bus : baudrate = 500k
{
delay(500);
}
digitalWrite(9, HIGH); // disable CAN
digitalWrite(10, LOW); // enable SD
// initialize SD card
while (!sd.begin(SDChipSelect, SPI_HALF_SPEED))
{
delay(500);
}
}
And then each time the SD or CAN is needed in loop(), I have to disable the unused one and enable the one to be used. Such as:
void ReceiveCAN()
{
digitalWrite(10, HIGH); // disable SD
digitalWrite(9, LOW); // enable CAN
if (CAN_MSGAVAIL == CAN.checkReceive())
{
// do CAN stuff
}
}
void ReadSD()
{
digitalWrite(9, HIGH); // disable CAN
digitalWrite(10, LOW); // enable SD
// do SD stuff
}
EDIT: It looks like CAN.checkReceive() does select/deselect the CS pin, but this doesn't handle disabling the other SPI devices (for me, the SD card). Maybe this is my problem.