cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4Discovery ADXL345 I2C Problem

Mustafa BAKIRCIOGLU
Associate II
Posted on March 05, 2018 at 06:22

Hello;

I have started to work on I2C communication by examining adxl345 sensor. I wrote basic code to test if my code works or not. According to the ADXL345 technical documentation, the 0x00 register should return device id which is 0xE5.When I tried this register , the return value is 0. This application should be basic but I guess , I still missing something. Beside my experience, I also make a search at this community about the adxl345 problems,but I could not find answer. I would be very appreciated if you guide me in this problems. I attached my code.

&sharpinclude 'stm32f4xx.h' // Device header

&sharpinclude 'stm32f4xx_hal_conf.h' // Keil::Device:STM32Cube Framework:Classic

&sharpinclude 'stm32f4xx_hal.h' // Keil::Device:STM32Cube HAL:Common

&sharpdefine SensAddr  0x1D // 7-bit Address of device

&sharpdefine WRT (0x3A<<1) // Write

&sharpdefine RD (0x3B<<1) // Read

void SysTick_Handler(void)

{

HAL_IncTick();

HAL_SYSTICK_IRQHandler();

}

void SysClockEn();

/*System Configuration PA8-> I2C Clock , PC9-> I2C Data Lane*/

int main(){

SysClockEn();

HAL_Init();

/*------GPIO Configuration For I2C3------*/

__GPIOA_CLK_ENABLE();

GPIO_InitTypeDef *ptrB6,addrB6;

ptrB6 = &addrB6;

ptrB6->Alternate = GPIO_AF4_I2C3;

ptrB6->Pin = GPIO_PIN_8;

ptrB6->Pull = GPIO_NOPULL;

ptrB6->Speed =GPIO_SPEED_FREQ_HIGH;

ptrB6->Mode = GPIO_MODE_AF_OD;

HAL_GPIO_Init(GPIOA,ptrB6);

__GPIOC_CLK_ENABLE();

GPIO_InitTypeDef *ptrC,addrC;

ptrC = &addrC;

ptrC->Alternate =GPIO_AF4_I2C3;

ptrC->Mode =GPIO_MODE_AF_OD;

ptrC->Pin =GPIO_PIN_9;

ptrC->Pull =GPIO_NOPULL;

ptrC->Speed =GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(GPIOC,ptrC);

/*-----I2C Configurations-----*/

//__HAL_RCC_I2C3_CLK_ENABLE();

__I2C3_CLK_ENABLE();

I2C_HandleTypeDef *ptrI2C,addrI2C;

ptrI2C = &addrI2C;

ptrI2C->Instance = I2C3;

ptrI2C->Init.ClockSpeed = 100000; //100Khz

ptrI2C->Init.DutyCycle = I2C_DUTYCYCLE_2;

ptrI2C->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;

ptrI2C->Mode =HAL_I2C_MODE_MASTER;

//ptrI2C->Init.GeneralCallMode =I2C_GENERALCALL_DISABLE;

//ptrI2C->Init.NoStretchMode=I2C_NOSTRETCH_DISABLE;

HAL_I2C_Init(ptrI2C);

__HAL_I2C_ENABLE(ptrI2C);

uint8_t data=0x00;

unsigned char buffer[2];

uint8_t *buf;

unsigned char pt;

uint32_t ptr;

uint8_t val;

while(1){

val=HAL_I2C_IsDeviceReady(ptrI2C,0x1D,0xe5,1000);

pt=HAL_I2C_GetState(ptrI2C);

//HAL_I2C_Master_Transmit(ptrI2C,0x1d,0x00,1,0);

//HAL_I2C_Master_Receive(ptrI2C,0x1d,buffer,1,100);

//HAL_Delay(2);

HAL_I2C_Mem_Read(ptrI2C,

SensAddr

,0x00,1,buffer,2,1000);

ptr=HAL_I2C_GetError(ptrI2C);

}

}

void SysClockEn(){

__PWR_CLK_ENABLE();

}

#i2c #stm32f4-discovery #hal-i2c #adxl345
10 REPLIES 10
Posted on March 07, 2018 at 07:22

The Logic Analyser Result;

In the 9.th bit cycle, the master switches SDA from output to input, and leaves it floating.

The slave is supposed to pull SDA low then, as acknowledge.

This does happen nowhere, so the slave does not 'feel addressed'.