Thread: How to read one bit at one time

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    68

    How to read one bit at one time

    let say i have one byte MSB 1010 0101 LSB I want to read one bit at one time

    here is my attempt

    Code:
      unsigned char ReceiveByte(){
    	unsigned char i, data=0;
    	
    	for (i = 0; i < 8; i++) {
    		clock = 1;
            if(Pin1 == 1)
            data |= 0x01;  
            else
            data &= 0xfe; 
    	     
    		data = data << 1;  
    
    
    		clock = 0;
    	}
    	return data;
    }

  2. #2
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    I'm not really sure what you want to do, but maybe the program below will give you an idea (?)

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
        const unsigned char testbyte = 100;
        int i;
        
        for (i = CHAR_BIT - 1; i >= 0; --i) {
            if ((testbyte & 1 << i) != 0)
                printf("1");
            else
                printf("0");
        }
        printf("\n");
        
        return 0;
    }
    Edit: If you want to go from LS bit to MS bit, which it seems you want to, then the following is probably better

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    int main(void)
    {
        const unsigned char testbyte = 100;
        int i;
        
        for (i = 0; i < CHAR_BIT; ++i) {
            if ((testbyte & 1 << i) != 0)
                printf("1");
            else
                printf("0");
        }
        printf("\n");
        
        return 0;
    }
    Last edited by Hodor; 12-16-2019 at 04:47 AM.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    Actually, maybe I see what you mean now. Examine

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    const unsigned char pin1 = 165;
    
    int getPin1(int pos)
    {
        return (pin1 & (1 << pos)) != 0;
    }
    
    int main(void)
    {
        unsigned result = 0;
        int i;
        
        for (i = 0; i < CHAR_BIT; ++i) {
            int b = getPin1(i);        
            result |= b << i;
        }
        printf("result (hex) = 0x%x\n", result);
        
        return 0;
    }
    getPin1() is obviously a made up function to get the bits as you describe, but the rest probably (?) does what you want
    Last edited by Hodor; 12-16-2019 at 05:43 AM. Reason: changed pin1 to be 165 (1010 0101)

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,738
    I have no way to check this

    Code:
    unsigned char ReceiveByte(){
        unsigned char i, data=0;
         
        for (i = 0; i < 8; i++) {
            clock = 1;
            data |= Pin1 << i;
            clock = 0;
        }
        return data;
    }

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Hodor View Post
    I have no way to check this
    I2C implementation on 8051 microcontroller

    Code:
    unsigned char  I2CRead () 
    { 
         unsigned char  i , Data = 0 ; 
         for ( i  = 0 ;  i  < 8 ;  i ++) { 
            SCL  = 1 ; 
             if ( SDA ) 
                 Data |= 1 ; 
             if ( i < 7 ) 
                 Data <<= 1 ; 
            SCL  = 0 ; 
         } 
         return Data ; 
    }
    Does this function read bit one by one
    Last edited by Salem; 12-16-2019 at 11:32 AM. Reason: Removed crayola

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,681
    Yes it does.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2019
    Posts
    68
    Quote Originally Posted by Salem View Post
    Yes it does.
    Can you tell me How it does ?

    My understanding

    Data = 0000 0000 (MSB to LSB)

    when i = 0

    check SDA

    if set then 0000 0000 |= 0000 00001 = 0000 0001

    if (0 <7)

    0000 0001 << 1 = 000 00010

    return data is 0000 0001

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,681
    Code:
             if ( SDA ) 
                 Data |= 1 ;
    This is the 1-bit-at-a-time code.

    Since it goes round the loop 8 times, it collects 8 single bits, and puts them into a byte.

    This is simpler.
    Code:
    unsigned char  I2CRead () 
    { 
         unsigned char  i , Data = 0 ; 
         for ( i  = 0 ;  i  < 8 ;  i ++) { 
            SCL  = 1 ; 
              Data <<= 1 ;
              if ( SDA ) 
                 Data |= 1 ; 
            SCL  = 0 ; 
         } 
         return Data ; 
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how convert uint32_t time to human read form of time?
    By barracuda in forum C Programming
    Replies: 19
    Last Post: 03-21-2015, 12:29 AM
  2. read the server time
    By carlyn in forum C++ Programming
    Replies: 8
    Last Post: 02-26-2008, 06:53 AM
  3. How to read in time format?
    By witchiz brews in forum C Programming
    Replies: 1
    Last Post: 02-12-2006, 02:10 AM
  4. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM
  5. How can I read one number at a time?
    By Bad_Scooter in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2003, 09:58 PM

Tags for this Thread