使用Python在Raspberry Pi上使用daisychaining MCP3008 SPI时出现问题

2024-09-30 22:10:12 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我正试图让两个MCP3008通过SPI与我的raspberry pi和Python脚本进行通信。电位计应向MCP3008输入通道发送一定的模拟值

以下是我在Fritzing中的设置:

Breadboard Schematic

以下是示意图概述:

Schematic Overview

SPI接线基于标准daisychain示意图,如所示:

SPI Daisy Chain

我使用的Python代码是:

import spidev
import time

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 1000000

def read_spi(channel):
  spidata = spi.xfer2([0,(8+channel)<<4,0])
  return ((spidata[1] & 3) << 8) + spidata[2]

try:
  while True:
    channeldata = read_spi(0)
    print (channeldata)
    time.sleep(.1)

except KeyboardInterrupt:
  spi.close()

我得到了一些值,但它们波动很大,每一个值都可能在0到1023之间

我用一个没有daisychaining的MCP3008试过,当然,它工作得很好,所以我猜它与daisychain不正确或者MCP3008的寻址有关

你们能帮帮我吗?非常感谢

干杯

德瓦图


Tags: importspireadtimechannelraspberry示意图spidev