This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Construct a byte from bits

Hello everybody,

I am very new to C language. In my project I get data serially from a port pin. I want to save the first 8 bits to one variable, another to second & so on. In all I want to save 32 bits in 4 bytes. Can you suggest C code.

I know it in assembly by using RRC or RLC, but how to achieve it in C?

Thanks

Parents
  • It shouldn't be too complicated. Something is the lines of:

    BOOL data_full;
    BYTE bits_received;
    insigned int data_received;
    
    // initialise global variables;
    data_full = __FALSE;
    bits_received = 0;
    data_received = 0;
    
    
    interrupt_handler()
    {
    
    
       if(DATA)
            {
            data_received =| ( 1 << bits_received );
            }
       bits_received++;
    
       if(bits_received == 32)
          {
            bits_received = 0;
            data_full = __TRUE;
            // do something with data_received.
          }
    
    }
    

Reply
  • It shouldn't be too complicated. Something is the lines of:

    BOOL data_full;
    BYTE bits_received;
    insigned int data_received;
    
    // initialise global variables;
    data_full = __FALSE;
    bits_received = 0;
    data_received = 0;
    
    
    interrupt_handler()
    {
    
    
       if(DATA)
            {
            data_received =| ( 1 << bits_received );
            }
       bits_received++;
    
       if(bits_received == 32)
          {
            bits_received = 0;
            data_full = __TRUE;
            // do something with data_received.
          }
    
    }
    

Children