'&'操作后存储的值是多少?

问题描述 投票:0回答:2

在下面的代码中

void I2C_Write(uint8_t v_i2cData_u8)
{
    uint8_t i;

    for(i=0;i<8;i++)                   // loop 8 times to send 1-byte of data
    {
        SDA_PIN = v_i2cData_u8 & 0x80;     // Send Bit by Bit on SDA line
        i2c_Clock();                   // Generate Clock at SCL
        v_i2cData_u8 = v_i2cData_u8<<1;// Bring the next bit to be transmitted to MSB position
    }

    i2c_Clock();
}

在声明中:SDA_PIN = v_i2cData_u8&0x80;它被告知数据将逐位发送,如果数据逐位发送,那么将存储在SDA_PIN中,SDA_PIN的值是否为0x80或1?

c microcontroller i2c keil
2个回答
0
投票

SDA_PIN将被分配0x80(128)或0,具体取决于该循环中v_i2cData_u8的高位。如果要确保写入0x01字节,则需要执行以下操作:

SDA_PIN = (v_i2cData_u8 & 0x80) ? 1 : 0;


0
投票

当v_i2cData_u8的第7位为1时,SDA_PIN将为0x80,否则为0。

如果你想让它1而不是0x80你可以做到

SDA_PIN = !!(v_i2cData_u8 & 0x80); or
SDA_PIN = (v_i2cData_u8 & 0x80) >> 7;

或者作为塞尔比的回答说。

© www.soinside.com 2019 - 2024. All rights reserved.