简体   繁体   English

STM32F4 ADXL345 I2C通讯

[英]STM32F4 ADXL345 I2C communication

I have started to work on I2C communication by examining adxl345 sensor. 我已经开始通过检查adxl345传感器来进行I2C通信。 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. 根据ADXL345技术文档,0x00寄存器应返回设备ID,即0xE5。当我尝试该寄存器时,返回值为0。此应用程序应该是基本的,但我想我仍然缺少一些东西。 Beside my experience, I also make a search at this community about the adxl345 problems,but I could not find answer. 除了我的经验外,我还在这个社区中搜索了adxl345问题,但找不到答案。 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();

}

The documentation of the sensor says: 传感器的文档说:

the 7-bit I2C address for the device is 0x1D 设备的7位I2C地址为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. 这是因为ST HAL认为左移了7位地址。

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 可以通过将SDO / ALT地址接地来选择备用I2C地址0x53(紧随其后的R / W位)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM