间单点说就是对时序图及相关操作理解不到位,python 这边也就卡住了~
连接方式:温度传感器 -> ADC -> 树莓派,
在树莓派上使用 SPI,用到 Python Spidev
ADC 我用的是 ADC0832 (2 通道,8 位),搜到的教程都是以 MCP3008 (8 通道,10 位)为例子,对照着弄了一下,接线大概没什么问题了,
但是代码部分 analog_read (channel )这个函数没看懂.
我用 CH0 作为输入端,也就是需要设置 (Din = 1, Dout = 0 ),从哪里设置?
adc_tmp36.py
import spidev, time
spi = spidev.SpiDev ()
spi.open (0,0 )
def analog_read (channel ):
# !下面这两行不懂!
r = spi.xfer2 ([1, (8 + channel ) << 4, 0])
adc_out = ((r[1]&3 ) << 8 ) + r[2] #3 字节?
return adc_out
while True:
reading = analog_read (0 )
voltage = reading * 3.3 / 1024
temp_c = voltage * 100 - 50
temp_f = temp_c * 9.0 / 5.0 + 32
print ("Temp C=%f\t\tTemp f=%f" % (temp_c, temp_f ))
time.sleep (1 )
用到的方法:
open (bus, device )
xfer2 (list of values[, speed_hz, delay_usec, bits_per_word])
#Performs an SPI transaction. Chip-select should be held active
#between blocks.
ADC0832 datasheet:
MCP3008 datasheet 和 连接方法(跟我看的那个例子比较像):