2015-01-14 11:41:43 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
# Author: Henry Bruce <henry.bruce@intel.com>
|
|
|
|
|
# Copyright (c) 2016 Intel Corporation.
|
2015-01-14 11:41:43 +00:00
|
|
|
#
|
2019-05-09 09:47:11 -07:00
|
|
|
# SPDX-License-Identifier: MIT
|
2015-01-14 11:41:43 +00:00
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
# Example Usage: Read from MCP3004 ADC pin 0 in single ended mode
|
2015-01-14 11:41:43 +00:00
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
import mraa
|
|
|
|
|
import time
|
2015-01-14 11:41:43 +00:00
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
# initialise SPI
|
|
|
|
|
dev = mraa.Spi(0)
|
2015-01-14 11:41:43 +00:00
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
# prepare data to send
|
|
|
|
|
txbuf = bytearray(3)
|
|
|
|
|
txbuf[0] = 0x01
|
|
|
|
|
txbuf[1] = 0x80
|
|
|
|
|
txbuf[2] = 0x00
|
2015-01-14 11:41:43 +00:00
|
|
|
|
2018-01-30 11:32:18 +05:30
|
|
|
while True:
|
|
|
|
|
# send data through SPI
|
|
|
|
|
rxbuf = dev.write(txbuf)
|
|
|
|
|
value = ((rxbuf[1] & 0x03) << 8) | rxbuf[2]
|
|
|
|
|
print(value)
|
|
|
|
|
|
|
|
|
|
time.sleep(0.5)
|