2021-01-29 06:31:58 -05:00
|
|
|
//
|
|
|
|
// FILE: MCP_ADC.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-12-21 09:31:31 -05:00
|
|
|
// VERSION: 0.1.8
|
2021-01-29 06:31:58 -05:00
|
|
|
// DATE: 2019-10-24
|
|
|
|
// PURPOSE: Arduino library for MCP3002, MCP3004, MCP3008, MCP3202, MCP3204, MCP3208
|
|
|
|
// URL: https://github.com/RobTillaart/MCP_ADC
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "MCP_ADC.h"
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP_ADC::MCP_ADC(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
|
|
|
|
{
|
|
|
|
_dataIn = dataIn;
|
|
|
|
_dataOut = dataOut;
|
|
|
|
_clock = clock;
|
2021-07-31 11:43:47 -04:00
|
|
|
_select = 0;
|
2021-02-06 09:52:51 -05:00
|
|
|
_hwSPI = (dataIn == 255) || (dataOut == 255) || (clock == 255);
|
2021-07-31 11:43:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MCP_ADC::begin(uint8_t select)
|
|
|
|
{
|
2021-08-01 09:51:21 -04:00
|
|
|
_select = select;
|
2021-07-31 11:43:47 -04:00
|
|
|
pinMode(_select, OUTPUT);
|
|
|
|
digitalWrite(_select, HIGH);
|
|
|
|
|
|
|
|
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
|
|
|
|
|
|
|
|
if (_hwSPI)
|
|
|
|
{
|
|
|
|
#if defined(ESP32)
|
|
|
|
if (_useHSPI) // HSPI
|
|
|
|
{
|
|
|
|
mySPI = new SPIClass(HSPI);
|
2021-08-01 09:51:21 -04:00
|
|
|
mySPI->end();
|
|
|
|
mySPI->begin(14, 12, 13, select); // CLK=14 MISO=12 MOSI=13
|
2021-07-31 11:43:47 -04:00
|
|
|
}
|
|
|
|
else // VSPI
|
|
|
|
{
|
|
|
|
mySPI = new SPIClass(VSPI);
|
2021-08-01 09:51:21 -04:00
|
|
|
mySPI->end();
|
|
|
|
mySPI->begin(18, 19, 23, select); // CLK=18 MISO=19 MOSI=23
|
2021-07-31 11:43:47 -04:00
|
|
|
}
|
2021-08-01 09:51:21 -04:00
|
|
|
#else // generic hardware SPI
|
2021-07-31 11:43:47 -04:00
|
|
|
mySPI = &SPI;
|
2021-08-01 09:51:21 -04:00
|
|
|
mySPI->end();
|
2021-07-31 11:43:47 -04:00
|
|
|
mySPI->begin();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else // software SPI
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
|
|
|
pinMode(_dataIn, INPUT);
|
|
|
|
pinMode(_dataOut, OUTPUT);
|
|
|
|
pinMode(_clock, OUTPUT);
|
|
|
|
digitalWrite(_dataOut, LOW);
|
|
|
|
digitalWrite(_clock, LOW);
|
2021-02-06 09:52:51 -05:00
|
|
|
}
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-07-31 11:43:47 -04:00
|
|
|
#if defined(ESP32)
|
|
|
|
void MCP_ADC::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
|
2021-01-29 06:31:58 -05:00
|
|
|
{
|
2021-07-31 11:43:47 -04:00
|
|
|
_clock = clk;
|
|
|
|
_dataIn = miso;
|
|
|
|
_dataOut = mosi;
|
|
|
|
_select = select;
|
2021-08-01 09:51:21 -04:00
|
|
|
pinMode(_select, OUTPUT);
|
|
|
|
digitalWrite(_select, HIGH);
|
|
|
|
|
2021-11-08 07:14:13 -05:00
|
|
|
mySPI->end(); // disable SPI
|
2021-08-01 09:51:21 -04:00
|
|
|
mySPI->begin(clk, miso, mosi, select);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2021-07-31 11:43:47 -04:00
|
|
|
#endif
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-11-08 07:14:13 -05:00
|
|
|
uint32_t MCP_ADC::count()
|
|
|
|
{
|
|
|
|
uint32_t cnt = _count;
|
|
|
|
_count = 0;
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t MCP_ADC::analogRead(uint8_t channel)
|
|
|
|
{
|
|
|
|
if (channel >= _channels) return 0;
|
|
|
|
return readADC(channel, true);
|
|
|
|
}
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t MCP_ADC::differentialRead(uint8_t channel)
|
|
|
|
{
|
|
|
|
if (channel >= _channels) return 0;
|
|
|
|
return readADC(channel, false);
|
|
|
|
}
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t MCP_ADC::deltaRead(uint8_t channel)
|
|
|
|
{
|
|
|
|
if (channel >= _channels) return 0;
|
|
|
|
|
|
|
|
int16_t val0 = differentialRead(channel & 0xFE);
|
|
|
|
// int16_t val1 = differentialRead(channel | 0x01);
|
|
|
|
// no need to read if val0 has a positive value
|
|
|
|
int16_t val1 = (val0 > 0) ? 0 : differentialRead(channel | 0x01);
|
|
|
|
|
|
|
|
if (channel & 0x01) return val1 - val0;
|
|
|
|
return val0 - val1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-31 11:43:47 -04:00
|
|
|
void MCP_ADC::setSPIspeed(uint32_t speed)
|
|
|
|
{
|
|
|
|
_SPIspeed = speed;
|
|
|
|
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
int16_t MCP_ADC::readADC(uint8_t channel, bool single)
|
|
|
|
{
|
|
|
|
if (channel >= _channels) return 0;
|
2021-11-08 07:14:13 -05:00
|
|
|
|
|
|
|
_count++;
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t data[3] = { 0,0,0 };
|
|
|
|
uint8_t bytes = buildRequest(channel, single, data);
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
digitalWrite(_select, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
if (_hwSPI)
|
|
|
|
{
|
2021-07-31 11:43:47 -04:00
|
|
|
mySPI->beginTransaction(_spi_settings);
|
2021-01-29 06:31:58 -05:00
|
|
|
for (uint8_t b = 0; b < bytes; b++)
|
|
|
|
{
|
2021-07-31 11:43:47 -04:00
|
|
|
data[b] = mySPI->transfer(data[b]);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
2021-07-31 11:43:47 -04:00
|
|
|
mySPI->endTransaction();
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
else // Software SPI
|
|
|
|
{
|
|
|
|
for (uint8_t b = 0; b < bytes; b++)
|
|
|
|
{
|
|
|
|
data[b] = swSPI_transfer(data[b]);
|
|
|
|
}
|
|
|
|
}
|
2021-02-06 09:52:51 -05:00
|
|
|
digitalWrite(_select, HIGH);
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
if (bytes == 2) return ((256 * data[0] + data[1]) & _maxValue);
|
|
|
|
// data[0]?
|
|
|
|
return ((256 * data[1] + data[2]) & _maxValue);
|
|
|
|
}
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
// MSBFIRST
|
|
|
|
uint8_t MCP_ADC::swSPI_transfer(uint8_t val)
|
|
|
|
{
|
2021-07-31 11:43:47 -04:00
|
|
|
uint8_t clk = _clock;
|
|
|
|
uint8_t dao = _dataOut;
|
|
|
|
uint8_t dai = _dataIn;
|
2021-11-08 07:14:13 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
uint8_t rv = 0;
|
|
|
|
for (uint8_t mask = 0x80; mask; mask >>= 1)
|
|
|
|
{
|
2021-07-31 11:43:47 -04:00
|
|
|
digitalWrite(dao, (val & mask));
|
|
|
|
digitalWrite(clk, HIGH);
|
|
|
|
if (digitalRead(dai) == HIGH) rv |= mask;
|
|
|
|
digitalWrite(clk, LOW);
|
2021-01-29 06:31:58 -05:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MCP3002
|
2021-11-08 07:14:13 -05:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP3002::MCP3002(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
|
|
|
|
:MCP_ADC(dataIn, dataOut, clock)
|
|
|
|
{
|
|
|
|
_channels = 2;
|
|
|
|
_maxValue = 1023;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MCP3002::buildRequest(uint8_t channel, bool single, uint8_t * data)
|
|
|
|
{
|
2021-11-08 07:14:13 -05:00
|
|
|
// P17 fig 6.1 MCP3002
|
2021-01-29 06:31:58 -05:00
|
|
|
data[0] = 0x44; // start bit + MSB first bit
|
2021-04-07 07:31:22 -04:00
|
|
|
if (single) data[0] |= 0x20; // single read | differential
|
2021-01-29 06:31:58 -05:00
|
|
|
if (channel) data[0] |= (channel << 4); // channel = 0 or 1;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MCP3004
|
2021-11-08 07:14:13 -05:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP3004::MCP3004(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
|
|
|
|
:MCP_ADC(dataIn, dataOut, clock)
|
|
|
|
{
|
|
|
|
_channels = 4;
|
|
|
|
_maxValue = 1023;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MCP3004::buildRequest(uint8_t channel, bool single, uint8_t * data)
|
|
|
|
{
|
|
|
|
// P21 fig 6.1 MCP3004/3008
|
|
|
|
data[0] = 0x01; // start bit
|
|
|
|
if (single) data[1] = 0x80; // single read | differential
|
|
|
|
if (channel) data[1] |= (channel << 4); // channel
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MCP3008
|
2021-11-08 07:14:13 -05:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP3008::MCP3008(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
|
|
|
|
:MCP_ADC(dataIn, dataOut, clock)
|
|
|
|
{
|
|
|
|
_channels = 8;
|
|
|
|
_maxValue = 1023;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MCP3008::buildRequest(uint8_t channel, bool single, uint8_t * data)
|
|
|
|
{
|
|
|
|
// P21 fig 6.1 MCP3004/3008
|
|
|
|
data[0] = 0x01; // start bit
|
|
|
|
if (single) data[1] = 0x80; // single read | differential
|
|
|
|
if (channel) data[1] |= (channel << 4); // channel
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MCP3202
|
2021-11-08 07:14:13 -05:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP3202::MCP3202(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
|
|
|
|
:MCP_ADC(dataIn, dataOut, clock)
|
|
|
|
{
|
|
|
|
_channels = 2;
|
|
|
|
_maxValue = 4095;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MCP3202::buildRequest(uint8_t channel, bool single, uint8_t * data)
|
|
|
|
{
|
2021-11-08 07:14:13 -05:00
|
|
|
// P17 fig 6.2 MCP3202
|
2021-01-29 06:31:58 -05:00
|
|
|
data[0] = 0x01; // start bit
|
|
|
|
data[1] = 0x20; // MSB first bit
|
2021-11-08 07:14:13 -05:00
|
|
|
if (single) data[1] |= 0x80; // single read | differential
|
2021-01-29 06:31:58 -05:00
|
|
|
if (channel) data[1] |= (channel << 6); // channel = 0 or 1;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MCP3204
|
2021-11-08 07:14:13 -05:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP3204::MCP3204(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
|
|
|
|
:MCP_ADC(dataIn, dataOut, clock)
|
|
|
|
{
|
|
|
|
_channels = 4;
|
|
|
|
_maxValue = 4095;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MCP3204::buildRequest(uint8_t channel, bool single, uint8_t * data)
|
|
|
|
{
|
|
|
|
// P21 fig 6.1 MCP3204/3208
|
|
|
|
data[0] = 0x04; // start bit
|
2021-11-08 07:14:13 -05:00
|
|
|
if (single) data[0] |= 0x02; // single read | differential
|
2021-01-29 06:31:58 -05:00
|
|
|
if (channel > 3) data[0] |= 0x01; // msb channel (D2)
|
|
|
|
if (channel) data[1] |= (channel << 6); // other 2 bits (D1 D0)
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MCP3208
|
2021-11-08 07:14:13 -05:00
|
|
|
//
|
2021-01-29 06:31:58 -05:00
|
|
|
MCP3208::MCP3208(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
|
|
|
|
:MCP_ADC(dataIn, dataOut, clock)
|
|
|
|
{
|
|
|
|
_channels = 8;
|
|
|
|
_maxValue = 4095;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t MCP3208::buildRequest(uint8_t channel, bool single, uint8_t * data)
|
|
|
|
{
|
|
|
|
// P21 fig 6.1 MCP3204/3208
|
|
|
|
data[0] = 0x04; // start bit
|
2021-11-08 07:14:13 -05:00
|
|
|
if (single) data[0] |= 0x02; // single read | differential
|
2021-01-29 06:31:58 -05:00
|
|
|
if (channel > 3) data[0] |= 0x01; // msb channel (D2)
|
|
|
|
if (channel) data[1] |= (channel << 6); // other 2 bits (D1 D0)
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
2021-12-21 09:31:31 -05:00
|
|
|
|