Reading data out of an ADC (MCP3001) with python (SPI) -



Reading data out of an ADC (MCP3001) with python (SPI) -

i seek read info adc python, unfortunatly doesn't work. hope has hint me, because script creates chaos-data. don't see error bits...

i've updated script below i'm reading 16 bits , cutting of first , lastly 3 bits. need 10 middle bits info - if understand datasheet correctly...

datasheet mcp3001: (page 15) http://ww1.microchip.com/downloads/en/devicedoc/21293c.pdf

import time import rpi.gpio gpio gpio.setmode(gpio.bcm) high = true low = false def readanalogdata(sclkpin, mosipin, misopin, cspin): gpio.output(cspin, high) gpio.output(cspin, low) gpio.output(sclkpin, low) adcvalue = 0 in range(16): gpio.output(sclkpin, high) gpio.output(sclkpin, low) adcvalue <<= 1 if(gpio.input(misopin)): adcvalue |= 0x01 adcvalue >>= 3 adcvalue &= 0x3ff time.sleep(0.5) homecoming adcvalue sclk = 11 # serial-clock mosi = 10 # master-out-slave-in miso = 9 # master-in-slave-out cs = 17 # chip-select gpio.setup(sclk, gpio.out) gpio.setup(mosi, gpio.out) gpio.setup(miso, gpio.in) gpio.setup(cs, gpio.out) while true: print readanalogdata(sclk, mosi, miso, cs)

from cursory read of info sheet , code, looks you're trying discard info initial 3 clock cycles converting str , using str slicing

adcvalue = str(adcvalue) adcvalue = adcvalue[3:12] adcvalue = int(adcvalue)

if so, not work. adcvalue int , converting str produce decimal (base 10) string. instead, either don't sample info during first 3 cycles, or utilize bit mask strip integer (ie adcvalue &= 0x1ff)

in addition, seem sampling info on negative clock edge, when info in transient state. suspect either need set in delay here, or sample on +ve clock border when info has stabilized.

edit

as stated above, seek sampling on +ve clock edge. figure 6-1 on pg.16 of datasheet shows this. looks pumps out 9 bits in msb format, repeats info in lsb format (see fig 6-2), don't think want maintain 10 bits. need clock out total of 12bits, discard first 3.

something (untested):

def readanalogdata(sclkpin, mosipin, misopin, cspin): gpio.output(cspin, high) gpio.output(cspin, low) adcvalue = 0 in range(12): gpio.output(sclkpin, low) # reverse order of hi/lo here gpio.output(sclkpin, high) adcvalue <<= 1 if(gpio.input(misopin)): adcvalue |= 0x01 adcvalue >>= 3 # adcvalue &= 0x3ff <- commented out line should no longer necessary homecoming adcvalue

python

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -