Attiny45 I2C PCF8574 output


http://www.arduino.cc/playground/Code/USIi2c
code:

//#define PCF8574A_ID 0x70  //01110000 :000 slave address
#define PCF8574_ID 0x40   //01000000 :000 slave address
// Arduino Pins
#define SDA   0                         // Serial data
#define SCL   1                         // Serial clock
char outdata;


void i2c_start(void);
void i2c_stop(void);
void i2c_write(unsigned char output_data);
void PCF8574A_Write(unsigned char data);
void i2c_write_bit(int data_bit);


void i2c_start(void)
{
  digitalWrite(SDA, LOW);
  delayMicroseconds(1);
  digitalWrite(SCL, LOW);
  delayMicroseconds(1);
}
void i2c_acknowledge(void)
{
  digitalWrite(SDA, LOW);
  delayMicroseconds(1);
  digitalWrite(SCL, HIGH);
  delayMicroseconds(1);
  digitalWrite(SCL, LOW);
  delayMicroseconds(1);
  digitalWrite(SDA, LOW);
  delayMicroseconds(1);
}
void i2c_stop(void)
{
  digitalWrite(SCL, HIGH);
  delayMicroseconds(1);
  digitalWrite(SDA, HIGH);
}
void i2c_write(unsigned char output_data)
{
  int iBit=0;
  for(int iloop = 0 ; iloop < 8 ; iloop++)
  {
    // if  output_data=0xBF;    0xBF = 1011 1111
    //(first)  output_data= 1011 1111 :>  1011 1111 & 1000 0000(0x80) = 1000 0000(0x80)  |:> 1000 0000(0x80)>> 7 = 0000 0001(0x01)     ; HEX
    //(second) output_data= 0111 1110 :>  0111 1110 & 1000 0000(0x80) = 0000 0000(0x00)  |:> 0000 0000(0x80)>> 7 = 0000 0000(0x01)     ; HEX
    iBit = ((output_data & 0x80) >> 7 ) ;
    delayMicroseconds(1);
    digitalWrite(SDA,iBit);   // bit output
    delayMicroseconds(1);
    digitalWrite(SCL, HIGH);
    delayMicroseconds(1);
    digitalWrite(SCL, LOW);
    delayMicroseconds(1);
    digitalWrite(SDA, LOW);
    output_data = output_data << 1 ;    //    output_data= 1011 1111       |:>      1011 1111 << 1 = 0111 1110    ; BIN
  }
  delayMicroseconds(1);
}
void PCF8574_Write(unsigned char data)
{
  //SDA :> start[S]--\_  _/-0-\__/-1-\__/-0-\__/-0-\__/-0-\__/-0-\__/-0-\__/-0-\__/-0-\__/-1-\__/-0-\__/-1-\__/-1-\__/-1-\__/-1-\__/-1-\__/-1-\__/-0-\__ ___/---stop bit [P]
  //SCL :>         ---\_ __/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\____/-\___ _/-----
  //             |-[S]-|                                                  |-R/W-|       |-------------------data to port-----------------------|         |-[P]-|
  //                     |---Slave address (PCF8574)--[A2=0,A1=0,A0=0]---|       |-[A]-|     |-[A]-|


  i2c_start();              //         |-[S]-|  ;Start Condition
  i2c_write(PCF8574_ID);    //         |---Slave address (PCF8574)--[A2=0,A1=0,A0=0]---|   +    |-R/W-|
  i2c_acknowledge();        //         |-[A]-|  ;Active LOW
  i2c_write(data);          //         |-------------------data to port-----------------------|
  i2c_acknowledge();        //         |-[A]-|  ;Active LOW
  i2c_stop();               //         |-[P]-|  ;Stop Condition
}


void setup()
{
  pinMode(SCL, OUTPUT);
  pinMode(SDA, OUTPUT);
  digitalWrite(SDA, HIGH);
  digitalWrite(SCL, HIGH);
}


void loop()
{  


  PCF8574_Write(0xFE);//1111 1110
  delay(80);
  PCF8574_Write(0xFD);//1111 1101
  delay(80);
  PCF8574_Write(0xFB);//1111 1011
  delay(80);
  PCF8574_Write(0xF7);//1111 0111
  delay(80);
  PCF8574_Write(0xEF);//1110 1111
  delay(80);
  PCF8574_Write(0xDF);//1101 1111
  delay(80);
  PCF8574_Write(0xBF);//1011 1111
  delay(80);
  PCF8574_Write(0x7F);//0111 1111
  delay(80);
}