0.3.0 DAC8552

This commit is contained in:
Rob Tillaart 2023-12-02 10:38:46 +01:00
parent 1f503b9d80
commit efae7d205f
13 changed files with 154 additions and 141 deletions

View File

@ -6,10 +6,17 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.3.0] - 2023-12-01
- refactor constructor interface - breaking changes.
- minimize conditional code. -- create SPI_CLASS macro to solve it.
- update readme.md
- update examples
----
## [0.2.5] - 2023-10-19
- update readme.md (badges)
## [0.2.4] - 2022-10-30
- add changelog.md
- add rp2040 to build-CI

View File

@ -2,7 +2,7 @@
// FILE: DAC8552.cpp
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DAC8552 SPI Digital Analog Convertor
// VERSION: 0.2.5
// VERSION: 0.3.0
// DATE: 2017-12-14
// URL: https://github.com/RobTillaart/DAC8552
@ -10,23 +10,23 @@
#include "DAC8552.h"
#define MAXVOLTAGE 5.0
#define MAXVALUE 0xFFFF
DAC8552::DAC8552(uint8_t slaveSelect)
DAC8552::DAC8552(uint8_t select, __SPI_CLASS__ * spi)
{
_hwSPI = true;
_select = slaveSelect;
_select = select;
_dataOut = 255;
_clock = 255;
_mySPI = spi;
_hwSPI = true;
}
DAC8552::DAC8552(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)
DAC8552::DAC8552(uint8_t select, uint8_t spiData, uint8_t spiClock)
{
_hwSPI = false;
_select = select;
_dataOut = spiData;
_clock = spiClock;
_select = slaveSelect;
_mySPI = NULL;
_hwSPI = false;
}
@ -41,27 +41,11 @@ void DAC8552::begin()
if(_hwSPI)
{
#if defined(ESP32)
if (_useHSPI) // HSPI
{
mySPI = new SPIClass(HSPI);
mySPI->end();
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
}
else // VSPI
{
mySPI = new SPIClass(VSPI);
mySPI->end();
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
}
#else // generic hardware SPI
mySPI = &SPI;
mySPI->end();
mySPI->begin();
#endif
_mySPI->end();
_mySPI->begin();
delay(1);
}
else // software SPI
else // SOFTWARE SPI
{
pinMode(_dataOut, OUTPUT);
pinMode(_clock, OUTPUT);
@ -76,21 +60,6 @@ void DAC8552::begin()
}
#if defined(ESP32)
void DAC8552::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clk;
_dataOut = mosi;
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
mySPI->end(); // disable SPI
mySPI->begin(clk, miso, mosi, select);
}
#endif
// channel = 0, 1, 2, 3 depending on type
// value = 0..65535
void DAC8552::bufferValue(uint8_t channel, uint16_t value)
@ -148,7 +117,7 @@ void DAC8552::setSPIspeed(uint32_t speed)
//////////////////////////////////////////////////////////////////
//
// PRIVATE
// PROTECTED
//
// channel = 0, 1, 2, 3 depending on type
@ -162,13 +131,13 @@ void DAC8552::updateDevice(uint8_t channel, bool directWrite)
digitalWrite(_select, LOW);
if (_hwSPI)
{
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(configRegister);
mySPI->transfer(_value[channel] >> 8);
mySPI->transfer(_value[channel] & 0xFF);
mySPI->endTransaction();
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer(configRegister);
_mySPI->transfer(_value[channel] >> 8);
_mySPI->transfer(_value[channel] & 0xFF);
_mySPI->endTransaction();
}
else // Software SPI
else // Software SPI
{
swSPI_transfer(configRegister);
swSPI_transfer(_value[channel] >> 8);
@ -192,5 +161,19 @@ void DAC8552::swSPI_transfer(uint8_t value)
}
/////////////////////////////////////////////////////////
//
// DERIVED CLASSES DAC8532
//
DAC8532::DAC8532(uint8_t select, __SPI_CLASS__ * spi) : DAC8552(select, spi)
{
}
DAC8532::DAC8532(uint8_t select, uint8_t spiData, uint8_t spiClock)
: DAC8552(select, spiData, spiClock)
{
}
// -- END OF FILE --

View File

