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

I²C communication between a nrf5240 and a MCP3221

Hello, 

I would like to implement an I²C communication between a nrf52840 and a MCP3221 (which is an ADC). 

The first 7-bit of the MCP3121 are 0b1001101. I guess the slave address is 0x4D. 

I developed the following function in order to communication with the MCP3221. 

ret_code_t I2C_register_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t * pdata, uint32_t bytes)
{
ret_code_t ret_code;
ret_code = nrf_drv_twi_tx(p_twi_master,slave_addr, &reg_addr,1,false);
if(ret_code != NRF_SUCCESS)
{
return ret_code;
}

ret_code = nrf_drv_twi_rx(p_twi_master,slave_addr, pdata, bytes);

return ret_code;
}

I think that slave_addr=0x4D but I don't know what reg_addr to use. 

When I read the MCP3121 datasheet, it appears that there is no register. So what register address (reg_addr) am I suppoed to use ? Shall I use other TWI function ? 

Thank you for your help ! 

  • From the MCP3121 datasheet, "The eighth bit of the slave address determines if the master device wants to read conversion data or write to the MCP3021. When set to a 1, a read operation is selected. When set to a 0, a write operation is selected. There are no writable registers on the MCP3021, therefore, this bit must be set to a 1 to initiate a conversion."


    ret_code = nrf_drv_twi_rx(p_twi_master,slave_addr, pdata, bytes);

    should be sufficient.

  • Hello,

    In addition to Prasad's comment I would like to ask: are you checking the error codes returned by I2C_register_read?
    The error codes returned by SDK functions should always be checked, for example by passing them to an APP_ERROR_CHECK.
    Are you getting any non-NRF_SUCCESS errors from either call to nrf_drv_twi_tx or nrf_drv_twi_rx? If so, which one?

    You should also try to run the TWI Scanner example from the SDK to verify that your hardware connections is correct. The example will scan through all devices on the TWI bus and report any found devices in the log. If all your hardware connections are correct and the TWI Slave is operating as intended it will be found here.

    Please also make sure that you have DEBUG defined in your preprocessor defines, like shown in the included image.

    This will make a detailed error message be output by your logger in the case that a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.

    For future reference, please use the Insert -> Code option when sharing code here on DevZone.

    Best regards,
    Karl

Related