mcp3008.py 464 B

123456789101112131415161718
  1. from spidev import SpiDev
  2. class MCP3008:
  3. def __init__(self, bus = 0, device = 0):
  4. self.bus, self.device = bus, device
  5. self.spi = SpiDev()
  6. self.open()
  7. def open(self):
  8. self.spi.open(self.bus, self.device)
  9. def read(self, channel = 0):
  10. adc = self.spi.xfer2([1, (8 + channel) << 4, 0])
  11. data = ((adc[1] & 3) << 8) + adc[2]
  12. return data
  13. def close(self):
  14. self.spi.close()