<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Problems Sequential reading channels ADC MCP3424 via I2C]]></title><description><![CDATA[<p dir="auto">Hello, I am Alexander and probing a lot with Rexygen, with good results until now. However, at this moment I am trying to read data with the Rexlang block (I2C communication) from an ADC (analog-digital converter) MCP3424, without succes. I am relatively new with respect to the Rexlang block and i2c. I know that I have to send a not-acknowledge (NAK) bit and a stop bit to exit the current read operation and send a new read command for the latest conversion data. However I don't know how to do that.</p>
<p dir="auto">Hopefully somebody is willing to help me with my problem.<br />
Many thanks in advance.</p>
<p dir="auto">Below my code:</p>
<pre><code>/************************************************************
*
* REXLANG - Reading data from MCP3424 AD converter via I2C
*
*************************************************************/

string parameter(0) i2c_dev; // the I2C bus is defined by the p0 parameter

//assigning variables to outputs, these variables are WRITE-ONLY
long output(0) channel1; // output of value of ADC channel 1
long output(1) channel2; // output of value of ADC channel 2
long output(2) channel3; // output of value of ADC channel 3
long output(3) channel4; // output of value of ADC channel 4

//declaration of variables
long i2c_bufTx[3]; //buffer for transmitting data
long i2c_bufRx[3]; //buffer for receiving data
long i2c_bus_handle;
long i2c_chip_address;
long i2c_write_count;
long i2c_read_count;
long i2c_ret_fun;

//the init procedure is executed once when the REXLANG function block initializes
int init(void)
{
  i2c_bus_handle = OpenI2C(i2c_dev); // open I2C bus
  /* Address: 1 1 0 1 A2 A1 A0 */
  i2c_chip_address = 0x68; // 7-bit address of the I2C device
  
  return 0;
}

//the main procedure is executed once in each sampling period
long main(void)
{
  /* Configuration register: 
     =====================================================================================
     | bit 7                | bit 6-5        | bit 4          | bit 3-2     | bit 1-0      |
     -------------------------------------------------------------------------------------
     | 1 = start conversion | 00 = channel 1 | 1 = continuous | 00 = 12-bit | 00 = gain x1 |
     | 0 = no effect        | 01 = channel 2 | 0 = one-shot   | 01 = 14-bit | 01 = gain x2 |
     |                      | 10 = channel 3 |                | 10 = 16-bit | 10 = gain x4 |
     |                      | 11 = channel 4 |                | 11 = 18-bit | 11 = gain 8x |
     =====================================================================================
  */
  i2c_bufTx[0] = 0x90; // read channel 1, continuous, 12bit, gain 1 (see MCP3424 datasheet)
  i2c_write_count = 1;
  i2c_read_count = 2;
  //Sending data via I2C
  i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);
  channel1 = ((i2c_bufRx[0]&lt;&lt;8) + i2c_bufRx[1])/2;
  
  return  0;

  i2c_bufTx[0] = 0xB0; // read channel 2 continuous, 12bit, gain 1 (see MCP3424 datasheet)
  i2c_write_count = 1;
  i2c_read_count = 2;
  //Sending data via I2C
  i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);
  channel2 = ((i2c_bufRx[0]&lt;&lt;8) + i2c_bufRx[1])/2;
  
  return 0;
}

//the exit procedure is executed once when the task is correctly terminated
// (system shutdown, downloading new control algorithm, etc.)
long exit(void)
{
  if(i2c_bus_handle&gt;=0) Close(i2c_bus_handle); // close I2C bus
  return 0;
}</code></pre>
]]></description><link>https://forum.rexygen.com/topic/620/problems-sequential-reading-channels-adc-mcp3424-via-i2c</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 19:37:57 GMT</lastBuildDate><atom:link href="https://forum.rexygen.com/topic/620.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 10 Jan 2026 14:49:00 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problems Sequential reading channels ADC MCP3424 via I2C on Tue, 10 Feb 2026 09:59:48 GMT]]></title><description><![CDATA[<p dir="auto">Hi everyone,</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.rexygen.com/uid/231">@AlexanderH</a> solved his MCP3424 multi-channel reading issue! Here's the key takeaway:</p>
<h2>The Problem</h2>
<p dir="auto">When reading multiple channels sequentially from MCP3424, <strong>conversion time must be respected</strong>. Each configuration byte starts a new conversion:</p>
<ul>
<li><strong>12-bit</strong>: ~5ms (1/240 s)</li>
<li><strong>18-bit</strong>: ~267ms (1/3.75 s)</li>
</ul>
<p dir="auto"><strong>Table 4.3 from MCP3424 datasheet</strong> lists all data rates by resolution.</p>
<h2>The Solution</h2>
<p dir="auto"><strong>One-shot mode + proper timing between configure/read operations:</strong></p>
<p dir="auto"><strong>For 12-bit (2 channels):</strong></p>
<pre><code class="language-c">// Channel 1 - configure + convert + read
i2c_bufTx[0] = 0x80; // CH1, one-shot, 12-bit, gain x1  
i2c_write_count = 1;
i2c_read_count = 0;
i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);

