Shop OBEX P1 Docs P2 Docs Learn Events
Coding question — Parallax Forums

Coding question

ArchiverArchiver Posts: 46,084
edited 2002-05-08 06:18 in General Discussion
Can someone translate into plain english what this code snippit is doing, in
particular the "initbits" line. The sub is setting the mode and channel number
in a 8 channel A/D. I know the sub is going through all the channels, my
question is "what" is it doing to initbits??

Thanks.

(P.S. It owrks fine, I just don't understand the line)

Mike B.



for Channel = 0 to 7

InitBits = %11000 | ((Channel & %001) << 2) | ((Channel & %110) >> 1)

gosub GetADCResult

debug "Channel ",DEC Channel, ": ",DEC ADCResult,CR



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

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2002-05-08 03:57
    This forms a bit pattern that looks like this:

    11ACB

    Where the channel number (in binary) looks like CBA.

    So if CBA = 101 then Initbits=11110
    If CBA=011 then Initbits = 11101

    The & %001 strips the least significant bit
    The & %110 isolates the two bits to the left
    The <<2 shifts the first bit 2 places over to the left
    The >>1 shifts the other two bits one place to the right
    The | (or) operator glues it all together.

    Al Williams
    AWC
    * Floating point A/D
    http://www.al-williams.com/awce/pak9.htm

    >
    Original Message
    > From: Mike Blier [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=a4TTET3_dTo0wJ1EDRfNvUsI_C5GtFkK9lzCmIAHHdW4cyiJvPP1jai0YEf6-RY1-fr7r0dVOTOr]w6ffc@p...[/url
    > Sent: Tuesday, May 07, 2002 9:31 PM
    > To: Basicstamps@Yahoogroups.Com
    > Subject: [noparse][[/noparse]basicstamps] Coding question
    >
    >
    > Can someone translate into plain english what this code
    > snippit is doing, in particular the "initbits" line. The sub
    > is setting the mode and channel number in a 8 channel A/D. I
    > know the sub is going through all the channels, my question
    > is "what" is it doing to initbits??
    >
    > Thanks.
    >
    > (P.S. It owrks fine, I just don't understand the line)
    >
    > Mike B.
    >
    >
    >
    > for Channel = 0 to 7
    >
    > InitBits = %11000 | ((Channel & %001) << 2) | ((Channel & %110) >> 1)
    >
    > gosub GetADCResult
    >
    > debug "Channel ",DEC Channel, ": ",DEC ADCResult,CR
    >
    >
    >
    > [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/
    >
  • ArchiverArchiver Posts: 46,084
    edited 2002-05-08 05:16
    Ok thanks Al... That's clear as mud.. <g> Seems like a round about way to
    cycle through all the 8 combos.

    Looking forward to getting your RS232 adaptor I ordered the other day..

    MIke B.

    Original Message
    From: "Al Williams" <alw@a...>
    To: <basicstamps@yahoogroups.com>
    Sent: Tuesday, May 07, 2002 7:57 PM
    Subject: RE: [noparse][[/noparse]basicstamps] Coding question


    >
    > This forms a bit pattern that looks like this:
    >
    > 11ACB
    >
    > Where the channel number (in binary) looks like CBA.
    >
    > So if CBA = 101 then Initbits=11110
    > If CBA=011 then Initbits = 11101
    >
    > The & %001 strips the least significant bit
    > The & %110 isolates the two bits to the left
    > The <<2 shifts the first bit 2 places over to the left
    > The >>1 shifts the other two bits one place to the right
    > The | (or) operator glues it all together.
    >
    > Al Williams
    > AWC
    > * Floating point A/D
    > http://www.al-williams.com/awce/pak9.htm
    >
    > >
    Original Message
    > > From: Mike Blier [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=KzanpCiydEceCRwZxl_dFz-9pnx64h4cGcZhllyBBOwXe83BM6akUWq6CFR3rsCjbFS-lBIFgJod-Ic]w6ffc@p...[/url
    > > Sent: Tuesday, May 07, 2002 9:31 PM
    > > To: Basicstamps@Yahoogroups.Com
    > > Subject: [noparse][[/noparse]basicstamps] Coding question
    > >
    > >
    > > Can someone translate into plain english what this code
    > > snippit is doing, in particular the "initbits" line. The sub
    > > is setting the mode and channel number in a 8 channel A/D. I
    > > know the sub is going through all the channels, my question
    > > is "what" is it doing to initbits??
    > >
    > > Thanks.
    > >
    > > (P.S. It owrks fine, I just don't understand the line)
    > >
    > > Mike B.
    > >
    > >
    > >
    > > for Channel = 0 to 7
    > >
    > > InitBits = %11000 | ((Channel & %001) << 2) | ((Channel & %110) >> 1)
    > >
    > > gosub GetADCResult
    > >
    > > debug "Channel ",DEC Channel, ": ",DEC ADCResult,CR
    > >
    > >
    > >
    > > [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/
    > >
    >
    >
    > 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/
    >
    >
  • ArchiverArchiver Posts: 46,084
    edited 2002-05-08 06:18
    The binary channel numbering scheme for a National ADC0838 A/D
    converter is: 000, 100, 001, 101, 010, 110, 011, 111 for channels 0
    to 7 respectively. The code will generate these bit patterns
    (preceded by a binary 11) and step through all channels in ascending
    sequence.

    Regards, Marylou
    _______________________________________

    --- In basicstamps@y..., Mike Blier <w6ffc@p...> wrote:
    > Ok thanks Al... That's clear as mud.. <g> Seems like a round
    about way to
    > cycle through all the 8 combos.
    >
    > Looking forward to getting your RS232 adaptor I ordered the other
    day..
    >
    > MIke B.
    >
    >
    Original Message
    > From: "Al Williams" <alw@a...>
    > To: <basicstamps@y...>
    > Sent: Tuesday, May 07, 2002 7:57 PM
    > Subject: RE: [noparse][[/noparse]basicstamps] Coding question
    >
    >
    > >
    > > This forms a bit pattern that looks like this:
    > >
    > > 11ACB
    > >
    > > Where the channel number (in binary) looks like CBA.
    > >
    > > So if CBA = 101 then Initbits=11110
    > > If CBA=011 then Initbits = 11101
    > >
    > > The & %001 strips the least significant bit
    > > The & %110 isolates the two bits to the left
    > > The <<2 shifts the first bit 2 places over to the left
    > > The >>1 shifts the other two bits one place to the right
    > > The | (or) operator glues it all together.
    > >
    > > Al Williams
    > > AWC
    > > * Floating point A/D
    > > http://www.al-williams.com/awce/pak9.htm
    > >
    > > >
    Original Message
    > > > From: Mike Blier [noparse][[/noparse]mailto:w6ffc@p...]
    > > > Sent: Tuesday, May 07, 2002 9:31 PM
    > > > To: Basicstamps@Y...
    > > > Subject: [noparse][[/noparse]basicstamps] Coding question
    > > >
    > > >
    > > > Can someone translate into plain english what this code
    > > > snippit is doing, in particular the "initbits" line. The sub
    > > > is setting the mode and channel number in a 8 channel A/D. I
    > > > know the sub is going through all the channels, my question
    > > > is "what" is it doing to initbits??
    > > >
    > > > Thanks.
    > > >
    > > > (P.S. It owrks fine, I just don't understand the line)
    > > >
    > > > Mike B.
    > > >
    > > >
    > > >
    > > > for Channel = 0 to 7
    > > >
    > > > InitBits = %11000 | ((Channel & %001) << 2) | ((Channel & %110)
    >> 1)
    > > >
    > > > gosub GetADCResult
    > > >
    > > > debug "Channel ",DEC Channel, ": ",DEC ADCResult,CR
    > > >
    > > >
    > > >
    > > > [noparse][[/noparse]Non-text portions of this message have been removed]
    > > >
    > > >
    > > > To UNSUBSCRIBE, just send mail to:
    > > > basicstamps-unsubscribe@y...
    > > > 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/
    > > >
    > >
    > >
    > > To UNSUBSCRIBE, just send mail to:
    > > basicstamps-unsubscribe@y...
    > > 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.