cancel
Showing results for 
Search instead for 
Did you mean: 

STM32VL Discovery and I2C Accelerometer

peppea82
Associate II
Posted on November 02, 2012 at 07:54

I need some help setting up I2C connection to LIS302DL or a generic I2C accelerometer. I need some code examples to get the accelerometer working. Thanks in advance.

2 REPLIES 2
Posted on November 02, 2012 at 11:13

Have you looked at the iNemo stuff?

http://www.st.com/internet/evalboard/product/250367.jsp

http://www.st.com/internet/com/SOFTWARE_RESOURCES/SW_COMPONENT/SW_FUNCTION/inemo_gui_2_0_0.zip

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
peppea82
Associate II
Posted on November 02, 2012 at 12:00

Sorry but I think iNemo is unrelated. I'll try to explain better.

I have an STM32VLDiscovery board and a LIS302DL accelerometer. They are connected using i2c to the stm32vl discovery.

LIS302DL is provided in an adapter board where I connected

LIS side -> STM32VL side

VDD -> 3v3

CS -> 3v3

SCL -> PB6

SDx -> PB7

SDO -> 3v3

GND -> GND

I have to accomplish a very simple task: get data from i2c and put data to usart.

I2C is initialized using the following code:

static void I2C1_GPIO_Config(void)

{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

  GPIO_InitTypeDef GPIO_InitStructure;

  I2C_InitTypeDef I2C_InitStructure;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

  I2C_Cmd(I2C1, ENABLE);

  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;

  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;

  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;

  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;

  I2C_InitStructure.I2C_ClockSpeed = 100000;

  I2C_InitStructure.I2C_OwnAddress1 = 0;

  I2C_Init(I2C1, &I2C_InitStructure);

}

Read from i2c is done by

I2C_GenerateSTART(I2C1, ENABLE);

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

I2C_Send7bitAddress(I2C1, 0x1d, I2C_Direction_Transmitter);

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

I2C_SendData(I2C1, 0x20);

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, 0x3B, I2C_Direction_Receiver);

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

data = I2C_ReceiveData(I2C1);

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));

I2C_GenerateSTOP(I2C1, ENABLE);

I'm using Atollic free version.

Code gets stuck on

while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));