<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/89048/i2c-on-nrf52833-with-mcp4561</link><description>Hi Everyone, 
 I am trying to communicate with IC MCP4561 with NRF52833. I am not able to read and write 
 any data from the IC. The slave address of IC is 0x5C. so in the code I have modified this address as 0x2E( right shift of 0x5C). The IC basically</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 24 Aug 2023 05:03:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/89048/i2c-on-nrf52833-with-mcp4561" /><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/442864?ContentTypeID=1</link><pubDate>Thu, 24 Aug 2023 05:03:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1b3f5f11-5009-458a-a29d-87d7a29bf4d1</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;Hi Vidar,&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; I am using nrf52840. I have a variable uin16_t size , and a buffer&amp;nbsp;uint8_t Buf[250];. I want to write this data via I2C. How to create an I2c function for this? I tried to do the following way, but it is giving wrong results:-&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;bool Oled_Graphicdata(uint16_t size, uint8_t *Buff)
{

    ret_code_t err_code;

    uint8_t tx_buf[2];
	
    //Write the register address and data into transmit buffer
    tx_data[0] = ((size &amp;gt;&amp;gt; 8)&amp;amp; 0xFF );
    tx_data[1] = size &amp;amp; 0xFF;
	
    //Set the flag to false to show the transmission is not yet completed
    m_xfer_done = false;
    
    //Transmit the data over TWI Bus
    err_code = nrf_drv_twi_tx(&amp;amp;m_twi, 0x37, Buff, size, false);
    NRF_LOG_INFO(&amp;quot;oled graph&amp;quot;);
       //Wait until the transmission of the data is finished
    while (m_xfer_done == false)
    {
    }

    // if there is no error then return true else return false
    if (NRF_SUCCESS != err_code)
    {
        NRF_LOG_INFO(&amp;quot;oled graph FAILED&amp;quot;);
        return false;
    }

    return true;	
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I guess we need to add the&amp;nbsp;&lt;span&gt;Buff into tx_data. How can we do this?&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks &amp;amp; Regards,&lt;br /&gt;Snehal.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/374007?ContentTypeID=1</link><pubDate>Thu, 23 Jun 2022 15:24:11 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:11b57a28-2cec-4311-9892-7b7742e48bf6</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;If you didn&amp;#39;t already, please place a breakpoint inside the &amp;quot;NRF_DRV_TWI_EVT_DONE&amp;quot; event&amp;nbsp; transaction and let this breakpoint get hit before you read out the twim_rx_buf buffer. This event is raised when the TX RX transfer is complete.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/373570?ContentTypeID=1</link><pubDate>Wed, 22 Jun 2022 06:03:33 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0aa7fdb3-d388-4926-92d2-beabd9953837</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;Hi Vidar,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;I am reading the buffers&amp;nbsp;&lt;span&gt;twim_rx_buf. . Are we passing the values correctly??&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;img style="max-height:240px;max-width:320px;" src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/pastedimage1655877841204v1.png" alt=" " /&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I want to read register 0x00 .&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/373420?ContentTypeID=1</link><pubDate>Tue, 21 Jun 2022 11:33:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:7cb6cfee-c041-4d06-936f-1111de5d629a</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Can you please show where and how you read the RX buffer? I&amp;#39;m not familiar with the sensor you use, but I assume it is not supposed to echo back the command you send.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/373344?ContentTypeID=1</link><pubDate>Tue, 21 Jun 2022 05:56:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:99eb7ec5-f2fc-4afa-8d7b-aaa625201366</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;I guess there is something wrong going on... For reading register I did the testing with the code you provided..&lt;/p&gt;
&lt;p&gt;#define SHT3x_ADDRESS (0x2EU)&lt;br /&gt;#define SHT3x_MEAS_CMD_H {0x00, 0x0C} &lt;br /&gt; static uint8_t twim_rx_buf[2];&lt;/p&gt;
&lt;p&gt;void read_sensor_data()&lt;br /&gt;{ &lt;br /&gt; uint32_t err_code;&lt;br /&gt; static uint8_t twim_tx_buf[] = SHT3x_MEAS_CMD_H;&lt;/p&gt;
&lt;p&gt;nrf_drv_twi_xfer_desc_t xfer_desc = NRF_DRV_TWI_XFER_DESC_TXRX(&lt;br /&gt; SHT3x_ADDRESS, twim_tx_buf, sizeof(twim_tx_buf), twim_rx_buf, sizeof(twim_rx_buf));&lt;/p&gt;
&lt;p&gt;err_code = nrf_drv_twi_xfer(&amp;amp;m_twi, &amp;amp;xfer_desc, NRF_DRV_TWI_FLAG_TX_NO_STOP);&lt;br /&gt; if (err_code == NRF_ERROR_BUSY)&lt;br /&gt; {&lt;br /&gt; NRF_LOG_INFO(&amp;quot;TWIM driver is busy&amp;quot;);&lt;br /&gt; }&lt;br /&gt; else&lt;br /&gt; {&lt;br /&gt; APP_ERROR_CHECK(err_code);&lt;br /&gt; }&lt;br /&gt;}&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Now when I read the buffer&amp;nbsp;&lt;span&gt;twim_rx_buf i can see values as 0x00,0x0C.&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;If I change the&amp;nbsp;#define SHT3x_MEAS_CMD_H&amp;nbsp; as {0x00, 0x0D} and again try to read then in the&amp;nbsp;twim_rx_buf i can read 0x00,0x0D. why is this happening? Why is the command byte being read instead of the register value here??&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/373343?ContentTypeID=1</link><pubDate>Tue, 21 Jun 2022 05:30:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:182cb649-b5c8-4979-9a38-9f2f38be7dbb</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;Hi Vidar,&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Yes i checked for NACK. But it was not raised. Is my code for writing correct?&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Regards,&lt;/p&gt;
&lt;p&gt;Snehal Bundele.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/373301?ContentTypeID=1</link><pubDate>Mon, 20 Jun 2022 15:16:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ab9196e2-e64f-434a-94bb-502688dc1862</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi Snehal,&lt;/p&gt;
&lt;p&gt;You can use nrf_drv_twi_tx() to set up a write only transaction, but you should find out first if the addressed is ACKed or not. Did you check if the NRF_DRV_TWI_EVT_DATA_NACK event was raised?&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/373245?ContentTypeID=1</link><pubDate>Mon, 20 Jun 2022 12:55:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:10d5ae6e-17aa-4838-b50f-bb874c4e775c</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;Hi Vidar,&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Please reply.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thanks &amp;amp; Regards,&lt;/p&gt;
&lt;p&gt;Snehal Bundele.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/373027?ContentTypeID=1</link><pubDate>Fri, 17 Jun 2022 12:38:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ea51d4e1-6ba4-48ad-a9c5-69b389eee5b6</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;Hi Vidar,&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Thanks for sharing the code. Can you also help me with write mechanism?? How to write data along with command??&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The start bit -&amp;gt; slave address -&amp;gt; command -&amp;gt; data -&amp;gt; stop bit&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;It will be really helpful to me.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanks &amp;amp; Regards,&lt;br /&gt;Snehal.&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/373024?ContentTypeID=1</link><pubDate>Fri, 17 Jun 2022 12:29:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:76390411-9bbf-4824-bfc9-b96729795d35</guid><dc:creator>Vidar Berg</dc:creator><description>&lt;p&gt;Hi &lt;span&gt;Snehal&lt;/span&gt;,&lt;/p&gt;
&lt;p&gt;Please check if you are getting&amp;nbsp; address &amp;quot;NACKs&amp;quot; in your TWI callback when you write to your sensor like I&amp;#39;m doing in the code snippet below.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * Copyright (c) 2015 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA &amp;quot;AS IS&amp;quot; AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
/** @file
 * @defgroup tw_sensor_example main.c
 * @{
 * @ingroup nrf_twi_example
 * @brief TWI Sensor Example main file.
 *
 * This file contains the source code for a sample application using TWI.
 *
 */

#include &amp;lt;stdio.h&amp;gt;
#include &amp;quot;boards.h&amp;quot;
#include &amp;quot;app_util_platform.h&amp;quot;
#include &amp;quot;app_error.h&amp;quot;
#include &amp;quot;nrf_drv_twi.h&amp;quot;
#include &amp;quot;nrf_delay.h&amp;quot;


#include &amp;quot;nrf_log.h&amp;quot;
#include &amp;quot;nrf_log_ctrl.h&amp;quot;
#include &amp;quot;nrf_log_default_backends.h&amp;quot;

/* TWI instance ID. */
#define TWI_INSTANCE_ID     0

/* Common addresses definition for temperature sensor. */
#define SHT3x_ADDRESS                   (0x45U)
#define SHT3x_MEAS_CMD_H                {0x2c, 0x06}                       /**&amp;lt;Start high repeatability measurement with clock stretching*/

/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);


