Shop OBEX P1 Docs P2 Docs Learn Events
Converting 3 bit ASCII to a decimal (Byte) number — Parallax Forums

Converting 3 bit ASCII to a decimal (Byte) number

ArchiverArchiver Posts: 46,084
edited 2004-02-24 01:21 in General Discussion
Hi All:

Well I have managed several projects using the BS2P with great success but
now I am stumped (Doesn't take too mich) . My latest project is using the
stamp to communicate with another microprocesser which runs 20 different
DS1820 temp sensors. The microprosser reports to the stamp via a 9600 baud
(direct connrct TTL) to a PIC buffer which handles flow control to the stamp
(because of the lack of interrupts). The data received from the buffer is a
fixed 3 byter ASCII for each sensor and is not changeable by me. For a
temperature reading say of 87 I will get in ASCII a space then a 8 then a 7
or for 160 I will get a 1 then a 6 then a 0.

I store these readings in SPRAM. At the completion of all sensor data I send
the 3 bytes in ASCII to another PC for display. My need however is for the
BS2P to also convert these 3 bytes in SPRAM to a single decimal (byte)
number. This decimal number will then be used to determine if a (high temp
alarm or Low temp alarm) exists on that sensor. So my question to all is
how do I convert a 3 byte charactor stored in SPRAM to a 1 byte decimal
number. Any help would be appreciated.

Thanks All

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2004-02-23 21:14
    AsciiToDecimal:

    TempStr VAR BYTE(3)
    MyWord VAR WORD

    MAIN:
    TempStr = " 16"
    GOSUB AsciiToDecimal
    debug [noparse][[/noparse]DEC MyWord, 13]
    PAUSE 500
    GOTO MAIN

    AsciiToDecimal: MyWord = 0
    FOR I = 0 TO 2
    IF TempStr(I) = 32 THEN NextFor
    MyWord = MyWord * 10 + (TempStr(I) - "0")
    NextFor:
    NEXT
    RETURN

    ' Note: the result of doing ' TempStr(I) - "0" '
    ' is to convert the ASCII value in TempStr(I) into
    ' a decimal number value for that character.
    '

    --- In basicstamps@yahoogroups.com, "Eric Adams" <hugs102@b...> wrote:
    > Hi All:
    >
    > Well I have managed several projects using the BS2P with great
    success but
    > now I am stumped (Doesn't take too mich) . My latest project is
    using the
    > stamp to communicate with another microprocesser which runs 20
    different
    > DS1820 temp sensors. The microprosser reports to the stamp via a
    9600 baud
    > (direct connrct TTL) to a PIC buffer which handles flow control to
    the stamp
    > (because of the lack of interrupts). The data received from the
    buffer is a
    > fixed 3 byter ASCII for each sensor and is not changeable by me.
    For a
    > temperature reading say of 87 I will get in ASCII a space then a 8
    then a 7
    > or for 160 I will get a 1 then a 6 then a 0.
    >
    > I store these readings in SPRAM. At the completion of all sensor
    data I send
    > the 3 bytes in ASCII to another PC for display. My need however is
    for the
    > BS2P to also convert these 3 bytes in SPRAM to a single decimal
    (byte)
    > number. This decimal number will then be used to determine if a
    (high temp
    > alarm or Low temp alarm) exists on that sensor. So my question
    to all is
    > how do I convert a 3 byte charactor stored in SPRAM to a 1 byte
    decimal
    > number. Any help would be appreciated.
    >
    > Thanks All
  • ArchiverArchiver Posts: 46,084
    edited 2004-02-23 21:25
    Not tough to solve at all. Try this demo, then plug the code (as
    required) into your program.

    ' {$STAMP BS2p}
    ' {$PBASIC 2.5}

    sensor VAR Byte(3)
    idx VAR Nib
    digit VAR Nib
    spAddr VAR Byte
    spChar VAR Byte

    Setup:
    PUT 0, " 87" ' ASCII demo data
    PUT 3, "120"
    PUT 6, "255"

    Main:
    FOR idx = 0 TO 2
    GOSUB Convert_Str
    DEBUG "Sensor ", DEC idx, " : ",
    DEC sensor(idx), CR
    NEXT
    END

    Convert_Str:
    spAddr = 3 * idx
    sensor(idx) = 0
    FOR digit = 0 TO 2
    sensor(idx) = sensor(idx) * 10
    GET (spAddr + digit), spChar
    IF (spChar <> " ") THEN
    sensor(idx) = sensor(idx) + (spChar - "0")
    ENDIF
    NEXT
    RETURN


    -- Jon Williams
    -- Applications Engineer, Parallax
    -- Dallas Office
    -- (972) 659-9090


    Original Message
    From: Eric Adams [noparse]/noparse]mailto:[url=http://forums.parallaxinc.com/group/basicstamps/post?postID=4h1fSbi7SKRMHBUTegIUXlINRJNqN6ZqQ5SZl79K45bbOqKjgcjd-ENzpMFfoDcoBNa1Qa65t5BKFCoQKzb8]hugs102@b...[/url
    Sent: Monday, February 23, 2004 2:01 PM
    To: basicstamps@yahoogroups.com
    Subject: [noparse][[/noparse]basicstamps] Converting 3 bit ASCII to a decimal (Byte) number


    Hi All:

    Well I have managed several projects using the BS2P with great success
    but now I am stumped (Doesn't take too mich) . My latest project is
    using the stamp to communicate with another microprocesser which runs 20
    different DS1820 temp sensors. The microprosser reports to the stamp
    via a 9600 baud (direct connrct TTL) to a PIC buffer which handles flow
    control to the stamp (because of the lack of interrupts). The data
    received from the buffer is a fixed 3 byter ASCII for each sensor and is
    not changeable by me. For a temperature reading say of 87 I will get in
    ASCII a space then a 8 then a 7 or for 160 I will get a 1 then a 6 then
    a 0.

    I store these readings in SPRAM. At the completion of all sensor data I
    send the 3 bytes in ASCII to another PC for display. My need however is
    for the BS2P to also convert these 3 bytes in SPRAM to a single decimal
    (byte) number. This decimal number will then be used to determine if a
    (high temp alarm or Low temp alarm) exists on that sensor. So my
    question to all is how do I convert a 3 byte charactor stored in SPRAM
    to a 1 byte decimal number. Any help would be appreciated.

    Thanks All
  • ArchiverArchiver Posts: 46,084
    edited 2004-02-24 01:21
    Thanks so much Allen,Jon and Steve. I thank you so much for your help.
    This group is great when some of us suffer temporary brain death.

    Thanks Again
    Eric

    Original Message
    From: "Eric Adams" <hugs102@b...>
    To: <basicstamps@yahoogroups.com>
    Sent: Monday, February 23, 2004 3:01 PM
    Subject: [noparse][[/noparse]basicstamps] Converting 3 bit ASCII to a decimal (Byte) number


    > Hi All:
    >
    > Well I have managed several projects using the BS2P with great success but
    > now I am stumped (Doesn't take too mich) . My latest project is using
    the
    > stamp to communicate with another microprocesser which runs 20 different
    > DS1820 temp sensors. The microprosser reports to the stamp via a 9600
    baud
    > (direct connrct TTL) to a PIC buffer which handles flow control to the
    stamp
    > (because of the lack of interrupts). The data received from the buffer is
    a
    > fixed 3 byter ASCII for each sensor and is not changeable by me. For a
    > temperature reading say of 87 I will get in ASCII a space then a 8 then a
    7
    > or for 160 I will get a 1 then a 6 then a 0.
    >
    > I store these readings in SPRAM. At the completion of all sensor data I
    send
    > the 3 bytes in ASCII to another PC for display. My need however is for
    the
    > BS2P to also convert these 3 bytes in SPRAM to a single decimal (byte)
    > number. This decimal number will then be used to determine if a (high
    temp
    > alarm or Low temp alarm) exists on that sensor. So my question to all
    is
    > how do I convert a 3 byte charactor stored in SPRAM to a 1 byte decimal
    > number. Any help would be appreciated.
    >
    > Thanks All
    >
    >
    >
    >
    > 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.
    >
    > Yahoo! Groups Links
    >
    >
    >
    >
    >
    >
Sign In or Register to comment.