News:

Let's find out together what makes a PIC Tick!

Main Menu

Issues with the MCP3422

Started by Yves, Today at 01:19 PM

Previous topic - Next topic

Yves

hello all
 I have on board a MCP3422 with a clearly marked A0 address (%10100000) . I'm using Les routine (of wish i can't find anymore on this forum). The problem is I get 2056 with both channel. I also tested with the Microchip MCP3422 module which as the same address. My SDA and SCL connection is right and I checked and it is rightly connected and both have a pull-up resistor of 4.7K.
 Please let ne know where I'm going wrong. Many thanks in advance
best RegardsMCP Les MCP3422.zip
Yves
Yves

trastikata

After writing to the configuration register, can you read it back to confirm the correct communication?

Frizie

I don't know what you're doing wrong, but the example I gave you in another topic works fine.
Ohm sweet Ohm | www.picbasic.nl

top204

#3
Looking at my files, I wrote some code for an MCP3422 back in 2020, which is the code you are referring too.

I have just re-wrote my original code with some formatting improvements, and tried it on a device, and it works. The code listing I tried is below:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Read from an MCP3422 ADC device.
' Written by Les Johnson for the Positron8 BASIC compiler.
'
    Device = 18F26K22                               ' Tell the compiler what device to compile for
    Declare Xtal = 64                               ' Tell the compiler what frequency the device is operating at (in MHz)
    Declare Auto_Heap_Strings = On                  ' Make all Strings "Heap" types, so they always get placed after standard variables
    Declare Auto_Variable_Bank_Cross = On           ' Make sure all multi-byte variables remain within a single RAM bank
    Declare Float_Display_Type = Fast               ' Use the faster and more accurate Floating Point to ASCII converter
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                     ' Set the Baud for USART1
    Declare Hserout_Pin PORTC.6                     ' Choose the pin for TX
'
' Setup I2C
'
    Declare SDA_Pin = PORTC.4                       ' Choose the pin to use for the I2C SDA line
    Declare SCL_Pin = PORTC.3                       ' Choose the pin to use for the I2C SCL line
    Declare Slow_Bus = On                           ' Slow down the I2C bus for the BusIn and BusOut commands

$define cMCP34_SlaveAddress %11010000               ' The slave address of the MCP3422 chip
'
' The values below need to be ORed together to make a Config register value
'
' Ready Bit
' This bit is the data ready flag. In read mode, this bit indicates if the output register has been updated with a latest conversion result.
' In One-Shot Conversion mode, writing this bit to "1" initiates a new conversion.
'   Reading RDY bit with the read command:
'       1 =  Output register has not been updated
'       0 =  Output register has been updated with the latest conversion result
'  Writing RDY bit with the write command:
'       Continuous Conversion mode: No effect
'       One-Shot Conversion mode:
'           1 =  Initiate a new conversion
'           0 =  No effect
'
$define cMCP34_Rdy %10000000                        ' RDY: Ready Value to start a conversion
$define cMCP34_RdyBit 7                             ' Ready bit to examine in the program
'
' C1-C0: Channel Selection Bits
'
$define cMCP34_Ch_1 %00000000                       ' Select Channel 1 (Default)
$define cMCP34_Ch_2 %00100000                       ' Select Channel 2
$define cMCP34_Ch_3 %01000000                       ' Select Channel 3 (MCP3424 only, treated as "00" by the MCP3422/MCP3423)
$define cMCP34_Ch_4 %01100000                       ' Select Channel 4 (MCP3424 only, treated as "01" by the MCP3422/MCP3423)
'
' O/C: Conversion Mode Bit
'
$define cMCP34_Mode_Cont %00010000                  ' Continuous Conversion Mode (Default). The device performs data conversions continuously
$define cMCP34_Mode_One  %00000000                  ' One-Shot Conversion Mode. The device performs a single conversion and enters a low power
                                                    ' standby mode until it receives another write or read command
'
' S1-S0: Sample Rate Selection Bit
'
$define cMCP34_Sample_12 %00000000                  ' 240 SPS (12 bits) (Default)
$define cMCP34_Sample_14 %00000100                  ' 60 SPS (14 bits)
$define cMCP34_Sample_16 %00001000                  ' 15 SPS (16 bits)
$define cMCP34_Sample_18 %00001100                  ' 3.75 SPS (18 bits)
'
' G1-G0: PGA Gain Selection Bits
'
$define cMCP34_Gain_1 %00000000                     ' x1 (Default)
$define cMCP34_Gain_2 %00000001                     ' x2
$define cMCP34_Gain_4 %00000010                     ' x4
$define cMCP34_Gain_8 %00000011                     ' x8
'
' Some values for standard one-shot ADC configurations
'
$define cConvert_Ch1_12  cMCP34_Rdy | cMCP34_Ch_1 | cMCP34_Mode_One | cMCP34_Sample_12 | cMCP34_Gain_1  ' Read channel 1 at a gain of 1 in 12-bit mode
$define cConvert_Ch2_12  cMCP34_Rdy | cMCP34_Ch_2 | cMCP34_Mode_One | cMCP34_Sample_12 | cMCP34_Gain_1  ' Read channel 2 at a gain of 1 in 12-bit mode
$define cConvert_Ch1_14  cMCP34_Rdy | cMCP34_Ch_1 | cMCP34_Mode_One | cMCP34_Sample_14 | cMCP34_Gain_1  ' Read channel 1 at a gain of 1 in 14-bit mode
$define cConvert_Ch2_14  cMCP34_Rdy | cMCP34_Ch_2 | cMCP34_Mode_One | cMCP34_Sample_14 | cMCP34_Gain_1  ' Read channel 2 at a gain of 1 in 14-bit mode
$define cConvert_Ch1_16  cMCP34_Rdy | cMCP34_Ch_1 | cMCP34_Mode_One | cMCP34_Sample_16 | cMCP34_Gain_1  ' Read channel 1 at a gain of 1 in 16-bit mode
$define cConvert_Ch2_16  cMCP34_Rdy | cMCP34_Ch_2 | cMCP34_Mode_One | cMCP34_Sample_16 | cMCP34_Gain_1  ' Read channel 2 at a gain of 1 in 16-bit mode
'
' Create variables for the MCP3422 routine
'
    Dim MCP34_wResult As Word                           ' Holds the result of the ADC reading
   
'------------------------------------------------------------------------------
' The main program loop starts here
' Read channel 1 and channel 2 of the MCP3422 ADC, and display the values on a serial terminal
'
Main:
    Do
        MCP34_wResult = MCP3422_Read(1)                 ' Read channel 1 from the MCP3422
        HRSOutLn "Channel 1 = ", SDec MCP34_wResult     ' Transmit the result to the serial terminal

        MCP34_wResult = MCP3422_Read(2)                 ' Read channel 2 from the MCP3422
        HRSOutLn "Channel 2 = ", SDec MCP34_wResult     ' Transmit the result to the serial terminal

        DelayMS 255                                     ' A delay so we can see things happening
    Loop                                                ' Do it forever
   
'------------------------------------------------------------------------------
' Read the MCP3422 ADC chip
' Input     : pChannel holds the ADC channel to read
' Output    : Returns the result of the ADC reading
' Notes     : To change the ADC resolution alter the bConfig value
'           : The procedure will not work for 18-bit resolution
'           : If it is required, use a Dword variable for the result
'
Proc MCP3422_Read(pChannel As Byte), Word
    Dim bConfig As Byte                                 ' Holds the Config for the MCP3422 chip

    If pChannel = 1 Then                                ' Are we reading channel 1?
        bConfig = cConvert_Ch1_16                       ' Yes. So setup for channel 1 at 16-bits
    Else                                                ' Otherwise...
        bConfig = cConvert_Ch2_16                       ' Setup for channel 2 at 16-bits
    EndIf
    BusOut cMCP34_SlaveAddress, [bConfig]               ' Start a conversion
