Having problems reading data from an ADC083x chip using SPI

Hello, i’m using an Arduino simulation model in ISIS to try and receive data from an ADC0832 chip. As far as I can see I have configured my protocols as per the datasheet (http://www.ti.com.cn/cn/lit/ds/symlink/adc0838-n.pdf), but on my data outline (MISO) i’m not reading anything.

Just a few ideas as to where I may be going wrong would be massively appreciated.

Fanks a lot,

Glenn

Here is my very simple code:

#include <SPI.h>

void setup (void)
{
pinMode(SCK, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(SS, OUTPUT);

digitalWrite(SS, HIGH);

SPI.setClockDivider(SPI_CLOCK_DIV128);
SPI.setDataMode(SPI_MODE1);
SPI.begin();
}

void loop (void)
{
byte a;

digitalWrite(MOSI, HIGH);
digitalWrite(SS, LOW);
transferAndWait(‘a’);
digitalWrite(SS, LOW);
digitalWrite(SS, HIGH);
delay(100);
}

byte transferAndWait (const byte x)
{
byte a = SPI.transfer(x);
delay(20);
return(a);
}

I would use setClockDivider and setDataMode after SPI.begin.

Okay, thank you. Hasn't rectified the problem though

The SPI.transfer() does not include the ChipSelect. Only the clock and the databits.
CS low, SPI.transfer, CS high.

My code does set the chip select low, transfers data and then sets chip select high again? I see now that I had an extra SS LOW statement that doesn't do anything, but regardless the sequence looks right to me?

Sorry, I see now. It should work.
The only way to see if it works is to grab an Arduino board and a ADC083x and upload your sketch.
Or you can search this forum to see more problems with simulations and how often that fails.

Okay great. So this could well just be a problem with the simulation? I'll breadboard it and let you know.

Thanks

Not tested as I don’t have an ADC083 but check comments in the code. Specifically…
Why are you setting MOSI HIGH in loop()
transferAndWait(‘a’); is not sending the previously defined variable called a but the ASCII code for ‘a’
I’m not sure const should be in this line ‘byte transferAndWait (const byte x)’

#include <SPI.h>

void setup (void)
{
  // Not needed as SPI.begin sets these
  //pinMode(SCK, OUTPUT);
  //pinMode(MOSI, OUTPUT);
  //pinMode(MISO, INPUT);
  
  // It also sets the SS pin
  //digitalWrite(SS, HIGH);
  //pinMode(SS, OUTPUT);
 
  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV128);
  SPI.setDataMode(SPI_MODE1);
} 


void loop (void)
{
  byte a;
 
  digitalWrite(MOSI, HIGH); // What is this?
  digitalWrite(SS, LOW);
  transferAndWait('a');     // This is not the variable a your sending
  digitalWrite(SS, LOW);
  digitalWrite(SS, HIGH);
  delay(100);
} 

byte transferAndWait (const byte x) // Does const work here?
{
  byte a = SPI.transfer(x);
  delay(20);
  return(a);
}

Okay, I've set the MOSI high because the chip needs two clock cycles to select the channel. 11 for channel 0 and 10 for channel 1 I think. So if I set the Master Out high, it will set one of the channels. I can move that to the setup I suppose.

I'm sending an 'a' just to shift the data out at the other end. It was just the character of choiced used in Nick Gammon's tutorial on SPI. Sorry if this caused any confusion.

And finally, I changed the input parameter to just byte but this hasn't fixed it.

Maybe it's worth noting that the data out from the ADC starts HIGH and clocks LOW on the falling edge of the third clock pulse. Ie, a binary value of 1100000 is read in each time?

Thanks for you help,

Glenn

Did you see this for the ADC0831-n : http://forum.arduino.cc/index.php?topic=69413.0
The sketch at the bottom was working.

Thank you for that link Peter. The thread indicated that the clock speed used for the SPI communication was too high for the ADC device. Would this likely be a factor when using a simulation model? I wouldn’t of thought it would be but maybe.

Anyway, I tried the code at the end which set the clock pulses high and low in the code itself and I still have the MISO line going from HIGH to LOW on the failing edge of the third clock pulse.

Any ideas? If not i’ll scrap the simulation and buy some chips.

Thanks again

#include <SPI.h>

byte result;

void setup(){
  pinMode(SS, OUTPUT);
  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(MISO, INPUT);
  
  digitalWrite(SS, HIGH);
  digitalWrite(SCK, HIGH);
  digitalWrite(MOSI, HIGH);
  
  Serial.begin(9600);
}

void loop(){
  result = ADCread();
  Serial.println("Data: " + String(result));
  delay(1000);
}

byte ADCread(){
  byte _byte = 0;
  
  digitalWrite(SS, LOW);
  digitalWrite(SCK, HIGH);
  delayMicroseconds(2);
  digitalWrite(SCK, LOW);
  delayMicroseconds(2);
  
  
  for (int i=7; i>=0; i--){
    digitalWrite(SCK, HIGH);
    delayMicroseconds(2);
    digitalWrite(SCK, LOW);
    delayMicroseconds(2);
  }
  digitalWrite(SS, HIGH);
  
  return _byte;
}

gspice91:
Any ideas? If not i'll scrap the simulation and buy some chips.

That's what you should have done in the first place :stuck_out_tongue:

Simulations are okay for analog circuits and many logic chips. But this ADC083x chip is already complicated enough in the real world, and I happen to dislike Arduino simulations (just because I can).

Ha, okay i'll let you know how I get on.