2009-04-13 12:04 AM
Problems with I2C1 and LIS302DL
2011-05-17 04:09 AM
Hi,
I've a problem using a STM32101RBT6 I2C1 port and A LIS302 accelerometer. The device answers, but the only value I receive when I read the 0x0F register ( Who am I) is 0x38 instead of 0x3B. On the hardware side, everything seems ok. Pullpu resistors (4k7), the CS pin up and SDO down. On the Sw side, I initialize the I2C1 this way: I2C_InitTypeDef I2C_InitStructure; /* Enable I2C1----------------------------------------------------*/ I2C_Cmd(I2C1, ENABLE); /* I2C1 configuration ------------------------------------------------------*/ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = ClockSpeed; I2C_Init(I2C1, &I2C_InitStructure); and ask the device this way: I2C_GenerateSTART(I2C1, ENABLE); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1, DEVICE_WRITE, I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); I2C_SendData(I2C1, register_name); while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); I2C_GenerateSTART(I2C1, ENABLE); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); I2C_Send7bitAddress(I2C1, DEVICE_READ, I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); in_byte = I2C_ReceiveData(I2C1); I2C_GenerateSTOP(I2C1, ENABLE); return(in_byte); Always the same value...WHY??? Where am I doing a mistake? Thanks, Claudio2011-05-17 04:09 AM
Hi everyone,
the nighttime sometimes helps...and I've found some errors in my code, expecially about the device addressing and the communication direction. I have another problem now, that I solved usnig a tip suggested by Brj in this topic http://www.st.com/mcu/forums-cat-6701-23.html every byte read I deinit and reinit I2C. I'm sure that is not efficent, but now it works. If anyone has a suggestion... here is the code: I2C_GenerateSTART(I2C1, ENABLE); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); Dly15us(); I2C_Send7bitAddress(I2C1, DEVICE_ADDR, I2C_Direction_Transmitter); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); Dly15us(); I2C_SendData(I2C1, register_name); while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); Dly15us(); I2C_GenerateSTART(I2C1, ENABLE); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)); Dly15us(); I2C_Send7bitAddress(I2C1, DEVICE_ADDR, I2C_Direction_Receiver); while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); // Inactive ACK I2C_AcknowledgeConfig(I2C1, DISABLE); Dly15us(); while( !I2C_CheckEvent( I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED ) ); // EV7 in_byte = I2C_ReceiveData(I2C1); // while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)); I2C_GenerateSTOP(I2C1, ENABLE); // active ACK I2C_AcknowledgeConfig(I2C1, ENABLE); I2C_DeInit(I2C1); InitI2C(); return(in_byte); bye , Claudio