Skip to content

Commit 974ef93

Browse files
some simple bitbang mcp3008 test code
1 parent b11e5f4 commit 974ef93

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

Diff for: Adafruit_MCP3008/mcp3008.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 all 8 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 MCP3008 chip, 8 possible adc's (0 thru 7)
37+
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
38+
if ((adcnum > 7) 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
46+
commandout |= 0x18 # start bit + single-ended bit
47+
commandout <<= 3 # we only need to send 5 bits here
48+
for i in range(5):
49+
if (commandout & 0x80):
50+
GPIO.output(mosipin, True)
51+
else:
52+
GPIO.output(mosipin, False)
53+
commandout <<= 1
54+
GPIO.output(clockpin, True)
55+
GPIO.output(clockpin, False)
56+
57+
adcout = 0
58+
# read in one empty bit, one null bit and 10 ADC bits
59+
for i in range(12):
60+
GPIO.output(clockpin, True)
61+
GPIO.output(clockpin, False)
62+
adcout <<= 1
63+
if (GPIO.input(misopin)):
64+
adcout |= 0x1
65+
66+
GPIO.output(cspin, True)
67+
68+
adcout /= 2 # first bit is 'null' so drop it
69+
return adcout
70+
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 \t #2 \t #3 \t #4 \t #5 \t #6 \t #7\t|"
88+
print "-----------------------------------------------------------------"
89+
while True:
90+
print "|",
91+
for adcnum in range(8):
92+
ret = readadc(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS)
93+
print ret,"\t",
94+
print "|"

0 commit comments

Comments
 (0)