Announcement

Collapse
No announcement yet.

I2CRead, I2CWrite Code in PIC Basic Pro

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • I2CRead, I2CWrite Code in PIC Basic Pro

    I2CRead, I2CWrite Code in PIC Basic Pro

    I2CRead, I2CWrite Code required to communicate with a Microchip MCP3421 Differential amplifier & 18Bit ADC has anyone ever done this. it works fine using the Mirochip Demo board and VB software it dose
    exactly what I need.I have used PBP for 20 Years and I am not able to learn the C language as is required to use Micrchip software.
    I2CREAD DataPin, ClockPin,%11010000, %11010000, [mcp3421_value.BYTE1,_
    mcp3421_value.BYTE0, mcp3421_config], i2c_error
    Do I have the right Code for TX the data
    i2c_error:
    ' put error indicator here
    Toggle PortA.7

    This dose not work and it should
    '**********************************************************************************
    '* Name : Load_Cell.PBP *
    '* Author : [Lawrence C Windrem] *
    '* Notice : Copyright (c) 2016 [Gaming Essentials Inc.] *
    '* Notice : All Rights Reserved *
    '* Date : 12/27/2016 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '**********************************************************************************
    '*********************** Calibrate PBP to 64MHz clock******************************
    DEFINE OSC 64
    '**************************** Set system clock to 64MHz internal ******************
    OSCTUNE.6 = 1
    OSCCON = %01110000
    '*********************** Set Defines For Serial Port *****************************
    Define ADC_BITS 12 ' Set number of bits in result
    DEFINE ADC_SAMPLEUS 0 ' ADC on single channel doesn't need settling time
    DEFINE ADC_CLOCK 6 ' Fosc/64 for fastest conversion
    DEFINE HSER_CLROERR 1
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_BAUD 115200
    '***********************************************************************************
    ADCON2 = %10111011 ' right justified, 20Tad acqt, Frc (Tad = 4uS)
    ANSELA = %00000110 ' AN1 analog,AN2 anolog
    ANSELB = %00000000
    ANSELC = %00000000
    TRISA = %11111111 'All inputs
    TRISB = %11111111 'All inputs
    TRISC = %11111111 'All inputs
    '**********************************************************************************
    ' READMCP3421: 'INITIALIZE MCP3421 AND TAKE READING


    ADREADS Var Long
    SCRATCH Var Long
    RAWDATA Var Long
    MCP3421 Var Long
    Control Var Long
    Channel Var Long
    SDA1 Var PortC.3
    SDA2 Var PortC.3
    SCL Var PortC.4
    '**********************************************************************************
    'READMCP3421: 'INITIALIZE MCP3421 AND TAKE READING
    '*********************************************************************
    NEWREAD:
    ' UNIT = MCP3421 'SET UNIT IDENTIFIER
    ' CONTROL = %10011100 'CONT.CONVERSION,18 BITS,1V/V
    ' CONTROL = %10011101 'CONT.CONVERSION,18 BITS,2V/V
    CONTROL = %10011110 'CONT.CONVERSION,18 BITS,4V/V
    ' CONTROL = %10011111 'CONT.CONVERSION,18 BITS,8V/V
    'SELECT CASE CHANNEL
    'CASE 0
    I2CWRITE SDA1,SCL,MCP3421,[CONTROL],WERROR 'Did not Write went straight to WERROR
    'CASE 1
    'I2CWRITE SDA2,SCL,MCP3421,[CONTROL],WERROR
    'Hserout ["Start",13,10] 'xx
    'Pause 2000 'xx
    'goto NewREAD 'xx
    'CASE ELSE
    RETURN
    'END SELECT
    REREAD:
    'SELECT CASE CHANNEL
    'CASE 0
    I2CREAD SDA1,SCL,MCP3421,[RAWDATA(3),RAWDATA(2),RAWDATA(1),RAWDATA(0)],RERROR
    'CASE 1
    'I2CREAD SDA2,SCL,MCP3421,[RAWDATA(3),RAWDATA(2),RAWDATA(1),RAWDATA(0)],RERROR
    'END SELECT
    Hserout [RAWDATA ,13,10]
    IF RAWDATA.7 = 1 THEN REREAD
    SCRATCH = RAWDATA(1) >> 1 'LEAST SIGNIFICANT BYTE
    SCRATCH = SCRATCH | (RAWDATA(2) << 7)
    SCRATCH.15 = RAWDATA.0(24) 'COPY MSB
    IF RAWDATA.0(25) = 1 THEN SCRATCH = 0 'IF LESS THAN ZERO
    ADREADS(CHANNEL) = SCRATCH
    RETURN
    WERROR:
    Hserout ["ACK ERROR DURING WRITE",10,13]
    GOTO NEWREAD
    RERROR:
    Hserout ["ACK ERROR DURING READ",10,13]
    GOTO NEWREAD

    ...
    ;--------[Device Configuration]--(See manual section 4.9)---------------------------
    #CONFIG
    CONFIG FOSC = INTIO67 ; Internal oscillator block
    CONFIG PLLCFG = ON ;Oscillator multiplied by 4
    CONFIG PRICLKEN = OFF ; Primary clock can be disabled by software
    CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor disabled
    CONFIG IESO = OFF ; Oscillator Switchover mode disabled
    CONFIG PWRTEN = OFF ; Power up timer disabled
    CONFIG BOREN = ON ;Brown-out Reset enabled and controlled by software (SBOREN is enabled)
    CONFIG BORV = 285 ;VBOR set to 2.85 V nominal
    CONFIG WDTEN = ON ; WDT is always enabled. SWDTEN bit has no effect
    CONFIG WDTPS = 32768 ; 1:32768
    CONFIG CCP2MX = PORTC1 ; CCP2 input/output is multiplexed with RC1
    CONFIG PBADEN = OFF ; PORTB<5:0> pins are configured as digital I/O on Reset
    CONFIG CCP3MX = PORTB5 ; P3A/CCP3 input/output is multiplexed with RB5
    CONFIG HFOFST = ON ; HFINTOSC output and ready status are not delayed by the oscillator stable status
    CONFIG T3CMX = PORTC0 ; T3CKI is on RC0
    ;CONFIG P2BMX = PORTB5 ; P2B is on RB5
    CONFIG MCLRE = EXTMCLR ; MCLR pin enabled, RE3 input pin disabled
    CONFIG STVREN = ON ; Stack full/underflow will cause Reset
    CONFIG LVP = OFF ; Single-Supply ICSP enabled
    CONFIG XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    CONFIG DEBUG = OFF ; Disabled
    CONFIG CP0 = ON ; Block 0 (000800-001FFFh) code-protected
    CONFIG CP1 = ON ; Block 1 (002000-003FFFh) code-protected
    CONFIG CP2 = ON ; Block 2 (004000-005FFFh) code-protected
    CONFIG CP3 = ON ; Block 3 (006000-007FFFh) code-protected
    CONFIG CPB = ON ; Boot block (000000-0007FFh) not code-protected
    CONFIG CPD = OFF ; Data EEPROM not code-protected
    CONFIG WRT0 = OFF ; Block 0 (000800-001FFFh) not write-protected
    CONFIG WRT1 = OFF ; Block 1 (002000-003FFFh) not write-protected
    CONFIG WRT2 = OFF ; Block 2 (004000-005FFFh) not write-protected
    CONFIG WRT3 = OFF ; Block 3 (006000-007FFFh) not write-protected
    CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
    CONFIG WRTB = OFF ; Boot Block (000000-0007FFh) not write-protected
    CONFIG WRTD = OFF ; Data EEPROM not write-protected
    CONFIG EBTR0 = OFF ; Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
    CONFIG EBTR1 = OFF ; Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
    CONFIG EBTR2 = OFF ; Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
    CONFIG EBTR3 = OFF ; Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
    CONFIG EBTRB = OFF ; Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
    #ENDCONFIG

    I did not know how to handle the select case
    I did not know how to handle *UNIT = MCP3412
    I wonder because the units can have a specific address
    I am using a Microchip demo board for testing is it possible they have made the unit on the Demo Board unique
    Last edited by Lawrence; 2 weeks ago. Reason: I am still not successful

  • #2
    a quick read of the data sheet looks like you need

    1. Set up the correct mode
    I2CWRITE DataPin, ClockPin,%11010000, [$88] ; SET 16 BIT MODE


    then read data like this
    I2CREAD DataPin, ClockPin,%11010000, [mcp3421_value.BYTE1,mcp3421_value.BYTE0, mcp3421_config], i2c_error

    Comment


    • #3
      Perhaps try something like this....

      '*********************************************************************
      READMCP3421: 'INITIALIZE MCP3421 AND TAKE READING
      '*********************************************************************
      NEWREAD:
      UNIT = MCP3421 'SET UNIT IDENTIFIER
      ' CONTROL = %10011100 'CONT.CONVERSION,18 BITS,1V/V
      ' CONTROL = %10011101 'CONT.CONVERSION,18 BITS,2V/V
      CONTROL = %10011110 'CONT.CONVERSION,18 BITS,4V/V
      ' CONTROL = %10011111 'CONT.CONVERSION,18 BITS,8V/V
      SELECT CASE CHANNEL
      CASE 0
      I2CWRITE SDA1,SCL,UNIT,[CONTROL],WERROR
      CASE 1
      I2CWRITE SDA2,SCL,UNIT,[CONTROL],WERROR
      CASE ELSE
      RETURN
      END SELECT
      REREAD:
      SELECT CASE CHANNEL
      CASE 0
      I2CREAD SDA1,SCL,UNIT,[RAWDATA(3),RAWDATA(2),RAWDATA(1),RAWDATA(0)],RERROR
      CASE 1
      I2CREAD SDA2,SCL,UNIT,[RAWDATA(3),RAWDATA(2),RAWDATA(1),RAWDATA(0)],RERROR
      END SELECT
      IF RAWDATA.7 = 1 THEN REREAD
      SCRATCH = RAWDATA(1) >> 1 'LEAST SIGNIFICANT BYTE
      SCRATCH = SCRATCH | (RAWDATA(2) << 7)
      SCRATCH.15 = RAWDATA.0(24) 'COPY MSB
      IF RAWDATA.0(25) = 1 THEN SCRATCH = 0 'IF LESS THAN ZERO
      ADREADS(CHANNEL) = SCRATCH
      RETURN
      WERROR:
      DEBUG "ACK ERROR DURING WRITE",10,13
      GOTO NEWREAD
      RERROR:
      DEBUG "ACK ERROR DURING READ",10,13
      GOTO NEWREAD

      It works for me...
      Dave Purola,
      N8NTA
      EN82fn

      Comment

      Working...
      X