#python #spi #raspberry-pi4 #adc
#python #spi #raspberry-pi4 #АЦП
Вопрос:
У меня есть АЦП (MAX22530) на плате Mikroe click (ISO ADC 5). Который я пытаюсь прочитать через SPI с помощью Python (spidev). Я написал код на python, следуя примеру C из: https://www.maximintegrated.com/en/design/technical-documents/app-notes/7/7566.html
Но когда я запускаю код, я получаю только байты с 0 обратно. Я уже дважды проверил настройки подключения и SPI. Но я не могу понять, что не так. Мой код приведен ниже, кто-нибудь может определить, что я сделал не так?
Ссылка на код Mikroe C: https://github.com/MikroElektronika/mikrosdk_click_v2/tree/master/clicks/isoadc5 Ссылка на спецификацию АЦП: https://datasheets.maximintegrated.com/en/ds/MAX22530-MAX22532.pdf
import time
import spidev
import struct
import RPi.GPIO as GPIO
# ----GPIO----
# MOSI = GPIO10 = SD0 = pin 19
# MISO = GPIO9 = SDI = pin 21
# SCLK = GPIO11 = SCK = pin 23
# CE0 = GPIO8 (Chip enable 0) = CS0 = CS = pin 24
# GND
# 3.3V
# https://www.maximintegrated.com/en/design/technical-documents/app-notes/7/7566.html
# SPI command with CRC enabled
# 8 Bits 16 Bits 8 Bits
# 8 Bits -> First 6 Bits address of register, 1 Bit Control: Read = 0, Write = 1, 1 Bit Burst: 0 = Normal operation, 1 = Burst read operation
# 16 Bits -> Data
# 8 Bits -> CRC
# SPI command with CRC disabled
# 8 Bits 16 Bits
# 8 Bits -> First 6 Bits address of register, 1 Bit Control: Read = 0, Write = 1, 1 Bit Burst: 0 = Normal operation, 1 = Burst read operation
# 16 Bits -> Data
ADC1 = 0b00000001 #0x01
ADC2 = 0b00000010 #0x02
ADC3 = 0b00000011 #0x03
ADC4 = 0b00000100 #0x04
PROD_ID = 0x00
CONTROL = 0b00010100 #0x14
MAX22530_ID = 0x81
spiChannel = 0
vref = 1.80
def initSPI():
spi.open(0, spiChannel)
# Settings
spi.max_speed_hz = 100000
spi.mode = 0b00 # Default mode 0 (min = 0b00 = 0, max 0b11 = 3)
def readADC(adcNumber):
sendmsg = adcNumber
sendmsg = (((sendmsg << 1) 0x00) << 1 0x00) # = 0b00000100 when ADC1 = adcNumber
sendmsg = [sendmsg, 0b00000000, 0b00000000]
print("Send msg ADC: " str(bytes(sendmsg)))
replymsg = spi.xfer2(sendmsg)
print("Reply msg ADC: " str(bytes(replymsg)))
data = bytes(replymsg)[1:]
data = bytes([0b00000000, 0b00000000])
[adc] = struct.unpack('f', data)
voltage = (vref * adc) / 2048
print("Voltage ADC: " str(voltage))
def disableCRC(register):
sendmsg = register
sendmsg = (((sendmsg << 1) 0x00) << 1 0x00) # = 0b00000100 when ADC1 = adcNumber
sendmsg = [sendmsg, 0b00000000, 0b00000000]
print("Send msg CRC: " str(bytes(sendmsg)))
replymsg = spi.xfer2(sendmsg)
print("Reply msg CRC: " str(bytes(replymsg)))
if len(bytes(replymsg)) > len(bytes(b'x00x00x00')):
print("CRC is enabled (true)")
#TODO Disable CRC
print("n")
if __name__ == "__main__":
spi = spidev.SpiDev()
initSPI()
disableCRC(CONTROL)
readADC(ADC1) # 4 ADC numbers (0 thru 3)
readADC(ADC2)
readADC(ADC3)
readADC(ADC4)
# Disconnect from SPI device
spi.close()
Выходной код:
Send msg CRC: b'Px00x00'
Reply msg CRC: b'x00x00x00'
Send msg ADC: b'x04x00x00'
Reply msg ADC: b'x00x00x00'
Voltage ADC: 0.0
Send msg ADC: b'x08x00x00'
Reply msg ADC: b'x00x00x00'
Voltage ADC: 0.0
Send msg ADC: b'x0cx00x00'
Reply msg ADC: b'x00x00x00'
Voltage ADC: 0.0
Send msg ADC: b'x10x00x00'
Reply msg ADC: b'x00x00x00'
Voltage ADC: 0.0