Proton BASIC Compiler - Interfacing with an MCP3426, MCP3427, or MCP3428 16 bit A/D converter


  • PicŪ Basic


  • Interfacing with an MCP3426, MCP3427, or MCP3428 16 bit A/D converter

    I've been working on a project that needs several 16-bit A/D inputs. The MCP3428 is a complete analog front end that contains a 16-bit A/D converter, four differential inputs, selectable gains of 1, 2, 4, or 8, and a precision 2.048 volt reference. These chips automatically calibrate gain and offset before every conversion.

    The MCP3426 and MCP3427 are two-channel versions of the same chip. If you need more than four inputs you can connect up to eight MCP3428's to the same I2C bus by changing the address bits ADR0 and ADR1. Note that these are tristate address pins, and that "floating" is a valid condition. That's how they get eight addresses using only two pins.

    Note that the output of these chips is 2's complement, meaning that if the result is negative (they can sense input voltages below ground), the MSB will be a "1".

    I used the declare "bus_scl ON", because in a single master I2C system the master is the only thing driving the clock (SCL) bus, so a pullup is only necessary on the data (SDA) line.

    Use this code if your PIC has MSSP:
    Code:
        
    'A/D converter setup 
       Symbol ADC_W = %11010000   'Address the A/D & write
       Symbol ADC_R = %11010001   'Address the A/D & read
       Symbol CH1 = %10001000     'channel 1 one-shot, 16 bits, x1 gain
       Symbol CH2 = %10101000     'channel 2 one-shot, 16 bits, x1 gain
       Symbol CH3 = %11001000     'channel 3 one-shot, 16 bits, x1 gain
       Symbol CH4 = %11000000     'channel 4 one-shot, 12 bits, x1 gain
      
       Declare bus_scl On              'pullup resistor not needed on clock line
       Declare Hbus_Bitrate 400       '100, 400, or 1000 
        
    'Set up variables
       Dim In1 As Word   'the data from input 1
       Dim In2 As Word   'the data from Input 2
       Dim In3 As Word   'the data from Input 3
       Dim In4 As Word   'the data from Input 4
       Dim Setup As Byte 'A/D configuration register read info   
       
    'Here's where we read the 4 channels and put the result in 4 variables:
       HBusOut ADC_W, [CH1]   'channel 1, single conversion, 16 bits, gain x1
       Repeat
       HBusIn ADC_R, [In1.HighByte, In1.LowByte, Setup] 'read the data into the variables
       Until Setup.7 = 0      'wait for the conversion to complete
       HBusOut ADC_W, [CH2]   'channel 2, single conversion, 16 bits, gain x1
       Repeat
       HBusIn ADC_R, [In2.HighByte, In2.LowByte, Setup]
       Until Setup.7 = 0      'wait for the conversion to complete
       HBusOut ADC_W, [CH3]   'channel 3, single conversion, 16 bits, gain x1
       Repeat
       HBusIn ADC_R, [In3.HighByte, In3.LowByte, Setup]
       Until Setup.7 = 0      'wait for the conversion to complete
       HBusOut ADC_W, [CH4]   'channel 4, single conversion, 16 bits, gain x1
       Repeat
       HBusIn ADC_R, [In4.HighByte, In4.LowByte, Setup]
       Until Setup.7 = 0
    If your PIC does NOT have an MSSP module, use this code:
    Code:
        
    'A/D converter setup 
       Symbol ADC_W = %11010000   'Address the A/D & write
       Symbol ADC_R = %11010001   'Address the A/D & read
       Symbol CH1 = %10001000     'channel 1 one-shot, 16 bits, x1 gain
       Symbol CH2 = %10101000     'channel 2 one-shot, 16 bits, x1 gain
       Symbol CH3 = %11001000     'channel 3 one-shot, 16 bits, x1 gain
       Symbol CH4 = %11000000     'channel 4 one-shot, 12 bits, x1 gain
      
       Declare bus_scl On              'pullup resistor not needed on clock line
         
    'Set up variables
       Dim In1 As Word   'the data from input 1
       Dim In2 As Word   'the data from Input 2
       Dim In3 As Word   'the data from Input 3
       Dim In4 As Word   'the data from Input 4
       Dim Setup As Byte 'A/D configuration register read info
    
    'Define the I2C bus pins (edit as necessary)
       Symbol SDA = PORTC.4
       Symbol SCL = PORTC.3   
       
    'Here's where we read the 4 channels and put the result in 4 variables:
       I2Cout SDA, SCL, ADC_W, [CH1]   'channel 1, single conversion, 16 bits, gain x1
       Repeat
       I2Cin SDA, SCL, ADC_R, [In1.HighByte, In1.LowByte, Setup] 'read the data into the variables
       Until Setup.7 = 0      'wait for the conversion to complete
       I2Cout SDA, SCL, ADC_W, [CH2]   'channel 2, single conversion, 16 bits, gain x1
       Repeat
       I2Cin SDA, SCL, ADC_R, [In2.HighByte, In2.LowByte, Setup]
       Until Setup.7 = 0      'wait for the conversion to complete
       I2Cout SDA, SCL, ADC_W, [CH3]   'channel 3, single conversion, 16 bits, gain x1
       Repeat
        I2Cin SDA, SCL, ADC_R, [In3.HighByte, In3.LowByte, Setup]
       Until Setup.7 = 0      'wait for the conversion to complete
       I2Cout SDA, SCL, ADC_W, [CH4]   'channel 4, single conversion, 16 bits, gain x1
       Repeat
       I2Cin SDA, SCL, ADC_R, [In4.HighByte, In4.LowByte, Setup]
       Until Setup.7 = 0
    Note that in the last example you may need to add the declare Slow_Bus ON if the clock speed is faster than 4 Mhz.