数据传输Pi/MCP3008

Data transmission Pi/MCP3008

我有一个关于从 Raspberry Pi 到 mcp3008 的数据传输的问题。这只是一个理论上的。当他们交换字节时,主机发送 1 个字节并接收 1 个字节。然后发送第二个字节并接收第二个字节。或者主机发送 3 个字节并在之后接收 3 个字节。我的理解是第一个吧?

适用于 MCP3008 的 Adafruit 库有您的答案。查看 read_adc() 函数:

def read_adc(self, adc_number):
    """Read the current value of the specified ADC channel (0-7).  The values
    can range from 0 to 1023 (10-bits).
    """
    assert 0 <= adc_number <= 7, 'ADC number must be a value of 0-7!'
    # Build a single channel read command.
    # For example channel zero = 0b11000000
    command = 0b11 << 6                  # Start bit, single channel read
    command |= (adc_number & 0x07) << 3  # Channel number (in 3 bits)
    # Note the bottom 3 bits of command are 0, this is to account for the
    # extra clock to do the conversion, and the low null bit returned at
    # the start of the response.
    resp = self._spi.transfer([command, 0x0, 0x0])
    # Parse out the 10 bits of response data and return it.
    result = (resp[0] & 0x01) << 9
    result |= (resp[1] & 0xFF) << 1
    result |= (resp[2] & 0x80) >> 7
    return result & 0x3FF

它似乎发送了一个三字节的命令(其中只有一个字节是非零的):

resp = self._spi.transfer([command, 0x0, 0x0])

响应是三个字节,其中包含打包的 10 位 ADC 值。

resp = self._spi.transfer([command, 0x0, 0x0])
# Parse out the 10 bits of response data and return it.
result = (resp[0] & 0x01) << 9
result |= (resp[1] & 0xFF) << 1
result |= (resp[2] & 0x80) >> 7