I am trying to read data from an AD7314 Temperature Sensor that is connected to an Arduino Uno and output that value onto 6 LEDs also connected to the Arduino Uno board. From the image below, the sensor is showing only an output of 0011111111111111
during the streams of 16 clock cycles even after the chip enable goes from low to high before conversion to initiate a new one (it seems that the conversion is stuck). The datasheet says:
A leading zero and 10 bits of data are transferred during a read operation. Read operations occur during streams of 16 clock pulses. Output data is updated on the rising edge of SCLK. The serial data is accessed in a number of bytes if 10 bits of data are being read. At the end of the read operation, the SDO line remains in the state of the last bit of data clocked out of the AD7314 until CE returns low
I am not sure where exactly I am going wrong during the read operation:
Note: for the Waveform image (Top to bottom):
waveform 1: Chip Enable
waveform 2: Serial Clock
waveform 3: MISO Line
waveform 4: MOSI Line
My code is below:
#include <SPI.h>
const int MOSI_PIN = 11;
const int MISO_PIN = 12;
const int SCK_PIN = 13;
const int Slave_PIN = A0;
// module data
uint16_t myValFromTempSensor = 0;
void setup () {
SPI.begin ();
SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE0));
pinMode(5, OUTPUT); //LED 3
pinMode(6, OUTPUT); //LED 4
pinMode(7, OUTPUT); //LED 5
pinMode(8, OUTPUT); //LED 6
pinMode(9, OUTPUT); //LED 7
pinMode(10, OUTPUT);//LED 8
pinMode(MOSI_PIN, OUTPUT); // MOSI to SDI
pinMode(MISO_PIN, INPUT); //MISO to SDO
pinMode(SCK_PIN, OUTPUT); //CLOCK PIN to SCK
pinMode(Slave_PIN,OUTPUT); //Slave pin
digitalWrite(Slave_PIN, LOW);//Set slave low before communication
for (int i = 5; i < 11; i++) {
digitalWrite(i, LOW);
}
delay(100);
} // end of setup
void loop () {
digitalWrite(Slave_PIN, HIGH); //Set slave high to read data
myValFromTempSensor = SPI.transfer(0x00); // Read Data from the device
myValFromTempSensor |= (uint8_t)SPI.transfer(0x00) << 8;// Bitshift to read next 8 bits
digitalWrite(Slave_PIN, LOW);
if(( myValFromTempSensor>=0) && ( myValFromTempSensor<=196)) {
//Range 0C to 60C is 0 to 196
digitalWrite(5, bitRead(myValFromTempSensor,0));
digitalWrite(6, bitRead(myValFromTempSensor,1));
digitalWrite(7, bitRead(myValFromTempSensor,2));
digitalWrite(8, bitRead( myValFromTempSensor,3));
digitalWrite(9, bitRead( myValFromTempSensor,4));
digitalWrite(10, bitRead( myValFromTempSensor,5));
} else {
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
digitalWrite(9, 0);
digitalWrite(10, 0);
}
}
01000000000
”. – Edgar Bonet Jan 3 '18 at 19:20