cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 Cube and ADXL345 ACC

Michael Melamud
Associate
Posted on May 31, 2018 at 13:46

Hi,

I am trying to use STM32 cube and ADXL345 ACC using SPI , this is my first time trying , and currently without luck as i am not able to read the accelerometer 

This is my SPI init code :

void MX_SPI1_Init(void)

{

hspi1.Instance = SPI1;

hspi1.Init.Mode = SPI_MODE_MASTER;

hspi1.Init.Direction = SPI_DIRECTION_2LINES;

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi1.Init.NSS = SPI_NSS_SOFT;

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi1.Init.CRCPolynomial = 10;

if (HAL_SPI_Init(&hspi1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

And below the code i am trying to run:

ADXL345_write(ADXL345_DATA_FORMAT_ADDRESS, &data, 1); //0x31 - resetting

ADXL345_write(ADXL345_POWER_CTL_ADDRESS, &data, 1); //0x2D - resetting

ADXL345_set_bit(ADXL345_DATA_FORMAT_ADDRESS, ADXL345_DATA_FORMAT_FULL_RES_BIT); //setting 3 bit (0000 1000)?

ADXL345_set_bw(ADXL345_BW_25); //0x2C BW_Rate reg: setting 0x09 - no low power, bw 1001

ADXL345_set_range_bits(1); //setting +- 4G

ADXL345_set_bit(ADXL345_POWER_CTL_ADDRESS, ADXL345_POWER_CTL_MEAS_BIT); //0x2D setting 3 bit (0000 1000)- start measure?

And here a sample implementation of the write and read functions:

uint8_t ADXL345_write(unsigned char address, unsigned char* adata, unsigned char n_byte)

{

uint8_t status = 1;

unsigned char i;

address &= ~ADXL345_RW_FLAG;

address = (n_byte > 1) ? (address | ADXL345_BM_FLAG) : (address & ~ADXL345_BM_FLAG);

HAL_GPIO_WritePin (ADC_CNV_GPIO_Port, Acc_SC_Pin, GPIO_PIN_SET);

AD7689_delay();

status = HAL_SPI_Transmit(&hspi1, &address, 1, 1000);

if ( status != HAL_OK )

{

return 1;

}

for(i = 0; i < n_byte; i++)

{

status = HAL_SPI_Transmit(&hspi1, adata +i , 1, 1000);

if ( status != HAL_OK )

{

return 1;

}

}

HAL_GPIO_WritePin (ADC_CNV_GPIO_Port, Acc_SC_Pin, GPIO_PIN_RESET);

return 0;

}

uint8_t ADXL345_read(unsigned char address, unsigned char* adata, unsigned char n_byte)

{

uint8_t status = 1;

unsigned char i = 0;

//Wire.requestFrom((int)ADXL345_ADDRESS, (int)n_byte);

address |= ADXL345_RW_FLAG;

address = (n_byte > 1) ? (address | ADXL345_BM_FLAG) : (address & ~ADXL345_BM_FLAG);

HAL_GPIO_WritePin (ADC_CNV_GPIO_Port, Acc_SC_Pin, GPIO_PIN_SET);

AD7689_delay();

status = HAL_SPI_Transmit(&hspi1, &address, 1, 1000);

if ( status != HAL_OK )

{

return 1;

}

while(i < n_byte)

{

status = HAL_SPI_Receive(&hspi1, adata + i, 1, 1000 );

if ( status != HAL_OK )

{

return 1;

}

i++;

}

HAL_GPIO_WritePin (ADC_CNV_GPIO_Port, Acc_SC_Pin, GPIO_PIN_RESET);

if(i == n_byte)

{

return 0;

}

else

{

return 1;

}

}

any help will be greatly appreciated.

Thank,

Michael

#adxl345
0 REPLIES 0