I am doing a project that consists of 16 analog sensors, and I am acquiring its data through SPI connected to an ADC type tlc2543m, the idea is to acquire them by SPI using the Python of the Intel Galileo, that is, I am programming from the Linux of The Galileo and I communicate by ETH as if it were a server. The fact is that this is my code: but I only get strange data. I already read the datasheet of the ADC, here I leave it anyway.
How to program SPI communication from python using MRAA library???
Code:
#This Python file uses the following encoding: utf-8
import os, sys
#Configure ports of SPI protocol
import mraa
import time
spi=mraa.Spi(0)
#spi.mode(mraa.SPI_MODE3)
spi.mode(0)
spi.frequency(20000000)
spi.lsbmode(False)
ss=mraa.Gpio(9)
ss.dir(mraa.DIR_OUT)
ss.write(1)
sc=mraa.Gpio(8)
sc.dir(mraa.DIR_OUT)
sc.write(1)
def readadc(pinaleer,ss):
#primera configuracion
direccionaleer=0x0c
resp=0x00
segundobyte=0x00
valordigital=0x00
pinaleer=int(hex(pinaleer),16)
direccionaleer = direccionaleer | (pinaleer<<4) #guardo en los primeros 4 bits la direccion a leer
#direccionaleer = (direccionaleer<<8)
#print hex(dieccionaleer)
ss.write(1) #Enciendo la transferencia de datos
resp=spi.write(bytearray(hex(direccionaleer))) #Le digo que pin quiero leer
otra=spi.write(bytearray(0x00))#hex(direccionaleer))) #le mando ceros, ya acabé
ss.write(0) #Apago la transferencia de datos
time.sleep(0.0002) #Espero 20us
ss.write(0)#Enciedo la transferencia de datos
primerbyte=spi.write(bytearray(0x0000)) #Leo el primer byte de respuesta
segundobyte=spi.write(bytearray(0x00)) #Leo el segundo byte, total 2Byte= 16bits de repuesta
ss.write(1)#Apago la transferencia de datos
salida1= (resp[3]<<8)|(resp[2])#<<4)|(resp[1])
salida= (salida1>>4)+1
return salida
d=0
while d<5:
#d+=1
dato1 = readadc(1,ss)
dato2 = readadc(2,ss)
dato3 = readadc(3,ss)
dato4 = readadc(4,ss)
dato5 = readadc(5,ss)
dato6 = readadc(6,ss)
dato7 = readadc(7,ss)
dato8 = readadc(8,ss)
dato9 = readadc(9,ss)
dato10= readadc(10,ss)
dato11= readadc(11,ss)
dato12= readadc(12,ss)
dato13= readadc(13,ss)
#print "Dato recibido:"
print (dato1, dato2, dato3, dato4, dato5, dato6, dato7, dato8, dato9, dato10, dato11, dato12, dato13)
I already made it work. Instead of using the "write" function, use the "writeByte" function, Thanks for everything !!
Anyway I leave another example of SPI communication by Python, in this case I use the IntelGalileo Gen1 and ADC type tlc2543m: