function to send data

Nov 27, 2011
473
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) ?

Rixen

Feb 16, 2016
98

If ((the result from the first paranthesis) is 0))
{
Do stuff
}

Nov 27, 2011
473
If ((the result from the first paranthesis) is 0))
{
Do stuff
}
I am asking for this hex value 0x80. why there is 0x80?

Bluejets

Oct 5, 2014
6,199
Did you even try say, Wiki? I did and it's in the first few lines.

Nov 27, 2011
473
Did you even try say, Wiki? I did and it's in the first few lines.
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?

Rixen

Feb 16, 2016
98
I didn't understand what is the 0x80. is it address of low bit?

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:

davenn

Moderator
Sep 5, 2009
14,150
I didn't understand what is the 0x80. is it address of low bit?

0x80 = HEX80 = decimal 128

Nov 27, 2011
473
0x80 = HEX80 = decimal 128
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:

Replies
16
Views
379
Replies
2
Views
304
Replies
1
Views
234
Replies
1
Views
608
Replies
10
Views
2K