1

I'm currently trying to communicatie Raspberry Pi 2 B+ with ADS7851, an adc chip made from TI. ADS7851_DATASHEET

I've done pretty much of research, but I'm still stuck on how to communicate in SPI with the device. Since most of the devices have 'Data In' port, which is connected to MOSI port in GPIO of RasPi. However, this one doesn't have such port. The only digital input port it has is CS(Chip Selecet) port, which I believe needs to be connected to CE0 port of RasPi. Therefore I think it is impossible to use xfer2 or xfer commands, which sends bytes from Master to Slave. So under this condition, I think I need to use spi.readbytes function right after I set CS active(LOW for this device), and the code I've so far written is below.

###########CODES##############
#!/usr/bin/python
import time
import sys
import spidev
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
spi=spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=24000000   #16bits=1data, 1.5MSPS needed

SCLK=23
CS=24
MISO=21
GPIO.setup(SCLK,GPIO.OUT)
GPIO.setup(CS,GPIO.OUT)
GPIO.setup(MISO,GPIO.IN)
GPIO.output(CS,GPIO.HIGH)   #initialize CS

def readADC(clkPin,csPin,misoPin):
   buff=spi.readbytes(2)
   bData=bin(buff[0])[2:].zfill(8)[2:]+bin(buff[1])[2:].zfill(8)
   bData_rev=bData[::-1]
   data=0
   for bit in bData_rev:
         data=(data<<1)|int(bit)
return data
if __name__=='__main__':
   try:
         while True:
                GPIO.output(CS,GPIO.HIGH)  #CS active
                val=readADC(SCLK,CS,MISO)
                volt=val*3.3/(2**14)
                print 'ADC value', str(volt)
                GPIO.output(CS,GPIO.LOW)   #CS inactive
   except KeyboardInterrupt:
         GPIO.cleanup()
         spi.close()
         sys.exit(0)
#########################

However, this code doesn't work. More seriously, MISO only reads 0.

I'll be glad to hear any helpful advice. Thank you.

*Update

Wiring

It's obvious that the picture itself is hard to understand. To explain it in detail with written words..

In ADS7851, 1. port 1 is connected to port 15, which is connected to mid-point of the variable resistor. 2. port2, port3, port8, port13, port16 is connected to ground 3. port4, port5, port6, port12 are left unconnected.(channel 2 not used) 4. port7(DVDD) is connected to 3.3V of raspi(GPIO pin 1) 5. port9(CS) is connected to raspi CE0 (GPIO pin 24) 6. port10(SCLK) is connected to raspi SCLK (GPIO pin 23) 7. port11(SDO A) is connected to raspi MISO (GPIO pin 21) 8. port14(AVDD) is connected to 5.0V of raspi(GPIO pin 2) 9. raspi GND(GPIO pin 6) is connected to ground 10. input signal comes from oscilloscope probe comp(5V, 1kHz square wave pulse), connected to one end of the variable resistor. The other end of the resistor is grounded.

Probing the ADC port 11, I could detect bundles of 16 pulses, which refers to the 16bits needed to send one data. So, ADC chip I believe works well. However, MISO still reads zeros only, and none of the commands xfer/xfer2/readbytes/pigpio works.

2
  • You only set cs once to low, but from the timing diagram it's the falling edge to start a cycle. So initalize cs to high and also after each read.
    – LotPings
    Aug 16, 2017 at 7:47
  • Thanks. Doing so and also wiring the VrefOut port to the voltage under half of AVDD(which was guided on the datasheet) solved the problem
    – J.Daniel
    Aug 21, 2017 at 14:47

1 Answer 1

1

You can use xfer and xfer2 but just send any arbitrary data (traditionally 0).

Presumably the device starts sending back data after slave select is asserted and the clock is toggled. That protocol for an ADC is not that unusual.

My pigs utility would let you see what is going on.

sudo pigpiod # to start the daemon

pigs spio 0 50000 0 # open CE0, 50kbps, no flags

pigs spir 0 3 # read 3 bytes

SPIO

SPIR

4
  • Thanks. I looked in to the pigpiod module. However, pigs spir only receives zero, no matter which voltage adc chip reads. Is it then hardware problem?
    – J.Daniel
    Aug 16, 2017 at 7:59
  • The most likely problem is incorrect wiring. A clear photo showing the wiring between the Pi and the device may help. Add the photo to your question.
    – joan
    Aug 16, 2017 at 11:02
  • I've uploaded the picture. If you don't mind, I'll be very very thankful for the feedback.
    – J.Daniel
    Aug 17, 2017 at 4:21
  • 1
    Oh, I solved it. It was definitely wiring problem. I've missed the caution that I should connect Vref less than half of AVDD. Now I get nonzero value. Thanks a lot.
    – J.Daniel
    Aug 18, 2017 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.