Accessing a 9 bit number from two bytes


Closed Thread
Results 1 to 2 of 2
  1. #1
    Radiance's Avatar
    Radiance Guest

    Default Accessing a 9 bit number from two bytes

    [doh!] This should probably be posted in the General area[/doh!]

    I have a device that outputs a string of data that I read in a pic and have to output a number to an LCD that is in the range of 0-512 (9 bits). The serial stream is read like this:
    <pre>
    serin XpodRX,2,1800,NoRx,oxdata1,oxdata2,spo2
    </pre>
    The problem is, only the first 7 bits of oxdata2 are the number i need. The other two bits are the first two bits of oxdata1. The way I'm doing it now appears extremely ineficient, and takes up a lot of code space, which I need. Can someone maybe give me a pointer on a cleaner way to do this?


    Here's how I calculate the number I need:
    <pre>
    if oxdata1.1 and 1 then
    numout= numout+ 256
    endif

    if oxdata1.0 and 1 then
    numout= numout+ 128
    endif

    if oxdata2.6 and 1 then
    numout= numout+ 64
    endif

    if oxdata2.5 and 1 then
    numout= numout+ 32
    endif

    if oxdata2.4 and 1 then
    numout= numout+ 16
    endif

    if oxdata2.3 and 1 then
    numout= numout+ 8
    endif

    if oxdata2.2 and 1 then
    numout= numout+ 4
    endif

    if oxdata2.1 and 1 then
    numout= numout+ 2
    endif

    if oxdata2.0 and 1 then
    numout= numout+ 1
    endif
    </pre>
    Last edited by Radiance; - 9th June 2005 at 15:37.

  2. #2
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Post

    NumOut = Oxdata2 & %01111111
    NumOut.7 = Oxdata1.0
    NumOut.8 = Oxdata1.1

    ..... is one way of doing it.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. first project, and yes, doesn`t work
    By bogdan in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 28th April 2009, 06:13
  3. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 00:55
  4. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts