Mustafa
Mustafa

Reputation: 147

STM32F4 ADXL345 I2C communication

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.

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();

}

Upvotes: 0

Views: 1079

Answers (1)

Guillaume Michel
Guillaume Michel

Reputation: 1204

The documentation of the sensor says:

the 7-bit I2C address for the device is 0x1D

So in your code your should write:

#define SensAddr (0x1D<<1)
...
HAL_I2C_Mem_Read(ptrI2C,SensAddr,0x00,1,buffer,2,1000);
...

This is because ST HAL considers the 7 bit address left shifted.

The documentation also says:

An alternate I2C address of 0x53 (followed by the R/W bit) can be chosen by grounding the SDO/ALT ADDRESS

If this is the case of your hardware, change the code to:

#define SensAddr (0x53<<1)
...
HAL_I2C_Mem_Read(ptrI2C,SensAddr,0x00,1,buffer,2,1000);
...

Upvotes: 1

Related Questions