- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Hello,
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
# 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</span>
# direccionaleer = (direccionaleer<<8)</span>
# 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])</span>
salida= (salida1>>4)+1
return salida
d=0
while d<5:</span>
# 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)
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Hi NandaRuiz,
Thank you for contacting us. We'd like to help you solve this issue. First, what exactly do you mean with strange data, is it incorrect data or random characters. Please post a picture of your output. We also reviewed the datasheet of the tlc2543m. I've personally never used the tlc2543m, but, from what I can see from the datasheet you should be able to use it in your circuit.
One thing I noticed is that it has 11 analog inputs and in your post you said you wanted to connect 16 sensors. Confirm that everything is properly connected, you can also send us a picture so we can review the connection diagram.
Also, a good way to start troubleshooting issues with SPI is using a logic analyzer. You can measure the SPI signals and make sure that it's behaving as you expect. You can also measure in different parts of your circuit and analyze in which point the signal is lost and turns into the strange data you mention. This way you can isolate the problem and determine if what needs to be fixed is the programming or something related to the external circuitry.
We'll be waiting for your response.
-Sergio A
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Hi Sergio,
Well.. I've 16 sensors but only use 10, (I've two ADC's), the others input are test:
dato11= readadc(11,ss) <---- (Vref+ - Vref-)/2</span>
dato12= readadc(12,ss) <---- Vref-</span>
dato13= readadc(13,ss) <---- Vref+</span>
I've tried doing the acquisition using the Arduino IDE, and it works, therefore, the circuit is fine:
# include
int cs = 9; // signal CS of ADC1
int cs1 = 8; // signal CS of ADC2
int dat[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char aux[256];
void setup()
{
// PINES I/O
pinMode(cs, OUTPUT);
pinMode(cs1, OUTPUT);
digitalWrite(cs, 1);
digitalWrite(cs1, 1);
// CONFIGURACION SPI
SPI.setClockDivider(SPI_CLOCK_DIV16);
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE3);
SPI.begin();
// PUERTO SERIAL
Serial.begin(57600);
}
void loop()
{
// READING WIRE//
//---------- DAQ ----------//
// MODULO 1
dat[0] = read_adc(1, 1); // 1
dat[1] = read_adc(2, 1); // 2
dat[2] = read_adc(3, 1); // 3
dat[3] = read_adc(4, 1); // 4
dat[4] = read_adc(5, 1); // 5
dat[5] = read_adc(6, 1); // 6
dat[6] = read_adc(7, 1); // 7
dat[7] = read_adc(8, 1); // 8
// MODULO 2
dat[8] = read_adc(1, 2); // 9
dat[9] = read_adc(2, 2); // 10
dat[10] = read_adc(3, 2); // 11
dat[11] = read_adc(4, 2); // 12
dat[12] = read_adc(5, 2); // 13
dat[13] = read_adc(6, 2); // 14
dat[14] = read_adc(7, 2); // 15
dat[15] = read_adc(8, 2); // 16
//---------------------------//
//-- ENVIO DE DATOS --//
sprintf(aux, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
dat[0], dat[1], dat[2], dat[3], dat[4], dat[5], dat[6], dat[7], dat[8],
dat[9], dat[10], dat[11], dat[12], dat[13], dat[14], dat[15]);
Serial.println(aux);
}
int read_adc(int x, int pa)
{
int digitalvalor = 0;
int paq = 0;
byte primeraconfig = 0b00000000;
byte primerbyte = 0b00000000;
byte segundobyte = 0b00000000;
primeraconfig |= (x << 4);
primeraconfig |= (3 << 2);
if (pa == 1)
{
paq = cs;
}
else
{
paq = cs1;
}
noInterrupts();
digitalWrite(paq, 0);
SPI.transfer(primeraconfig);
SPI.transfer(0x00);
digitalWrite(paq, 1);
delayMicroseconds(20);
digitalWrite(paq, 0);
primerbyte = SPI.transfer(0x00);
segundobyte = SPI.transfer(0x00);
digitalWrite(paq, 1);
//Serial.println(primerbyte,HEX);
//Serial.println(segundobyte,HEX);
digitalvalor |= (primerbyte << 8);
digitalvalor |= (segundobyte);
return ((digitalvalor >> 4)+1);
}
I have already done many configurations and tests, and this is the output:
Note that the test signals do not match. The last three numbers should be 2048, 1 and 4096.
I appreciate your help!!, thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
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:
# This Python file uses the following encoding: utf-8
import os, sys
import mraa
import time
spi=mraa.Spi(0) # Bus comunication
spi.mode(mraa.SPI_MODE0) # Comunication MODE0
spi.frequency(2000000) # Frequency of 2MHz
spi.lsbmode(False) # Format data MSB
ss=mraa.Gpio(8) # wire CS
ss.dir(mraa.DIR_OUT)
ss.write(1)
def readadc(pinaleer,ss):
dieccionaleer=0x0c # Input c=1100 so Output data length 16bits, MSB first, Unipolar
pinaleer=int(hex(pinaleer),16) # address bits (4)
dieccionaleer = dieccionaleer | (pinaleer<<4) # Put address bit in the 4th bits
# Comunication
ss.write(0) # CS=0 <-- Enables the device
esc=spi.writeByte(dieccionaleer) # First byte with input info
esc2=spi.writeByte(0x00) # Complete the 16bits --> Clock
ss.write(1) # CS=1 <--- Disables the device
time.sleep(0.00020) # Wait more time than 10us= time conversion AD
ss.write(0) # CS=0 <-- Enables the device
primerbyte=spi.writeByte(0x00) # First byte with 8bit of information
segundobyte=spi.writeByte(0x00) # Second byte with 4bit (MSB) information , 8+4=12bits
ss.write(1) # CS=1 <--- Disables the device
salida1=0x0000
salida1=salida1 | (primerbyte << 8)
salida1=salida1 | (segundobyte)
salida = (salida1>>4)+1 # Output 12bits
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) # pin Test (Vref+ - Vref-) /2 =~ 2048
dato12= readadc(12,ss) # pin Test Vref- 0000
dato13= readadc(13,ss) # pin Test Vref+ 4096
print (dato1, dato2, dato3, dato4, dato5, dato6, dato7, dato8, dato9, dato10, dato11, dato12, dato13)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
Hi NandaRuiz,
It's great to hear you were able to make it work. Thank you for sharing your results with the community. Don't hesitate to contact us again if help is needed.
Regards,
-Sergio

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page