简体   繁体   中英

pi won't read values from MCP3424

when I try to read data from a MCP3424 ADC, I get unexpected, wrong results. I know the device is connected, butthe results I'm reading are wrong

I write to channels 3 and 4 of the ADC. When I read the result back, the data in the config register doesn't match what I programmed

import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# I2C address of the device
MCP3425_DEFAULT_ADDRESS = 0x68
# MCP3425 Configuration Command Set
MCP3425_CMD_NEW_CNVRSN = 0x80 # Initiate a new conversion(One-Shot Conversion mode only)
MCP3425_CMD_MODE_CONT = 0x10 # Continuous Conversion Mode
MCP3425_CMD_MODE_ONESHOT = 0x00 # One-Shot Conversion Mode
MCP3425_CMD_SPS_240 = 0x00 # 240 SPS (12-bit)
MCP3425_CMD_SPS_60 = 0x04 # 60 SPS (14-bit)
MCP3425_CMD_SPS_15 = 0x08 # 15 SPS (16-bit)
MCP3425_CMD_GAIN_1 = 0x00 # PGA Gain = 1V/V
MCP3425_CMD_GAIN_2 = 0x01 # PGA Gain = 2V/V
MCP3425_CMD_GAIN_4 = 0x02 # PGA Gain = 4V/V
MCP3425_CMD_GAIN_8 = 0x03 # PGA Gain = 8V/V
MCP3425_CMD_READ_CNVRSN = 0x00 # Read Conversion Result Data
MCP3425_CMD_CH4 =0x60
MCP3425_CMD_CH3 =0x40

class MCP3425():
    def __init__(self):
        self.config_command()

    def config_command(self):
        """Select the Configuration Command from the given provided values"""
        CONFIG_CMD4 = (MCP3425_CMD_CH4| MCP3425_CMD_MODE_CONT | MCP3425_CMD_SPS_60 | MCP3425_CMD_GAIN_2)
        bus.write_byte(MCP3425_DEFAULT_ADDRESS, CONFIG_CMD4)
        CONFIG_CMD3 = (MCP3425_CMD_CH3| MCP3425_CMD_MODE_CONT | MCP3425_CMD_SPS_240 | MCP3425_CMD_GAIN_1)
        bus.write_byte(MCP3425_DEFAULT_ADDRESS, CONFIG_CMD3)
        print ('-C-', CONFIG_CMD4, CONFIG_CMD3)

    def read_adc(self, channel):
        """Read data back from MCP3425_CMD_READ_CNVRSN(0x00), 2 bytes
        raw_adc MSB, raw_adc LSB"""
        data = bus.read_i2c_block_data(MCP3425_DEFAULT_ADDRESS, (MCP3425_CMD_READ_CNVRSN | channel), 3)
        print (channel, data)
        # Convert the data to 12-bits
        raw_adc = ((data[0] & 0x0F) * 256) + data[1]
        if raw_adc > 2047 :
            raw_adc -= 4095
        return {'r' : raw_adc}

#from MCP3425 import MCP3425
mcp3425 = MCP3425()
while True :
    adc = mcp3425.read_adc(MCP3425_CMD_CH4)
    print ("Digital Value of Analog Input 4: %d "%(adc['r']))
    adc = mcp3425.read_adc(MCP3425_CMD_CH3)
    print ("Digital Value of Analog Input 3: %d "%(adc['r']))
    print (" ********************************* ")
    time.sleep(0.8)

I write 117 (01110101)to channel 4, and 80 (01010000) to channel 3 config registers. Which means for both channels I should get 3 bytes back: 2 data bytes and one config register byte

this is the printout I'm getting, no values read (Ch4+ is connected to voltage divider (2.5v), Ch4- and Ch3- are connected to ground, Ch3+ is floating) and byte 3 is just the address, not the Config register

-C- 117 80
96 [0, 0, 96]
Digital Value of Analog Input 4: 0 
64 [0, 0, 64]
Digital Value of Analog Input 3: 0 
 *********************************

now it took some time but I found the issue. The trick was that the MCP3434 is not a 4-channel ADC, but a single ADC with an 4-input multiplexer. Once I realized that, the schematic from the datasheet was obvious. It is just not explained very well in the description on how to handle dataflow on the I2C bus.

So you can not configure two ADC's to measure in parallel but when you need to read more channels you have to

1) configure/start channel 1

2) read data

3) configure/start channel 2

4) read data

the read data step is thus channel independant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM