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

How to read a bit (I2C)

Hi there,

I have been doing I2C and very new to it. I already make it to work using PCF8575C. I managed to read all two bytes, but I can't seem to figure out how to only get the value of one bit. For example, previously before using I2C, I juts directly create a condition which (if p1_0 == 1, True.. Else, False). I want to use the same concept but unable to get it right. Below is my code:

case 'R':
   I2C_Start();
   I2C_Write(0x43);
   I2C_Read();
   I2C_Read_End();
   I2C_Stop();
break;

void I2C_Write(unsigned char Data)
{
  unsigned char i;

  for (i = 1; i <= 8; i++)
  {
    SDA = (Data >> 7);
    SCL = 1;
    Delay_1sec();
    Data = Data << 1;
    SCL = 0;
    Delay_1sec();
  }
  SCL = 0;
  SDA = 1;
  Delay_1sec();
  SCL = 1;

  if(SDA)
  {
    printf("Ack bit missing %02X\n",(unsigned int)Data);
  }
  else
  {}
  Delay_1sec();
  SCL = 0;
}

unsigned char I2C_Read()
{
  unsigned char i;
  for(i=1;i<=8;i++)
  {
    SCL = 1;
    Data = Data << 1;                  //Rotate Data Bit
    Data = Data | (unsigned char)SDA;  //Move SDA to Data and Combine Existing Data Value
    Delay_1sec();
    SCL = 0;
    Delay_1sec();
  }
  SDA = 0;
  SCL = 1;
  Delay_1sec();
  SCL  = 0;
  Delay_1sec();
  SDA = 1;
  printf("%02X",(unsigned int)Data);
  return Data;
}

unsigned char I2C_Read_End()
{
  unsigned char i, Data;
  for(i=1;i<=8;i++)
  {
   SCL = 1;
   Data = Data << 1;                  //Rotate Data Bit
   Data = Data | (unsigned char)SDA;  //Move SDA to Data and Combine Existing Data Value
   Delay_1sec();
   SCL = 0;
   Delay_1sec();
  }
  SCL = 1;
  Delay_1sec();
  SCL  = 0;
  Delay_1sec();
  SDA = 1;
  printf("%02X\n",(unsigned int)Data);
  return Data;
}

Would be glad if someone can help me with this. Thanks!

0