KeithSloan
Posts: 321
Joined: Tue Dec 27, 2011 9:09 pm

Trying to read pressure difference via SPI.

Mon Oct 31, 2016 11:25 am

I am trying to read the pressure difference as measured by a ClickDiffPressure see http://www.mikroe.com/click/diff-pressure/

I have written the following code

Code: Select all

import spidev
import time
spi = spidev.SpiDev()                   # create spi object
spi.open(0, 0)                          # open spi port 0, device (CS) 1
spi.mode = 0b11                         # 1,1 - 3 bytes read; 0,0 - 4 bytes read
try:
    while True:
        resp = spi.readbytes(3)         # read three bytes
        print resp
        time.sleep(0.1)                 # sleep for 0.1 secs
    # end while
except KeyboardInterrupt :
    spi.close()
When I run and blow into a tube attached to the top connector on the device I get

Code: Select all

[1, 72, 82]
[255, 255, 255]
[1, 72, 14]
[255, 255, 255]
[1, 72, 81]
[255, 255, 255]
[1, 72, 91]
[255, 255, 255]
[1, 72, 85]
[255, 255, 255]
[1, 72, 127]
Any ideas why the alternate 3 bytes are 255,255,255 I am guessing that the MCP3551 is not ready to deliver the next reading but don't see how I can check from python. The MCP3551 that the devices uses to communicate via SPI is documented here http://ww1.microchip.com/downloads/en/d ... 21950b.pdf

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

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 11:42 am

Yes, it's not ready so SDO is high (being seen as a stream of 1 bits).

Do the following.

Assert slave select (set to zero) and then wait for SDO to go low (it may already be low). When SDO goes low you do the SPI transfer. Then set slave select back high.

I.e. you need to control the slave select line and check the SDO level.

KeithSloan
Posts: 321
Joined: Tue Dec 27, 2011 9:09 pm

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 11:54 am

joan wrote:Yes, it's not ready so SDO is high (being seen as a stream of 1 bits).

Do the following.

Assert slave select (set to zero) and then wait for SDO to go low (it may already be low). When SDO goes low you do the SPI transfer. Then set slave select back high.

I.e. you need to control the slave select line and check the SDO level.
Is that possible from Python?
The only way I can see of doing it is to read one byte until its not 255 and then read the next two bytes

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

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 12:00 pm

I'd try this from pigpio.

Code: Select all

#!/usr/bin/env python

import time

import pigpio

CE0 = 8
MISO = 9

pi = pigpio.pi()

if not pi.connected:
   exit()

h = pi.spi_open(0, 50000, 0b11)

pi.write(CE0, 1) # deselect sensor

try:
   while True:

      pi.write(CE0, 0) # select sensor

      while pi.read(MISO) == 1:
         time.sleep(0.01)

      b, d = pi.spi_read(h, 3) # read three bytes

      pi.write(CE0, 1) # deselect sensor

      print(b, d)

      time.sleep(0.1)

except KeyboardInterrupt :
   pass

pi.spi_close(h)

pi.stop()

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 12:08 pm

Try Joan's pigpio. If you want to continue with spidev then try using spi.xfer (toggles the slave select between bytes) or spi.xfer2 (which does a single continuous transfer, slave select held low).

I've got a sample for an MCP3002 but not for an MCP3551.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

KeithSloan
Posts: 321
Joined: Tue Dec 27, 2011 9:09 pm

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 12:39 pm

DougieLawson wrote:Try Joan's pigpio. If you want to continue with spidev then try using spi.xfer (toggles the slave select between bytes) or spi.xfer2 (which does a single continuous transfer, slave select held low).

I've got a sample for an MCP3002 but not for an MCP3551.
Will try pigpio.

As far as I can see spi.xfer seems to write a value before reading. My reading of the doc on MCP3551 is it just outputs values

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 12:59 pm

All SPI is a two way street. You always have to write to an SPI device to read it.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

KeithSloan
Posts: 321
Joined: Tue Dec 27, 2011 9:09 pm

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 1:52 pm

DougieLawson wrote:All SPI is a two way street. You always have to write to an SPI device to read it.
I think you might be wrong if I try a xfer it just gets a Segmentation fault

Code: Select all

import spidev
import time
spi = spidev.SpiDev()			# create spi object
spi.open(0, 0)				# open spi port 0, device (CS) 1
spi.mode = 0b11				# 1,1 - 3 bytes read; 0,0 - 4 bytes read
try:
    while True:
        resp =spi.xfer(1)		
	print resp
	time.sleep(0.1)			# sleep for 0.1 secs
    # end while
except KeyboardInterrupt :
    spi.close()


Got it working with pigpio but don't like pigpio as it uses a daemon, sockets and pipes seems totally overkill as a solution.

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 1:57 pm

KeithSloan wrote: Got it working with pigpio but don't like pigpio as it uses a daemon, sockets and pipes seems totally overkill as a solution.
Wow.

PIGPIO is a tool, designed for multiple use cases, you don't need to use them all yourself, i don't think you even need to use the daemon if you use the C version.
Doug.
Building Management Systems Engineer.

KeithSloan
Posts: 321
Joined: Tue Dec 27, 2011 9:09 pm

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 2:08 pm

BMS Doug wrote:
KeithSloan wrote: Got it working with pigpio but don't like pigpio as it uses a daemon, sockets and pipes seems totally overkill as a solution.
Wow.

PIGPIO is a tool, designed for multiple use cases, you don't need to use them all yourself, i don't think you even need to use the daemon if you use the C version.
When I tried to run the python is told me I needed to start the daemon

BMS Doug
Posts: 3824
Joined: Thu Mar 27, 2014 2:42 pm
Location: London, UK

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 2:18 pm

KeithSloan wrote: PIGPIO is a tool, designed for multiple use cases, you don't need to use them all yourself, i don't think you even need to use the daemon if you use the C version.
When I tried to run the python is told me I needed to start the daemon[/quote]

For python you will need the daemon yes, the timings required are too tight for python.
Doug.
Building Management Systems Engineer.

KeithSloan
Posts: 321
Joined: Tue Dec 27, 2011 9:09 pm

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 2:20 pm

Fixed segmentation fault need to pass values to xfer as a list. Still cannot see a way of getting this to work with spidev and not get the rogue 255 values.

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

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 2:41 pm

I gave the pigpio example as it's simplest for me. You can use exactly the same technique with any GPIO library of your choice.

KeithSloan
Posts: 321
Joined: Tue Dec 27, 2011 9:09 pm

Re: Trying to read pressure difference via SPI.

Mon Oct 31, 2016 3:41 pm

Read the datasheet for MCP3551 more carefully it does continuous conversion and if the high order bit of a set of bytes is on it means its still reading the pressure difference. So whilst I have a pigpio version that works think I will stick with spidev and just ignore any set of bytes where the high order bit is set.

Thanks to all those who made comments

Return to “General discussion”