arynhard
arynhard

Reputation: 473

How to program I2C slave mode

I am failing to understand how I would code my MCU to listen for it's address on the I2C bus and execute accordingly. A lot of example code in C is for a master device. I would like to better understand how to write code for a slave device. Do I use the SMB0STA status register? I posted a function below that I think would "listen" for the cue to start and output data. 0xA8 is the status code for "Own address + R received. ACK transmitted." according to my C8051F020 MCU datasheet.

unsigned char i2c_Slave_Read (void)
{
    unsigned char data_out[8];

    data_out[8] = "LED 1 ON";

    while (SI)
    if (SMB0STA == 0xA8)
    {
        P5 = 0x10;
        SMB0DAT = data_out;
    }
}

Upvotes: 1

Views: 1963

Answers (1)

TJD
TJD

Reputation: 11896

Slave operation is described starting in section 18.3.3 of the datasheet

You program the SMB0ADR register with your slave address. Address detection is done by the hardware, which then gives you an interrupts when data arrives. You then can read the status in SMB0STA to see what is happening, and read/write data using SMB0DAT. See table 18.1, which explains the SMB0STA state machines and the actions your code should take.

Upvotes: 2

Related Questions