MrCapri
Posts: 16
Joined: Fri Mar 18, 2016 11:17 am

Reading second SPI channel?

Sat Mar 26, 2016 10:11 am

Hi

I'm new to 'real' coding I used to program plc and is currently working whit cnc, I have a few question about how you code for Reading different channels.

I'm currently using a R pi 3, have a set up of 2 x type k running thru amp's (ad8495) in to a mcp3208.

After about a day at banking my head against other peoples awesome code I managed to figure out the basic of Reading a channel, converting incoming bit's to volt/temp and then how to Wright this to a log.

Now my project is a Data Logger for a racecar http://www.rawvalley.se, I'm going to need quite a few sensors to read, I require 15 analog signals in for 2 mcp3208 on separate SPI channels.

This is the code I now use to read my mcp3208 in python.

Code: Select all

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
 
# Function to read SPI data from MCP3208 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
  adc = spi.xfer2([6+((4&channel)>>2),(3&channel)<<6,0])
  data = ((adc[1]&15) << 8) + adc[2]
  return data
 
I'm far to green to actually understand exactly how this works, how would I wright this to read from spi channel 1 as well as 0.

I also have a Quick question, i require reasonably fast samplings per seconds on all channels at least 100 samples per second.

I plan to use two mcp3208 read 11 analog temp signals, 4 analog pressure signals, 3 incoming pulse signals and an accelerometer.

Can i run two mcp3208 on spi and run the accelerometer on a non spi channel? and still get 100sps?

Thanks for any sugestions ovservations or moments of facepalming on my ineptitude :)

Jonas Ljungberg

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reading second SPI channel?

Sat Mar 26, 2016 10:19 am

The Linux SPI driver can handle about 20 thousand samples per second on the Pi 2 and earlier. The Pi 3 seems to be able to handle 75 thousand samples per second.

To use both SPI devices just create two SPI devices with separate calls to SpiDev. i.e. open 0,0 and 0,1.

MrCapri
Posts: 16
Joined: Fri Mar 18, 2016 11:17 am

Re: Reading second SPI channel?

Sat Mar 26, 2016 10:44 am

Would this be correct thinking?

Code: Select all

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
 
# Function to read SPI data from MCP3208 chip
# Channel must be an integer 0-7
def ReadChannel_01(channel):
  adc = spi.xfer2([6+((4&channel)>>2),(3&channel)<<6,0])
  data = ((adc[1]&15) << 8) + adc[2]
  return data
  
  # Open SPI bus
spi = spidev.SpiDev()
spi.open(0,1)
 
# Function to read SPI data from MCP3208 chip
# Channel must be an integer 0-7
def ReadChannel_02(channel):
  adc = spi.xfer2([6+((4&channel)>>2),(3&channel)<<6,0])
  data = ((adc[1]&15) << 8) + adc[2]
  return data

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Reading second SPI channel?

Sat Mar 26, 2016 10:51 am

I'd lean more towards this style, although I'm certainly the wrong person to advise on Python style (being an old C coder).

Code: Select all

# Open SPI devices

spi0 = spidev.SpiDev()
spi0.open(0,0)

spi1 = spidev.SpiDev()
spi1.open(0,1)
     
# Function to read SPI data from MCP3208 chip
# Channel must be an integer 0-7

def ReadChannel(device, channel):

   if device == 0:
      spi = spi0
   else:
      spi = spi1

   adc = spi.xfer2([6+((4&channel)>>2),(3&channel)<<6,0])
   data = ((adc[1]&15) << 8) + adc[2]

   return data

MrCapri
Posts: 16
Joined: Fri Mar 18, 2016 11:17 am

Re: Reading second SPI channel?

Sat Mar 26, 2016 12:44 pm

would spi 0 be spcefied as channel 0-7 and sp1 as 8-15?

MrCapri
Posts: 16
Joined: Fri Mar 18, 2016 11:17 am

Re: Reading second SPI channel?

Sat Mar 26, 2016 12:46 pm

btw thanks for the help whit this it's realy apricate

MR.Chang41
Posts: 1
Joined: Sat Nov 04, 2017 9:34 am

Re: Reading second SPI channel?

Sat Nov 04, 2017 9:37 am

I want it to save the file.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Reading second SPI channel?

Sat Nov 04, 2017 8:20 pm

You will be able to see how to wire 2 SPI devices to the 1 SPI bus with different clock select pins here https://en.wikipedia.org/wiki/Chip_select

On raspberry the CS (chip select pins,) are called SPI_CE0_N and SPI_CE1_N

from above posted code

Code: Select all

# Open SPI devices

spi0 = spidev.SpiDev()
spi0.open(0,0) #open spi0 on bus 0 CS 0

spi1 = spidev.SpiDev()
spi1.open(0,1)#open spi1 on bus 0 cs 1

Return to “Python”