<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[REXYGEN Community Forum / Integrating PCF8591 i2c A/D converter with rex]]></title>
	<link rel="self" href="https://www.rexygen.com/oldforum/extern.php?action=feed&amp;tid=74&amp;type=atom"/>
	<link href="http://www.rexygen.com/oldforum/viewtopic.php?id=74"/>
	<updated>2016-03-17T07:44:14Z</updated>
	<generator>FluxBB</generator>
	<id>http://www.rexygen.com/oldforum/viewtopic.php?id=74</id>
	<entry>
		<title type="html"><![CDATA[Re: Integrating PCF8591 i2c A/D converter with rex]]></title>
		<link rel="alternate" href="http://www.rexygen.com/oldforum/viewtopic.php?pid=340#p340"/>
		<content type="html"><![CDATA[<p>Good to hear that it&#039;s working now! <img src="http://www.rexygen.com/oldforum/img/smilies/smile.png" width="15" height="15" alt="smile" /><br />Thank you for your reply and good luck!</p>]]></content>
		<author>
			<name><![CDATA[tomáš čechura]]></name>
			<uri>http://www.rexygen.com/oldforum/profile.php?id=39</uri>
		</author>
		<updated>2016-03-17T07:44:14Z</updated>
		<id>http://www.rexygen.com/oldforum/viewtopic.php?pid=340#p340</id>
	</entry>
	<entry>
		<title type="html"><![CDATA[Re: Integrating PCF8591 i2c A/D converter with rex]]></title>
		<link rel="alternate" href="http://www.rexygen.com/oldforum/viewtopic.php?pid=336#p336"/>
		<content type="html"><![CDATA[<p>It worked perfectly !. </p><p>Thanks a lot for taking the time to go through the device&#039;s datasheet and explain the solution so patiently.<br />Bless you ! Thanks !</p>]]></content>
		<author>
			<name><![CDATA[deepak]]></name>
			<uri>http://www.rexygen.com/oldforum/profile.php?id=22</uri>
		</author>
		<updated>2016-03-11T15:10:16Z</updated>
		<id>http://www.rexygen.com/oldforum/viewtopic.php?pid=336#p336</id>
	</entry>
	<entry>
		<title type="html"><![CDATA[Re: Integrating PCF8591 i2c A/D converter with rex]]></title>
		<link rel="alternate" href="http://www.rexygen.com/oldforum/viewtopic.php?pid=327#p327"/>
		<content type="html"><![CDATA[<p>Hi,</p><p>according to <a href="http://www.nxp.com/documents/data_sheet/PCF8591.pdf" rel="nofollow">PFC8591 datasheet</a>, the A/D converter can transmit more channels in one response - using auto-increment flag. It means that if you send control byte 0x04 and read 3 bytes, you will get previously converted byte (and probably throw it away) and 2 bytes (channel 0 and channel 1).</p><p>PFC8591 is 8bit ADC - it means that you don&#039;t have to combine two received bytes into one analog input:</p><div class="codebox"><pre><code>//analog_in1 = (i2c_bufRx[0]&lt;&lt;8) + i2c_bufRx[1];
analog_in1 = i2c_bufRx[1];</code></pre></div><p>I changed your code, mainly Configuration register table for your information and control byte command. See the code:</p><div class="codebox"><pre class="vscroll"><code>//assigning variables to outputs, these variables are WRITE-ONLY
//long output(0) adc_value;

long output(0) analog_in1;
long output(1) analog_in2;


//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;


//long output(2) freq;


//the init procedure is executed once when the REXLANG function block initializes
int init(void)
{
  i2c_bus_handle = Open(I2CDEV_FNAME); // open I2C bus
  /* Address: 1 1 0 1 A2 A1 A0 */
  i2c_chip_address = 0x48; // 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                    | bit 5-4                    | bit 3 |  bit 2             | bit 1-0        |
     ---------------------------------------------------------------------------------------------------------------
     | 0     | 1 = Analog output enable | 00 = single ended inputs   | 0     | 1 = auto-increment | 00 = channel 0 |
     |       |                          | 01 = 3 differential inputs |       |                    | 01 = channel 1 |
     |       |                          | 10 = mixed inputs          |       |                    | 10 = channel 2 |
     |       |                          | 11 = 2 differential inputs |       |                    | 11 = channel 3 |
     ===============================================================================================================
  */
  i2c_bufTx[0] = 0x04; // read channel 0 with auto-increment flag (00000100b)
  i2c_write_count = 1;
  i2c_read_count = 3;
  //Sending data via I2C
  i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);
  //some old data in i2c_bufRx[0] from previous conversion
  analog_in1 = i2c_bufRx[1];
  analog_in2 = i2c_bufRx[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></div><p>Unfortunately we don&#039;t have this AD converter in stock so we couldn&#039;t test it.<br />Hope it will help you.</p>]]></content>
		<author>
			<name><![CDATA[tomáš čechura]]></name>
			<uri>http://www.rexygen.com/oldforum/profile.php?id=39</uri>
		</author>
		<updated>2016-03-10T12:57:38Z</updated>
		<id>http://www.rexygen.com/oldforum/viewtopic.php?pid=327#p327</id>
	</entry>
	<entry>
		<title type="html"><![CDATA[Integrating PCF8591 i2c A/D converter with rex]]></title>
		<link rel="alternate" href="http://www.rexygen.com/oldforum/viewtopic.php?pid=324#p324"/>
		<content type="html"><![CDATA[<p>Hi ,</p><p>I am unable to figure out what buffers to define to read multiple analog input channels of the PCF8591 A/D converter. I am able to read only the first channel successfully. The device address and the input channel addresses are correct. Please have a look at my code.</p><p>//assigning variables to outputs, these variables are WRITE-ONLY<br />//long output(0) adc_value;</p><p>long output(0) analog_in1;<br />long output(1) analog_in2;</p><br /><p>//declaration of variables<br />long i2c_bufTx[3]; //buffer for transmitting data<br />long i2c_bufRx[3]; //buffer for receiving data<br />long i2c_bus_handle;<br />long i2c_chip_address;<br />long i2c_write_count;<br />long i2c_read_count;<br />long i2c_ret_fun;</p><br /><p>//long output(2) freq;</p><br /><p>//the init procedure is executed once when the REXLANG function block initializes<br />int init(void)<br />{<br />&#160; i2c_bus_handle = Open(I2CDEV_FNAME); // open I2C bus<br />&#160; /* Address: 1 1 0 1 A2 A1 A0 */<br />&#160; i2c_chip_address = 0x48; // 7-bit address of the I2C device<br />&#160; <br />&#160; <br />&#160; return 0;<br />}</p><p>//the main procedure is executed once in each sampling period<br />long main(void)<br />{<br />&#160; /* Configuration register: <br />&#160; &#160; &#160;=====================================================================================<br />&#160; &#160; &#160;| bit 7&#160; &#160; &#160; &#160; &#160; &#160; &#160; | bit 6-5&#160; &#160; &#160; &#160; | bit 4&#160; &#160; &#160; &#160; &#160; | bit 3-2&#160; &#160; &#160;| bit 1-0&#160; &#160; &#160; |<br />&#160; &#160; &#160;-------------------------------------------------------------------------------------<br />&#160; &#160; &#160;| 1=start conversion | 00 = channel 1 | 1 = continuous | 00 = 12-bit | 00 = gain x1 |<br />&#160; &#160; &#160;| 0=no effect&#160; &#160; &#160; &#160; | 01 = channel 2 | 0 = one-shot&#160; &#160;| 01 = 14-bit | 01 = gain x2 |<br />&#160; &#160; &#160;|&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; |&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; |&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; | 10 = 16-bit | 10 = gain x4 |<br />&#160; &#160; &#160;|&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; |&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; |&#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; | 11 = 18-bit | 11 = gain 8x |<br />&#160; &#160; &#160;=====================================================================================<br />&#160; */<br />&#160; i2c_bufTx[0] = 0x00; // read channel 1 .I am able to read channel 1 successfully<br />&#160; i2c_bufTx[0] = 0x01; // read channel 2&#160; <br />&#160; i2c_write_count = 1;<br />&#160; i2c_read_count = 2;<br />&#160; //Sending data via I2C<br />&#160; i2c_ret_fun = I2C(i2c_bus_handle, i2c_chip_address, i2c_bufTx, i2c_write_count, i2c_bufRx, i2c_read_count);<br />&#160; analog_in1 = (i2c_bufRx[0]&lt;&lt;8) + i2c_bufRx[1];<br />&#160; analog_in2 = (i2c_bufRx[0]&lt;&lt;8) + i2c_bufRx[1];<br />&#160; <br />&#160; </p><p>&#160; return 0;<br />}</p><p>//the exit procedure is executed once when the task is correctly terminated<br />// (system shutdown, downloading new control algorithm, etc.)<br />long exit(void)<br />{<br />&#160; if(i2c_bus_handle&gt;=0) Close(i2c_bus_handle); // close I2C bus<br />&#160; return 0;</p>]]></content>
		<author>
			<name><![CDATA[deepak]]></name>
			<uri>http://www.rexygen.com/oldforum/profile.php?id=22</uri>
		</author>
		<updated>2016-03-09T15:33:56Z</updated>
		<id>http://www.rexygen.com/oldforum/viewtopic.php?pid=324#p324</id>
	</entry>
</feed>
