以下代码使用连接到Rpi和我的家庭报警面板的2个MCP3008芯片。这些16个模拟值在1023中的600到715之间波动,具体取决于PIR等。
Int值打印出来:
Chip A - 1023 Chip B - 242
Chip A - 1023 Chip B - 1023
Chip A - 0 Chip B - 68
Chip A - 1023 Chip B - 599
Chip A - 1023 Chip B - 1023
Chip A - 16 Chip B - 1023
Chip A - 1023 Chip B - 1023
Chip A - 1023 Chip B - 1023
我需要:
Value 1 = chip 1 readvalue 1
Value 2 = chip 1 readvalue 2
etc...
我尝试了字符串,列表,IO,包装器,但我只获得了第一个值。
您能否详细了解您的需求?目前尚不清楚。 - Sildoreth 35分钟前
MCP3008_A和MCP3008_B打印出来自芯片的模拟值。我想将每个值分配给一个特定的名称,例如,Value1 = 1023,Value2 = 654等。最终我希望能够写出(如果值2> = 653)和(值10 <= 702)做某事。希望这能解释得更好 - maurice1 19分钟前
也许更清楚,在代码的底部我有#V1 = input.readline(),#V2 =等我需要知道我应该把它放在哪里读取MCP3008_A的输出 - maurice1 1分钟前编辑
您能否将所有这些信息添加到原始帖子中? - Sildoreth 38秒前
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/blob/master/Adafruit_MCP3008/mcp3008.py
# just some bitbang code for testing all 8 channels
import RPi.GPIO as GPIO, time, os
import time # import time for the sleep function
import os
import sys
import cStringIO
from cStringIO import StringIO
import io
DEBUG = 1
GPIO.setmode(GPIO.BCM)
# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
def readadc(adcnum, clockpin, mosipin, misopin, cspin):
if ((adcnum > 16) or (adcnum < 0)):
return -1
GPIO.output(cspin, True)
GPIO.output(clockpin, False) # start clock low
GPIO.output(cspin, False) # bring CS low
commandout = adcnum
commandout |= 0x18 # start bit + single-ended bit
commandout <<= 3 # we only need to send 5 bits here
for i in range(5):
if (commandout & 0x80):
GPIO.output(mosipin, True)
else:
GPIO.output(mosipin, False)
commandout <<= 1
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout = 0
# read in one empty bit, one null bit and 10 ADC bits
for i in range(12):
GPIO.output(clockpin, True)
GPIO.output(clockpin, False)
adcout <<= 1
if (GPIO.input(misopin)):
adcout |= 0x1
GPIO.output(cspin, True)
adcout /= 2 # first bit is 'null' so drop it
return adcout
if __name__=='__main__':
try:
# change these as desired
SPICLK = 11 #MCP3008_13
SPIMISO = 9 #MCP3008_12
SPIMOSI = 10 #MCP3008_11
SPICS = 8 #MCP3008_10
SPICLK2 = 17 #MCP3008_13
SPIMISO2 = 27 #MCP3008_12
SPIMOSI2 = 22 #MCP3008_11
SPICS2 = 7 #MCP3008_10
# set up the SPI interface pins
GPIO.setup(SPICLK, GPIO.OUT)
GPIO.setup(SPIMISO, GPIO.IN)
GPIO.setup(SPIMOSI, GPIO.OUT)
GPIO.setup(SPICS, GPIO.OUT)
GPIO.setup(SPICLK2, GPIO.OUT)
GPIO.setup(SPIMISO2, GPIO.IN)
GPIO.setup(SPIMOSI2, GPIO.OUT)
GPIO.setup(SPICS2, GPIO.OUT)
while True:
#print "",
time.sleep(1)
for adcnum in range(8):
MCP3008_A = readadc(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS)
MCP3008_B = readadc(adcnum, SPICLK2, SPIMOSI2, SPIMISO2, SPICS2)
print ' Chip A - ' "%4d" % MCP3008_A,
print ' Chip B - ' "%4d" % MCP3008_B,
print ''
time.sleep(0.3)
d1=str(MCP3008_A)
d2=str(MCP3008_B)
print "--------------------------------------"
#V1 = input.readline()
#V2 = input.readline(2)
#V3 = input.readline()
#v4 = input.read()
except KeyboardInterrupt:
pass
GPIO.cleanup()