0.1.0 ADC081S

This commit is contained in:
Rob Tillaart 2024-01-12 17:11:19 +01:00
parent b50edf7ff8
commit ed33b12017
17 changed files with 716 additions and 0 deletions

View File

@ -0,0 +1,27 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:
packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
- uno
# - due
# - zero
# - leonardo
- m4
- esp32
# - esp8266
# - mega2560
- rpipico

4
libraries/ADC081S/.github/FUNDING.yml vendored Normal file
View File

@ -0,0 +1,4 @@
# These are supported funding model platforms
github: RobTillaart

View File

@ -0,0 +1,13 @@
name: Arduino-lint
on: [push, pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: arduino/arduino-lint-action@v1
with:
library-manager: update
compliance: strict

View File

@ -0,0 +1,17 @@
---
name: Arduino CI
on: [push, pull_request]
jobs:
runTest:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
- run: |
gem install arduino_ci
arduino_ci.rb

View File

@ -0,0 +1,18 @@
name: JSON check
on:
push:
paths:
- '**.json'
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: json-syntax-check
uses: limitusus/json-syntax-check@v1
with:
pattern: "\\.json$"

View File

@ -0,0 +1,155 @@
//
// FILE: ADC081S.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2024-01-10
// PURPOSE: Arduino library for ADC081S 8 bit ADC (SPI)
// URL: https://github.com/RobTillaart/ADC081S
#include "ADC081S.h"
// HARDWARE SPI
ADC081S::ADC081S(__SPI_CLASS__ * mySPI)
{
_dataIn = 255;
_clock = 255;
_select = 255;
_hwSPI = true;
_mySPI = mySPI;
_maxValue = 255;
}
// SOFTWARE SPI
ADC081S::ADC081S(uint8_t dataIn, uint8_t clock)
{
_dataIn = dataIn;
_clock = clock;
_select = 255;
_hwSPI = false;
_mySPI = NULL;
_maxValue = 255;
}
void ADC081S::begin(uint8_t select)
{
_select = select;
pinMode(_select, OUTPUT);
digitalWrite(_select, HIGH);
digitalWrite(_select, LOW);
digitalWrite(_select, HIGH);
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
if (_hwSPI) // hardware SPI
{
_mySPI->end();
_mySPI->begin();
}
else // software SPI
{
pinMode(_dataIn, INPUT);
pinMode(_clock, OUTPUT);
digitalWrite(_clock, HIGH);
}
}
uint16_t ADC081S::maxValue()
{
return _maxValue;
}
uint32_t ADC081S::count()
{
return _count;
}
uint16_t ADC081S::read()
{
return readADC();
}
void ADC081S::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
}
uint32_t ADC081S::getSPIspeed()
{
return _SPIspeed;
}
bool ADC081S::usesHWSPI()
{
return _hwSPI;
}
/////////////////////////////////////////////////////////////////////////////
//
// PROTECTED
//
uint16_t ADC081S::readADC()
{
_count++;
uint16_t data = 0;
digitalWrite(_select, LOW);
if (_hwSPI)
{
_mySPI->beginTransaction(_spi_settings);
data = _mySPI->transfer16(0);
_mySPI->endTransaction();
}
else // Software SPI
{
data = swSPI_transfer16();
}
digitalWrite(_select, HIGH);
return data >> 4; // remove 4 trailing zero's
}
// MSBFIRST
uint16_t ADC081S::swSPI_transfer16()
{
uint8_t clk = _clock;
uint8_t dai = _dataIn;
uint16_t rv = 0;
for (uint16_t mask = 0x8000; mask; mask >>= 1)
{
digitalWrite(clk, LOW);
digitalWrite(clk, HIGH);
if (digitalRead(dai) == HIGH) rv |= mask;
}
return rv;
}
//////////////////////////////////////////////////////////////////////
//
// DERIVED CLASSES
//
// TODO
// -- END OF FILE --

View File

@ -0,0 +1,79 @@
#pragma once
//
// FILE: ADC081S.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// DATE: 2024-01-10
// PURPOSE: Arduino library for ADC081S 8 bit ADC (SPI)
// URL: https://github.com/RobTillaart/ADC081S
//
#include "Arduino.h"
#include "SPI.h"
#define ADC081S_LIB_VERSION (F("0.1.0"))
#ifndef __SPI_CLASS__
#if defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
#endif
class ADC081S
{
public:
// HARDWARE SPI
ADC081S(__SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
ADC081S(uint8_t dataIn, uint8_t clock);
void begin(uint8_t select);
uint16_t maxValue();
uint16_t read();
// speed in Hz
void setSPIspeed(uint32_t speed);
uint32_t getSPIspeed();
// debugging
bool usesHWSPI();
uint32_t count(); // number of channels read.
protected:
uint8_t _dataIn;
uint8_t _clock;
uint8_t _select;
bool _hwSPI;
uint16_t _maxValue;
uint16_t readADC();
uint16_t swSPI_transfer16();
// 1 MHz is a safe value (datasheet); in a test 4 MHz worked.
uint32_t _SPIspeed = 1000000;
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
uint32_t _count;
};
//////////////////////////////////////////////////////////////////////
//
// DERIVED CLASSES
//
// TODO
// -- END OF FILE --

View File

@ -0,0 +1,11 @@
# Change Log ADC081S
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.0] - 2024-01-10
- initial version.

