unsigned long transfer(unsigned long value){ union ByteFourLib dataorig; uint8_t status = 10; uint8_t tx_data[2]; uint8_t rx_data[2]; uint32_t timer = 0; dataorig.bit32 = value; //HAL_Delay(1); __HAL_TIM_SET_COUNTER(&htim2,0); HAL_TIM_Base_Start(&htim2); while (__HAL_TIM_GET_COUNTER(&htim2) < 800) { //seek procesor } timer = __HAL_TIM_GET_COUNTER(&htim2); HAL_TIM_Base_Stop(&htim2); HAL_GPIO_WritePin (GPIOB, SPI2_CSB_Pin, GPIO_PIN_RESET); // pull the cs pin low for cgip select for (int i = 3; i >= 0; i--) { tx_data[0] = dataorig.bit8[i]; status = HAL_SPI_TransmitReceive(&hspi2, tx_data, rx_data, 1, 100); dataorig.bit8[i] = rx_data[0]; } HAL_GPIO_WritePin (GPIOB, SPI2_CSB_Pin, GPIO_PIN_SET); SCL3300_DATA = dataorig.bit8[1] + (dataorig.bit8[2] << 8); SCL3300_CRC = dataorig.bit8[0]; SCL3300_CMD = dataorig.bit8[3]; CalculateCRC(dataorig.bit32); if (SCL3300_CRC == CalculateCRC(dataorig.bit32)) crcerr = false; else crcerr = true; //check RS bits if ((SCL3300_CMD & 0x03) == 0x01) statuserr = false; else statuserr = true; return dataorig.bit32; } // if I call this function - it works, I get X tilt double getAngleX(void){ transfer(RdAngX); angleX = (SCL3300_DATA / 16384.) * 90.; // 16384 = 2^14 //if (angleX < 0.) angleX += 360.; if (crcerr || statuserr) return -1; } // if I call this function - it doesn´t work, I get wrong data on all axises double getAngleXYZ(void){ transfer(RdAngX); angleX = (SCL3300_DATA / 16384.) * 90.; // 16384 = 2^14 //if (angleX < 0.) angleX += 360.; if (crcerr || statuserr) return -1; transfer(RdAngY); angleY = (SCL3300_DATA / 16384.) * 90.; // 16384 = 2^14 //if (angleX < 0.) angleX += 360.; if (crcerr || statuserr) return -1; transfer(RdAngZ); angleZ = (SCL3300_DATA / 16384.) * 90.; // 16384 = 2^14 //if (angleX < 0.) angleX += 360.; if (crcerr || statuserr) return -1; }