'
' Wait for the conversion to be ready
'
    Do
        BusIn cMCP34_SlaveAddress, [Result.Byte1,_      ' Read the high byte of the ADC value
                                    Result.Byte0,_      ' Read the low byte of the ADC value
                                    bConfig]            ' Read the Config byte
        If bConfig.cMCP34_RdyBit = 0 Then Break         ' Exit the loop when the conversion is complete
    Loop
EndProc

'------------------------------------------------------------------------------------
' Configure the fuses to operate at 64MHz using an external 16MHz crystal.
' For a PIC18F26K22 device
'
Config_Start
    FOSC = HSHP           ' HS oscillator
    PLLCFG = On           ' Oscillator multiplied by 4
    PRICLKEN = On         ' Primary clock enabled
    FCMEN = Off           ' Fail-Safe Clock Monitor disabled
    IESO = Off            ' Internal/External Oscillator Switchover mode disabled
    PWRTEN = On           ' Power up timer enabled
    BOREN = SBORDIS       ' Brown-out Reset enabled in hardware only (SBOREN is disabled)
    BORV = 190            ' Brown Out Reset Voltage set to 1.90 V nominal
    WDTEN = Off           ' Watch dog timer is always disabled. SWDTEN has no effect
    WDTPS = 128           ' Watchdog Timer Postscale 1:128
    CCP2MX = PORTC1       ' CCP2 input/output is multiplexed with RC1
    PBADEN = Off          ' PORTB<5:0> pins are configured as digital I/O on Reset
    CCP3MX = PORTB5       ' P3A/CCP3 input/output is multiplexed with RB5
    HFOFST = On           ' HFINTOSC output and ready status are not delayed by the oscillator stable status
    T3CMX = PORTC0        ' Timer3 Clock Input (T3CKI) is on RC0
    P2BMX = PORTB5        ' ECCP2 B (P2B) is on RB5
    MCLRE = EXTMCLR       ' MCLR pin enabled, RE3 input pin disabled
    STVREN = Off          ' Stack full/underflow will not cause Reset
    LVP = Off             ' Single-Supply ICSP disabled
    XINST = Off           ' Instruction set extension and Indexed Addressing mode disabled
    Debug = Off           ' Disabled
    Cp0 = Off             ' Block 0 (000800-001FFF) not code-protected
    CP1 = Off             ' Block 1 (002000-003FFF) not code-protected
    CP2 = Off             ' Block 2 (004000-005FFF) not code-protected
    CP3 = Off             ' Block 3 (006000-007FFF) not code-protected
    CPB = Off             ' Boot block (000000-0007FF) not code-protected
    CPD = Off             ' Data EEPROM not code-protected
    WRT0 = Off            ' Block 0 (000800-001FFF) not write-protected
    WRT1 = Off            ' Block 1 (002000-003FFF) not write-protected
    WRT2 = Off            ' Block 2 (004000-005FFF) not write-protected
    WRT3 = Off            ' Block 3 (006000-007FFF) not write-protected
    WRTC = Off            ' Configuration registers (300000-3000FF) not write-protected
    WRTB = Off            ' Boot Block (000000-0007FF) not write-protected
    WRTD = Off            ' Data EEPROM not write-protected
    EBTR0 = Off           ' Block 0 (000800-001FFF) not protected from table reads executed in other blocks
    EBTR1 = Off           ' Block 1 (002000-003FFF) not protected from table reads executed in other blocks
    EBTR2 = Off           ' Block 2 (004000-005FFF) not protected from table reads executed in other blocks
    EBTR3 = Off           ' Block 3 (006000-007FFF) not protected from table reads executed in other blocks
    EBTRB = Off           ' Boot Block (000000-0007FF) not protected from table reads executed in other blocks
Config_End