I am using Arduino Due's SPI (MISO) interface to communicate with an LTC2366 16 bit ADC (edit: it is 12 bit).
I am using the suggested libraries and yet I see all 1s in mu output. This is the code I'm using:
#include <SPI.h>
#include <Arduino.h>
#include <Wire.h>
#include <LTC2366.h>
uint16_t val;
uint16_t *ptrVal=&val;
float final;
void setup(){
Serial.begin(9600);
SPI.begin(10);
pinMode(MISO, INPUT);
SPI.setClockDivider(10, 42);
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE1));
}
void loop(){
LTC2366_read(10,ptrVal);
final = LTC2366_code_to_voltage(val,3.3);
//transfer 0x0F to the device on pin 10, keep the chip selected
// val= SPI.transfer16(10);
//transfer 0x00 to the device on pin 10, keep the chip selected
//transfer 0x00 to the device on pin 10, store byte received in response1, keep the chip selected
Serial.println(final);
}
Where the functions used are defined as follows:
// Reads and sends a word
// Return 0 if successful, 1 if failed
void spi_transfer_word(uint8_t cs_pin, uint16_t tx, uint16_t *rx)
{
union
{
uint8_t b[2];
uint16_t w;
} data_tx;
union
{
uint8_t b[2];
uint16_t w;
} data_rx;
data_tx.w = tx;
output_low(cs_pin); //! 1) Pull CS low
data_rx.b[1] = SPI.transfer(data_tx.b[1]); //! 2) Read MSB and send MSB
data_rx.b[0] = SPI.transfer(data_tx.b[0]); //! 3) Read LSB and send LSB
*rx = data_rx.w;
output_high(cs_pin); //! 4) Pull CS high
}
// Reads the ADC and returns 16-bit data
void LTC2366_read(uint8_t cs, uint16_t *ptr_adc_code)
{
uint16_t dummy_command = 0;
spi_transfer_word(cs, dummy_command, ptr_adc_code);
return;
}
// Calculates the voltage corresponding to an adc code in offset binary, given the reference voltage (in volts)
float LTC2366_code_to_voltage(uint16_t adc_code, float vref)
{
float voltage;
adc_code = adc_code << 2; //the data is left justified to bit_13 of a 16 bit word
voltage = (float)adc_code;
voltage = voltage / (pow(2,16)-1); //! 2) This calculates the input as a fraction of the reference voltage (dimensionless)
voltage = voltage * vref; //! 3) Multiply fraction by Vref to get the actual voltage at the input (in volts)
return(voltage);
}```
NO VALUE IS PRINTED. THE code makes so much sense, can you please help me understand what I am missing.