/**
 * @brief Function for handling data from temperature sensor.
 *
 * @param[in] temp          Temperature in Celsius degrees read from sensor.
 */
__STATIC_INLINE void data_handler(nrf_drv_twi_evt_t const * p_event)
{
    uint16_t      raw_value;
    float         temperature;
    uint16_t      humidity;
    
    /* Raw data conversion. We are ignoring the checksum value here */ 
    raw_value = uint16_big_decode(&amp;amp;p_event-&amp;gt;xfer_desc.p_secondary_buf[0]); /* Swap byte order to little endian */
    
    temperature = 175 * (uint16_t)raw_value / 65535.0f -45.0f;

    raw_value = uint16_big_decode(&amp;amp;p_event-&amp;gt;xfer_desc.p_secondary_buf[3]);
    humidity = 100.0f * raw_value / 65535.0f;
    
    NRF_LOG_INFO(&amp;quot;Temperature: &amp;quot;NRF_LOG_FLOAT_MARKER&amp;quot; Celsius degrees.&amp;quot;, NRF_LOG_FLOAT(temperature));
    NRF_LOG_INFO(&amp;quot;Humidity: %d%%&amp;quot;, humidity);
}

/**
 * @brief TWI events handler.
 */
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    switch (p_event-&amp;gt;type)
    {
        case NRF_DRV_TWI_EVT_DONE:
            if (p_event-&amp;gt;xfer_desc.type == NRF_DRV_TWI_XFER_TXRX)
            {
                data_handler(p_event);
            }
            break;
        case NRF_DRV_TWI_EVT_ADDRESS_NACK:
            NRF_LOG_WARNING(&amp;quot;Received NRF_DRV_TWI_EVT_ADDRESS_NACK event&amp;quot;);
            break;
        case NRF_DRV_TWI_EVT_DATA_NACK:
            NRF_LOG_WARNING(&amp;quot;Received NRF_DRV_TWI_EVT_DATA_NACK event&amp;quot;);
            break;
        default:
            break;
    }
}

