#define I2CTXD (*(unsigned char volatile xdata *)0xfe86)
#define I2CRXD (*(unsigned char volatile xdata *)0xfe87)
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P4M1 = 0xb3;
sfr P4M0 = 0xb4;
sfr P5M1 = 0xc9;
sfr P5M0 = 0xca;
sbit SDA = P1^4;
sbit SCL = P1^5;
bit isda; //Device address flag
bit isma; //Storage address flag
unsigned char addr;
unsigned char pdata buffer[256];
void main()
{
P0M0 = 0x00;
P0M1 = 0x00;
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P4M0 = 0x00;
P4M1 = 0x00;
P5M0 = 0x00;
P5M1 = 0x00;
P_SW2 = 0x80;
I2CCFG = 0x81; //Enable I2C slave mode
I2CSLADR = 0x5a; //Set the slave device address to 5A
//That is, I2CSLADR[7:1]=010_1101B, MA=0B.
//Since MA is 0, the device address sent by the host must be
// the same as I2CSLADR[7:1] to access this I2C slave device.
//If the host needs to write data, it will send 5AH(0101_1010B)
//If the host needs to read data, it will send 5BH (0101_1011B)
I2CSLST = 0x00;
I2CSLCR = 0x00; //Disable interrupt of slave mode
isda = 1; //User variable initialization
isma = 1;
addr = 0;
I2CTXD = buffer[addr];
while (1)
{