STM32F3 with an LSM303DLHC

I'm trying to make a compass using an LSM303DLHC embedded in STM32F3Discovery. I have prepared such a code.

void LSM303DLHCMag_Config(void)
{
LSM303DLHCMag_InitStructure.Temperature_Sensor = LSM303DLHC_TEMPSENSOR_DISABLE;
LSM303DLHCMag_InitStructure.MagFull_Scale = LSM303DLHC_FS_1_9_GA;
LSM303DLHCMag_InitStructure.MagOutput_DataRate = LSM303DLHC_ODR_200_HZ;
LSM303DLHCMag_InitStructure.Working_Mode = LSM303DLHC_CONTINUOS_CONVERSION;
LSM303DLHC_MagInit(&LSM303DLHCMag_InitStructure);
}

{
static uint8_t XYZ[6] = {0};

convertedData[0] = (float) ((XYZ[1] << 8) | (XYZ[0]))/LSM303DLHC_M_SENSITIVITY_XY_1_9Ga;
convertedData[1] = (float) ((XYZ[3] << 8) | (XYZ[2]))/LSM303DLHC_M_SENSITIVITY_Z_1_9Ga ;
convertedData[2] = (float) ((XYZ[5] << 8) | (XYZ[4]))/LSM303DLHC_M_SENSITIVITY_XY_1_9Ga;
}

uint32_t LSM303DLHC_TIMEOUT_UserCallback(void)
{
/* Block communication and all processes */
while (1)
{
}
}

int main()
{
LSM303DLHCMag_Config();
float azimuth = 0;
float convertedData[3] = {0};

while(1)
{

• IF your code gets to LSM303DLHC_TIMEOUT_UserCallback() then you have a TIMEOUT error which needs to be handled. For some reason your LSM303DLHC has not responded and you need to figure out why. – brhans May 14 '18 at 11:23
• Doesn't the read function return some sort of result? Also note that you only zero-out XYZ once at start-up, but never at each function call. And you assume that the device uses big endian (whereas ARM is little endian per default). – Lundin May 14 '18 at 14:49