SPI Question

Hello World,

I need some guidance on SPI.. I have 3 devices running on SPI and need to know if I'm barking up the right tree.

I have:

  1. Wifi Module CS3300
  2. ADC Module
  3. LCD Shift Register

Couple Questions:

  1. Do all the devices need to use the same Data Mode, i.e. SPI.setDataMode(Mode0, Mode1, Mode2, Mode3)
  2. Do all devices need to be on the same Bus Speed, i.e. SPI.setClockDivider(SPI_CLOCK_DIV2)
  3. If all my devices need different MODES can I set them when that device is called?

my ADC module requires MODE 1 or 3, not sure what the LCD Shift Register requires. I plan on experimenting but wanted some advice from the community.

Thanks All,

  1. If all my devices need different MODES can I set them when that device is called?

That's what you are supposed to do.

  1. NO
  2. NO
  3. YES

The SOP (Standard Operating Procedure) for a forum post about a hardware issue is to link the hardware
(vendor) used so we can research your question. You have not posted any links. How would we know without the datasheets ? (or vendor link)

This is what Google has to say about your
wifi module.

This is what Google says about your shift register.

This is what Google says about your ADC module.

Get the point ?

Did you read this ?

The SPI standard is loose and each device implements it a little differently. This means you have to pay special attention to the device's datasheet when writing your code.
Generally speaking, there are four modes of transmission. These modes control whether data is shifted in and out on the rising or falling edge of the data clock signal (called the clock phase), and whether the clock is idle when high or low (called the clock polarity). The four modes combine polarity and phase according to this table:
Mode Clock Polarity (CPOL) Clock Phase (CPHA)
SPI_MODE0 0 0
SPI_MODE1 0 1
SPI_MODE2 1 0
SPI_MODE3 1 1
The SPI.setDataMode() function lets you set the mode to control clock polarity and phase.
Every SPI device has a maximum allowed speed for SPI Bus. The SPI.setClockDivider() allows you to change the clock speed to make your device working properly (default is 4MHz).
Once you have your SPI parameters set correctly you just need to figure which registers in your device control which functions, and you're good to go. This will be explained in the data sheet for your device.
For more on SPI, see Wikipedia's page on SPI.

Consider this:
If your SPI code is written correctly, EACH communication with EACH device is done ONLY after configuring your SPI mode. This another way of saying each conversation you have with another person is unique to that person. You don't speak Spanish to your BMW mechanic, but you might speak SPANISH
if you went to a Mexican restaurant. There is no reason why it would make any difference if they all used
different modes if you are doing the proper configuration at the beginning of each data transfer with each
device. Once you have configured THAT device, the mode is stored in a register in that device. It shouldn't be necessary to reconfigure it later in the code because it was already done. There is no SPI central where the mode is saved in the arduino.

See Section 21.4 ,Table 21-2 , page 197 of ATmega328 DATASHEET
SEE SECTION 21.8.4. on page 207 for info on the UDORDn bit (2)

It is being sent to some SPI device and the arduino doesn't care about it. One SPI device doesn't know what you have said to another so there shouldn't be
a conflict. I have never used different modes in the same sketch but that is my conclusion. If it is wrong,
I hope someone will correct me. (I know at least three members who are just waiting to get in line)

raschemmel:
That's what you are supposed to do.

  1. NO
  2. NO
  3. YES

The SOP (Standard Operating Procedure) for a forum post about a hardware issue is to link the hardware
(vendor) used so we can research your question. You have not posted any links. How would we know without the datasheets ? (or vendor link)

Get that, but it was a more generic question, that way I can get an idea if I'm going in the right direction before I spew more questions than I can get answers for. I will need more research so bear with me

This is what Google has to say about your
wifi module.

This is what Google says about your shift register.

This is what Google says about your ADC module.

Get the point ?

Did you read this ?

Got the point, but it was not my intention to get into detail yet. Like I said I was getting the overall question(s) out there first. I read the SPI article on Arduino a few days ago, did not state any of the questions I needed answers for in a way that I understood...

Consider this:
If your SPI code is written correctly, EACH communication with EACH device is done ONLY after configuring your SPI mode. This another way of saying each conversation you have with another person is unique to that person. You don't speak Spanish to your BMW mechanic, but you might speak SPANISH
if you went to a Mexican restaurant. There is no reason why it would make any difference if they all used
different modes if you are doing the proper configuration at the beginning of each data transfer with each
device. Once you have configured THAT device, the mode is stored in a register in that device. It shouldn't be necessary to reconfigure it later in the code because it was already done. There is no SPI central where the mode is saved in the arduino. It is being sent to some SPI device and the arduino doesn't care about it. One SPI device doesn't know what you have said to another so there shouldn't be
a conflict. I have never used different modes in the same sketch but that is my conclusion. If it is wrong,
I hope someone will correct me. (I know at least three members who are just waiting to get in line)

Well said, makes sense... thanks for your help!

raschemmel:
There is no SPI central where the mode is saved in the arduino.

CPOL and CPHA bits 3 and 2 in the SPCR register.

Maybe I didn’t read you right, but if one device only works with say mode 3 and another only works with mode 4 then you’d have to change these bits each time you switch over from talking to one to talking to another.

The DORD bit might be important too if one device expect MSBF and the other expects LSBF.

See Section 21.4 ,Table 21-2 , page 197 of ATmega328 DATASHEET

The DORD bit might be important too if one device expect MSBF and the other expects LSBF.

SEE SECTION 21.8.4. on page 207 for info on the UDORDn bit (2)

@Delta_G,
Thanks for the correction.

I found this example on page 199

void USART_Init( unsigned int baud )
{
UBRRn = 0;
/* Setting the XCKn port pin as output, enables master
mode. */
XCKn_DDR |= (1<<XCKn);
/* Set MSPI mode of operation and SPI data mode 0. */
UCSRnC =
(1<<UMSELn1)|(1<<UMSELn0)|(0<<UCPHAn)|(0<<UCPOLn);
/* Enable receiver and transmitter. */
UCSRnB = (1<<RXENn)|(1<<TXENn);
/* Set baud rate. */
/* IMPORTANT: The Baud Rate must be set after the
transmitter is enabled */
UBRRn = baud;
}