0.1.2 MCP_DAC

This commit is contained in:
rob tillaart 2021-07-31 17:44:31 +02:00
parent 9effd4e85e
commit d76462113f
6 changed files with 107 additions and 28 deletions

View File

@ -1,7 +1,7 @@
//
// FILE: MCP_DAC.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// VERSION: 0.1.2
// DATE: 2021-02-03
// PURPOSE: Arduino library for MCP_DAC
// URL: https://github.com/RobTillaart/MCP_DAC
@ -9,6 +9,10 @@
// HISTORY
// 0.1.0 2021-02-03 initial version
// 0.1.1 2021-05-26 moved SPI.begin() from constructor to begin()
// 0.1.2 2021-07-29 VSPI / HSPI support for ESP32 (default pins only
// faster software SPI
// minor optimizations / refactor
#include "MCP_DAC.h"
@ -17,17 +21,10 @@ MCP_DAC::MCP_DAC(uint8_t dataOut, uint8_t clock)
{
_dataOut = dataOut;
_clock = clock;
_select = 0;
_hwSPI = (dataOut == 255) || (clock == 255);
if (_hwSPI == false)
{
pinMode(_dataOut, OUTPUT);
pinMode(_clock, OUTPUT);
digitalWrite(_dataOut, LOW);
digitalWrite(_clock, LOW);
}
_channels = 1;
_maxValue = 255;
_select = 0;
reset();
}
@ -47,13 +44,48 @@ void MCP_DAC::begin(uint8_t select)
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
if (_hwSPI)
{
SPI.begin();
#if defined(ESP32)
if (_useHSPI) // HSPI
{
mySPI = new SPIClass(HSPI);
mySPI->begin(14, 12, 13, _select); // CLK MISO MOSI SELECT
}
else // VSPI
{
mySPI = new SPIClass(VSPI);
mySPI->begin(18, 19, 23, _select); // CLK MISO MOSI SELECT
}
#else // generic SPI
mySPI = &SPI;
mySPI->begin();
#endif
}
else // software SPI
{
pinMode(_dataOut, OUTPUT);
pinMode(_clock, OUTPUT);
digitalWrite(_dataOut, LOW);
digitalWrite(_clock, LOW);
}
}
#if defined(ESP32)
void MCP_DAC::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clk;
_dataOut = mosi;
_select = select;
mySPI->begin(_clock, miso, _dataOut, _select); // CLK MISO MOSI SELECT
}
#endif
bool MCP_DAC::setGain(uint8_t gain)
{
if ((0 < gain) && (gain < 2)) return false;
@ -121,9 +153,12 @@ void MCP_DAC::setLatchPin(uint8_t latchPin)
void MCP_DAC::triggerLatch()
{
if (_latchPin != 255)
{
digitalWrite(_latchPin, HIGH);
delayMicroseconds(1); // 100 ns - Page 7
digitalWrite(_latchPin, LOW);
}
}
@ -134,16 +169,27 @@ void MCP_DAC::shutDown()
}
void MCP_DAC::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
};
//////////////////////////////////////////////////////////////////
void MCP_DAC::transfer(uint16_t data)
{
// DATA TRANSFER
digitalWrite(_select, LOW);
if (_hwSPI)
{
SPI.beginTransaction(SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0));
SPI.transfer((uint8_t)(data >> 8));
SPI.transfer((uint8_t)(data & 0xFF));
SPI.endTransaction();
// mySPI->beginTransaction(SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0));
mySPI->beginTransaction(_spi_settings);
mySPI->transfer((uint8_t)(data >> 8));
mySPI->transfer((uint8_t)(data & 0xFF));
mySPI->endTransaction();
}
else // Software SPI
{
@ -157,11 +203,13 @@ void MCP_DAC::transfer(uint16_t data)
// MSBFIRST
uint8_t MCP_DAC::swSPI_transfer(uint8_t val)
{
uint8_t clk = _clock;
uint8_t dao = _dataOut;
for (uint8_t mask = 0x80; mask; mask >>= 1)
{
digitalWrite(_dataOut,(val & mask) != 0);
digitalWrite(_clock, HIGH);
digitalWrite(_clock, LOW);
digitalWrite(dao, (val & mask));
digitalWrite(clk, HIGH);
digitalWrite(clk, LOW);
}
return 0;
}

View File

@ -2,7 +2,7 @@
//
// FILE: MCP_DAC.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// VERSION: 0.1.2
// DATE: 2021-02-03
// PURPOSE: Arduino library for MCP_DAC
// URL: https://github.com/RobTillaart/MCP_DAC
@ -13,7 +13,8 @@
#include "SPI.h"
#define MCP_DAC_LIB_VERSION (F("0.1.1"))
#define MCP_DAC_LIB_VERSION (F("0.1.2"))
///////////////////////////////////////////////////////////////
@ -53,7 +54,7 @@ public:
bool isActive() { return _active; };
// speed in Hz
void setSPIspeed(uint32_t speed) { _SPIspeed = speed; };
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed() { return _SPIspeed; };
//
@ -67,6 +68,15 @@ public:
void reset();
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; };
// to overrule ESP32 default hw pins...
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif
protected:
uint8_t _dataOut;
@ -85,6 +95,13 @@ protected:
void transfer(uint16_t data);
uint8_t swSPI_transfer(uint8_t d);
SPIClass * mySPI;
SPISettings _spi_settings;
#if defined(ESP32)
bool _useHSPI = true;
#endif
};

View File

@ -105,12 +105,21 @@ MCP49xxx series only, see page 20 ==> not functional for MCP48xx series.
- **bool usesHWSPI()** returns true if HW SPI is used.
### ESP32 specific
- **void selectHSPI()** in case hardware SPI, the ESP32 has two options HSPI and VSPI.
- **void selectVSPI()** see above.
- **bool usesHSPI()** returns true if HSPI is used.
- **bool usesVSPI()** returns true if VSPI is used.
## Future
- test test test and ....
- **incr(channel)** increases until max value is reached.
- **decr(channel)** decreases until 0 is reached.
- set gpio pins of the ESP32 for hardware SPI
- ...
## Operation

View File

@ -43,6 +43,11 @@ getSPIspeed KEYWORD2
setBufferedMode KEYWORD2
getBufferedMode KEYWORD2
selectHSPI KEYWORD2
selectVSPI KEYWORD2
usesHSPI KEYWORD2
usesVSPI KEYWORD2
setGPIOpins KEYWORD2
# Instances (KEYWORD2)

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MCP_DAC"
},
"version": "0.1.1",
"version": "0.1.2",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*"

View File

@ -1,5 +1,5 @@
name=MCP_DAC
version=0.1.1
version=0.1.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for Microchip SPI DAC, 8, 10, 12 bit; 1 or 2 channel.