Hi,
I’m trying to read ADXL345 accelerometer from I2C bus of f103RET6.
My codes are below.
I can master write. The ADXL_Configuration() function works succesfully.
And also I can read a register by ADXL_Read(0x00) (0x00 is device ID and returns 229, succesfully.)
But It can be only for first time…
I can’t do it in a timer loop. It is stopping at GenerateStart function of ADXL_Read…
Any help would be greatly appreciated!
void ADXL345_Configuration(void)
{
printf("\r Line Busy?");
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
//Send I2C2 START condition
I2C_GenerateSTART(I2C2, ENABLE);
//Test on I2C2 EV5 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//Send I2C2 slave Address for write
I2C_Send7bitAddress(I2C2, ADXL345_W, I2C_Direction_Transmitter);
//Test on I2C2 EV6 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//Send I2C2 data
I2C_SendData(I2C2, 0x2D);
//Test on I2C2 EV8 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//Send I2C2 data
I2C_SendData(I2C2, (1<<3));
//Test on I2C2 EV8 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Send I2C2 STOP Condition
I2C_GenerateSTOP(I2C2, ENABLE);
printf("\r ADXL Configured...OK");
}
u16 ADXL345_Read(u8 regAddr)
{
u8 lsb,msb,temp;
printf("\r Busy?");
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY));
printf("\r Starting...");
//Send I2C2 START condition
I2C_GenerateSTART(I2C2, ENABLE);
//Test on I2C2 EV5 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
//At the second loop program is locking there..
printf("\r Sending Adress...");
//Send I2C2 slave Address for write
I2C_Send7bitAddress(I2C2, ADXL345_W, I2C_Direction_Transmitter);
//Test on I2C2 EV6 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
//I2C_Cmd(I2C2, ENABLE);
printf("\r Sending Data...");
//Send I2C2 data
I2C_SendData(I2C2, regAddr);
//Test on I2C2 EV8 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Send STOP Condition
I2C_GenerateSTOP(I2C2, ENABLE);
printf("\r Restarting...");
//Send I2C2 START condition
I2C_GenerateSTART(I2C2, ENABLE);
//Test on I2C2 EV5 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT));
printf("\r Sending Adress(Receive Mode)...");
//Send I2C2 slave Address for write
I2C_Send7bitAddress(I2C2, ADXL345_W, I2C_Direction_Receiver);
//Test on I2C2 EV6 and clear it
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
// Clear ACK Disable Acknowledgement
I2C_AcknowledgeConfig(I2C2, DISABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED));
lsb = I2C_ReceiveData(I2C2); //Returns Device ID Correctly.
printf(" LSB: %d ",lsb);
// Send STOP Condition
I2C_GenerateSTOP(I2C2, ENABLE);
// Enable Acknowledgement to be ready for another reception
I2C_AcknowledgeConfig(I2C2, ENABLE);
printf("\r The END...\r");
return( lsb);
}