I'm trying to read from an LTC1286. This is a dumb SPI device that can only be read from, it cannot be written to.
My sample code is something like this:
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE2); // clock is idle high, and clock out on falling edge
SPI.setClockDivider(SPI_CLOCK_DIV128); // use 125kHz clock, LTC1286 can only do 12.5ksps maximum which is 150kHz @ 12 bits
digitalWrite(channel, LOW);
delayMicroseconds(16);
int ah = SPI.transfer(0x00) & 0x0f; // MSB, 4 bits only
int al = SPI.transfer(0x00);
digitalWrite(channel, HIGH);
long val = (ah * 256) + al;
if (val == 0) val = 1; // avoid NaN
return val;
My problem is that the readings I get fluctuate wildly. I would understand if the least-significant 1-3 bits are flipping randomly, but for the next-to-the-MSB bit to flip... seems a bit excessive.
Admittedly I have everything on a breadboard right now, so it could be analog noise. But I am not sure if I am talking to the LTC1286 properly. The data sheet says that after pulling CS low, the ADC will output one null bit, followed by data (MSB to LSB, 12 bits total).
Right now I am reading two bytes in a row. Since I am reading 16 bits,
am I getting the null bit at the very left?
right now I am masking the most significant nibble of the first byte. But this might be wrong...
I think your data needs more manipulating.
SPI will bring in a bit on every clock
Looks to me like you will see
x-x-x-B11-B10-B9-B8-B7
and then
B6-B5-B4-B3-B2-B1-B0-B1
so final (int) result might go together as
final = ( ((ah & B00011111)<<7) + (al >>1) );
Try some adjustments like that. I may be off 1 bit position with the rising edge/falling edge capturing.
I know the original thread is way old, but finding this thread helped me with my recent problem. I tried CrossRoads' suggestion and it worked. Here is my code: