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

twi, mpu650 + hmc5883l

HI, i m beginner in TWI with nrf52832. i want to write a bit in a registre of hmc5883l but i didnt succeed. what i want to do is :

writeByte(devAddr, regAddr, b) ==> ( 0x1E,0x37,1)

tgis my own code that i tried but i didn't understand why is not working! help if u can

#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"

#define NRF_LOG_MODULE_NAME "APP"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"

/* TWI instance ID. */
#define TWI_INSTANCE_ID     0

 /* Number of possible TWI addresses. */
 #define TWI_ADDRESSES      127

/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

/**
 * @brief TWI initialization.
 */
void twi_init (void)
{
    ret_code_t err_code;

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

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}


/**
 * @brief Function for main application entry.
 */
bool writeBit()
{
		
		uint8_t packet[1] = {0x37};
		ret_code_t err_code;
		err_code = nrf_drv_twi_tx(&m_twi,(0x1E),packet,sizeof(packet),false);
		APP_ERROR_CHECK(err_code);


   return err_code;
}

int main(void)
{
    ret_code_t err_code;
    uint8_t address;
    uint8_t sample_data;
		uint8_t data;
    bool detected_device = false;

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_INFO("TWI scanner.\r\n");
    NRF_LOG_FLUSH();
    twi_init();
	if(writeBit()==true)
			{
				NRF_LOG_INFO("No hmc5883l device was found.\r\n");
        NRF_LOG_FLUSH();
			}
    for (address = 1; address <= TWI_ADDRESSES; address++)
    {
        err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
        if (err_code == NRF_SUCCESS)
        {
            detected_device = true;
            NRF_LOG_INFO("TWI device detected at address 0x%x.\r\n", address);
        }
        NRF_LOG_FLUSH();
    }

    if (!detected_device)
    {
        NRF_LOG_INFO("No device was found.\r\n");
        NRF_LOG_FLUSH();
    }

    while (true)
    {
        /* Empty loop. */
    }
}
Parents
  • Hi @omar,

    I think you are setting the bypass bit wrong. You need to write a '1' to the I2C_BYPASS_EN bit in the INT_PIN_CFG register in the MPU6050, not in the magnetometer. In other words, try something like this:

    bool writeBit()
    {
            uint8_t packet[2] = {0x37, 2}; // <- WRITE THE VALUE 2 TO REGISTER 0x37
            ret_code_t err_code;
            err_code = nrf_drv_twi_tx(&m_twi,0x68,packet,sizeof(packet),false); // <- USE THE ADDRESS OF THE MPU
            APP_ERROR_CHECK(err_code);
    
       return err_code;
    }
    

    After doing this you should be able to get a response from the magnetometer by using the twi_scanner example in our SDK.

  • The three chip are included in the same card, we use gyro 87 and the mpu6050 is actived by default but the HMC5883L need the bypass active to get the communication with it. The problem is resolved and now I get communication with all chips, we get library for NRF51 that simplify the communication TWI so we have updated it to NRF52 and we use it, one library for both MPU6050 and HMC5883L.

Reply
  • The three chip are included in the same card, we use gyro 87 and the mpu6050 is actived by default but the HMC5883L need the bypass active to get the communication with it. The problem is resolved and now I get communication with all chips, we get library for NRF51 that simplify the communication TWI so we have updated it to NRF52 and we use it, one library for both MPU6050 and HMC5883L.

Children
No Data
Related