Single SPI with Two Slave Devices using different Bitrates and Clock Dividers

Hi,

I am have this project that I would like to connect to two different SPI slave devices on an Arduino UNO. My datasheets for the two devices state the have different baud and bit rates.
It is look like:

  • Device 1 has a bitrate of 8 bit and will require clock divider of 16
  • Device 2 has a bitrate of 16 bit and will require clock divider of 128

My thoughts on coding approach:

byte Send_VSPI_Device_1(byte TX_data) {
	SPI.setClockDivider(SPI_CLOCK_DIV16);
	digitalWrite(SPI1_CS_PIN, LOW);
	byte Response = SPI.transfer(TX_data);
	digitalWrite(SPI1_CS_PIN, HIGH);  
	return Response;
} 
  

uint16_t Send_VSPI_Device_2(uint16_t TX_data) {  
	SPI.setClockDivider(SPI_CLOCK_DIV128);  
	digitalWrite(SPI2_CS_PIN, LOW);
	uint16_t Response =  SPI.transfer16(TX_data);
	digitalWrite(SPI2_CS_PIN, HIGH);
	return Response;  
}

void setup() {  
	SPI.begin(); 
	SPI.setBitOrder(MSBFIRST);
	SPI.setDataMode(SPI_MODE1);
}

void loop() {
	Send_VSPI_Device_1(SPI_DATA_8);
	Send_VSPI_Device_1(0x00); //flush between modes
	Send_VSPI_Device_2(SPI_DATA_16);
	Send_VSPI_Device_2(0x00); //flush between modes
	delay(10);
}

Is there any foreseen issues in doing this? I may also add an SD card on for logging.

Thank you

Some SD card modules don't play nice on the SPI bus, just keep that in mind.

What are the other two mystery devices? Advice will be better the more you can share about what you are hooking up.

a7

Use SPI Transactions and SPISettings rather than messing with the clock divider, bit order, etc yourself.

Great, thank you. I will definitely have a look into this.

In SPI Platform, bit rate refers to number of bits being transmitted/received in one second. If clock divider 16 is chosen, then for Device 1/Slave 1, the bit rate is 16 MHz/16 = 1 MBits/sec.

For Device 2/Slave 2, the bit rate is 16 MHz/128 = 125 KBits/sec.

True, the third-party code was actually snippets from Slave 1 and Slave 2 which I merged. The datasheet for Slave 2 actually states a data rate of 2MBaud, so I should be able to use clock divider the same as Slave 1 or even 8 if wanting maximum? It is strange that they chose such slow data rate in their code. I wonder what the reason is.

1 Like

You are calling a function that returns a byte type data item which you are not catching/saving. Should the above code be something like --?

byte recvData = Send_VSPI_Device_1(SPI_DATA_8);

It is just an example, and completely not the topic, but if just sending and not interested in the response why not forget the response?

1 Like

Any opportunity to see your full codes (Master and two Slaves) to test how they go with my setup composed of UNO1-SPI Master, NANO1-SPISLave1, and UNO2-SPISlave2.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.