21
libraries/ADC081S/LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2024-2024 Rob Tillaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -0,0 +1,88 @@
[![Arduino CI](https://github.com/RobTillaart/ADC081S/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/ADC081S/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/ADC081S/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/ADC081S/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/ADC081S/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/ADC081S.svg)](https://github.com/RobTillaart/ADC081S/issues)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/ADC081S/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/ADC081S.svg?maxAge=3600)](https://github.com/RobTillaart/ADC081S/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/ADC081S.svg)](https://registry.platformio.org/libraries/robtillaart/ADC081S)
# ADC081S
Arduino library for ADC081S 8 bit ADC (SPI).
## Description
This library reads the ADC ports of the ADC081S ADC convertor.
Feedback is as always welcome.
#### Related
- https://gammon.com.au/adc tutorial about ADC's (UNO specific)
- https://github.com/RobTillaart/MCP_ADC
- https://github.com/RobTillaart/ADS1x15 (12 & 16 bit ADC, I2C, slow)
- https://github.com/RobTillaart/PCF8591 (8 bit ADC + 1 bit DAC)
- https://github.com/RobTillaart/MCP_DAC
## Interface
```cpp
#include "ADC081S.h"
```
#### Constructors
- **ADC081S(SPIClassRP2040 \* mySPI = &SPI)** hardware constructor RP2040
- **ADC081S(SPIClass \* mySPI = &SPI)** hardware constructor other
- **ADC081S(uint8_t dataOut, uint8_t clock)**
- **void begin(uint8_t select)** set select pin.
- **int16_t maxValue()** returns maxReading of ADC, => 256
#### Base
- **uint16_t read()** reads the value of a single channel.
- **void setSPIspeed(uint32_t speed)** sets SPI clock in **Hz**, please read datasheet
of the ADC first to get optimal speed.
- **uint32_t getSPIspeed()** gets current speed in **Hz**.
### Debug
- **bool usesHWSPI()** returns true if hardware SPI is used.
- **uint32_t count()** returns number of reads since start.
## Future
#### Must
- improve documentation
- improve all
- get hardware to test
#### Should
- add shutDown() + wakeUp() // select between normal and shutdown mode.
-
#### Could
#### Wont
## Support
If you appreciate my libraries, you can support the development and maintenance.
Improve the quality of the libraries by providing issues and Pull Requests, or
donate through PayPal or GitHub sponsors.
Thank you,

View File

@ -0,0 +1,67 @@
//
// FILE: ADC081S_performance.ino
// AUTHOR: Rob Tillaart
// PURPOSE: performance measurement for ADC081S SPI based ADC
// URL: https://github.com/RobTillaart/ADC081S
//
#include "ADC081S.h"
ADC081S adc; // HW SPI
// ADC081S adc(6, 7); // SW SPI UNO - adjust pins if needed
// ADC081S adc(20, 21); // SW SPI ESP32 - adjust pins if needed
const uint8_t SELECT_PIN = 10;
uint32_t start, stop, read_time;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("ADC081S_LIB_VERSION: ");
Serial.println(ADC081S_LIB_VERSION);
Serial.println();
Serial.println("ADC\tMAXVALUE");
Serial.print("adc\t");
Serial.println(adc.maxValue());
Serial.println("\nTiming in micros().\n");
delay(100);
Serial.println("***************************************\n");
for (int s = 1; s <= 16; s++)
{
uint32_t val = 0;
uint32_t speed = s * 1000000UL;
adc.setSPIspeed(speed);
adc.begin(SELECT_PIN);
delay(100);
start = micros();
for (int i = 0; i < 1000; i++)
{
val += adc.read();
}
stop = micros();
read_time = stop - start;
Serial.print(speed);
Serial.print("\tadc.read()\t");
Serial.print(read_time);
Serial.print("\t");
Serial.println(val);
delay(100);
}
Serial.println("done...");
}
void loop()
{
}
// -- END OF FILE --

View File

@ -0,0 +1,29 @@
## Performance test
Test sketch: ADC081S_performance.ino
### Library version: 0.1.0
There is only a transfer of 2 bytes. Times in microseconds.
| device | Test | HW 1 MHz | HW 2 MHz | HW 4 MHz | HW 8 MHz | HW 10 MHz | HW 12 MHz | HW 14 MHz | HW 16 MHz |
|:-----------|:-------------|:--------:|:--------:|:--------:|:---------:|:---------:|:---------:|:---------:|:---------:|
| UNO | adc.read() | 33.6 | 25.6 | 21.6 | 19.6 | - | - | - | - |
| ESP32 | adc.read() | 25.4 | 16.7 | 12.2 | 10.0 | 9.6 | 9.4 | 9.2 | 8.9 |
| device | Test | SW max speed | notes |
|:---------|:-------------|--------------:|:--------|
| UNO | adc.read() | 238.2 | UNO has relative slow digital IO
| ESP32 | adc.read() | 6.9 | faster than HW SPI as it has no transfer overhead.
### Notes

View File

@ -0,0 +1,67 @@
//
// FILE: ADC081S_read.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/ADC081S
#include "ADC081S.h"
ADC081S adc01; // use HWSPI
ADC081S adc02(6, 7); // use SWSPI
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("ADC081S_LIB_VERSION: ");
Serial.println(ADC081S_LIB_VERSION);
adc01.begin(10);
adc02.begin(5);
Serial.println();
Serial.println("ADC\tMAXVALUE");
Serial.print("adc01\t");
Serial.println(adc01.maxValue());
delay(10);
start = micros();
uint16_t val = adc01.read();
stop = micros();
Serial.print("hwspi:\t");
Serial.print(stop - start);
Serial.print("\t");
Serial.println(val);
Serial.print("adc02\t");
Serial.println(adc02.maxValue());
delay(10);
start = micros();
val = adc02.read();
stop = micros();
Serial.print("swspi:\t");
Serial.print(stop - start);
Serial.print("\t");
Serial.println(val);
Serial.println();
}
void loop()
{
Serial.print("adc01:\t");
uint16_t val = adc01.read();
Serial.println(val);
Serial.print("adc02:\t");
val = adc02.read();
Serial.println(val);
delay(5000);
}
// -- END OF FILE --

