Skip to content

Commit 2d4678f

Browse files
committed
Adding MCP3002.py
Modified MCP3008.py to support MCP3002
1 parent 00adf3d commit 2d4678f

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

Adafruit_MCP3002/MCP3002.py

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
3+
# just some bitbang code for testing both channels
4+
5+
import RPi.GPIO as GPIO, time, os
6+
7+
DEBUG = 1
8+
GPIO.setmode(GPIO.BCM)
9+
10+
# this function is not used, its for future reference!
11+
def slowspiwrite(clockpin, datapin, byteout):
12+
GPIO.setup(clockpin, GPIO.OUT)
13+
GPIO.setup(datapin, GPIO.OUT)
14+
for i in range(8):
15+
if (byteout & 0x80):
16+
GPIO.output(datapin, True)
17+
else:
18+
GPIO.output(datapin, False)
19+
byteout <<= 1
20+
GPIO.output(clockpin, True)
21+
GPIO.output(clockpin, False)
22+
23+
# this function is not used, its for future reference!
24+
def slowspiread(clockpin, datapin):
25+
GPIO.setup(clockpin, GPIO.OUT)
26+
GPIO.setup(datapin, GPIO.IN)
27+
byteout = 0
28+
for i in range(8):
29+
GPIO.output(clockpin, False)
30+
GPIO.output(clockpin, True)
31+
byteout <<= 1
32+
if (GPIO.input(datapin)):
33+
byteout = byteout | 0x1
34+
return byteout
35+
36+
# read SPI data from MCP3002 chip, 2 possible adc's (0 thru 1)
37+
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
38+
if ((adcnum > 1) or (adcnum < 0)):
39+
return -1
40+
GPIO.output(cspin, True)
41+
42+
GPIO.output(clockpin, False) # start clock low
43+
GPIO.output(cspin, False) # bring CS low
44+
45+
commandout = adcnum << 1;
46+
commandout |= 0x0D # start bit + single-ended bit + MSBF bit
47+
commandout <<= 4 # we only need to send 4 bits here
48+
49+
for i in range(4):
50+
if (commandout & 0x80):
51+
GPIO.output(mosipin, True)
52+
else:
53+
GPIO.output(mosipin, False)
54+
commandout <<= 1
55+
GPIO.output(clockpin, True)
56+
GPIO.output(clockpin, False)
57+
58+
adcout = 0
59+
60+
# read in one null bit and 10 ADC bits
61+
for i in range(11):
62+
GPIO.output(clockpin, True)
63+
GPIO.output(clockpin, False)
64+
adcout <<= 1
65+
if (GPIO.input(misopin)):
66+
adcout |= 0x1
67+
GPIO.output(cspin, True)
68+
69+
adcout /= 2 # first bit is 'null' so drop it
70+
return adcout
71+
# change these as desired
72+
SPICLK = 18
73+
SPIMOSI = 17
74+
SPIMISO = 21
75+
SPICS = 22
76+
77+
# set up the SPI interface pins
78+
GPIO.setup(SPIMOSI, GPIO.OUT)
79+
GPIO.setup(SPIMISO, GPIO.IN)
80+
GPIO.setup(SPICLK, GPIO.OUT)
81+
GPIO.setup(SPICS, GPIO.OUT)
82+
83+
# Note that bitbanging SPI is incredibly slow on the Pi as its not
84+
# a RTOS - reading the ADC takes about 30 ms (~30 samples per second)
85+
# which is awful for a microcontroller but better-than-nothing for Linux
86+
87+
print "| #0 \t #1|"
88+
print "-----------------------------------------------------------------"
89+
while True:
90+
print "|",
91+
for adcnum in range(2):
92+
ret = readadc(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS)
93+
print ret,"\t",
94+
print "|"

0 commit comments

Comments
 (0)