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. */
    }
}
Related