I'm trying to read the output of an ADC chip LTC 1867 (need the high precision). I think I understand how it how works. Since it's output is 16 bit I need to send 2 8-bit words to it, the first word starts with a 7 bit input word that controls, what channel, mode and other parameters.
When I ground the channel, it returns 0 as expected but otherwise, it's output is very wrong. A 2.5V reference voltage returns about 1.525. I have tried spidev and pigpio and results seem to be consistent across both.
I decided to use piscope (seems very convenient and useful! especially as I don't have access to an oscilloscope in the evening) to see what was going on with my MISO, MOSI, CLK, CE0. The clock seems to be missing some pulses and I think that is the problem
The returned values are something like this
What I would like to know is:
- Could it be that one of my pins is not working well, (My digital output from the ADC was 5V so it could have messed it up). How do I check if that is the issue
- It shows the right value (2.5V) for a like 2 times before returning to the wrong ones (1.524V) and other random values
- How do I fix the missing pulses? -Does it matter that the clock pulses are uneven -Would software SPI/ bit banging be better?
My Code is
from time import sleep
# Import SPI library (for hardware SPI) and MCP3008 library.
import pigpio
from time import sleep
import os
# Open SPI bus
pi = pigpio.pi()
h=pi.spi_open(0,50000,0)
def twos_complement(input_value, num_bits):
'''Calculates a two's complement integer from the given input value's bits'''
mask = 2**(num_bits - 1)
return -(input_value & mask) + (input_value & ~mask)
## http://cds.linear.com/docs/en/datasheet/18637fc.pdf
#Function to read SPI data from LTC1867 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
#pin 7-GND=244, pin 5-GND=228, pin 1-GND=196
(count, rx_data) = pi.spi_xfer(h,[228,0])
print "rx_data"
#print rx_data
index=0
rx_array=[0,0]
for byte in rx_data:
rx_array[index]= byte
index=index+1
print rx_array
data = (rx_data[0]<<8) + rx_data[1]
print "data"
#print data
#print "2's complement"
#data= twos_complement(data, 16)
print data
return data
# Converts data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
volts = (data * 2.5) / float(pow(2,16))
volts = round(volts,places)
return volts
print('Reading LTC1867 values, press Ctrl-C to quit...')
tilt_channel_pos=0
tilt_channel_neg=1
delay=5
while True:
print "--------------------------------------------"
tilt_bits_pos = ReadChannel(tilt_channel_pos)
tilt_volts_pos = ConvertVolts(tilt_bits_pos,3)
print("Volts: {}V".format(tilt_volts_pos))
break
# Wait before repeating loop
sleep(5)