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

Reading from HMC5883L using I2C

Hi,

I am currently in the process of interfacing the HMC5883L magnetometer with my nrf52 development board. I have verified the connection to be working using a logic analyzer. Furthermore, I have configured the sensor according to another post made here on this forum:

#define HMC5883L_ADDR (0x3CU>>1)
#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)
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_master, HMC5883L_ADDR, reg, sizeof(reg), false);  
APP_ERROR_CHECK(err_code);
nrf_delay_ms(100);
err_code = nrf_drv_twi_tx(&m_twi_master, HMC5883L_ADDR, reg1, sizeof(reg), false);  
APP_ERROR_CHECK(err_code);
nrf_delay_ms(100);
err_code = nrf_drv_twi_tx(&m_twi_master, HMC5883L_ADDR, reg2, sizeof(reg), false);  
APP_ERROR_CHECK(err_code);

Currently I am trying to read the data into a buffer according to the following code

uint8_t reg3[2] = {0x3D, 0x03};
uint8_t reg4[2] = {0x3C, 0x03};

uint8_t buff_t[6];
short combined_x;
short combined_y;
short combined_z;

while(1)
{
    nrf_delay_ms(10);

    nrf_drv_twi_tx(&m_twi_master, HMC5883L_ADDR, reg3, sizeof(reg3),false); // Point to register			
    nrf_drv_twi_rx(&m_twi_master, HMC5883L_ADDR, buff_t, 6); // Read register and fill buffer		
    nrf_drv_twi_tx(&m_twi_master, HMC5883L_ADDR, reg4, sizeof(reg4),false); // Reset register			
		
    combined_x = (buff_t[0] << 8 ) | (buff_t[1] & 0xff);
    combined_y = (buff_t[2] << 8 ) | (buff_t[3] & 0xff);
    combined_z = (buff_t[4] << 8 ) | (buff_t[5] & 0xff);
}

which gives me this logic:image description

There is no data visible in the buffer and according to the logic analyzer, there is also no data received over the data pin.

Can someone help me out here?

Related