0
\$\begingroup\$

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);
}

void LSM303DLHCMag_Read(float *convertedData)
{
    static uint8_t XYZ[6] = {0};
    LSM303DLHC_Read(MAG_I2C_ADDRESS,LSM303DLHC_OUT_X_H_M, XYZ, 6);

    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)
    {
        LSM303DLHCMag_Read(*convertedData);
        azimuth = atan2(convertedData[2],convertedData[0])*180.0/M_PI;
    }
}

My problem is that the code executes once only. What should I add to the UserCallback function, if I should at all?

\$\endgroup\$
  • \$\begingroup\$ Why have you added while(1) loop in the UserCallback function? and when this Callback happens? \$\endgroup\$ – charansai May 14 '18 at 8:01
  • \$\begingroup\$ Without this callback compilation is impossible. I don't know why. I don't know what is this function responsible for \$\endgroup\$ – Y. Markov May 14 '18 at 9:07
  • \$\begingroup\$ K then you have to provide the reference from where you have found this code so we can help you further! \$\endgroup\$ – charansai May 14 '18 at 10:12
  • \$\begingroup\$ 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. \$\endgroup\$ – brhans May 14 '18 at 11:23
  • \$\begingroup\$ 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). \$\endgroup\$ – Lundin May 14 '18 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.