This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf52 dk use twi_sensor example exchange it to hmc5883l not work.

Hi,

I wish to exchange the sensor form mma7660 to hmc5883l, so I simply replace it with my own function just like mma7660 did. Those function are

define HMC5883L_ADDR (0x1E)

define HMC5883L_REG_CONF_A (0x00) define HMC5883L_REG_CONF_B (0x01) define HMC5883L_REG_MODE (0x02) define HMC5883L_REG_X_MSB (0x03) define HMC5883L_REG_X_LSB (0x04) ...

static const nrf_drv_twi_t m_twi_hmc_5883l = NRF_DRV_TWI_INSTANCE(0);

void twi_hmc_5883l_init (void) { ret_code_t err_code;

const nrf_drv_twi_config_t twi_hmc_5883l_config = {
   .scl                = ARDUINO_SCL_PIN,
   .sda                = ARDUINO_SDA_PIN,
   .frequency          = NRF_TWI_FREQ_100K,
   .interrupt_priority = APP_IRQ_PRIORITY_HIGH
};

err_code = nrf_drv_twi_init(&m_twi_hmc_5883l, &twi_hmc_5883l_config, twi_hmc_5883l_handler, NULL);
APP_ERROR_CHECK(err_code);

nrf_drv_twi_enable(&m_twi_hmc_5883l);

}

void HMC5883L_set_mode(void) { ret_code_t err_code;

	uint8_t reg[3] = {0x3C, HMC5883L_REG_CONF_A, 0x70};
	uint8_t reg1[3] = {0x3C, HMC5883L_REG_CONF_B, 0xA0};
	uint8_t reg2[3] = {0x3C, HMC5883L_REG_MODE, 0x00};
err_code = nrf_drv_twi_tx(&m_twi_hmc_5883l, HMC5883L_ADDR, reg, sizeof(reg), false);  
APP_ERROR_CHECK(err_code);
	nrf_delay_ms(100);
err_code = nrf_drv_twi_tx(&m_twi_hmc_5883l, HMC5883L_ADDR, reg1, sizeof(reg), false);  
APP_ERROR_CHECK(err_code);
	nrf_delay_ms(100);
	err_code = nrf_drv_twi_tx(&m_twi_hmc_5883l, HMC5883L_ADDR, reg2, sizeof(reg), false);  
APP_ERROR_CHECK(err_code);
while(m_set_mode_done == false);

}

and I can get callback NRF_DRV_TWI_EVT_DONE,

  1. Why the mma7660 address use (0x98U >> 1) in define, I will not get NRF_DRV_TWI_EVT_DONE if I define hmc5883l as (0x1EU >> 1)?

  2. the other question is: After I get callback and I wish to print my data,but it's all zero!!!

That confuse me....

I need help thx a lot.

  • Yes @LJohn. If I remember it should return something back to u after u change that reg. I didn't verify the data on reg is correct or not, just make sure there're value changed, so u must check that urself.

  • The values should be between -2048 and 2047 according to the datasheet. When using reg = 0x06 the values were between -45000 and +45000, so I figured I may be reading the wrong register. I changed reg = 0x03 now and it gives me values between -400 and +400, which I believe depends on the gain, but at least it is withing the range limitations. Only problem I experiencing now is that the Y-axis seems to give a significantly stronger magnetic field value than the others (Y-axis about 400, X and Z-axis about 50).

Related