I2C Beispiel

 

Beispiel: EEPROM 24C64 lesen und schreiben ohne I2C_Status Abfrage

 

// I2C Initialization, Bit Rate 100kHz

 

main(void)

{

    word address;

    byte data,EEPROM_data;

 

    address=0x20;

    data=0x42;

 

    I2C_Init(I2C_100kHz );

    // write data to 24C64 (8k x 8) EEPROM

    I2C_Start();

    I2C_Write(0xA0);         // DEVICE ADDRESS : A0

    I2C_Write(address>>8);   // HIGH WORD ADDRESS

    I2C_Write(address);      // LOW WORD ADDRESS

    I2C_Write(data);         // write Data

    I2C_Stop();

    AbsDelay(5);             // delay for EEPROM Write Cycle

 

    // read data from 24C64 (8k x 8) EEPROM

    I2C_Start();

    I2C_Write(0xA0);         // DEVICE ADDRESS : A0

    I2C_Write(address>>8);   // HIGH WORD ADDRESS

    I2C_Write(address);      // LOW WORD ADDRESS

    I2C_Start();             // RESTART

    I2C_Write(0xA1);         // DEVICE ADDRESS : A1

    EEPROM_data=I2C_Read_NACK();

    I2C_Stop();

    Msg_WriteHex(EEPROM_data);

}