1

I'm trying to get data from the lis3dsh accelerometer (STM32f4discovery board). I use HAL library. As far as I understood, the incoming data values from the accelerometer might be negative value (int type), but HAL library function operates with uint type

HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)    
{    
......    
}

There is the same situation with other functions for SPI. And USART as well. How one is supposed to recieve and transmit signed values&

mkom
  • 103
  • 1
  • 1
  • 8
  • SPI transmit and receive bit streams. Looking at the function header the function expects 8bits. Signed and unsigned is only matter for the higher level function that uses the values, you need type casting in between. – Thanushan Mar 11 '16 at 12:15

1 Answers1

0

I had an experience with MPU6050 accelerometer(STM32F0). As Thanushan Balakrishnan puts in his comment 'SPI transmit and receive bit streams. Looking at the function header the function expects 8bits.' If you are trying to get data from accelerometer, probably you use bit shift operations. For bit shifting operations, you should avoid signed values because of sign extension.Thus, you should assign the value from sensor to an unsigned type Rx Buffer. After bit shifting and or'ing,choose the output type as a signed value.That signed type output will produce the negative and positive values for you. Here is an example about what will be the data types for when a data is received from an IMU(It is made by I2C) :

  uint8_t RxData[]; //Variable to be assigned for sensor values by SPI 
  int16_t Accel_X_Out;  //Output
  HAL_SPI_...(//arguments)
{
Accel_X_Out=((RxData[0] << 8) | RxData[1]);

}

If there is a need for more information you can have a look to links. Working with signed types: What does AND 0xFF do? Sign Extension: https://en.wikipedia.org/wiki/Sign_extension

Community
  • 1
  • 1
B. Canturk
  • 21
  • 1