Advertisement
silver2row

MCP3008 SPI BBIO

Nov 9th, 2019
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. from Adafruit_BBIO.SPI import SPI
  2.  
  3. SPI0_CS0  = "P9_17"
  4. SPI0_D0   = "P9_21"
  5. SPI0_D1   = "P9_18"
  6. SPI0_SCLK = "P9_22"
  7.  
  8. spi = SPI(1, 0)
  9.  
  10. class MCP3008:
  11.     def __init__(self, bus = 0, device = 0):
  12.         self.bus, self.device = bus, device
  13.         self.spi = SPI()
  14.         self.open()
  15.  
  16.     def open(self):
  17.         self.spi.open(self.bus, self.device)
  18.  
  19.     def read(self, channel = 0):
  20.         adc = self.spi.xfer2([1, (8 + channel) << 4, 0])
  21.         data = ((adc[1] & 3) << 8) + adc[2]
  22.         return data
  23.  
  24.     def close(self):
  25.         self.spi.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement