Trouble getting values with I2C using HAL_Library

Former Member
Former Member

I cant get values with I2C using HAL_Library .I use HAL_I2C_Mem_Read and I cant manage ,maybe my adress wrong I didnt understand please help

int main(void)
{

HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
LCD_Init(_LCD_4BIT, _LCD_FONT_5x8, _LCD_2LINE );
Setup_Acc();

while (1)
{
//sprintf(abc,"%3.2f",sonuc1);
// LCD_Out(1,1,abc);
HAL_GPIO_WritePin(GPIOD, LED1_Pin, GPIO_PIN_SET);
HAL_I2C_Mem_Read(&hi2c1,ADXL345_deviceAdress_w,INT_SOURCE,I2C_MEMADD_SIZE_8BIT,a,1,1000);
HAL_Delay(200);
while((a[0] & 0x80) == 0)
{
HAL_Delay(200);
HAL_I2C_Mem_Read(&hi2c1,ADXL345_deviceAdress_w,INT_SOURCE,I2C_MEMADD_SIZE_8BIT,a,1,1000);
}
HAL_GPIO_WritePin(GPIOD, LED2_Pin, GPIO_PIN_SET);

HAL_I2C_Mem_Read(&hi2c1,ADXL345_deviceAdress_w,DATAXL_INC,I2C_MEMADD_SIZE_8BIT,out,2,1000);
HAL_GPIO_WritePin(GPIOD, LED3_Pin, GPIO_PIN_SET);

sonuc = (int16_t)(((uint16_t)out[0]<<8) | out[1]);
sonuc1 = sonuc*0.004F;

}
}

void Setup_Acc()
{
if(HAL_I2C_IsDeviceReady(&hi2c1,ADXL345_I2C_Adress,5,100)==HAL_OK)
{
degisken[0]=0x0D;
HAL_I2C_Mem_Write(&hi2c1,ADXL345_deviceAdress_w,BW_RATE,1,degisken,1,50);
degisken1[0]=0x08;
HAL_I2C_Mem_Write(&hi2c1,ADXL345_deviceAdress_w,POWER_CTL,1,degisken1,1,50);
}
}

Parents
  • Former Member
    0 Former Member in reply to Myy
    #define DATA_FORMAT 0x31 // 3.bit Full_Res 1 ise tam çözünürlük. 2. bit justify 0 ise sağa yaslanmış mod
    #define BW_RATE 0x2C
    #define POWER_CTL 0x2D
    #define FIFO_CTL 0x38
    #define DATAXL_INC 0xB2 // çoklu okuma için gereken ilk adres//First adress for multiple byte read
    #define DATAXL 0x32
    #define DATAXH 0x33
    #define DATAYL 0x34
    #define DATAYH 0x35
    #define DATAZL 0x36
    #define DATAZH 0x37
    #define OFSX 0x1E
    #define OFSY 0x1F
    #define OFSZ 0x20
    #define INT_ENABLE 0x2E //7.bit Data_Ready enable
    #define INT_SOURCE 0x30 //7.bit Data_Ready control

    #define ADXL345_I2C_Adress 0x1D
    #define ADXL345_I2C_Adress1 ADXL345_I2C_Adress<<1
    #define ADXL345_deviceAdress_w 0xA6
    #define ADXL345_deviceAdress_r 0xA7

    and these include files

    include "main.h"
    #include "stm32f4xx_hal.h"
    #include "stdio.h"
    #include "Character_LCD.h"
    #include "ADXL345.h"
    and
    uint8_t a[1];
    uint8_t out[2];
    uint8_t degisken[1];
    uint8_t degisken1[1];
    int16_t sonuc=0;
    float sonuc1;
Reply
  • Former Member
    0 Former Member in reply to Myy
    #define DATA_FORMAT 0x31 // 3.bit Full_Res 1 ise tam çözünürlük. 2. bit justify 0 ise sağa yaslanmış mod
    #define BW_RATE 0x2C
    #define POWER_CTL 0x2D
    #define FIFO_CTL 0x38
    #define DATAXL_INC 0xB2 // çoklu okuma için gereken ilk adres//First adress for multiple byte read
    #define DATAXL 0x32
    #define DATAXH 0x33
    #define DATAYL 0x34
    #define DATAYH 0x35
    #define DATAZL 0x36
    #define DATAZH 0x37
    #define OFSX 0x1E
    #define OFSY 0x1F
    #define OFSZ 0x20
    #define INT_ENABLE 0x2E //7.bit Data_Ready enable
    #define INT_SOURCE 0x30 //7.bit Data_Ready control

    #define ADXL345_I2C_Adress 0x1D
    #define ADXL345_I2C_Adress1 ADXL345_I2C_Adress<<1
    #define ADXL345_deviceAdress_w 0xA6
    #define ADXL345_deviceAdress_r 0xA7

    and these include files

    include "main.h"
    #include "stm32f4xx_hal.h"
    #include "stdio.h"
    #include "Character_LCD.h"
    #include "ADXL345.h"
    and
    uint8_t a[1];
    uint8_t out[2];
    uint8_t degisken[1];
    uint8_t degisken1[1];
    int16_t sonuc=0;
    float sonuc1;
Children