Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions Arduino/MCP3428_Arduino_Library/MCP3428.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/****************************************************************************
/*
Distributed with a free-will license.
Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
MCP3428
This code is designed to work with the MCP3428_I2CADC I2C Mini Module available from ControlEverything.com.
https://www.controleverything.com/content/Analog-Digital-Converters?sku=MCP3428_I2CADC#tabs-0-product_tabset-2
*/
/****************************************************************************/
****************************************************************************/

#include <Wire.h>
#include <MCP3428.h>
Expand All @@ -19,7 +17,7 @@
MCP3428::MCP3428(uint8_t devAddress)
{
Wire.begin();
devAddr = 1101<<3;
devAddr = 0x68;
devAddr |= devAddress;
}

Expand Down Expand Up @@ -116,8 +114,16 @@ long MCP3428::readADC()
{

raw_adc = 0;

while(CheckConversion() == 1);
int tries = 0;
while(CheckConversion() == 1 && tries++ < 5) {
unsigned long convTime = getConversionTime(SPS);
if (convTime > 16383) {
convTime /= 1000;
delay(convTime);
} else {
delayMicroseconds(convTime);
}
}

switch (SPS)
{
Expand All @@ -133,7 +139,7 @@ long MCP3428::readADC()
raw_adc = raw_adc - 4096;
}

// raw_adc = raw_adc*1000/GAIN;
raw_adc = raw_adc*1000/GAIN;

break;

Expand All @@ -148,7 +154,7 @@ long MCP3428::readADC()
raw_adc = raw_adc - 16384;
}

// raw_adc = raw_adc*250/GAIN;
raw_adc = raw_adc*250/GAIN;

break;

Expand All @@ -163,9 +169,24 @@ long MCP3428::readADC()
raw_adc = raw_adc - 65536;
}

// raw_adc = raw_adc*62.5/GAIN;
raw_adc = raw_adc*62.5/GAIN;

break;
}
return raw_adc;
}

unsigned long MCP3428::getConversionTime(uint8_t resolution) {
switch (resolution) {
case 12:
return 4167; // 240 SPS
case 14:
return 16667; // 60 SPS
case 16:
return 66667; // 15 SPS
case 18:
return 266667; // 3.75 SPS
default:
return 0;
}
}
5 changes: 2 additions & 3 deletions Arduino/MCP3428_Arduino_Library/MCP3428.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/****************************************************************************
/*
Distributed with a free-will license.
Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
MCP3428
This code is designed to work with the MCP3428_I2CADC I2C Mini Module available from ControlEverything.com.
https://www.controleverything.com/content/Analog-Digital-Converters?sku=MCP3428_I2CADC#tabs-0-product_tabset-2
*/
/****************************************************************************/
****************************************************************************/

#include <Wire.h>
#include <math.h>
Expand All @@ -23,6 +21,7 @@ class MCP3428
long readADC();

private:
unsigned long getConversionTime(uint8_t resolution);

uint8_t devAddr;
long raw_adc;
Expand Down