Reading Individual bits from 2 bytes

This is a discussion on Reading Individual bits from 2 bytes within the C Programming forums, part of the General Programming Boards category; Hi all, I'm writing a piece of embedded software and I'm kinda stuck and wondered if anyone can point me ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    Reading Individual bits from 2 bytes

    Hi all,

    I'm writing a piece of embedded software and I'm kinda stuck and wondered if anyone can point me in the right direction. I've been thinking for hours and hope that new eyes could see what tired eyes can't.

    I'm reading in characters from the USB which is set up as a virtual COM port. I need the characters to represent one of 16 outputs from a serial-in-parallel-out shift register.

    Anyway, I'm hardcoding the values I need into an array and then when the character comes in I subtract an offset and set ASCII '0' to element 0 of the array and then read a 16 bit word.

    Code:
    static unsigned int ASCII_Offsets[73] = {0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000, 0x0000,0x0000,
    0x0000,0x0000,0x0000, 0x0000,0x0000,0x0000, 0x0000,0x1000,0x00F1,0x00810x0001,0x0030,0xFF00,0xFFFF,
    0x0002,0xB482,0x0010,0x1800,0x7F00,0x8F00,0x0004,0x0011,
    0x0080,0xA800,0x0100,0x0800,0x0008,0x1C00,0x00F0,
    0x0020,0xE080,0xC080,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x1000,0x00F1,0x0081,0x0001,0x0030,0xFF00,0xFFFF,
    0x0002,0xB482,0x0010,0x1800,0x7F00,0x8F00,0x0004,0x0011,
    0x0080,0xA800,0x0100,0x0800,0x0008,0x1C00,0x00F0,
    0x0020,0xE080,0xC080};  
    // Don't worry about the numbers in the array and, sorry, I can see they are a little bit mucked up with the 
    formatting. Also the 0x0000 terms are just ones that I haven't computed yet.
    
    difference = ledval - 48;	//provides an offset and set ASCII '0' to element 0 of array.
    
    motors = ASCII_Values[difference];	//gets the values for the required motors that need to be switched on for 
    specific characters.


    So far so good. I have the 16 bit hex number in the variable 'motors' but this is where I'm stumped.

    I need to read each of the bits, of motors, individually so I can output them on a port pin.

    Can anyone point me in the right direction please?

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    This code gets each bit starting with the LSB.
    Code:
    int value = /* 16 bit value */
    int mask = 1, i;
    
    for(i = 0; i < 16; i++)
    {
       if(value & mask)
          /* bit is set */
       else
          /* bit is not set */
       value = value >> 1;
    }

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Of course, a mask then shft right. How logical.

    Cheers bithub.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-28-2008, 02:07 AM
  2. Shared class members over Dll Boundary
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 11-13-2007, 12:43 PM
  3. Rendering 32-bit images - alpha is ignored
    By ulillillia in forum Windows Programming
    Replies: 59
    Last Post: 04-21-2007, 03:50 PM
  4. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  5. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21