cancel
Showing results for 
Search instead for 
Did you mean: 

AS5047u SPI Driver

kavinkumar
Associate III

So i started to learn stm32cubeide recently i decided to write a driver for AS5047u magnetic sensor using SPI and i can communicate with the IC with SPI. On reading the data i get a 16 bit value from it the first two bits are don't care bits and i have now 14 bits of data. How do i convert them into angles ? should i convert the 14bit Binary vaue into something else ??? This is the datasheet of the IC
https://look.ams-osram.com/m/48d90c982e0879e8/original/AS5047U-DS000637.pdf
 Any help would be appreciated. Thank you 

11 REPLIES 11

No

You're not supposed to be sending 16 bytes, only 2

You don't show what your sending, or how it's constructed.

Your receive looks broken, how's content finding it's way to the data array?

You should use the TransmitReceive in both cases, construct the data being sent out, discard/ignore the returned data in the first case. You use that method so you don't drive CS high too early

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
const uint16_t ANGLECOM = 0b0111111111111111;
const uint16_t NOP = 0b0100000000000000;
uint8_t data[4];
float angle = 0.0;

these are my global variables and this is my main code now 

while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
	  HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)&ANGLECOM,data, 2, HAL_MAX_DELAY);
	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
	  HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)&NOP, data, 2, HAL_MAX_DELAY);
	  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

	  uint16_t value = ((data[2]<<8) | data[3]) & 0x3FFF;
	  angle = (float)value*0.02197f;
	  HAL_Delay(250);

  }

the angle value is 180 but in the live expressions it is not changing when i rotate the motor 

kavinkumar_0-1725209023444.png