@ -3,7 +3,7 @@
// FILE: DAC8552.h
// AUTHOR: Rob Tillaart
// PURPOSE: Arduino library for DAC8552 SPI Digital Analog Convertor
// VERSION: 0.2.5
// VERSION: 0.3.0
// DATE: 2017-12-14
// URL: https://github.com/RobTillaart/DAC8552
@ -12,7 +12,7 @@
#include "SPI.h"
#define DAC8552_LIB_VERSION (F("0.2.5"))
#define DAC8552_LIB_VERSION (F("0.3.0"))
#define DAC8552_POWERDOWN_NORMAL 0
@ -21,11 +21,20 @@
#define DAC8552_POWERDOWN_HIGH_IMP 3
#ifndef __SPI_CLASS__
#if defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
#endif
class DAC8552
{
public:
DAC8552(uint8_t slaveSelect);
DAC8552(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect);
DAC8552(uint8_t select, __SPI_CLASS__ * spi = &SPI);
DAC8552(uint8_t select, uint8_t spiData, uint8_t spiClock);
void begin();
@ -43,19 +52,8 @@ public:
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 hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif
private:
protected:
uint8_t _dataOut = 255;
uint8_t _clock = 255;
uint8_t _select = 255;
@ -69,12 +67,27 @@ private:
void updateDevice(uint8_t channel, bool directWrite);
void swSPI_transfer(uint8_t value);
SPIClass * mySPI;
SPISettings _spi_settings;
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
};
#if defined(ESP32)
bool _useHSPI = true;
#endif
/////////////////////////////////////////////////////////
//
// DERIVED DAC8532
//
#define DAC8532_POWERDOWN_NORMAL 0
#define DAC8532_POWERDOWN_1K 1
#define DAC8532_POWERDOWN_100K 2
#define DAC8532_POWERDOWN_HIGH_IMP 3
class DAC8532 : public DAC8552
{
public:
DAC8532(uint8_t select, __SPI_CLASS__ * spi = &SPI);
DAC8532(uint8_t select, uint8_t spiData, uint8_t spiClock);
};

View File

