SPI.transfer rotates bytes?

Hi All -

I’m using SPI to communicate with the AD7705 ADC, and in the process of troubleshooting connected a logic analyzer.
I noticed that the MISO bytes seen by the logic analyzer were rotated by one byte when interpreted by the Arduino.
So something like 01010111 (0x57) in the logic analyzer would be 10101011 (0xAB) when read/printed as a byte or uchar
by Arduino. The whole byte shifts to the right and the LSB gets bumped to the MSB.

Any ideas? The code that does this work is below.
Thanks, Adam

unsigned int readChannel(byte CH){
  //CH is 0 for AIN1 and 1 for AIN2    
  unsigned char b1, b2;  
  unsigned int DV;
  digitalWrite(ADSELECT,LOW);
    SPI.transfer(DATAREG|READ|CH); //0x38
    b1 = SPI.transfer(0xFF); 
    b2 = SPI.transfer(0xFF); 
    DV = b1<<8 | b2; //Serial.print(inByte,BIN);
  digitalWrite(ADSELECT,HIGH);
  Serial.print(b1,HEX);Serial.print(b2,HEX);
  return DV; 
}

I noticed that the MISO bytes seen by the logic analyzer were rotated by one byte ...

You mean one bit, right?

Is this in the sending or receiving direction?

However the Arduino works, something else must be wrong. I have done extensive analysis of the SPI data transfer:

http://gammon.com.au/spi

I'm currently working on a project with the AD7714 and I'm having trouble too. The ADC just returns whatever I send it.

  1. Which version of the IDE are you using?
  2. Watch your logic analyzer trigger setup, sometimes the triggers can cause the SPI to be interpreted incorrectly (looking for a one,transition but first bit is a zero)

hardcore: 1. Which version of the IDE are you using? 2. Watch your logic analyzer trigger setup, sometimes the triggers can cause the SPI to be interpreted incorrectly (looking for a one,transition but first bit is a zero)

Would there be a difference in SPI functionality between arduino-0023 and arduino-1.0?

Yes there appears to have been some work just prior to the 1.0 update related to SPI communication, it always pays to have the latest libraries.

hardcore:
Yes there appears to have been some work just prior to the 1.0 update related to SPI communication, it always pays to have the latest libraries.

Blah. Time to stop resisting and port my projects to Arduino 1.0, I guess.

Do you have the correct SPI mode set for your device? Just a shot in the dark there.

The SPI “library” in .22 is just an incredibly simple wrapper for a handful of commands. I haven’t looked at the new IDE yet, but how much different is it really?

Porting is really easy in most cases, as for library differences… it depends what you are into.

In one case there was an issue with the libraries not being able to send 0x00 bytes to attached devices, in another a bug in the compiler in 1.0 caused the corruption of 16bit values in the ethernet library, so you get rid of some bugs but may get others.

This sounds like it might be caused by a mismatch of the SPI "data mode", which controls which clock edges shift the data and which latch the bit... The datasheet has four possibilities...