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
  • this is the exemple of nordic sdk 13.0 (twi scanner), and it works well with this configuration. and i get the two adress of my mpu6050 and the bmp 180 ( which is 0x68 and 0x77) , but the magnetometer didnt works , what i tri to do it here is to set the bypass of my mpu6050 i2c enable which activate the magnetometer and then i can get magnetic field components.

    the problme is that when i use this code i get fatal error via the uart and i didnt understand why because it seems correct.

    Heading

    uint8_t packet[2] = {0x37,2};
    ret_code_t err_code;
    err_code = nrf_drv_twi_tx(&m_twi,(0x1E),packet,sizeof(packet),false);
    APP_ERROR_CHECK(err_code);
    
  • I have tested these adress and they work well but i want to activate in this program the bypass of mpu6050 to detect adress of magnetometer which is 0x1E, the activation of this bypass made by writting one byte into the register 0x37 and then i can detect magnetometer device and we can get magnetic component but i didn't to activate it and I think the problem is in the TWI connection or the TWI command. The major problem is that function worked very well with other device mpu6050 (0x68 0x77)

Reply
  • I have tested these adress and they work well but i want to activate in this program the bypass of mpu6050 to detect adress of magnetometer which is 0x1E, the activation of this bypass made by writting one byte into the register 0x37 and then i can detect magnetometer device and we can get magnetic component but i didn't to activate it and I think the problem is in the TWI connection or the TWI command. The major problem is that function worked very well with other device mpu6050 (0x68 0x77)

Children
No Data
Related