Shop OBEX P1 Docs P2 Docs Learn Events
I2c code explanation — Parallax Forums

I2c code explanation

ArchiverArchiver Posts: 46,084
edited 2002-02-27 18:39 in General Discussion
What you're seeing has to do with a BS2p syntax requirement that expects an
byte (usually an internal address) before reading data from an I2C device. I
simply re-send the control byte in this position. You're right, the device
doesn't need it, but tolerates the retransmission.

-- Jon Williams
-- Applications Engineer, Parallax


In a message dated 2/27/02 9:45:48 AM Central Standard Time,
ricky@m... writes:


> Ok its slowly coming together. I finally got the code working for my Max128
> A/D. Some things just are not very clear to me and if someone could
> explain something to me I would greatly appreciate it. The code below came
> off the parallax site for the PCF8591 A/D. In the I2Cout command below,
> the last part of the command is a byte sized variable called aout. What is
> the purpose of this byte? After giving out the D2a_Enable command, = to
> %01000000, is this not all the stamp needs to send to the A/D for it to
> begin working? And what would the value of aOut be at this point in the
> command?
>




[noparse][[/noparse]Non-text portions of this message have been removed]

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-02-27 17:48
    Ok its slowly coming together. I finally got the code working for my Max128 A/D.
    Some things just are not very clear to me and if someone could explain
    something to me I would greatly appreciate it. The code below came off the
    parallax site for the PCF8591 A/D. In the I2Cout command below, the last part
    of the command is a byte sized variable called aout. What is the purpose of
    this byte? After giving out the D2a_Enable command, = to %01000000, is this not
    all the stamp needs to send to the A/D for it to begin working? And what would
    the value of aOut be at this point in the command?

    I2Cpin CON 8 ' SDA on 8; SCL on 9

    '
    [noparse][[/noparse] Constants ]

    DevType CON %1001 << 4 ' device type

    DevAddr CON %000 << 1 ' address = %000 -> %111

    Wr8591 CON DevType | DevAddr ' write to PCF8591

    Rd8591 CON Wr8591 | 1 ' read from PCF8591

    D2A_Enable CON %01000000 ' enable analog output

    Auto_Inc CON %00000100 ' auto inc a2d channels

    MVPB CON $139C ' millivolts per bit factor



    '
    [noparse][[/noparse] Variables ]

    aOut VAR Byte ' analog out value

    aIn VAR Byte(4) ' analog input channels

    mVolts VAR Word ' convert to millivolts

    dummy VAR mVolts.LowByte

    chan VAR Nib ' channel

    '
    [noparse][[/noparse] Initialization ]

    'Initialize:

    DEBUG CLS ' call DEBUG window

    PAUSE 250 ' let it open

    Main:

    DEBUG Home, "PCF8591 Demo"

    Set_D2A:

    DEBUG Home, CR, CR, "D2A Out........ ", DEC aOut, " ", CR

    I2COUT I2Cpin, Wr8591, D2A_Enable, [noparse][[/noparse]aOut]

    Get_A2D:

    I2CIN I2Cpin, Rd8591, (D2A_Enable | Auto_Inc), [noparse][[/noparse]dummy, STR aIn\4]

    FOR chan = 0 TO 3

    DEBUG "Channel ", DEC1 chan, " In... ", DEC aIn(chan), " ", Tab

    mVolts = aIn(chan) */ MVPB

    DEBUG "(", DEC mVolts DIG 3, ".", DEC3 mVolts, " volts)", CR

    NEXT

    PAUSE 500 ' delay between updates

    aOut = aOut + 1 ' increment analog output

    GOTO Set_D2A ' go again

    END



    [noparse][[/noparse]Non-text portions of this message have been removed]
  • ArchiverArchiver Posts: 46,084
    edited 2002-02-27 18:39
    Thanks Jon!

    Original Message
    From: <jonwms@a...>
    To: <basicstamps@yahoogroups.com>
    Sent: Wednesday, February 27, 2002 8:17 AM
    Subject: Re: [noparse][[/noparse]basicstamps] I2c code explanation


    > What you're seeing has to do with a BS2p syntax requirement that expects
    an
    > byte (usually an internal address) before reading data from an I2C device.
    I
    > simply re-send the control byte in this position. You're right, the
    device
    > doesn't need it, but tolerates the retransmission.
    >
    > -- Jon Williams
    > -- Applications Engineer, Parallax
    >
    >
    > In a message dated 2/27/02 9:45:48 AM Central Standard Time,
    > ricky@m... writes:
    >
    >
    > > Ok its slowly coming together. I finally got the code working for my
    Max128
    > > A/D. Some things just are not very clear to me and if someone could
    > > explain something to me I would greatly appreciate it. The code below
    came
    > > off the parallax site for the PCF8591 A/D. In the I2Cout command below,
    > > the last part of the command is a byte sized variable called aout. What
    is
    > > the purpose of this byte? After giving out the D2a_Enable command, = to
    > > %01000000, is this not all the stamp needs to send to the A/D for it to
    > > begin working? And what would the value of aOut be at this point in the
    > > command?
    > >
    >
    >
    >
    >
    > [noparse][[/noparse]Non-text portions of this message have been removed]
    >
    >
    > To UNSUBSCRIBE, just send mail to:
    > basicstamps-unsubscribe@yahoogroups.com
    > from the same email address that you subscribed. Text in the Subject and
    Body of the message will be ignored.
    >
    >
    > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
    >
    >
    >
    >
Sign In or Register to comment.