Connect with us

function to send data

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Sep 9, 2017.

  1. vead

    vead

    473
    14
    Nov 27, 2011
    Hello
    I need help to understand this function. This function is use to send 8 bit serial data (8051 MCU, embedded c, I2C)
    Code:
    unsigned char I2CSend(unsigned char Data)
    {
         unsigned char i, ack_bit;
         for (i = 0; i < 8; i++) {
            if ((Data & 0x80) == 0)
                SDA = 0;
            else
                SDA = 1;
            SCL = 1;
             SCL = 0;
            Data<<=1;
         }
         SDA = 1;
         SCL = 1;
         ack_bit = SDA;
         SCL = 0;
         return ack_bit;
    }
    what is meaning of this line if ((Data & 0x80) == 0) ?
     
  2. Rixen

    Rixen

    98
    23
    Feb 16, 2016
    [​IMG]

    If ((the result from the first paranthesis) is 0))
    {
    Do stuff
    }
     
    vead likes this.
  3. vead

    vead

    473
    14
    Nov 27, 2011
    I am asking for this hex value 0x80. why there is 0x80?
     
  4. Bluejets

    Bluejets

    3,542
    719
    Oct 5, 2014
    Did you even try say, Wiki? I did and it's in the first few lines.
     
  5. vead

    vead

    473
    14
    Nov 27, 2011
    I have gone throw little search. https://en.wikipedia.org/wiki/Bit_banging there is c program ,
    // set line high if bit is 1, low if bit is 0
    if (data & 0x80)
    I didn't understand what is the 0x80. is it address of low bit?
     
  6. Rixen

    Rixen

    98
    23
    Feb 16, 2016
    Not sure I understand, but..

    0x80 translates to '1000 0000' in binary, because the eigth(0-7 from right to left) position has the weight of 128..

    This is the most significant bit. (or sign bit)
     
    Last edited: Sep 10, 2017
  7. davenn

    davenn Moderator

    13,351
    1,774
    Sep 5, 2009
    0x80 = HEX80 = decimal 128
     
  8. vead

    vead

    473
    14
    Nov 27, 2011
    trying to understand what the meaning of this condition if ((Data & 0x80) == 0)
    This is the bitwise & operator. It does an and-operation of these two Data , 0x80
    Code:
    bit7 bit6 bit5 bit4  bit3 bit2 bit1 bit0
     x     x   x     x     x   x    x   x
    
    x mean 0 or 1 digit
    
    Data = xxxx xxxx;
    0x80 = 1000 0000;
    ---------------------  &
              = xxxx xxxx
    
    I am confused on 0x80. it could be anything like 0x60 or 0x55. why there is only 0x80. any specific reason?
     
    Last edited: Sep 10, 2017
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-