This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

GY-87 , reading from Hmc5883L using I2C

hi i m using nrf52832 for detection acceleration , angular speed and detection of magetic field with GY-87 ( MPU 6050 and Hmc5883). in fact i wrote a program that read only magnetic sensor only. but i didnt succeed to recover value by i2c. this is my programme any help.

enter code here

include <stdio.h> #include "boards.h" #include "app_uart.h" #include "app_error.h" #include "nrf_delay.h" #include "app_mpu.h" #include "math.h" #include "time.h" #include "nrf_drv_twi.h" #include "nrf_drv_mpu.h"

/*UART buffer size. */ #define UART_TX_BUF_SIZE 256 #define UART_RX_BUF_SIZE 1 #define Rad_to_deg 180/3.14 double compAngleX,compAngleY; #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) #define HMC5883L_REG_OUT_Z_M (0x05) #define HMC5883L_REG_OUT_Z_L (0x06) #define HMC5883L_REG_OUT_Y_M (0x07) #define HMC5883L_REG_OUT_Y_L (0x08) #define HMC5883L_REG_STATUS (0x09) #define HMC5883L_REG_IDENT_A (0x0A) #define HMC5883L_REG_IDENT_B (0x0B) #define HMC5883L_REG_IDENT_C (0x0C)

uint8_t reg[2] = { HMC5883L_REG_CONF_A, 0x70}; uint8_t reg1[2] = { HMC5883L_REG_CONF_B, 0xA0}; uint8_t reg2[2] = { HMC5883L_REG_MODE, 0x00};

uint8_t reg3[1] = {HMC5883L_REG_X_MSB }; uint8_t buff_t[6]; short combined_x; short combined_y; short combined_z; nrf_drv_twi_t m_twi_master;

/**

  • @brief UART events handler. */ static void uart_events_handler(app_uart_evt_t * p_event) { switch (p_event->evt_type) { case APP_UART_COMMUNICATION_ERROR: APP_ERROR_HANDLER(p_event->data.error_communication); break;

     case APP_UART_FIFO_ERROR:
         APP_ERROR_HANDLER(p_event->data.error_code);
         break;
    
     default:
         break;
    

    } }

/**

  • @brief UART initialization.

  • Just the usual way. Nothing special here */ static void uart_config(void) { uint32_t err_code; const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, RTS_PIN_NUMBER, CTS_PIN_NUMBER, APP_UART_FLOW_CONTROL_DISABLED, false, UARTE_BAUDRATE_BAUDRATE_Baud115200 };

    APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_events_handler, APP_IRQ_PRIORITY_LOW, err_code);

    APP_ERROR_CHECK(err_code); }

void Magneto() { uint32_t err_code; hmc5883L_init (); err_code = nrf_drv_twi_tx(&m_twi_master, HMC5883L_ADDR, reg, sizeof(reg), false); APP_ERROR_CHECK(err_code); nrf_delay_ms(2); err_code = nrf_drv_twi_tx(&m_twi_master, HMC5883L_ADDR, reg1, sizeof(reg), false); APP_ERROR_CHECK(err_code); nrf_delay_ms(2); err_code = nrf_drv_twi_tx(&m_twi_master, HMC5883L_ADDR, reg2, sizeof(reg), false); APP_ERROR_CHECK(err_code);

}

void mpu_setup(void) { ret_code_t ret_code; // Initiate MPU driver ret_code = mpu_init(); APP_ERROR_CHECK(ret_code); // Check for errors in return value

// Setup and configure the MPU with intial values
mpu_config_t p_mpu_config = MPU_DEFAULT_CONFIG(); // Load default values
p_mpu_config.smplrt_div = 19;   // Change sampelrate. Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV). 19 gives a sample rate of 50Hz
p_mpu_config.accel_config.afs_sel = AFS_2G; // Set accelerometer full scale range to 2G
ret_code = mpu_config(&p_mpu_config); // Configure the MPU with above values
APP_ERROR_CHECK(ret_code); // Check for errors in return value 

}

/**

  • @brief Function for main application entry. */ int main(void) {
    uint32_t err_code; LEDS_CONFIGURE(LEDS_MASK); LEDS_OFF(LEDS_MASK); uart_config(); printf("\033[2J\033[;HMPU simple example. Compiled @ %s\r\n", TIME); mpu_setup();

    while(1) {

     		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
     		
    
     		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);
    
     			printf("X1: %06u\r\n Y1: 06u\r\nZ1:06u\n",combined_x,combined_y,combined_z);
     		nrf_gpio_pin_toggle(LED_1);
     nrf_delay_ms(40);
    

    } } void hmc5883L_init () { uint32_t err_code;

    // Read out MPU configuration register mpu_int_pin_cfg_t bypass_config; err_code = nrf_drv_mpu_read_registers(MPU_REG_INT_PIN_CFG, (uint8_t *)&bypass_config, 1);

    // Set I2C bypass enable bit to be able to communicate with magnetometer via I2C bypass_config.i2c_bypass_en = 1; // Write config value back to MPU config register err_code = mpu_int_cfg_pin(&bypass_config);

} /** @} */

Parents Reply Children
  • I don't find HMC5883L adress. I try to set bypass for gy-87 but I can't succeed to do it. any help proposed. mpu_int_pin_cfg_t bypass_config; err_code = nrf_drv_mpu_read_registers(MPU_REG_INT_PIN_CFG, (uint8_t *)&bypass_config, 1);

    // Set I2C bypass enable bit to be able to communicate with magnetometer via I2C bypass_config.i2c_bypass_en = 1; // Write config value back to MPU config register err_code = mpu_int_cfg_pin(&bypass_config);

Related