i2c communication not working.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

i2c communication not working.

608 Views
rishavsharma
Contributor I

Hi, i am using LPC1768 MC embedded on local development board. to fetch data from MCU 9250, i2c communication is needed. I haven't used any library for the purpose and all code is written in ARDUINO IDE

In Master sender mode, after sending start byte, and slave address, "I2STAT" should have one of definite state : 0x08 , 0x018, 0x28  But in this case, "I2STATE" comes out to be have "0xF8", which is INVALID. Please check the code, and tell what is missing/wrong.( I am struct with this thing from the past 4 days.).

#define WAIT_SI while (!(LPC_I2C1->I2CONSET & (1<<3)))
#define CLEAR_SI LPC_I2C1->I2CONCLR = 1<< 3

void i2c_enable(){
LPC_I2C1->I2CONSET |= 1<< 6; //ENABLE I2C1 (CONSET REGISTER)
}

void i2c_start(){
LPC_I2C1->I2CONSET |= 1<< 5;
WAIT_SI; // WAIT UNTIL DONE
}

void i2c_stop(){
LPC_I2C1->I2CONSET |= 1<<4; // stop i2c
CLEAR_SI; //clear SI
// while (LPC_I2C1->I2CONSET & (1<<4)); // wait until H/W stops i2c
}

unsigned char i2c_address(unsigned char add)

{
LPC_I2C1->I2DAT = add;
LPC_I2C1->I2CONCLR = 1<< 5;
LPC_I2C1->I2CONCLR = 1<< 3;
WAIT_SI;
return (LPC_I2C1->I2STAT);
}

void i2c_write(char data)

{
LPC_I2C1->I2DAT = data;
LPC_I2C1->I2CONCLR = 1<< 3;
CLEAR_SI;
WAIT_SI;
}

unsigned char i2c_read(char ack)

{
if(ack)
{
LPC_I2C1->I2CONSET = 1<<2;
}
else
{
LPC_I2C1->I2CONCLR = 1<< 2;
}
CLEAR_SI;
// WAIT_SI;
return (LPC_I2C1-> I2DAT);
}

void sysInit() {
LPC_GPIO0->FIODIR0 |= 0x33; //P0.5 P0.4 P0.1 P0.0 ext pins 29,30,10,9
LPC_GPIO0->FIODIR0 |= 1<<00;
LPC_GPIO0->FIODIR0 |= 1<<01;
LPC_SC->PCONP |= 1 << 19; //re-enable POWER to I2C_1 if required
LPC_SC->PCLKSEL1 |= 1<<6; //pclk = cclk
LPC_PINCON->PINSEL0 |= 0x03<<00; //Pin P0.00 allocated to alternate function 3
LPC_PINCON->PINSEL0 |= 0x03<<01; //Pin P0.01 allocated to alternate function 3
LPC_GPIO0->FIODIR |= 1<<00; //Bit P0.10 an output
LPC_GPIO0->FIODIR |= 1<<01; //Bit P0.11 an output
LPC_PINCON->PINMODE0 &= ~(3<<00); //P0.10 has pull up/down resistor
//LPC_PINCON->PINMODE0 |= (2<<20); //omit to use internal pull up
LPC_PINCON->PINMODE0 &= ~(3<<02); //P0.11 has pull up/down resistor
//LPC_PINCON->PINMODE0 |= (2<<22); //omit to use internal pull up
LPC_PINCON->PINMODE_OD0 |= 1<<00; //Bit P0.10 is open drain
LPC_PINCON->PINMODE_OD0 |= 1<<01; //Bit P0.11 is open drain
LPC_I2C1->I2SCLH = 60; //100kHz from 12MHz
LPC_I2C1->I2SCLL = 60; //100kHz from 12MHz
}


void setup() {

sysInit();
Serial.begin(9600);
}

void loop() {
unsigned char st;
i2c_enable();
i2c_start();
st=i2c_address(0xD0);
Serial.print(st,HEX);     //state value comes out to be 0xF8
if (st == 0x18)        
{
//do nothing;
}
if (st == 0x20)    // 0x20 signifies: slave address is send, ackno does not recieve.
{
Serial.println("success");
i2c_stop();
while(1);
}
//Serial.print("no sucess");
}

Code is tested over EXPLORE M3(locally made board) without any slave device connected. Slave Address used is random. In described  condition 'I2STATE' should have 0x20. 

Labels (1)
0 Kudos
0 Replies