View File

@ -0,0 +1,23 @@
# Syntax Colouring Map For ADC081S
# Data types (KEYWORD1)
ADC081S KEYWORD1
# Methods and Functions (KEYWORD2)
begin KEYWORD2
maxValue KEYWORD2
read KEYWORD2
setSPIspeed KEYWORD2
getSPIspeed KEYWORD2
usesHWSPI KEYWORD2
count KEYWORD2
# Constants (LITERAL1)
ADC081S_LIB_VERSION LITERAL1

View File

@ -0,0 +1,23 @@
{
"name": "ADC081S",
"keywords": "",
"description": "Arduino library for ADC081S 8 bit ADC (SPI)",
"authors":
[
{
"name": "Rob Tillaart",
"email": "Rob.Tillaart@gmail.com",
"maintainer": true
}
],
"repository":
{
"type": "git",
"url": "https://github.com/RobTillaart/ADC081S.git"
},
"version": "0.1.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
"headers": "ADC081S.h"
}

View File

@ -0,0 +1,11 @@
name=ADC081S
version=0.1.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for ADC081S 8 bit ADC (SPI)
paragraph=
category=Sensors
url=https://github.com/RobTillaart/ADC081S
architectures=*
includes=ADC081S.h
depends=

View File

@ -0,0 +1,63 @@
//
// FILE: unit_test_001.cpp
// AUTHOR: Rob Tillaart
// DATE: 2024-01-10
// PURPOSE: unit tests for the ADC081S
// https://github.com/RobTillaart/ADC081S
// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md
//
// supported assertions
// ----------------------------
// assertEqual(expected, actual); // a == b
// assertNotEqual(unwanted, actual); // a != b
// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b))
// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b))
// assertLess(upperBound, actual); // a < b
// assertMore(lowerBound, actual); // a > b
// assertLessOrEqual(upperBound, actual); // a <= b
// assertMoreOrEqual(lowerBound, actual); // a >= b
// assertTrue(actual);
// assertFalse(actual);
// assertNull(actual);
// // special cases for floats
// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon
// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon
// assertInfinity(actual); // isinf(a)
// assertNotInfinity(actual); // !isinf(a)
// assertNAN(arg); // isnan(a)
// assertNotNAN(arg); // !isnan(a)
#include <ArduinoUnitTests.h>
#include "Arduino.h"
#include "ADC081S.h"
unittest_setup()
{
fprintf(stderr, "ADC081S_LIB_VERSION: %s\n", (char *) ADC081S_LIB_VERSION);
}
unittest_teardown()
{
}
unittest(test_constructor)
{
ADC081S ADC_01;
ADC_01.begin(10);
assertEqual(255, ADC_01.maxValue());
}
unittest_main()
// -- END OF FILE --