ADXL3555 Accelerometer with STM32F4
Hi everyone,
I am trying to bring in sensor synchronization for accurate data acquisition in a wireless sensor network built using STM32F407 Discovery.
ADXL355 Digital accelerometer (shown below ) provides full external sync by applying an external clock (enabled via the EXT_CLK bit) at 1.024 MHz on the INT2 pin (Pin 13) and an external synchronization signal, SYNC, on the DRDY pin (Pin 14). The pulse width of the SYNC signal must be at least 3.91 μs, which represents four cycles of the external clock
(4 ÷ 1.024 MHz = ~3.91 μs).
I used (Digilent) Analog Discovery 2 to provide an external clock 1.024 MHz and a SYNC signal 256 KHz ( 1.024 MHz / 4 ).
When I tried this I can not get any response from the sensor but it worked good with internal clock.
Can anyone help me to fix this problem?
My functions that I used is found below :backhand_index_pointing_down:
ADXL355_Result Device_ID(SPI_HandleTypeDef* spix,GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
{
/****************** Check the Analog Devices ID, 0xAD *****************************************/
uint8_t DEVID_AD = (uint8_t)(ADXL355_DEVID_AD << 1 ) | 0x01;
uint8_t temp = 0;
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_RESET);
if(HAL_SPI_Transmit(spix, (uint8_t *)&DEVID_AD, 1, 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
// Receive multiple byte
if(HAL_SPI_Receive(spix, &temp, 1, 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_SET);
// Checking
while(temp != ADXL355_ID)
{
// Return error
return ADXL355_Result_DeviceInvalid;
}
return ADXL355_Result_Ok;
}
ADXL355_Result Dev_SYNC(SPI_HandleTypeDef* spix, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin , uint16_t SYNC_Type )
{
uint8_t d[2];
SPI_HandleTypeDef* Handle = spix;
/// Format array to send
d[0] = (uint8_t )(ADXL355_Sync<<1) | 0x00;
d[1] = (0x00 | SYNC_Type ) ;
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_RESET);
if(HAL_SPI_Transmit(Handle, (uint8_t *)d, 2 , 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_SET);
// Return OK
return ADXL355_Result_Ok;
}
ADXL355_Result ADXL355_SetDataRate(SPI_HandleTypeDef* spix, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin , uint8_t rate)
{
uint8_t d[2];
SPI_HandleTypeDef* Handle = spix;
/// Format array to send
d[0] = (uint8_t )(ADXL355_Filter<<1) | 0x00;
d[1] = rate;
// Set data sample rate
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_RESET);
if(HAL_SPI_Transmit(Handle, (uint8_t *)d, 2 , 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_SET);
// Return OK
return ADXL355_Result_Ok;
}
ADXL355_Result ADXL355_SetAccelerometer(SPI_HandleTypeDef* spix, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin ,ADXL355* DataStruct, ADXL355_Accelerometer AccelerometerSensitivity)
{
uint8_t temp;
SPI_HandleTypeDef* Handle = spix;
uint8_t regAdd =(uint8_t )ADXL355_Range;
uint8_t d[2];
uint8_t Add =(uint8_t )(ADXL355_Range<<1) | 0x01;
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_RESET);
/* Config accelerometer */
while(HAL_SPI_Transmit(Handle, (uint8_t *)&Add, 1, 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
while(HAL_SPI_Receive(Handle, (uint8_t *)&temp, 1, 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
temp = (temp & 0xFC) | (uint8_t)AccelerometerSensitivity ;
d[0] = (regAdd<<1) | 0x00;
d[1] = temp;
while(HAL_SPI_Transmit(Handle, (uint8_t *)d, 2 , 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_SET);
/* Set sensitivities for multiplying gyro and accelerometer data */
switch (AccelerometerSensitivity) {
case ADXL355_Accelerometer_2G:
DataStruct->Acce_Mult = (float)1 / ADXL355_ACCE_SENS_2;
break;
case ADXL355_Accelerometer_4G:
DataStruct->Acce_Mult = (float)1 / ADXL355_ACCE_SENS_4;
break;
case ADXL355_Accelerometer_8G:
DataStruct->Acce_Mult = (float)1 / ADXL355_ACCE_SENS_8;
break;
default:
break;
}
/* Return OK */
return ADXL355_Result_Ok;
}
ADXL355_Result ADXL355_START_meas(SPI_HandleTypeDef* spix, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin )
{
uint8_t d[2];
SPI_HandleTypeDef* Handle = spix;
/// Format array to send
d[0] = (uint8_t )(ADXL355_POWER_CTL<<1) | 0x00;
d[1] = 0x00 ;
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_RESET);
if(HAL_SPI_Transmit(Handle, (uint8_t *)d, 2 , 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_SET);
// Return OK
return ADXL355_Result_Ok;
}
ADXL355_Result ADXL355_ReadAcc_FIFO(SPI_HandleTypeDef* spix, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin ,ADXL355* DataStruct , bool x)
{
uint8_t data[9] = {0, 0, 0, 0, 0, 0, 0, 0 , 0};
uint8_t reg = (uint8_t )(ADXL355_FIFO_DATA<<1) | 0x01;
SPI_HandleTypeDef* Handle = spix;
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_RESET);
/* Config accelerometer */
while(HAL_SPI_Transmit(Handle, (uint8_t *)®, 1, 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
while(HAL_SPI_Receive(Handle, (uint8_t *)data, 9, 100) != HAL_OK)
{
return ADXL355_Result_Error;
}
HAL_GPIO_WritePin(GPIOx, GPIO_Pin , GPIO_PIN_SET);
DataStruct->Accelerometer_X = ((uint32_t)data[0] << 12) | ((uint32_t)data[1] << 4) | ((uint32_t)data[2] >> 4);
DataStruct->Accelerometer_Y = ((uint32_t)data[3] << 12) | ((uint32_t)data[4] << 4) | ((uint32_t)data[5] >> 4);
DataStruct->Accelerometer_Z = ((uint32_t)data[6] << 12) | ((uint32_t)data[7] << 4) | ((uint32_t)data[8] >> 4);
if (DataStruct->Accelerometer_X & 0x80000) {
DataStruct->Accelerometer_X = (DataStruct->Accelerometer_X & 0x7ffff) - 0x80000;
}
if (DataStruct->Accelerometer_Y & 0x80000) {
DataStruct->Accelerometer_Y = (DataStruct->Accelerometer_Y & 0x7ffff) - 0x80000;
}
if (DataStruct->Accelerometer_Z & 0x80000) {
DataStruct->Accelerometer_Z = (DataStruct->Accelerometer_Z & 0x7ffff) - 0x80000;
}
ADXL_AX = (float)DataStruct->Accelerometer_X*DataStruct->Acce_Mult;
ADXL_AY = (float)DataStruct->Accelerometer_Y*DataStruct->Acce_Mult;
ADXL_AZ = (float)DataStruct->Accelerometer_Z*DataStruct->Acce_Mult;
//* Return OK
return ADXL355_Result_Ok;
}
