Advertisement
Guest User

pot

a guest
Mar 4th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. from __future__ import division
  2. import spidev
  3.  
  4. def bitstring(n):
  5. s = bin(n)[2:]
  6. return '0'*(8-len(s)) + s
  7.  
  8. def read(adc_channel=0, spi_channel=0):
  9. conn = spidev.SpiDev(0, spi_channel)
  10. conn.max_speed_hz = 1200000 # 1.2 MHz
  11. cmd = 128
  12. if adc_channel:
  13. cmd += 32
  14. reply_bytes = conn.xfer2([cmd, 0])
  15. reply_bitstring = ''.join(bitstring(n) for n in reply_bytes)
  16. reply = reply_bitstring[5:15]
  17. return int(reply, 2) / 2**10
  18.  
  19. if __name__ == '__main__':
  20. print read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement