SW_I2C
 
Component SW_I2C
SW emulated I2C using two I/O pins.
Component Level: High
Typical Usage:
(Examples of a typical usage of the component in user code. For more information please see the page Component Code Typical Usage.)

(1) Data sending
Required component name is "EI2C1".

The typical usage of this component shows how to send data into slave. If the SendChar method returns ERR_OK, the transmission was successful.

MAIN.C

#define Slave 0x80

void main(void)
{
    
  /* Set slave address */
  (void)EI2C1_SelectSlave(Slave);
    
  /* Send character */
  while(EI2C1_SendChar(65) != ERR_OK) {}

}

(2) Communication with external serial EEPROM memory.
Required component name is "EI2C1" and the following component settings: Mode selection - MASTER, Automatic Stop condition - no. SelectSlave method is used for slave address selection in 7-bit addressing mode, SendBlock method is used for data sending and RecvBlock used for data receiving. Cpu_Delay100US - method of the CPU component used for time delay.

The example shows the communication with external serial EEPROM memory, specifically 24C128 device.

MAIN.C

byte buffer[3];  
word sent;
byte data;
byte err;

void main(void)
{
  //WRITE DATA
  err = EI2C1_SelectSlave(0x50);           //Select address of the EEPROM device (not required if only one device on the bus)
  buffer[0] = 0;                           //most significant byte of storage address
  buffer[1] = 0x50;                        //least significant byte of storage address
  buffer[2] = 0x10;                        //data  
  err = EI2C1_SendBlock(buffer, 3, &sent); //send storage address and data to the EEPROM
  err = EI2C1_SendStop();                  //Send STOP condition
  
  Cpu_Delay100US(100); //10ms delay for EEPROM to complete write cycle - for more details see EEPROM datasheet
 
  //READ DATA
  buffer[0] = 0;                           //most significant byte of storage address
  buffer[1] = 0x50;                        //least significant byte of storage address
  err = EI2C1_SendBlock(buffer, 2, &sent); //send address to the EEPROM
  err = EI2C1_RecvChar(&data);             //read data from EEPROM  
  err = EI2C1_SendStop();                  //Send STOP condition
}