<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[forum.rcl-radio.ru &mdash; MCP3421]]></title>
		<link>http://forum.rcl-radio.ru/viewtopic.php?id=463</link>
		<atom:link href="http://forum.rcl-radio.ru/extern.php?action=feed&amp;tid=463&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[Недавние сообщения в теме «MCP3421».]]></description>
		<lastBuildDate>Thu, 03 Mar 2022 05:38:50 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[MCP3421]]></title>
			<link>http://forum.rcl-radio.ru/viewtopic.php?pid=5500#p5500</link>
			<description><![CDATA[<p><span class="attention-yellow"></span> </p><p>MCP3421.h<br /></p><div class="codebox"><pre><code>#ifndef MCP3421_H
#define MCP3421_H

#define MCP3421_address 0x68 

#include &lt;Arduino.h&gt;

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

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

void MCP3421::setConfig(int bit, int gain){
     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 + bit + gain;
     writeWire(conf);
}


void MCP3421::writeWire(char a){
     Wire.beginTransmission(MCP3421_address);
     Wire.write (a);
     Wire.endTransmission();
}

int32_t MCP3421::readWire(){
        Wire.requestFrom(MCP3421_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><p>MCP3421_test.ino<br /></p><div class="codebox"><pre><code>// liman324@yandex.ru | Alexander Liman | rcl-radio.ru
#include &lt;Wire.h&gt;
#include &lt;MCP3421.h&gt;
  MCP3421 mcp;
  
  long dig;
  float u;
  
void setup(){ 
  Serial.begin(9600);Wire.begin();
  mcp.setConfig(3,0); // 12 14 16 18 bit - 0-3 // 1x 2x 4x 8x gain - 0-3
}

void loop(){
 dig = mcp.readWire();
 u = dig * 2.048 /131071;
 Serial.println(u,6);
}
 /* 1x = 2.048V; 2x = 1.024V; 4x = 0.512V; 8x = 0.256V
 
 18 bit = 17 bit (dec 131071)  + 1 bit (полярность напряжения) */</code></pre></div><p>MCP3421 АЦП 18 бит <a href="http://rcl-radio.ru/?p=62308">http://rcl-radio.ru/?p=62308</a> Библиотека протестирована</p>]]></description>
			<author><![CDATA[null@example.com (liman324)]]></author>
			<pubDate>Thu, 03 Mar 2022 05:38:50 +0000</pubDate>
			<guid>http://forum.rcl-radio.ru/viewtopic.php?pid=5500#p5500</guid>
		</item>
	</channel>
</rss>