@ -21,6 +21,26 @@ The DAC8552 is a SPI based 16 bit DAC with two channels.
**Warning** This library is not tested extensively.
#### 0.3.0 Breaking change
The version 0.3.0 has breaking changes in the interface.
The essence is removal of ESP32 specific code from the library.
This makes it possible to support the ESP32-S3 and other processors in the future.
Also it makes the library a bit simpler to maintain.
Note order of parameters changed.
#### Related
- https://github.com/RobTillaart/DAC8550
- https://github.com/RobTillaart/DAC8551
- https://github.com/RobTillaart/DAC8552
- https://github.com/RobTillaart/DAC8554
- https://github.com/RobTillaart/MCP_DAC
- https://github.com/RobTillaart/AD5680 (18 bit DAC)
## Interface
```cpp
@ -29,9 +49,10 @@ The DAC8552 is a SPI based 16 bit DAC with two channels.
### Core
- **DAC8552(uint8_t slaveSelect)** Constructor for hardware SPI,
since 0.2.0 the slaveSelect pin needs to be defined.
- **DAC8552(uint8_t spiData, uint8_t spiClock, uint8_t slaveSelect)** Constructor for the software SPI
- **DAC8552(uint8_t select, SPIClassRP2040 \* spi = &SPI)** Constructor HW SPI RP2040.
- **DAC8552(uint8_t select, SPIClass \* spi = &SPI)** Constructor HW SPI other.
- **DAC8552(uint8_t select, uint8_t spiData, uint8_t spiClock)** Constructor SW SPI.
- **DAC8532(...)** idem constructors for DAC8532.
- **void begin()** initializes all pins to default state
- **void bufferValue(uint8_t channel, uint16_t value)**
- **void setValue(uint8_t channel, uint16_t value)** set the value of the chosen channel to 0 - 65535
@ -47,24 +68,6 @@ To be used only if one needs a specific speed.
- **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.
The **selectVSPI()** or the **selectHSPI()** needs to be called
BEFORE the **begin()** function.
#### Experimental
- **void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)**
overrule GPIO pins of ESP32 for hardware SPI. needs to be called
AFTER the **begin()** function.
### Power down
Check datasheet for details.

View File

@ -2,22 +2,22 @@
// FILE: DAC8552_hw_spi.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo DAC8552 library Arduino with hardware SPI
// VERSION: 0.2.0
// URL: https://github.com/RobTillaart/DAC8552
//
#include "DAC8552.h"
// HW SPI
// HW SPI
DAC8552 mydac(10);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DAC8552_LIB_VERSION: ");
Serial.println(DAC8552_LIB_VERSION);
mydac.begin();
}
@ -26,7 +26,7 @@ void loop()
{
uint8_t channel = 0;
// minimal sawtooth
// minimal sawtooth
for (uint16_t val = 0; val < 65500; val+= 30)
{
mydac.setValue(channel, val);
@ -39,7 +39,7 @@ void loop()
}
Serial.println();
// minimal sinus
// minimal sinus
for (long i = 0; i < 360; i++ )
{
long s = 32768 + 32768 * sin( i * (PI / 180.0));
@ -55,4 +55,6 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,9 +2,7 @@
// FILE: DAC8552_powerdown.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo DAC8552 library Arduino
// VERSION: 0.2.0
// URL: https://github.com/RobTillaart/DAC8552
//
#include "DAC8552.h"
@ -20,7 +18,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DAC8552_LIB_VERSION: ");
Serial.println(DAC8552_LIB_VERSION);
mydac.begin();
mydac.setValue(chanA, 0);
@ -55,4 +55,6 @@ void loop()
mydac.setPowerDown(chanA, DAC8552_POWERDOWN_NORMAL);
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,15 +2,13 @@
// FILE: DAC8552_same_time_write.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo DAC8552 library Arduino with hardware SPI
// VERSION: 0.2.0
// URL: https://github.com/RobTillaart/DAC8552
//
#include "DAC8552.h"
// HW SPI
// HW SPI
DAC8552 mydac(10);
uint32_t lastTime = 0;
@ -21,7 +19,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DAC8552_LIB_VERSION: ");
Serial.println(DAC8552_LIB_VERSION);
mydac.begin();
}
@ -31,9 +31,9 @@ void loop()
uint8_t chanA = 0;
uint8_t chanB = 1;
// opposite square waves by setting A and B simultaneous
// first buffer one value and then set the other.
// freq 10 Hz
// opposite square waves by setting A and B simultaneous
// first buffer one value and then set the other.
// freq 10 Hz
if (millis() - lastTime > 100)
{
lastTime = millis();
@ -53,5 +53,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -2,16 +2,14 @@
// FILE: DAC8552_sequential_write.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo DAC8552 library Arduino
// VERSION: 0.2.0
// URL: https://github.com/RobTillaart/DAC8552
//
#include "DAC8552.h"
// HW SPI
DAC8552 mydac(10);
// HW SPI
DAC8532 mydac(10);
uint32_t lastTime = 0;
uint16_t state = 0;
@ -21,7 +19,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DAC8552_LIB_VERSION: ");
Serial.println(DAC8552_LIB_VERSION);
mydac.begin();
}
@ -31,9 +31,9 @@ void loop()
uint8_t chanA = 0;
uint8_t chanB = 1;
// opposite square waves by setting A and B simultaneous
// first buffer one value and then set the other.
// freq 10 Hz
// opposite square waves by setting A and B simultaneous
// first buffer one value and then set the other.
// freq 10 Hz
if (millis() - lastTime > 100)
{
lastTime = millis();
@ -53,5 +53,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -10,7 +10,7 @@
#include "DAC8552.h"
// SW SPI pins 4,5,6
// SW SPI pins select, data, clock
DAC8552 mydac(4, 5, 6);
@ -18,7 +18,9 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DAC8552_LIB_VERSION: ");
Serial.println(DAC8552_LIB_VERSION);
mydac.begin();
}
@ -27,7 +29,7 @@ void loop()
{
uint8_t channel = 0;
// minimal sawtooth
// minimal sawtooth
for (uint16_t val = 0; val < 65500; val+= 30)
{
mydac.setValue(channel, val);
@ -40,7 +42,7 @@ void loop()
}
Serial.println();
// minimal sinus
// minimal sinus
for (long i = 0; i < 360; i++ )
{
long s = 32768 + 32768 * sin( i * (PI / 180.0));
@ -56,5 +58,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -15,8 +15,8 @@
#include "DAC8552.h"
DAC8552 mydac(12, 13, 14); // SW SPI
// DAC8552 mydac(10); // HW SPI
DAC8552 mydac(12, 13, 14); // SW SPI
// DAC8552 mydac(10); // HW SPI
uint16_t value = 0;
uint32_t start, stop;
@ -26,14 +26,16 @@ void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("DAC8552_LIB_VERSION: ");
Serial.println(DAC8552_LIB_VERSION);
mydac.begin();
}
void loop()
{
// simulate fast sawtooth;
// simulate fast sawtooth;
start = micros();
mydac.setValue(0, value);
stop = micros();
@ -43,4 +45,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -1,6 +1,7 @@
# Syntax Colouring Map For DAC8552
# Data types (KEYWORD1)
DAC8532 KEYWORD1
DAC8552 KEYWORD1
@ -19,19 +20,16 @@ getSPIspeed KEYWORD2
usesHWSPI KEYWORD2
selectHSPI KEYWORD2
selectVSPI KEYWORD2
usesHSPI KEYWORD2
usesVSPI KEYWORD2
setGPIOpins KEYWORD2
# Constants (LITERAL1)
DAC8552_LIB_VERSION LITERAL1
DAC8532_POWERDOWN_NORMAL LITERAL1
DAC8532_POWERDOWN_1K LITERAL1
DAC8532_POWERDOWN_100K LITERAL1
DAC8532_POWERDOWN_HIGH_IMP LITERAL1
DAC8552_POWERDOWN_NORMAL LITERAL1
DAC8552_POWERDOWN_1K LITERAL1
DAC8552_POWERDOWN_100K LITERAL1
DAC8552_POWERDOWN_HIGH_IMP LITERAL1

View File

@ -1,7 +1,7 @@
{
"name": "DAC8552",
"keywords": "DAC8552, SPI, digital, analog, convertor",
"description": "Arduino library for DAC8552 SPI Digital Analog Convertor",
"keywords": "DAC8532, DAC8552, SPI, digital, analog, convertor",
"description": "Arduino library for DAC8532 and DAC8552 SPI Digital Analog Convertor.",
"authors":
[
{
@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/DAC8552"
},
"version": "0.2.5",
"version": "0.3.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,9 +1,9 @@
name=DAC8552
version=0.2.5
version=0.3.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for DAC8552 SPI Digital Analog Convertor
paragraph=experimental
sentence=Arduino library for DAC8532 and DAC8552 SPI Digital Analog Convertor.
paragraph=
category=Sensors
url=https://github.com/RobTillaart/DAC8552
architectures=*