I2C I2C transmit/receive in master mode

In this example, the LPC81x I2C is configured as the master. The master sends 8 bits to the slave and then receives 8 bits from the slave. The system clock is set to 30 MHz and the bit rate is about 400 KHz. Therefore, you can select any pin for the I2C0_SCL and I2C0_SDA functions. Special open-drain I2C pads are optional. The transmission of the address and data bits is controlled by the state of the MSTPENDING status bit. Whenever the status is Master pending, the master can read or write to the MSTDAT register and go to the next step of the transmission protocol by writing to the MSTCTRL register.

Configure the pins:

  • Select two pins for I2C0_SCL and I2C0_SDA through the switch matrix. See I2C I2C-bus pin description.
  • In the IOCON register for the selected pins, disable the internal pull-up if using a standard digital I/O pin.

Configure the I2C bit rate:

Configure the LPC81x I2C as master: Set the MSTEN bit to 1 in the CFG register. See I2C registerName = CFG addressOffset = 0x000.

Write data to the slave:

  1. Write the slave address with the RW bit set to 0 to the Master data register MSTDAT. See I2C registerName = MSTDAT addressOffset = 0x028.
  2. Start the transmission by setting the MSTSTART bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020. The following happens:
    • The pending status is cleared and the I2C bus is busy.
    • The I2C master sends the start bit and address with the RW bit to the slave.
  3. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  4. Write 8 bits of data to the MSTDAT register.
  5. Continue with the transmission of the data by setting the MSTCONT bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020. The following happens:
    • The pending status is cleared and the I2C bus is busy.
    • The I2C master sends the data bits to the slave address.
  6. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  7. Stop the transmission by setting the MSTSTOP bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020.

Read data from the slave:

  1. Write the slave address with the RW bit set to 1 to the Master data register MSTDAT. See I2C registerName = MSTDAT addressOffset = 0x028.
  2. Start the transmission by setting the MSTSTART bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020. The following happens:
    • The pending status is cleared and the I2C bus is busy.
    • The I2C master sends the start bit and address with the RW bit to the slave.
    • The slave sends 8 bit of data.
  3. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  4. Read 8 bits of data from the MSTDAT register.
  5. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  6. Stop the transmission by setting the MSTSTOP bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020.

Write data to the slave and read back 2 bytes of data from the slave:

  1. Write the slave address with the RW bit set to 0 to the Master data register MSTDAT. See I2C registerName = MSTDAT addressOffset = 0x028.
  2. Start the transmission by setting the MSTSTART bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020. The following happens:
    • The pending status is cleared and the I2C bus is busy.
    • The I2C master sends the start bit and address with the RW bit to the slave.
  3. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  4. Write 8 bits of data to the MSTDAT register.
  5. Continue with the transmission of the data by setting the MSTCONTINUE bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020. The following happens:
    • The pending status is cleared and the I2C bus is busy.
    • The I2C master sends the data bits to the slave address.
  6. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  7. Write the slave address with the RW bit set to 1 to the Master data register MSTDAT. See I2C registerName = MSTDAT addressOffset = 0x028.
  8. Re-start the transmission setting the MSTSTART bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020. The following happens:
    • The pending status is cleared and the I2C bus is busy.
    • The I2C master sends the start bit and address with the RW bit to the slave.
    • The slave sends 8 bit of data.
  9. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  10. Read the first byte of data from the MSTDAT register.
  11. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  12. Repeat reading data from the slave by setting the MSTCONTINUE bit to 1 in the Master control register.
  13. Wait for the pending status to be set (MSTPENDING = 1) by polling the STAT register.
  14. Read the second byte of data from the MSTDAT register.
  15. Stop the transmission by setting the MSTSTOP bit to 1 in the Master control register. See I2C registerName = MSTCTL addressOffset = 0x020.

-->