2021-01-29 06:31:58 -05:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: MCP_ADC.h
|
|
|
|
// 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 MCP_ADC
|
|
|
|
// URL: https://github.com/RobTillaart/MCP_ADC
|
|
|
|
//
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
#include "Arduino.h"
|
|
|
|
#include "SPI.h"
|
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
|
2021-12-21 09:31:31 -05:00
|
|
|
#define MCP_ADC_LIB_VERSION (F("0.1.8"))
|
2021-02-06 09:52:51 -05:00
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
class MCP_ADC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MCP_ADC(uint8_t dataIn = 255, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
void begin(uint8_t select);
|
|
|
|
|
|
|
|
uint8_t channels() { return _channels; };
|
|
|
|
int16_t maxValue() { return _maxValue; };
|
|
|
|
int16_t analogRead(uint8_t channel);
|
|
|
|
int16_t differentialRead(uint8_t channel);
|
|
|
|
int16_t deltaRead(uint8_t channel);
|
2021-07-31 11:43:47 -04:00
|
|
|
|
2021-02-06 09:52:51 -05:00
|
|
|
// speed in Hz
|
2021-07-31 11:43:47 -04:00
|
|
|
void setSPIspeed(uint32_t speed);
|
2021-02-06 09:52:51 -05:00
|
|
|
uint32_t getSPIspeed() { return _SPIspeed; };
|
2021-01-29 06:31:58 -05:00
|
|
|
|
2021-07-31 11:43:47 -04:00
|
|
|
// debugging
|
|
|
|
bool usesHWSPI() { return _hwSPI; };
|
|
|
|
|
|
|
|
// ESP32 specific
|
|
|
|
#if defined(ESP32)
|
|
|
|
void selectHSPI() { _useHSPI = true; };
|
|
|
|
void selectVSPI() { _useHSPI = false; };
|
|
|
|
bool usesHSPI() { return _useHSPI; };
|
|
|
|
bool usesVSPI() { return !_useHSPI; };
|
2021-08-01 09:51:21 -04:00
|
|
|
|
|
|
|
// to overrule ESP32 default hardware pins
|
2021-07-31 11:43:47 -04:00
|
|
|
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
|
|
|
|
#endif
|
|
|
|
|
2021-11-08 07:14:13 -05:00
|
|
|
uint32_t count();
|
|
|
|
|
|
|
|
|
2021-01-29 06:31:58 -05:00
|
|
|
protected:
|
|
|
|
uint8_t _dataIn;
|
|
|
|
uint8_t _dataOut;
|
|
|
|
uint8_t _clock;
|
|
|
|
uint8_t _select;
|
|
|
|
bool _hwSPI;
|
|
|
|
uint8_t _channels;
|
|
|
|
int16_t _maxValue;
|
2021-04-07 07:31:22 -04:00
|
|
|
uint32_t _SPIspeed = 1000000; // 1MHz is a safe value (datasheet); in a test 4 MHz worked.
|
2021-01-29 06:31:58 -05:00
|
|
|
|
|
|
|
// derived classes must implement this one
|
|
|
|
virtual uint8_t buildRequest(uint8_t channel, bool single, uint8_t * data) = 0;
|
|
|
|
|
|
|
|
int16_t readADC(uint8_t channel, bool single);
|
|
|
|
uint8_t swSPI_transfer(uint8_t d);
|
2021-07-31 11:43:47 -04:00
|
|
|
|
|
|
|
SPIClass * mySPI;
|
|
|
|
SPISettings _spi_settings;
|
|
|
|
|
|
|
|
#if defined(ESP32)
|
|
|
|
bool _useHSPI = true;
|
|
|
|
#endif
|
2021-11-08 07:14:13 -05:00
|
|
|
|
|
|
|
uint32_t _count;
|
2021-01-29 06:31:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
class MCP3002 : public MCP_ADC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MCP3002(uint8_t dataIn = 255, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
uint8_t buildRequest(uint8_t channel, bool single, uint8_t * data);
|
|
|
|
};
|
|
|
|
|
|
|
|
class MCP3004 : public MCP_ADC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MCP3004(uint8_t dataIn = 255, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
uint8_t buildRequest(uint8_t channel, bool single, uint8_t * data);
|
|
|
|
};
|
|
|
|
|
|
|
|
class MCP3008 : public MCP_ADC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MCP3008(uint8_t dataIn = 255, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
uint8_t buildRequest(uint8_t channel, bool single, uint8_t * data);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MCP3202 : public MCP_ADC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MCP3202(uint8_t dataIn = 255, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
uint8_t buildRequest(uint8_t channel, bool single, uint8_t * data);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MCP3204 : public MCP_ADC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MCP3204(uint8_t dataIn = 255, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
uint8_t buildRequest(uint8_t channel, bool single, uint8_t * data);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class MCP3208 : public MCP_ADC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MCP3208(uint8_t dataIn = 255, uint8_t dataOut = 255, uint8_t clock = 255);
|
|
|
|
uint8_t buildRequest(uint8_t channel, bool single, uint8_t * data);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
2021-11-08 07:14:13 -05:00
|
|
|
|