-->
Page 1 of 1

Interfacing ESP8266-03 with ADS8343 via SPI bus

PostPosted: Sun Mar 27, 2016 12:13 pm
by luketanti
I need some help to interface an ADS8343 16-bit ADC with and ESP8266-03 using SPI.
I have managed to read the ADC using an arduino Uno but I was not successful with the ESP.
Here is the code I am try to use. I have used a library for the ADS8343 from digilent analog shield.

Code: Select all#include <SPI.h>

#define CS_PIN 15
#define BUSY_PIN 2

#define multiplier 0.050140380859375
unsigned int count = 0;

  void setChannelAndModeByte(byte channel, bool mode){   
    //control byte
    //S - sentinel, always 1
    //A2 - channel select
    //A1 - channel select
    //A0 - channel select
    //- N/C
    //Single / Diff pair
    //PD1 - power down mode
    //PD0 - power down mode
    byte control = B10000010; //default to channel 1 '001'
   
    //channel mask
    if(channel == 3){
      control = control | B11100000;
    }
   
    else if(channel == 2){
      control = control | B10100000;
    }
   
    else if(channel == 1){
      control = control | B11010000;
    }
   
    else if(channel == 0){
      control = control | B00010000;
    }
   
    //differential mode active
    if(mode){
      control = control & B11111011;
    }
   
    else{
      control = control | B00000100;
    }
    SPI.transfer(control);
    return;
  }

  unsigned int readADC(int channel, bool mode) {
    // initialize SPI:
    SPI.setDataMode(SPI_MODE3);
   
    digitalWrite(CS_PIN,LOW);
    setChannelAndModeByte(channel, mode);
    digitalWrite(CS_PIN,HIGH);
         
    //wait for busy signal to rise. If it lasts a while, try resending.
    while(digitalRead(BUSY_PIN) == 0); //wait for pin 2 to == 0
    digitalWrite(CS_PIN,LOW);
       
    //collect data
    byte high = SPI.transfer(0x00);
    byte low = SPI.transfer(0x00);
   
    //release chip select
    digitalWrite(CS_PIN,HIGH);
   
    //compile the result into a 32 bit integer.
    int result;
      result = (int)high<<24;
    result+= low<<16;
   
    //make into an unsigned int for compatibility with the DAC used on the analog shield.
    if(result < 0)
    {
      result = result >> 16;
      result &= 0x7FFF;
    }
    else
    {
      result = result >> 16;
      result |= 0x8000;
    }

    return result;
  }

void setup()
{
  Serial.begin(115200); // For debugging output
  Serial.println("Program Starting...");
  pinMode(CS_PIN, OUTPUT);
  pinMode(BUSY_PIN, INPUT);
  pinMode(14, OUTPUT);
  pinMode(12, INPUT);
  pinMode(13, OUTPUT);
  SPI.begin(); 
  SPI.setFrequency(1000000L);
}

void loop()
{
  count = readADC(0, false);  //read in on port labeled 'IN0'
  Serial.print("Channel 0 = ");
  Serial.println(count);
  delay(500);
  count = readADC(1, false);  //read in on port labeled 'IN0'
  Serial.print("Channel 1 = ");
  Serial.println(count);
  delay(500);
  count = readADC(2, false);  //read in on port labeled 'IN0'
  Serial.print("Channel 2 = ");
  Serial.println(count);
  delay(500);
  count = readADC(3, false);  //read in on port labeled 'IN0'
  Serial.print("Channel 3 = ");
  Serial.println(count);
  Serial.println();
  delay(1000);
}


The serial output is full of random numbers that does not make sense. I think that the communication is not being done correctly. Unfortunately I do not have a logic analyzer to check the SPI lines. Is there someone that can help me on this?