0

I'm pretty new to SPI and trying to read Channel 0 off the MCP3462 ADC (16 bit) with Raspberry Pi 4. I cannot find any documentation on using Rpi/Python but I've done my best to write a converted version based on the Arduino Zero code provided on the microchip website (https://www.microchip.com/en-us/product/mcp3462). I'll eventually use C++ as its recommended but I am most comfortable in Python for now. Really just want a simple read function for the data but right now only 0's show up as output. I've got a pull up for IRQ. Perhaps it's something with settings that I need to configure but I am struggling to decipher the datasheet instructions. If anyone has any ideas or tips for reading datasheets, anything is appreciated, thanks! Code below:

import spidev
import time
import RPi.GPIO as GPIO


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

#GPIO Settings
GPIO.setmode(GPIO.BCM)

#Chip Select Pin
PIN_SPI_nCS = 8

#Control Register Address
WRT_CTRL = 0b01000010
RD_CTRL = 0b01000001

ADC_DATA = 0x00 #ADC DATA Register Address
CONFIG0 = 0x01
CONFIG1 = 0x02
CONFIG2 = 0X03
CONFIG3 = 0x04
IRQ = 0X05
SCAN = 0X07

#IRQ_MODE1 Settings
IRQ_MODE1_MDAT = 0b00001000
IRQ_MODE1_IRQ = 0b00000000

#IRQ_MODE0 Settings
IRQ_MODE0_HI = 0b00000100
IRQ_MODE0_HIZ = 0b00000000

#CONV_MODE Settings
CONV_MODE_CONT = 0b011000000




def SPI_WRT(REG_ADDR, REG_CFG):
    WR_DATA['DWORD'] = REG_CFG
    GPIO.output(PIN_SPI_nCS, GPIO.LOW)
    spi.xfer2([REG_ADDR << 2 | RD_CTRL])
    spi.xfer2(WR_DATA['BYTE']['LOW'])
    GPIO.output(PIN_SPI_nCS, GPIO.HIGH) 

def SPI_RD(REG_ADDR):
    RD_DATA = {'BYTE':{'STATUS':0, 'LOW':0, 'HIGH':0, 'UPPER':0}}
    GPIO.setup(PIN_SPI_nCS, GPIO.OUT) 
    GPIO.output(PIN_SPI_nCS, GPIO.LOW)
    RD_DATA['BYTE']['STATUS'] = spi.xfer2([REG_ADDR << 2 | RD_CTRL])
    RD_DATA['BYTE']['UPPER'] = spi.xfer2([0x00])[0]
    RD_DATA['BYTE']['HIGH'] = spi.xfer2([0x00])[0]
    RD_DATA['BYTE']['LOW'] = spi.xfer2([0x00])[0]
    print(RD_DATA)
    GPIO.output(PIN_SPI_nCS, GPIO.HIGH)
    


while True:
    #Configure SPI Settings
    
    
    #Read and Print ADC Register Data
    SPI_RD(ADC_DATA)
    time.sleep(1)
3
  • Why have you imported RPi.GPIO?
    – Milliways
    Apr 9 at 22:32
  • Hi @Milliways I used this because the original C++ code used a digital write which brought the CS low. I read that this was the RPi equivalent. Perhaps I should do this a different way? Thanks
    – MHart
    Apr 11 at 10:31
  • Most SPI code depends on the kernel drivers which manage CS. Unless you have enabled the driver spidev wouldn't do anything and if you are poking at CS (if it works at all) is only likely to interfere with normal operation. There are hundreds of SPI examples.
    – Milliways
    Apr 11 at 12:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.