/
mcp3551.py
67 lines (47 loc) · 1.26 KB
/
mcp3551.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
from adc import ADC
class MCP3551(ADC):
def setup_pins(self):
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(self._miso, GPIO.IN)
GPIO.setup(self._clk, GPIO.OUT)
GPIO.setup(self._cs, GPIO.OUT)
def get_resolution(self):
return 2097152.0
def read_value(self):
GPIO.output(self._cs, True)
GPIO.output(self._clk, True)
start = time.clock()
# toggle CS until SDO/RDY is low
while True:
GPIO.output(self._cs, False)
time.sleep(0.001)
if (GPIO.input(self._miso) == False):
break
GPIO.output(self._cs, True)
if (time.clock() - start > 2):
print "MCP3551 error: timeout"
return 0
# read in two overflow bits and 22 ADC bits
adcraw = 0
for i in range(24):
GPIO.output(self._clk, False)
GPIO.output(self._clk, True)
adcraw <<= 1
if (GPIO.input(self._miso)):
adcraw |= 0x1
time.sleep(0.001)
GPIO.output(self._cs, True)
if (adcraw & 0x400000):
print "MCP3551 error: overflow high"
if (adcraw & 0x800000):
print "MCP3551 error: overflow low"
# remove overflow and sign bits
adcout = adcraw & 0x1FFFFF
# convert to negative number if necessary
if (adcraw & 0x200000):
adcout = (~adcout & 0x1FFFFF) * -1 + 1
return adcout