Announcement

Collapse
No announcement yet.

A/D reading via I2C bus

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

  • A/D reading via I2C bus

    Hi,
    I'm using 35DT . I'm trying to read the data off a 12 bit I2C A/D device MCP3421 from Microchip. I have attached the timing diagram from the datasheet.
    Click image for larger version

Name:	timingMCP3421.png
Views:	71
Size:	64.0 KB
ID:	46126

    I'm reading 0x7f for both the MSB and the LSB from the device.
    Here is code

    // 3421_ADDR_READ 0xd3
    // 3421_ADDR_WRITE 0xd2
    func read3421Raw()
    var upperByte;
    var lowerByte;
    var valueRead:=0;

    I2C1_Idle();
    I2C1_Start();

    I2C1_Write(3421_ADDR_READ);

    upperByte:= I2C1_Read();
    gfx_MoveTo(100,210) ;
    print([HEX2]upperByte);

    I2C1_Ack();

    lowerByte:= I2C1_Read();

    I2C1_Nack();
    I2C1_Stop();

    gfx_MoveTo(100,250) ;
    print([HEX2]lowerByte);


    valueRead:=upperByte <<8;
    valueRead |= lowerByte;
    return valueRead;
    endfunc


    Any ideas as to what I'm doing incorrectly?

    regards
    --fatbrain


    Attached Files

  • #2
    It appears your using address 1. Was your part ordered with address 1 or 0?

    11010011 (D3) is read addr 1 11010001 (D1) is read address 0

    Is part number A0 or A1? They come in A0 - A7. Just a thought
    Last edited by ccsial; 26 June 2015, 06:32 AM.

    Comment


    • fatbrain007
      fatbrain007 commented
      Editing a comment
      Nice.
      I had ordered a few from element14 will cross check.

      thanks

    • fatbrain007
      fatbrain007 commented
      Editing a comment
      Cross checked the part number is MCP 3421A1 hence the address is 0b1101001X where x is the R/W bit

  • #3
    If the above is not the issue can you show enough of your code so that we can see the entire I2C sequence, i.e. including the pin setup
    Mark

    Comment


    • #4
      Hi Mark,
      Here is code snippet



      #constant I2C_CLK $PA13
      #constant I2C_DAT $PA12
      #const 3421_ADDR_READ 0xd3 //0b11010011 , as per datasheet
      #const 3421_ADDR_WRITE 0xd2 //0b11010010 , as per datasheet

      func InitPorts()

      pin_Set(PIN_ANAVG,PA0);
      pin_Set(PIN_ANAVG,PA1);
      pin_Set(PIN_ANAVG,PA2);
      pin_Set(PIN_ANAVG,PA3);

      I2C1_Open(I2C_SLOW,I2C_CLK,I2C_DAT);


      repeat
      pause(10);
      read3421Raw();
      forever

      endfunc

      func read3421Raw()
      var upperByte;
      var lowerByte;
      var valueRead:=0;
      I2C1_Idle();
      I2C1_Start();
      I2C1_Write(0xd3);
      I2C1_AckStatus();
      upperByte:= I2C1_Read();
      gfx_MoveTo(100,210) ;
      print([HEX2]upperByte);

      I2C1_Ack();

      lowerByte:= I2C1_Read();
      I2C1_Nack();
      I2C1_Stop();

      gfx_MoveTo(100,250) ;
      print([HEX2]lowerByte);

      valueRead:=upperByte <<8;
      valueRead |= lowerByte;
      return valueRead;

      endfunc

      Comment


      • #5
        Have you got I2C pullups on the pins?

        Have you checked the correct device yet?

        I don't think you should need the AckStatus.
        Mark

        Comment


        • fatbrain007
          fatbrain007 commented
          Editing a comment
          I already have an I/O port extender MCP 23017 and i can successfully write to it, so it rules out hardware connectivity issues.
          I will try without AckStatus and cross check the partnumber and sees how it goes.
      Working...
      X