<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[forum.rcl-radio.ru &mdash; MCP342x]]></title>
	<link rel="self" href="http://forum.rcl-radio.ru/extern.php?action=feed&amp;tid=35&amp;type=atom" />
	<updated>2019-08-26T09:20:23Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.rcl-radio.ru/viewtopic.php?id=35</id>
		<entry>
			<title type="html"><![CDATA[MCP342x]]></title>
			<link rel="alternate" href="http://forum.rcl-radio.ru/viewtopic.php?pid=41#p41" />
			<content type="html"><![CDATA[<p>Библиотека не тестировалась!!!</p><p><span class="attention-yellow"></span> </p><p>test.ino</p><div class="codebox"><pre><code>#include &lt;Wire.h&gt;
#include &lt;MCP342x.h&gt;
  MCP342x mcp;
 
  long dig0, dig1;
  float u0,u1;
 
void setup(){ 
  Serial.begin(9600);Wire.begin();
}
 
void loop(){
 mcp.setConfig(0,3,0); // in0 - in3 - 0-3 // 12 14 16 18 bit - 0-3 // 1x 2x 4x 8x gain - 0-3
 dig0 = mcp.readWire();
 u0 = dig0 * 2.048 /131071;
 Serial.print(dig0);
 Serial.print(&quot;  &quot;);
 Serial.println(u0,6);

 mcp.setConfig(1,3,0); // in0 - in1 - 0-3 // 12 14 16 18 bit - 0-3 // 1x 2x 4x 8x gain - 0-3
 dig1 = mcp.readWire();
 u1 = dig1 * 2.048 /131071;
 Serial.print(dig1);
 Serial.print(&quot;  &quot;);
 Serial.println(u1,6);
 delay(1000);
}
 /* 1x = 2.048V; 2x = 1.024V; 4x = 0.512V; 8x = 0.256V
 
 18 bit = 17 bit (dec 131071)  + 1 bit (полярность напряжения) */</code></pre></div><p>MCP342x.h</p><div class="codebox"><pre><code>#ifndef MCP342x_H
#define MCP342x_H

#define MCP342x_address 0x68 

#include &lt;Arduino.h&gt;

class MCP342x
{
  public:
    MCP342x();
        void setConfig(int in, int bit, int gain); 
        int32_t readWire(void);
    
  private:
    void writeWire(char a);      
};
    
#endif //MCP342x_H</code></pre></div><p>MCP342x.cpp</p><div class="codebox"><pre><code>#include &lt;Arduino.h&gt;
#include &lt;Wire.h&gt;
#include &quot;MCP342x.h&quot;

MCP342x::MCP342x(){
    Wire.begin();
}

void MCP342x::setConfig(int in,int bit, int gain){
     switch (in){
        case 0:in = 0b00000000;break;
        case 1:in = 0b00100000;break;
        case 2:in = 0b01000000;break;
        case 3:in = 0b01100000;break;
  }
     switch (bit){
        case 0:bit = 0b00000000;break;
        case 1:bit = 0b00000100;break;
        case 2:bit = 0b00001000;break;
        case 3:bit = 0b00001100;break;
  }
     switch (gain){
        case 0:gain = 0b00000000;break;
        case 1:gain = 0b00000001;break;
        case 2:gain = 0b00000010;break;
        case 3:gain = 0b00000011;break;
  }
        int conf = 0b00010000 + in + bit + gain;
     writeWire(conf);
}


void MCP342x::writeWire(char a){
     Wire.beginTransmission(MCP342x_address);
     Wire.write (a);
     Wire.endTransmission();
}

int32_t MCP342x::readWire(){
        Wire.requestFrom(MCP342x_address,3); //запросить 3 байта данных
   long value = ((Wire.read()&lt;&lt;8) | Wire.read());//упаковка в одну переменную.
        value = ((value&lt;&lt;8)| Wire.read());
   return (int32_t)value;
}</code></pre></div>]]></content>
			<author>
				<name><![CDATA[liman324]]></name>
				<uri>http://forum.rcl-radio.ru/profile.php?id=2</uri>
			</author>
			<updated>2019-08-26T09:20:23Z</updated>
			<id>http://forum.rcl-radio.ru/viewtopic.php?pid=41#p41</id>
		</entry>
</feed>
