cancel
Showing results for 
Search instead for 
Did you mean: 

Reading temperature from the LSM303AGR.

BMart.2
Associate II

I am initializing the 303 with the following code:

 /* Turn on accelerometer */

 WriteRegister(LSM303AGR_CTRL_REG1_A, LSM303AGR_ADDR_WRITE_A, 0x57);

 x = ReadRegister(LSM303AGR_CTRL_REG1_A, LSM303AGR_ADDR_READ_A);

  

 /* Turn on temp sensor and set BDU bit */

 WriteRegister(LSM303AGR_TEMP_CFG_REG_A, LSM303AGR_ADDR_WRITE_A, 0xc0);

 x = ReadRegister(LSM303AGR_TEMP_CFG_REG_A, LSM303AGR_ADDR_READ_A);

 WriteRegister(LSM303AGR_CTRL_REG4_A, LSM303AGR_ADDR_WRITE_A, 0x80);

 x = ReadRegister(LSM303AGR_CTRL_REG4_A, LSM303AGR_ADDR_READ_A);

In the mainline code I call the following:

t1 = Read16BitRegister(LSM303AGR_OUT_X_L_A, LSM303AGR_ADDR_READ_A);

where Read16Bit function reads 2 consecutive registers. I have also tried ReadRegister(LSM303AGR_REFERENCE_A, LSM303AGR_ADDR_READ_A);

where LSM303AGR_REFERENCE_A = 0x26h. No matter which way I do this the value(s) come back 0. I know these functions work because I can operate the part, acc, mag, all registers as per the datasheet. I just cannot get any data back from the temperature register.

Any help would be appreciated.

Thanks

6 REPLIES 6
Eleon BORLINI
ST Employee

Hi @BMart.2​ , are you sure you are communicating well with the sensor? I mean, are you well doing the read/write of the registers (for example if you are able to read well the WHO_AM_I_A (0Fh) register? And are you sure your multiple byte R/W is ok (and you are not reading only 1 temperature register)? Please consider also the github C drivers for LSM303AGR (especially the lsm303agr_reg.c file) to check your code.

  1. Setting the temperature sensor
/**
  * @brief  Temperature sensor enable.[set]
  *
  * @param  ctx    Read / write interface definitions.(ptr)
  * @param  val    Change the values of temp_en in reg TEMP_CFG_REG_A.
  * @retval        Interface status (MANDATORY: return 0 -> no Error).
  *
  */
int32_t lsm303agr_temperature_meas_set(stmdev_ctx_t *ctx,
                                       lsm303agr_temp_en_a_t val)
{
  lsm303agr_temp_cfg_reg_a_t temp_cfg_reg_a;
  int32_t ret;
 
  ret = lsm303agr_read_reg(ctx, LSM303AGR_TEMP_CFG_REG_A,
                           (uint8_t*)&temp_cfg_reg_a, 1);
  if(ret == 0){
    temp_cfg_reg_a.temp_en = (uint8_t)val;
    ret = lsm303agr_write_reg(ctx, LSM303AGR_TEMP_CFG_REG_A,
                              (uint8_t*)&temp_cfg_reg_a, 1);
  }
 
  return ret;
}
 
/**
  * @brief  Temperature sensor enable.[get]
  *
  * @param  ctx    Read / write interface definitions.(ptr)
  * @param  val    Get the values of temp_en in reg TEMP_CFG_REG_A.(ptr)
  * @retval        Interface status (MANDATORY: return 0 -> no Error).
  *
  */
int32_t lsm303agr_temperature_meas_get(stmdev_ctx_t *ctx,
                                      lsm303agr_temp_en_a_t *val)
{
  lsm303agr_temp_cfg_reg_a_t temp_cfg_reg_a;
  int32_t ret;
 
  ret = lsm303agr_read_reg(ctx, LSM303AGR_TEMP_CFG_REG_A,
                           (uint8_t*)&temp_cfg_reg_a, 1);
  switch (temp_cfg_reg_a.temp_en){
    case LSM303AGR_TEMP_DISABLE:
      *val = LSM303AGR_TEMP_DISABLE;
      break;
    case LSM303AGR_TEMP_ENABLE:
      *val = LSM303AGR_TEMP_ENABLE;
      break;
    default:
      *val = LSM303AGR_TEMP_DISABLE;
      break;
  }
 
  return ret;
}

  1. Decoding the temperature sensor data
// Normal mode
float_t lsm303agr_from_lsb_nm_to_celsius(int16_t lsb)
{
  return ( ( (float_t)lsb / 64.0f ) / 4.0f ) + 25.0f;
}
 
// Low power mode
float_t lsm303agr_from_lsb_lp_to_celsius(int16_t lsb)
{
  return ( ( (float_t)lsb / 256.0f ) * 1.0f ) + 25.0f;
}

Regards

Hi Eleon,

Yes, I can read/write with the same routing to all the magnetometer registers, make the part work, write hard iron registers, read dual registers with no problem. I can read who am I from both the mag and acc no issues. It's just when I read the temp data registers that I'm getting back 0 even though the setup register read backs are per the datasheet. I'm attaching some scope shots showing the init(multiple images due to the long length) and an image of the actual read. I read the status register first and then read the 2 temp data registers. As a side note, I'm not able to read the acc values though, should get 0,0,1 but it appears I'm getting stuff like 0x80, 0x80 for x low and high. May be related. But I can read/write all the mag registers with the same routine with no issue.

0693W000001rkzFQAQ.png0693W000001rkzAQAQ.png0693W000001rkz5QAA.png

BMart.2
Associate II

Some more data to follow up on my recent answer. I'm reading the mag data registers and the acc regs and temp regs here with 100ms loop delay.

0693W000001rl32QAA.png0693W000001rl2oQAA.png

ColdWeather
Senior

You want to read the temperature but the function has "LSM303AGR_OUT_X_L_A" as a parameter. Why?

Hi @BMart.2​ , referring to this last picture, are you maybe reading 3Ch and 3Dh (TIME_LATENCY_A and TIME_WINDOW_A) instead of OUT_TEMP_H_A (0Dh) + OUT_TEMP_L_A (0Ch) registers? Regards

msavi.11
Associate II

Hello, I have another problem, I do every steps like wrote in datasheet, but problem with reading temperature is still here. Accelerometer and magnetometer measures are true, but temperature is 0, what cuold be the problem??