7. Code-Beispiel zur Verwendung der analogen Anschlüsse
import spidev
import time
import sys
spi = spidev.SpiDev()
spi.open(0,0)
def readadc(adcnum):
if adcnum >7 or adcnum <0:
return-1
r = spi.xfer2([1,8+adcnum <<4,0])
adcout = ((r[1] &3) <<8)+r[2]
return adcout
while True:
if len(sys.argv) >1:
for i in range(len(sys.argv)):
if i == 0:
else:
else:
print "_______________________________________\n"
print "Channel 0"
value=readadc(0)
volts=(value*3.3)/1024
print("%4d/1023 => %5.3f V" % (value, volts))
print "Channel 1"
value=readadc(1)
volts=(value*3.3)/1024
print("%4d/1023 => %5.3f V" % (value, volts))
print "Channel 2"
value=readadc(2)
volts=(value*3.3)/1024
print("%4d/1023 => %5.3f V" % (value, volts))
print "Channel 3"
value=readadc(3)
volts=(value*3.3)/1024
print("%4d/1023 => %5.3f V" % (value, volts))
print "_______________________________________\n"
time.sleep(1.5)
Ausgabe 31.03.2017
print "_______________________________________\n"
adc_channel = int(sys.argv[i])
print "Channel " + str(adc_channel)
value=readadc(adc_channel)
volts=(value*3.3)/1024
print("%4d/1023 => %5.3f V" % (value, volts))
print " "
print "_______________________________________\n"
time.sleep(1.5)
Copyright by Joy-IT
12