Multiple SPI devices... some MSB, some LSB

Hi,

my project has multiple spi devices connected to the atmega 328. I have tested individual programs to run each device and have them functioning perfectly. Now I am beginning to write code to run all the devices. But, one of the devices is LSB first and it cannot be changed. The other devices are MSB first.

It seems the SPI library functionSPI.setBitOrder(LSBFIRST)only has effect if it is run before SPI.begin()

I attempted to change bit order every time I wanted to communicate with a different device but that doesnt seem to work. I was wondering if anyone else had this problem and had any suggestions on how to deal with it.

I am thinking I may be able to run the SPI.begin() function every time I change bit orders, but that seems clunky.

Thanks.

The change is immediate...
https://github.com/arduino/Arduino/blob/master/libraries/SPI/SPI.cpp#L46

And the datasheet makes no mention of a restrictive mode...

• Bit 5 – DORD: Data Order
When the DORD bit is written to one, the LSB of the data word is transmitted first.
When the DORD bit is written to zero, the MSB of the data word is transmitted first.

I suspect you're barking up the wrong tree.

I am thinking I may be able to run the SPI.begin() function every time I change bit orders, but that seems clunky.

You could just invert the bits and leave the bit order alone.

Invert the bits and let the spi run either msb or lsb but consistently so.

evjim:
Hi,

my project has multiple spi devices connected to the atmega 328. I have tested individual programs to run each device and have them functioning perfectly. Now I am beginning to write code to run all the devices. But, one of the devices is LSB first and it cannot be changed. The other devices are MSB first.

So? Just change the SPI bit order as needed (ie. SPI.setBitOrder())

Yea, you guys are right. I made a simple test program and it does change the bit order. Now I need to find out why it doesnt work in my other program.

Thanks

Found my problem...

I made my own library with a bunch of functions and at the end of each function for the LSB device I would end it by setting bit order back to MSBfirst. But, some of my functions called other functions for the same device and would switch back to MSB too early