Thank you for response. I've tested the code with added delay.
while(debug < 1){ //get_data looppi
status = 0;
//START bit + accelerometer adress + writebit
LPC_I2C->CONSET = (1<<5);
do{ //wait for start condition to be sent
status = LPC_I2C->STAT & 0xF8; //store current state (sec 15.7.2)
}
while(status != 0x08);
LPC_I2C->DAT = 0x38; //Device adress + W
LPC_I2C->CONCLR = (1<<3); //Reset SI
LPC_I2C->CONCLR = (1<<5); // Reset STA
//Transmit 1:st registry adress to start read from
while(LPC_I2C->STAT != 0x18);
LPC_I2C->DAT = 0x01;
LPC_I2C->CONCLR = (1<<3);
//start bit + accelerometer adress + readbit
while(LPC_I2C->STAT != 0x28);
_delay_ms(1);
LPC_I2C->CONSET |= (1<<5);
_delay_ms(1);
//while(LPC_I2C->STAT != 0x10)
LPC_I2C->DAT = 0x39; //Device adress + R
LPC_I2C->CONCLR = (1<<3);
_delay_ms(100);
debug = 2;
}
The delay didn' do the trick.
I have been reading the LPC11Uxx manual, the I2C section really closely to be precise.
The MMA8452 needs the following data sequence illustrated in the attached image. The initialization code works well and data transmission is correct.
I can't get an hold on about how the I2C bus actually works. I think i should change the operation mode to MASTER-RECEIVE from MASTER-TRANSMIT that is udes in the initialization of the accelerometer. The initialization is a set of binary instructions write to the various setup registry, that works and the accelerometer answers with ACK.
If I uncomment the STAT != 0x10 condition, code stops running at that point. Clearly there is a problem with repeated start with my code.
The documentation gave me an expression that the status registry as instructive register, I've got that expression that the status flags must be correct for repeated start to occur. The communication must be controller with the status registry.
I can't understand how the communication should be done.
Loop start{
CONSET bit to 1
Status = 0x08
send DAT, device adress + writebit (8bit) -> master transmit, status changes to 0x18
send DAT, registry adress (7bit) -> master transmit, status changes to 0x28
CONSET bit to 1 for repeated start.
send DAT, device adress + readbit (8bit) -> mode should be in master receive, status changes to 0x40
(of course the necessary SI and STA CONCLR bits are set between commands)
the 0x40 status for reading data expects the previous state to be 0x08 or 0x10. How can i write data without changing the state to 0x28.
The 0x28 sends automaticly STOP-flag if no data is sent after.
The more I try to understand, the more questions I've got.
I can't be to far away from working code, any further advice would be highly appreciated.
Oscari