Sleep(0.01); // wait for 10 ms before reading the first channel 
             // Data rate: 12bit = 240 SPS, 14bit = 60 SPS, 16bit = 15 SPS, 18bit = 3.75 SPS

i2c_write_count = 0;
i2c_read_count = 3;
i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);
// i2c_bufRx[2] contains configuration byte
channel1 = ((i2c_bufRx[0]&lt;&lt;8) + i2c_bufRx[1])/2;

i2c_bufTx[0] = 0xA0; // channel 2, one-shot, 12bit, gain 1 (see MCP3424 datasheet)
i2c_write_count = 1;
i2c_read_count = 0;
i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);
  
Sleep(0.01); // wait for 10 ms before reading the second channel 
             // Data rate: 12bit = 240 SPS, 14bit = 60 SPS, 16bit = 15 SPS, 18bit = 3.75 SPS
  
i2c_write_count = 0;
i2c_read_count = 3;
i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);
channel2 = ((i2c_bufRx[0]&lt;&lt;8) + i2c_bufRx[1])/2;
</code></pre>
<h2>Important Notes</h2>
<p dir="auto">Sleep time scales with resolution - 10ms works for 12-bit, 270ms needed for 18-bit</p>
<p dir="auto">Original MCP3422 example worked because it read only 1 channel (no channel switching). The message from the previous tick was probably returned as a response.</p>
<h2>Example Update</h2>
<p dir="auto">REXYGEN example library will be extended with:</p>
<ul>
<li>read_mcp3424_12bit.c - 2 channels, 12-bit</li>
<li>read_mcp3424_18bit.c - 2 channels, 18-bit</li>
</ul>
<p dir="auto">Big thanks to Alexander for the thorough debugging, testing different timing scenarios, and sharing his working scripts!</p>
<p dir="auto">Cheers,<br />
Jan</p>
]]></description><link>https://forum.rexygen.com/post/1742</link><guid isPermaLink="true">https://forum.rexygen.com/post/1742</guid><dc:creator><![CDATA[Jan Reitinger]]></dc:creator><pubDate>Tue, 10 Feb 2026 09:59:48 GMT</pubDate></item><item><title><![CDATA[Reply to Problems Sequential reading channels ADC MCP3424 via I2C on Tue, 20 Jan 2026 20:14:59 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.rexygen.com/uid/186">@Jan-Reitinger</a>:</p>
<p dir="auto">Dear Jan, thank you for this quick reply. In the coming days I will send an email to the supplied email address. To be continued.</p>
<p dir="auto">Greetings,<br />
Alex</p>
]]></description><link>https://forum.rexygen.com/post/1737</link><guid isPermaLink="true">https://forum.rexygen.com/post/1737</guid><dc:creator><![CDATA[AlexanderH]]></dc:creator><pubDate>Tue, 20 Jan 2026 20:14:59 GMT</pubDate></item><item><title><![CDATA[Reply to Problems Sequential reading channels ADC MCP3424 via I2C on Tue, 20 Jan 2026 10:55:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.rexygen.com/uid/231">@AlexanderH</a> Hi Alexander,</p>
<p dir="auto">we discussed the I2C communication details with the developers and both continuous reading and using multiple I2C commands within a single task cycle should be possible in your use case.</p>
<p dir="auto">Unfortunately, there is no MCP342x device available on my side right now, but I will try to get one so the behavior can be reproduced and tested. In the meantime, could you please send a minimal version of your project, either here on the forum or to <a href="mailto:support@rexygen.com">support@rexygen.com</a>? That will make it much easier to see what is going on in your specific setup and suggest a concrete solution.</p>
<p dir="auto">Cheers,<br />
Jan</p>
]]></description><link>https://forum.rexygen.com/post/1736</link><guid isPermaLink="true">https://forum.rexygen.com/post/1736</guid><dc:creator><![CDATA[Jan Reitinger]]></dc:creator><pubDate>Tue, 20 Jan 2026 10:55:53 GMT</pubDate></item><item><title><![CDATA[Reply to Problems Sequential reading channels ADC MCP3424 via I2C on Mon, 19 Jan 2026 19:15:48 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.rexygen.com/uid/186">@Jan-Reitinger</a> Dear Jan, I tried a lot last couple of days but nothing seems to work. I can only read one channel, and not multiple over time. I am also doubting if I should put the configuration of the different channels into the "init(void)" should keep that in the "main(void)". The channels cannot be read in parallel but should be read in sequential mode. I cannot figure out how to do that. Any help is appreciated.</p>
]]></description><link>https://forum.rexygen.com/post/1735</link><guid isPermaLink="true">https://forum.rexygen.com/post/1735</guid><dc:creator><![CDATA[AlexanderH]]></dc:creator><pubDate>Mon, 19 Jan 2026 19:15:48 GMT</pubDate></item><item><title><![CDATA[Reply to Problems Sequential reading channels ADC MCP3424 via I2C on Tue, 13 Jan 2026 19:21:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.rexygen.com/uid/186">@Jan-Reitinger</a> Dear Jan, Thanks for these valuable tips :-). I will start with it this weekend. Hopefully I will succeed. If not, I will let you know via this forum. I will keep you posted. Greetings, Alex</p>
]]></description><link>https://forum.rexygen.com/post/1734</link><guid isPermaLink="true">https://forum.rexygen.com/post/1734</guid><dc:creator><![CDATA[AlexanderH]]></dc:creator><pubDate>Tue, 13 Jan 2026 19:21:37 GMT</pubDate></item><item><title><![CDATA[Reply to Problems Sequential reading channels ADC MCP3424 via I2C on Mon, 12 Jan 2026 12:21:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.rexygen.com/uid/231">@AlexanderH</a>  Hi Alexander,</p>
<p dir="auto">Great to see you experimenting with REXYGEN and getting good results so far! It’s nice that you started from the example library — that’s definitely the right approach. The <code>I2C</code> function in REXYGEN works a bit differently compared to Arduino or other low-level implementations. Since REXYGEN tasks usually don’t run with very short cycle times, a single <code>I2C</code> call performs both write and read operations within one cycle. This command takes care of sending the message, waiting briefly, and then reading the response. That means you don’t need to manually handle the NAK or stop condition — the function itself takes care of it.</p>
<p dir="auto">I looked through your code and have a few notes that might help:</p>
<ul>
<li>I noticed that right after calculating <code>channel1</code>, you have a <code>return 0;</code>. Because of that, the code for channel2 never runs — I guess that’s just for debugging, but it’s worth pointing out.</li>
<li>In each REXYGEN cycle, you currently configure the converter for continuous read mode. This might not behave as you expect, and it’s probably unnecessary. Try switching to one-shot mode first and see what kind of values you get. Later, you can set continuous mode once, and then in your <code>main()</code> you can just read the data by setting <code>i2c_write_count = 0</code>. That way, the <code>I2C</code> function won’t send any configuration bytes, it will only read — according to the documentation, that should work.</li>
<li>You can use the <code>Trace()</code> function to print out the value of <code>i2c_ret_fun</code> for debugging. Keep in mind that you need to enable these messages in the system log:<br />
Go to <em>Target</em> → <em>Diagnostic messages</em>, tick <em>Information</em> in <em>Function block messages</em>, and make sure <em>Enable logging</em> is checked in the <em>Options</em> tab of the block properties dialog. Only then you’ll see the messages in the System log.</li>
<li>And finally, double-check the I2C address of the MCP3424 — it’s configurable, so make sure it matches your hardware setup.</li>
</ul>
<p dir="auto">Hope this helps you move forward! Please let us know how it goes — it’s always interesting to see REXLANG + I2C projects in action.</p>
<p dir="auto">Cheers,<br />
Jan</p>
]]></description><link>https://forum.rexygen.com/post/1733</link><guid isPermaLink="true">https://forum.rexygen.com/post/1733</guid><dc:creator><![CDATA[Jan Reitinger]]></dc:creator><pubDate>Mon, 12 Jan 2026 12:21:41 GMT</pubDate></item></channel></rss>