/**
 * @brief UART initialization.
 */
void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_sht3x_config = {
       .scl                = ARDUINO_SCL_PIN,
       .sda                = ARDUINO_SDA_PIN,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&amp;amp;m_twi, &amp;amp;twi_sht3x_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&amp;amp;m_twi);
}

/**
 * @brief Function for reading data from temperature sensor.
 */
static void read_sensor_data()
{  
    uint32_t err_code;
    static uint8_t twim_rx_buf[6];
    static uint8_t twim_tx_buf[] = SHT3x_MEAS_CMD_H;

    nrf_drv_twi_xfer_desc_t xfer_desc = NRF_DRV_TWI_XFER_DESC_TXRX(
        SHT3x_ADDRESS, twim_tx_buf, sizeof(twim_tx_buf), twim_rx_buf, sizeof(twim_rx_buf));

    err_code = nrf_drv_twi_xfer(&amp;amp;m_twi, &amp;amp;xfer_desc, NRF_DRV_TWI_FLAG_TX_NO_STOP);
    if (err_code == NRF_ERROR_BUSY)
    {
        NRF_LOG_INFO(&amp;quot;TWIM driver is busy&amp;quot;);
    }
    else
    {
        APP_ERROR_CHECK(err_code);
    }
}

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO(&amp;quot;\r\nTWI SHT3x example started.&amp;quot;);
    NRF_LOG_FLUSH();
    twi_init();

    while (true)
    {
        nrf_delay_ms(500);
        read_sensor_data();
        NRF_LOG_FLUSH();
    }
}

/** @} */&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;You can also use the xfer API that I used in read_sensor_data() to set up a read write transaction as shown in the datasheet figure you posted.&lt;/p&gt;
&lt;p&gt;Driver documentation for reference: &lt;span&gt;&lt;a title="TWI master" href="https://infocenter.nordicsemi.com/topic/sdk_nrf5_v17.1.0/hardware_driver_twi.html?cp=8_1_2_0_17"&gt;TWI master&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;Vidar&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: I2c on NRF52833 with MCP4561</title><link>https://devzone.nordicsemi.com/thread/372908?ContentTypeID=1</link><pubDate>Fri, 17 Jun 2022 06:04:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:935cd1e4-dff8-4cf8-9488-623805e5ac52</guid><dc:creator>sne_333</dc:creator><description>&lt;p&gt;The reading mechanism in the datasheet is mentioned as follows:-&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1655445285768v1.png" alt=" " /&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>