New

mcp_measure.py

780 chars
July 2016
264 views
Reclip Copy RAW
  1. #!/usr/bin/python
  2.  
  3. import spidev
  4. import time
  5. import os
  6.  
  7. spi = spidev.SpiDev()
  8. spi.open(0,0)
  9.  
  10. def readChannel(channel):
  11. adc = spi.xfer2([1,(8+channel)<<4,0])
  12. data = ((adc[1]&3) << 8 ) + adc[2]
  13. return data #adc
  14.  
  15. def convertVolts(data,places):
  16. volts = (data*4.96) / float(1023)
  17. volts = round( volts, places )
  18. return volts
  19.  
  20. def convertPSI(volts):
  21. psi =((volts-81)/72)*6
  22. return psi
  23.  
  24. pressure_channel = 1
  25. delay = 2
  26.  
  27. while True:
  28. pressure_lvl = readChannel(pressure_channel)
  29. pressure_volts = convertVolts(pressure_lvl,7777)
  30. psi = convertPSI( pressure_lvl )
  31.  
  32. print "----"
  33. print( "{} PSI - Channel data: {} ({}V)".format( psi, pressure_lvl, pressure_volts ) )
  34.  
  35. time.sleep(delay)