
UM10800
All information provided in this document is subject to legal disclaimers.
© NXP Semiconductors N.V. 2016. All rights reserved.
User manual
Rev. 1.2 — 5 October 2016
458 of 487
NXP Semiconductors
UM10800
Chapter 34: LPC82x Code examples
34.2.10 Master sending nack and repeated start on data
34.2.11 Master sending nack and repeated start on data. Interrupt mode
34.2.12 Slave read one byte from master
Table 414. I2C Code example
Master sending nack and repeated start on data. Address 0x23. Polling mode. No error
checking.
LPC_I2C->CFG = I2C_CFG_MSTEN;
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) != I2C_STAT_MSTST_IDLE) abort();
LPC_I2C->MSTDAT = (0x23 << 1) | 1; // address and 1 for RWn bit in order to read data
LPC_I2C->MSTCTL = I2C_MSTCTL_MSTSTART; // send start
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) != I2C_STAT_MSTST_RX) abort();
data = LPC_I2C->MSTDAT; // receive data
if(data != 0xdd) abort();
LPC_I2C->MSTDAT = (0x23 << 1) | 0; // address and 1 for RWn bit in order to read data
LPC_I2C->MSTCTL = I2C_MSTCTL_MSTSTART; // repeated start (nack implied)
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
LPC_I2C->MSTCTL = I2C_MSTCTL_MSTSTOP; // stop transaction
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) != I2C_STAT_MSTST_IDLE) abort();
Table 415. I2C Code example
Master sending nack and repeated start on data. Address 0x23. No error checking. Interrupt
mode
LPC_I2C->CFG = I2C_CFG_MSTEN;
LPC_I2C->INTENSET = I2C_STAT_MSTPENDING;
NVIC_EnableIRQ(I2c_IRQn);
while(LPC_I2C->INTENSET & I2C_STAT_MSTPENDING);
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) != I2C_STAT_MSTST_IDLE) abort();
NVIC_DisableIRQ(I2c_IRQn);
Table 416. I2C Code example
Slave read one byte from master. Address 0x23. Polling mode.
LPC_I2C->SLVADR0 = 0x23 << 1; // put address in address 0 register
LPC_I2C->CFG = I2C_CFG_SLVEN;
while(!(LPC_I2C->STAT & I2C_STAT_SLVPENDING));
if((LPC_I2C->STAT & I2C_STAT_SLVSTATE) != I2C_STAT_SLVST_ADDR) abort();
LPC_I2C->SLVCTL = I2C_SLVCTL_SLVCONTINUE; // ack address
while(!(LPC_I2C->STAT & I2C_STAT_SLVPENDING));
if((LPC_I2C->STAT & I2C_STAT_SLVSTATE) != I2C_STAT_SLVST_RX) abort();
data = LPC_I2C->SLVDAT; // read data
if(data != 0xdd) abort();
LPC_I2C->SLVCTL = I2C_SLVCTL_SLVCONTINUE; // ack data