Shop OBEX P1 Docs P2 Docs Learn Events
Reading a 24 bit value using I2C — Parallax Forums

Reading a 24 bit value using I2C

doughboydoughboy Posts: 12
edited 2008-05-30 20:11 in BASIC Stamp
Hello,
I'm using a bs2px to read values from a Capacitance to Digital Converter. This CDC has 3 8 bit registers to store its digital results for a total of 24 bits. it is recommended to use an auto address incrementor to read the three successive registers to prevent a Done/Stop bit being sent to the CDC so the next measuremnt is not taken. Currently I'm using three I2COUT commands with changing address's but I know it's going to send a "done" after each of them. is there a way to avoid this? or setup an auto incrementor in a single I2C command? Below is the lines of code I have:
I2CIN SDA,CDCRead,CapDataH,[noparse][[/noparse]STR  CDCData \9 \E]       'Read current Capacitance measurement and store it in CDCData array
I2CIN SDA,CDCRead,CapDataM,[noparse][[/noparse]STR CDCData(8) \9 \E]       'MSB = 0
I2CIN SDA,CDCRead,CapDataL,[noparse][[/noparse]STR CDCData(15) \9 \E]



Thanks for your help

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-30 19:18
    Assuming that the first (lowest numbered) register is CapDataH, you'd do

    I2CIN SDA, CDCRead, CapDataH, [noparse][[/noparse]CDCDataH, CDCDataM, CDCDataL]

    where CDCDataH, CDCDataM, and CDCDataL are defined as byte variables.
    The array is effectively only 3 bytes long and 8 bits are stored in each byte.
    The ACK/NAK bit is set to ACK after the 1st two bytes and NAK after the last
    which is as it should be. Normally most I2C interfaces will increment the
    starting address after each byte is transferred.

    If you really want to use an array, it needs to be defined as a 3 byte array and you'd use

    I2CIN SDA, CDCRead, CapDataH, [noparse][[/noparse]STR CDCData \3]

    Post Edited (Mike Green) : 5/30/2008 7:23:31 PM GMT
  • doughboydoughboy Posts: 12
    edited 2008-05-30 20:02
    Much appreciated. I wish you could do binary to more than 16 bits but oh well!
  • Mike GreenMike Green Posts: 23,101
    edited 2008-05-30 20:11
    Well, you can't do arithmetic directly on 24 or 32 bit quantities, but it's not hard to do it using arrays and loops. Check out www.emesystems.com for lots of good stuff on Stamp programming including multiple precision arithmetic.
Sign In or Register to comment.