AnsweredAssumed Answered

Read accelerometer values STM32F4Discovery using I2C

Question asked by mueller.gerd.001 on Feb 12, 2015
Latest reply on Feb 16, 2015 by mueller.gerd.001
Hi,

I'm trying to read the accelerometer (LIS302DL) in the STM32F4Discovery board using the I2C-Bus.
Unfortunately, my aapproach is not working. When i (try to) configure the IC an read the "Who_Am_I" register, the return value is zero, but it should be 0b00111011.
Currently I have no oscilloscope or logic analyzer at hand and therefore cannot check the bus signals.
Hopefully, somebody tried to read the accelerometer values in the past and can help me solve my problem.

Thanks in advance!


EDIT: I updated the code, see fourth comment!

001.#include "stm32f4xx.h"
002.#include "stm32f4xx_gpio.h"
003.#include "stm32f4xx_rcc.h"
004.#include "stm32f4xx_i2c.h"
005. 
006.#include <stdint.h>
007. 
008.void initI2C()
009.{
010.    // Use 168MHz
011.    SystemInit();
012. 
013.    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
014.    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
015. 
016. 
017.    GPIO_InitTypeDef GPIO_InitStructure;
018.    I2C_InitTypeDef I2C_InitStructure;
019. 
020. 
021.    // PA-Pins for I2C-Bus
022.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
023.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
024.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
025.    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
026.    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
027.    GPIO_Init(GPIOA, &GPIO_InitStructure);
028. 
029.    // PA6
030.    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
031.    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
032.    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
033.    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
034.    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
035.    GPIO_Init(GPIOA, &GPIO_InitStructure);
036. 
037.    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_I2C1);
038.    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_I2C1);
039. 
040.    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
041.    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
042.    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
043.    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
044.    I2C_InitStructure.I2C_ClockSpeed = 100000;
045.    I2C_InitStructure.I2C_OwnAddress1 = 0;
046.    I2C_Init(I2C1, &I2C_InitStructure);
047. 
048.    I2C_Cmd(I2C1, ENABLE);
049. 
050.    GPIO_ResetBits(GPIOA, GPIO_Pin_6);
051. 
052.}
053. 
054.// For procedure, see: stm32f4xx_i2c.h, line 303+
055.// i2cAdd: Address of slave in I2C-Bus
056.// regAdd: Register address of slave to write to
057.// data: Data to write
058.void i2cSendByte(uint8_t i2cAdd, uint8_t regAdd, uint8_t data)
059.{
060.    while(I2C_GetFlagStatus(I2C1 , I2C_FLAG_BUSY));
061. 
062.    // Generate Start Condition
063.    I2C_GenerateSTART(I2C1, ENABLE);
064.    while(I2C_GetFlagStatus(I2C1 , I2C_EVENT_MASTER_MODE_SELECT));
065. 
066.    // Send slave address
067.    I2C_Send7bitAddress(I2C1, i2cAdd, I2C_Direction_Transmitter);
068.    while(I2C_GetFlagStatus(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); // Wait for SACK
069. 
070.    // Transfer register address
071.    I2C_SendData(I2C1, regAdd);
072.    while(I2C_GetFlagStatus(I2C1 , I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // Wait for SACK
073. 
074.    // Transfer data to register
075.    I2C_SendData(I2C1, data);
076.    while(I2C_GetFlagStatus(I2C1 , I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // Wait for SACK
077. 
078.    I2C_GenerateSTOP(I2C1 , ENABLE);
079. 
080.}
081. 
082.int8_t i2cReceiveByte(uint8_t i2cAdd, uint8_t regAdd)
083.{
084.    I2C_AcknowledgeConfig(I2C1, DISABLE);
085.    while(I2C_GetFlagStatus(I2C1 , I2C_FLAG_BUSY));
086. 
087.    // Generate Start Condition
088.    I2C_GenerateSTART(I2C1, ENABLE);
089.    while(I2C_GetFlagStatus(I2C1 , I2C_EVENT_MASTER_MODE_SELECT));
090. 
091.    // Send slave address
092.    I2C_Send7bitAddress(I2C1, i2cAdd, I2C_Direction_Transmitter);
093.    while(I2C_GetFlagStatus(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); // Wait for SACK
094. 
095.    // Transfer register address
096.    I2C_SendData(I2C1, regAdd);
097.    while(I2C_GetFlagStatus(I2C1 , I2C_EVENT_MASTER_BYTE_TRANSMITTED)); // Wait for SACK
098. 
099.    // Generate Start Condition
100.    I2C_GenerateSTART(I2C1, ENABLE);
101.    while(I2C_GetFlagStatus(I2C1 , I2C_EVENT_MASTER_MODE_SELECT));
102. 
103.    // Send slave address
104.    I2C_Send7bitAddress(I2C1, i2cAdd, I2C_Direction_Receiver);
105.    while(I2C_GetFlagStatus(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ));
106.    while(I2C_GetFlagStatus(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED )); // Wait for SACK
107. 
108.    int8_t data = I2C_ReceiveData(I2C1);
109. 
110.    I2C_GenerateSTOP(I2C1 , ENABLE);
111.    I2C_AcknowledgeConfig(I2C1, ENABLE);
112. 
113.    return data;
114.}
115. 
116.int main(void)
117.{
118.    initI2C();
119.    i2cSendByte(0b0011100<<1, 0x20, 0b11100111);
120. 
121.    int8_t data;
122.    while(1)
123.    {
124.        data = i2cReceiveByte(0b0011100<<1, 0x0F);
125.        //data = i2cReceiveByte(0b00111000, 0x29);
126.    }
127.}

Outcomes