#include "LPC8xx.h"
#define I2C_CFG_MSTEN (0x1)
#define I2C_STAT_MSTPENDING (0x1)
#define I2C_STAT_MSTSTATE (0xe)
#define I2C_STAT_MSTST_IDLE (0x0)
#define I2C_STAT_MSTST_RX (0x2)
#define I2C_STAT_MSTST_TX (0x4)
#define I2C_MSTCTL_MSTCONTINUE (0x1)
#define I2C_MSTCTL_MSTSTART (0x2)
#define I2C_WSTCTL_MSTSTOP (0x4)
int main ()
{
//Set clock for SWM
LPC_SYSCON->SYSAHBCLKCTRL |= 1<<7;
//Disable special functions
LPC_SWM->PINENABLE0 |= 0xFF0FFFFFFUL;
//Set pins for i2c
LPC_SWM->PINASSIGN7 |= 0x03FFFFFFUL;
LPC_SWM->PINASSIGN8 |= 0xFFFFFF04UL;
//Set clock for IOCON
LPC_SYSCON->SYSAHBCLKCTRL |= 1<<18;
//Disable Pullups for PIO0_2 and PIO0_3
LPC_IOCON->PIO0_2 = 0x80;
LPC_IOCON->PIO0_3 = 0x80;
//Set clock for i2c
LPC_SYSCON->SYSAHBCLKCTRL |= 1<<5;
//Set preset for i2c
LPC_SYSCON->PRESETCTRL &= ~1<<6;
LPC_SYSCON->PRESETCTRL |= 1<<6;
//NVIC enable
NVIC_EnableIRQ(I2C_IRQn);
//Set i2c clock DIV
LPC_I2C->DIV = 0x1;
//Enable i2c master
LPC_I2C->CFG = I2C_CFG_MSTEN;
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_IDLE){
//Set address
LPC_I2C->MSTDAT = (0x3C << 1) | 0;
//Send startbit
LPC_I2C->MSTCTL = 0x2;
}
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX){
//Send subaddress
LPC_I2C->MSTDAT = 0x00;
//Send continue
LPC_I2C->MSTCTL = 0x1;
}
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX){
//Send value
LPC_I2C->MSTDAT = 0xAF;
//Send continue
LPC_I2C->MSTCTL = 0x1;
}
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_TX){
//Send stopbit
LPC_I2C->MSTCTL = 0x4;
}
while(!(LPC_I2C->STAT & I2C_STAT_MSTPENDING));
if((LPC_I2C->STAT & I2C_STAT_MSTSTATE) == I2C_STAT_MSTST_IDLE);
} |
0x23 = 0b00100011 << 1 = 0x46 = 0b01000110 |
LPC_I2C->MSTDAT = (0x23 << 1) | 0; // address and 0 for RWn bit in order to write // subaddress |
//Set address LPC_I2C->MSTDAT = (0x3C << 1) | 0; |
int i; i = 0xFF | 0; // The value assigned to i is 0xFF, bitwise inclusive ORing with 0 did nothing to the value 0xFF |
int i; i = 0xFF & ~(1<<0); // The value assigned to i is 0xFE, bitwise ANDing with ~(1<<0) cleared bit 0 |