mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.0 MCP_POT
This commit is contained in:
parent
afb6e19353
commit
90af9f4f5e
27
libraries/MCP_POT/.arduino-ci.yml
Normal file
27
libraries/MCP_POT/.arduino-ci.yml
Normal 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/MCP_POT/.github/FUNDING.yml
vendored
Normal file
4
libraries/MCP_POT/.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
# These are supported funding model platforms
|
||||
|
||||
github: RobTillaart
|
||||
|
13
libraries/MCP_POT/.github/workflows/arduino-lint.yml
vendored
Normal file
13
libraries/MCP_POT/.github/workflows/arduino-lint.yml
vendored
Normal 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
|
17
libraries/MCP_POT/.github/workflows/arduino_test_runner.yml
vendored
Normal file
17
libraries/MCP_POT/.github/workflows/arduino_test_runner.yml
vendored
Normal 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
|
18
libraries/MCP_POT/.github/workflows/jsoncheck.yml
vendored
Normal file
18
libraries/MCP_POT/.github/workflows/jsoncheck.yml
vendored
Normal 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$"
|
||||
|
11
libraries/MCP_POT/CHANGELOG.md
Normal file
11
libraries/MCP_POT/CHANGELOG.md
Normal file
@ -0,0 +1,11 @@
|
||||
# Change Log MCP_POT
|
||||
|
||||
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] - 2023-12-21
|
||||
- initial version.
|
||||
|
21
libraries/MCP_POT/LICENSE
Normal file
21
libraries/MCP_POT/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023-2023 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.
|
292
libraries/MCP_POT/MCP_POT.cpp
Normal file
292
libraries/MCP_POT/MCP_POT.cpp
Normal file
@ -0,0 +1,292 @@
|
||||
//
|
||||
// FILE: MCP_POT.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2023-12-21
|
||||
// PURPOSE: Arduino library for MCP41xxx and MCP42xxx SPI based digital potentiometers.
|
||||
// URL: https://github.com/RobTillaart/MCP_POT
|
||||
|
||||
|
||||
#include "MCP_POT.h"
|
||||
|
||||
// see page 18 datasheet
|
||||
#define MCP_POT_IGNORE_CMD 0x00
|
||||
#define MCP_POT_WRITE_CMD 0x10
|
||||
#define MCP_POT_SHUTDOWN_CMD 0x20
|
||||
#define MCP_POT_NONE_CMD 0x30
|
||||
|
||||
|
||||
// HARDWARE SPI
|
||||
MCP_POT::MCP_POT(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
|
||||
{
|
||||
_pmCount = 2;
|
||||
_select = select;
|
||||
_reset = reset;
|
||||
_shutdown = shutdown;
|
||||
_dataOut = 255;
|
||||
_clock = 255;
|
||||
_hwSPI = true;
|
||||
_mySPI = mySPI;
|
||||
}
|
||||
|
||||
|
||||
// SOFTWARE SPI
|
||||
MCP_POT::MCP_POT(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
||||
{
|
||||
_pmCount = 2;
|
||||
_select = select;
|
||||
_reset = reset;
|
||||
_shutdown = shutdown;
|
||||
_dataOut = dataOut;
|
||||
_clock = clock;
|
||||
_hwSPI = false;
|
||||
_mySPI = NULL;
|
||||
}
|
||||
|
||||
|
||||
void MCP_POT::begin(uint8_t value)
|
||||
{
|
||||
pinMode(_select, OUTPUT);
|
||||
digitalWrite(_select, HIGH);
|
||||
pinMode(_reset, OUTPUT);
|
||||
digitalWrite(_reset, HIGH);
|
||||
pinMode(_shutdown, OUTPUT);
|
||||
digitalWrite(_shutdown, HIGH);
|
||||
|
||||
setSPIspeed(8000000);
|
||||
|
||||
if(_hwSPI)
|
||||
{
|
||||
_mySPI->end();
|
||||
_mySPI->begin();
|
||||
delay(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
pinMode(_dataOut, OUTPUT);
|
||||
pinMode(_clock, OUTPUT);
|
||||
digitalWrite(_dataOut, LOW);
|
||||
digitalWrite(_clock, LOW);
|
||||
}
|
||||
|
||||
reset(value);
|
||||
}
|
||||
|
||||
|
||||
void MCP_POT::reset(uint8_t value)
|
||||
{
|
||||
digitalWrite(_reset, LOW);
|
||||
digitalWrite(_reset, HIGH);
|
||||
setValue(value); // set all to same value.
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SET VALUE
|
||||
//
|
||||
bool MCP_POT::setValue(uint8_t value)
|
||||
{
|
||||
_value[0] = value;
|
||||
_value[1] = value;
|
||||
updateDevice(2, value, MCP_POT_WRITE_CMD);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool MCP_POT::setValue(uint8_t pm, uint8_t value)
|
||||
{
|
||||
if (pm >= _pmCount) return false;
|
||||
_value[pm] = value;
|
||||
updateDevice(pm, value, MCP_POT_WRITE_CMD);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
uint8_t MCP_POT::getValue(uint8_t pm)
|
||||
{
|
||||
if (pm >= _pmCount) return 0;
|
||||
return _value[pm];
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// OTHER
|
||||
//
|
||||
uint8_t MCP_POT::pmCount()
|
||||
{
|
||||
return _pmCount;
|
||||
}
|
||||
|
||||
|
||||
void MCP_POT::powerOn()
|
||||
{
|
||||
digitalWrite(_shutdown, HIGH);
|
||||
}
|
||||
|
||||
|
||||
void MCP_POT::powerOff()
|
||||
{
|
||||
digitalWrite(_shutdown, LOW);
|
||||
}
|
||||
|
||||
|
||||
bool MCP_POT::isPowerOn()
|
||||
{
|
||||
return digitalRead(_shutdown) == HIGH;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// SPI
|
||||
//
|
||||
void MCP_POT::setSPIspeed(uint32_t speed)
|
||||
{
|
||||
_SPIspeed = speed;
|
||||
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
|
||||
}
|
||||
|
||||
|
||||
uint32_t MCP_POT::getSPIspeed()
|
||||
{
|
||||
return _SPIspeed;
|
||||
}
|
||||
|
||||
|
||||
bool MCP_POT::usesHWSPI()
|
||||
{
|
||||
return _hwSPI;
|
||||
}
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// PROTECTED
|
||||
//
|
||||
void MCP_POT::updateDevice(uint8_t pm, uint8_t value, uint8_t cmd)
|
||||
{
|
||||
uint8_t command = cmd;
|
||||
if (pm == 0) command |= 1; // 01
|
||||
if (pm == 1) command |= 2; // 10
|
||||
if (pm == 2) command |= 3; // 11 => both potentiometers
|
||||
// otherwise ignore
|
||||
digitalWrite(_select, LOW);
|
||||
if (_hwSPI)
|
||||
{
|
||||
_mySPI->beginTransaction(_spi_settings);
|
||||
_mySPI->transfer(command);
|
||||
_mySPI->transfer(value);
|
||||
_mySPI->endTransaction();
|
||||
}
|
||||
else // Software SPI
|
||||
{
|
||||
swSPI_transfer(command);
|
||||
swSPI_transfer(value);
|
||||
}
|
||||
digitalWrite(_select, HIGH);
|
||||
}
|
||||
|
||||
|
||||
// MSBFIRST
|
||||
void MCP_POT::swSPI_transfer(uint8_t val)
|
||||
{
|
||||
uint8_t clk = _clock;
|
||||
uint8_t dao = _dataOut;
|
||||
// MSBFIRST
|
||||
for (uint8_t mask = 0x80; mask; mask >>= 1)
|
||||
{
|
||||
digitalWrite(dao,(val & mask));
|
||||
digitalWrite(clk, HIGH);
|
||||
digitalWrite(clk, LOW);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED CLASSES MCP41000 SERIES
|
||||
//
|
||||
MCP41010::MCP41010(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
|
||||
:MCP_POT(select, reset, shutdown, mySPI)
|
||||
{
|
||||
_pmCount = 1;
|
||||
}
|
||||
|
||||
MCP41010::MCP41010(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
||||
:MCP_POT(select, reset, shutdown, dataOut, clock)
|
||||
{
|
||||
_pmCount = 1;
|
||||
}
|
||||
|
||||
MCP41050::MCP41050(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
|
||||
:MCP_POT(select, reset, shutdown, mySPI)
|
||||
{
|
||||
_pmCount = 1;
|
||||
}
|
||||
|
||||
MCP41050::MCP41050(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
||||
:MCP_POT(select, reset, shutdown, dataOut, clock)
|
||||
{
|
||||
_pmCount = 1;
|
||||
}
|
||||
|
||||
MCP41100::MCP41100(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
|
||||
:MCP_POT(select, reset, shutdown, mySPI)
|
||||
{
|
||||
_pmCount = 1;
|
||||
}
|
||||
|
||||
MCP41100::MCP41100(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
||||
:MCP_POT(select, reset, shutdown, dataOut, clock)
|
||||
{
|
||||
_pmCount = 1;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED CLASSES MCP42000 SERIES
|
||||
//
|
||||
MCP42010::MCP42010(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
|
||||
:MCP_POT(select, reset, shutdown, mySPI)
|
||||
{
|
||||
_pmCount = 2;
|
||||
}
|
||||
|
||||
MCP42010::MCP42010(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
||||
:MCP_POT(select, reset, shutdown, dataOut, clock)
|
||||
{
|
||||
_pmCount = 2;
|
||||
}
|
||||
|
||||
MCP42050::MCP42050(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
|
||||
:MCP_POT(select, reset, shutdown, mySPI)
|
||||
{
|
||||
_pmCount = 2;
|
||||
}
|
||||
|
||||
MCP42050::MCP42050(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
||||
:MCP_POT(select, reset, shutdown, dataOut, clock)
|
||||
{
|
||||
_pmCount = 2;
|
||||
}
|
||||
|
||||
MCP42100::MCP42100(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI)
|
||||
:MCP_POT(select, reset, shutdown, mySPI)
|
||||
{
|
||||
_pmCount = 2;
|
||||
}
|
||||
|
||||
MCP42100::MCP42100(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)
|
||||
:MCP_POT(select, reset, shutdown, dataOut, clock)
|
||||
{
|
||||
_pmCount = 2;
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
139
libraries/MCP_POT/MCP_POT.h
Normal file
139
libraries/MCP_POT/MCP_POT.h
Normal file
@ -0,0 +1,139 @@
|
||||
#pragma once
|
||||
//
|
||||
// FILE: MCP_POT.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.0
|
||||
// DATE: 2023-12-21
|
||||
// PURPOSE: Arduino library for MCP41xxx and MCP42xxx SPI based digital potentiometers.
|
||||
// URL: https://github.com/RobTillaart/MCP_POT
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "SPI.h"
|
||||
|
||||
|
||||
#define MCP_POT_LIB_VERSION (F("0.1.0"))
|
||||
|
||||
#ifndef MCP_POT_MIDDLE_VALUE
|
||||
#define MCP_POT_MIDDLE_VALUE 128
|
||||
#endif
|
||||
|
||||
#ifndef MCP_POT_MAX_VALUE
|
||||
#define MCP_POT_MAX_VALUE 255
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef __SPI_CLASS__
|
||||
#if defined(ARDUINO_ARCH_RP2040)
|
||||
#define __SPI_CLASS__ SPIClassRP2040
|
||||
#else
|
||||
#define __SPI_CLASS__ SPIClass
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
class MCP_POT
|
||||
{
|
||||
public:
|
||||
// HARDWARE SPI
|
||||
MCP_POT(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
|
||||
// SOFTWARE SPI
|
||||
MCP_POT(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
|
||||
|
||||
void begin(uint8_t value = MCP_POT_MIDDLE_VALUE);
|
||||
void reset(uint8_t value = MCP_POT_MIDDLE_VALUE);
|
||||
|
||||
// set both potmeters
|
||||
bool setValue(uint8_t value);
|
||||
// set single potmeter (0 or 1
|
||||
bool setValue(uint8_t pm, uint8_t value);
|
||||
uint8_t getValue(uint8_t pm = 0);
|
||||
|
||||
// speed in Hz
|
||||
void setSPIspeed(uint32_t speed);
|
||||
uint32_t getSPIspeed();
|
||||
|
||||
// MISC
|
||||
uint8_t pmCount();
|
||||
void powerOn();
|
||||
void powerOff();
|
||||
bool isPowerOn();
|
||||
|
||||
// debugging
|
||||
bool usesHWSPI();
|
||||
|
||||
|
||||
protected:
|
||||
uint8_t _dataOut;
|
||||
uint8_t _clock;
|
||||
uint8_t _select;
|
||||
uint8_t _reset;
|
||||
uint8_t _shutdown;
|
||||
bool _hwSPI;
|
||||
uint32_t _SPIspeed;
|
||||
|
||||
uint8_t _value[2];
|
||||
uint8_t _pmCount;
|
||||
|
||||
void updateDevice(uint8_t pm, uint8_t value, uint8_t cmd);
|
||||
void swSPI_transfer(uint8_t value);
|
||||
|
||||
__SPI_CLASS__ * _mySPI;
|
||||
SPISettings _spi_settings;
|
||||
};
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED CLASSES MCP41000 SERIES
|
||||
//
|
||||
class MCP41010 : public MCP_POT
|
||||
{
|
||||
public:
|
||||
MCP41010(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
|
||||
MCP41010(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
|
||||
};
|
||||
|
||||
class MCP41050 : public MCP_POT
|
||||
{
|
||||
public:
|
||||
MCP41050(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
|
||||
MCP41050(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
|
||||
};
|
||||
|
||||
class MCP41100 : public MCP_POT
|
||||
{
|
||||
public:
|
||||
MCP41100(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
|
||||
MCP41100(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
|
||||
};
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// DERIVED CLASSES MCP42000 SERIES
|
||||
//
|
||||
class MCP42010 : public MCP_POT
|
||||
{
|
||||
public:
|
||||
MCP42010(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
|
||||
MCP42010(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
|
||||
};
|
||||
|
||||
class MCP42050 : public MCP_POT
|
||||
{
|
||||
public:
|
||||
MCP42050(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
|
||||
MCP42050(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
|
||||
};
|
||||
|
||||
class MCP42100 : public MCP_POT
|
||||
{
|
||||
public:
|
||||
MCP42100(uint8_t select, uint8_t reset, uint8_t shutdown, __SPI_CLASS__ * mySPI = &SPI);
|
||||
MCP42100(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock);
|
||||
};
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
|
153
libraries/MCP_POT/README.md
Normal file
153
libraries/MCP_POT/README.md
Normal file
@ -0,0 +1,153 @@
|
||||
|
||||
[![Arduino CI](https://github.com/RobTillaart/MCP_POT/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
|
||||
[![Arduino-lint](https://github.com/RobTillaart/MCP_POT/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MCP_POT/actions/workflows/arduino-lint.yml)
|
||||
[![JSON check](https://github.com/RobTillaart/MCP_POT/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/MCP_POT/actions/workflows/jsoncheck.yml)
|
||||
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/MCP_POT.svg)](https://github.com/RobTillaart/MCP_POT/issues)
|
||||
|
||||
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MCP_POT/blob/master/LICENSE)
|
||||
[![GitHub release](https://img.shields.io/github/release/RobTillaart/MCP_POT.svg?maxAge=3600)](https://github.com/RobTillaart/MCP_POT/releases)
|
||||
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/MCP_POT.svg)](https://registry.platformio.org/libraries/robtillaart/MCP_POT)
|
||||
|
||||
|
||||
# MCP_POT
|
||||
|
||||
Arduino library for MCP41xxx and MCP42xxx SPI based digital potentiometers.
|
||||
|
||||
|
||||
## Description
|
||||
|
||||
**Experimental** to be tested on hardware.
|
||||
|
||||
The MCP_POT library implements digital potentiometers.
|
||||
The chips have 1 or 2 potentiometers, 10 KΩ, 50 KΩ and 100 KΩ and communicates over SPI.
|
||||
The library supports both hardware SPI and software SPI.
|
||||
|
||||
|
||||
| type | KΩ | potentiometers | notes |
|
||||
|:-----------|:------:|:----------------:|:--------|
|
||||
| MCP41010 | 10 | 1 | not tested yet.
|
||||
| MCP41050 | 50 | 1 |
|
||||
| MCP41100 | 100 | 1 |
|
||||
| MCP42010 | 10 | 2 | daisy chain allowed
|
||||
| MCP42050 | 50 | 2 |
|
||||
| MCP42100 | 100 | 2 |
|
||||
|
||||
|
||||
Current version allows manual override of the hardware SPI clock.
|
||||
|
||||
Alt-234 = Ω
|
||||
|
||||
|
||||
#### Related
|
||||
|
||||
Mainly other digital potentiometers.
|
||||
|
||||
- https://github.com/RobTillaart/AD520x
|
||||
- https://github.com/RobTillaart/AD524X
|
||||
- https://github.com/RobTillaart/AD5245
|
||||
- https://github.com/RobTillaart/AD5144A
|
||||
- https://github.com/RobTillaart/AD5263
|
||||
- https://github.com/RobTillaart/X9C10X
|
||||
|
||||
|
||||
## Interface
|
||||
|
||||
```cpp
|
||||
#include "MCP_POT.h"
|
||||
```
|
||||
|
||||
#### Constructors
|
||||
|
||||
Base class.
|
||||
- **MCP_POT(uint8_t select, uint8_t reset, uint8_t shutdown, SPIClassRP2040 \* mySPI = &SPI)** hardware constructor RP2040
|
||||
- **MCP_POT(uint8_t select, uint8_t reset, uint8_t shutdown, SPIClass \* mySPI = &SPI)** hardware constructor other
|
||||
- **MCP_POT(uint8_t select, uint8_t reset, uint8_t shutdown, uint8_t dataOut, uint8_t clock)**
|
||||
|
||||
The derived classes have same constructors with same parameters as the base class.
|
||||
- **MCP41010(...)** constructor 1 potentiometer, 10 KΩ
|
||||
- **MCP41050(...)** constructor 1 potentiometer, 50 KΩ
|
||||
- **MCP41100(...)** constructor 1 potentiometer, 100 KΩ
|
||||
- **MCP42010(...)** constructor 2 potentiometer, 10 KΩ
|
||||
- **MCP42050(...)** constructor 2 potentiometer, 50 KΩ
|
||||
- **MCP42100(...)** constructor 2 potentiometer, 100 KΩ
|
||||
- **void begin(uint8_t value = MCP_POT_MIDDLE_VALUE)**
|
||||
- **void reset(uint8_t value = MCP_POT_MIDDLE_VALUE)**
|
||||
- **bool setValue(uint8_t value)** set both potmeters.
|
||||
- **bool setValue(uint8_t pm, uint8_t value)**
|
||||
- **uint8_t getValue(uint8_t pm = 0)**
|
||||
|
||||
|
||||
#### SPI
|
||||
|
||||
- **void setSPIspeed(uint32_t speed)** sets SPI clock in **Hz**.
|
||||
- **uint32_t getSPIspeed()** gets current speed in **Hz**.
|
||||
|
||||
|
||||
#### Miscellaneous
|
||||
|
||||
- **uint8_t pmCount()** returns the number of potmeters.
|
||||
- **void powerOn()** set SHUTDOWN pin HIGH (device enabled).
|
||||
- **void powerOff()** set SHUTDOWN pin LOW (device disabled).
|
||||
- **bool isPowerOn()** idem.
|
||||
|
||||
|
||||
#### Debug
|
||||
|
||||
- **bool usesHWSPI()** returns true if hardware SPI is used.
|
||||
|
||||
|
||||
## About SPI Speed
|
||||
|
||||
SPI code is based upon MCP_ADC.
|
||||
|
||||
The default SPI speed is reduced to 1 MHz.
|
||||
This is the value recommended in the datasheet for 2.7 Volt.
|
||||
|
||||
| Board | Voltage | safe | max |
|
||||
|:-------:|:---------:|:-------:|:-------:|
|
||||
| ESP32 | 2.7V | 1 MHz | 4 MHz |
|
||||
| UNO | 5.0V | 2 MHz | 4 MHz |
|
||||
|
||||
For hardware SPI the ESP32 uses the VSPI pins. (see ESP examples).
|
||||
|
||||
|
||||
## Daisy Chaining
|
||||
|
||||
Not supported yet. (need hardware)
|
||||
|
||||
The MCP42xxx series have a **dataout** pin which allows to daisy chain the devices.
|
||||
The devices must share a CS signal, or at least all of them should have been
|
||||
selected to forward bytes.
|
||||
WHen the CS signal goes HIGH again, all devices will simultaneously change to their
|
||||
new values. However per device at most one potmeter can be set in a daisy chain, or both have the same value.
|
||||
|
||||
|
||||
## Future
|
||||
|
||||
#### Must
|
||||
|
||||
- improve documentation
|
||||
- buy hardware and test
|
||||
|
||||
#### Should
|
||||
|
||||
- investigate and implement daisy chaining of MCP42xxx
|
||||
|
||||
|
||||
#### Could
|
||||
|
||||
- improve SWSPI for AVR
|
||||
(code is under test for MCP23S17)
|
||||
|
||||
|
||||
#### 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,
|
||||
|
122
libraries/MCP_POT/examples/MCP_POT_demo/MCP_POT_demo.ino
Normal file
122
libraries/MCP_POT/examples/MCP_POT_demo/MCP_POT_demo.ino
Normal file
@ -0,0 +1,122 @@
|
||||
//
|
||||
// FILE: MCP_POT_demo.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo
|
||||
// URL: https://github.com/RobTillaart/MCP_POT
|
||||
|
||||
|
||||
#include "MCP_POT.h"
|
||||
|
||||
uint32_t start, stop;
|
||||
|
||||
|
||||
// select, reset, shutdown, data, clock == SOFTWARE SPI
|
||||
MCP_POT pot(10, 11, 12, 8, 9);
|
||||
|
||||
// select, reset, shutdown, &SPI === HW SPI UNO clock = 13, data = 11
|
||||
// MCP_POT pot(5, 6, 7, &SPI);
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
pot.begin();
|
||||
|
||||
// test_extremes();
|
||||
// test_sinus();
|
||||
// test_sawtooth();
|
||||
test_timing();
|
||||
|
||||
Serial.println("\nDone...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
void test_extremes()
|
||||
{
|
||||
Serial.println(__FUNCTION__);
|
||||
delay(10);
|
||||
|
||||
Serial.println("0");
|
||||
pot.setValue(0, 0);
|
||||
delay(2000);
|
||||
|
||||
Serial.println(MCP_POT_MIDDLE_VALUE);
|
||||
pot.setValue(0, MCP_POT_MIDDLE_VALUE);
|
||||
delay(2000);
|
||||
|
||||
Serial.println(MCP_POT_MAX_VALUE);
|
||||
pot.setValue(0, MCP_POT_MAX_VALUE);
|
||||
delay(2000);
|
||||
}
|
||||
|
||||
|
||||
// connect all A GND and B 5V
|
||||
// every W will have a different signal (same freq).
|
||||
void test_sinus()
|
||||
{
|
||||
Serial.println(__FUNCTION__);
|
||||
delay(10);
|
||||
|
||||
start = millis();
|
||||
uint32_t i = 0;
|
||||
while (millis() - start < 10000)
|
||||
{
|
||||
int8_t value = 127 * sin(i * TWO_PI / 100);
|
||||
pot.setValue(0, 128 + value);
|
||||
pot.setValue(1, 128 + value / 2);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// straightforward sawtooth.
|
||||
void test_sawtooth()
|
||||
{
|
||||
Serial.println(__FUNCTION__);
|
||||
delay(10);
|
||||
|
||||
start = millis();
|
||||
uint8_t i = 0;
|
||||
while (millis() - start < 25500)
|
||||
{
|
||||
pot.setValue(0, i++); // auto wrap is fast...
|
||||
delay(100);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void test_timing()
|
||||
{
|
||||
Serial.println(__FUNCTION__);
|
||||
delay(10);
|
||||
|
||||
start = micros();
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
pot.setValue(0, i++); // auto wrap is fast...
|
||||
}
|
||||
stop = micros();
|
||||
Serial.print("1000 x setValue():\t");
|
||||
Serial.println(stop - start);
|
||||
delay(10);
|
||||
|
||||
volatile int x = 0;
|
||||
start = micros();
|
||||
for (int i = 0; i < 500; i++)
|
||||
{
|
||||
x += pot.getValue(0);
|
||||
x += pot.getValue(1);
|
||||
}
|
||||
stop = micros();
|
||||
Serial.print("1000 x getValue():\t");
|
||||
Serial.println(stop - start);
|
||||
delay(10);
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
24
libraries/MCP_POT/examples/MCP_POT_demo/output_0.1.0.txt
Normal file
24
libraries/MCP_POT/examples/MCP_POT_demo/output_0.1.0.txt
Normal file
@ -0,0 +1,24 @@
|
||||
|
||||
IDE: 1.8.19
|
||||
board: UNO
|
||||
|
||||
MCP_POT_demo.ino
|
||||
|
||||
HARDWARE SPI
|
||||
================
|
||||
test_timing
|
||||
1000 x setValue(): 7548
|
||||
1000 x getValue(): 760
|
||||
|
||||
Done...
|
||||
|
||||
|
||||
SOFTWARE SPI
|
||||
================
|
||||
test_timing
|
||||
1000 x setValue(): 106448
|
||||
1000 x getValue(): 752
|
||||
|
||||
|
||||
Done...
|
||||
|
30
libraries/MCP_POT/keywords.txt
Normal file
30
libraries/MCP_POT/keywords.txt
Normal file
@ -0,0 +1,30 @@
|
||||
# Syntax Colouring Map For MCP_POT
|
||||
|
||||
# Data types (KEYWORD1)
|
||||
MCP_POT KEYWORD1
|
||||
MCP41010 KEYWORD1
|
||||
MCP41050 KEYWORD1
|
||||
MCP41100 KEYWORD1
|
||||
MCP42010 KEYWORD1
|
||||
MCP42050 KEYWORD1
|
||||
MCP42100 KEYWORD1
|
||||
|
||||
|
||||
# Methods and Functions (KEYWORD2)
|
||||
begin KEYWORD2
|
||||
reset KEYWORD2
|
||||
|
||||
setValue KEYWORD2
|
||||
getValue KEYWORD2
|
||||
|
||||
setSPIspeed KEYWORD2
|
||||
getSPIspeed KEYWORD2
|
||||
pmCount KEYWORD2
|
||||
usesHWSPI KEYWORD2
|
||||
|
||||
|
||||
# Constants (LITERAL1)
|
||||
MCP_POT_LIB_VERSION LITERAL1
|
||||
|
||||
MCP_POT_MIDDLE_VALUE LITERAL1
|
||||
MCP_POT_MAX_VALUE LITERAL1
|
23
libraries/MCP_POT/library.json
Normal file
23
libraries/MCP_POT/library.json
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "MCP_POT",
|
||||
"keywords": "MCP41010, MCP41050, MCP41100, MCP42010, MCP42050, MCP42100",
|
||||
"description": "Arduino library for MCP41xxx and MCP42xxx SPI based digital potentiometers.",
|
||||
"authors":
|
||||
[
|
||||
{
|
||||
"name": "Rob Tillaart",
|
||||
"email": "Rob.Tillaart@gmail.com",
|
||||
"maintainer": true
|
||||
}
|
||||
],
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/MCP_POT.git"
|
||||
},
|
||||
"version": "0.1.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "*",
|
||||
"platforms": "*",
|
||||
"headers": "MCP_POT.h"
|
||||
}
|
11
libraries/MCP_POT/library.properties
Normal file
11
libraries/MCP_POT/library.properties
Normal file
@ -0,0 +1,11 @@
|
||||
name=MCP_POT
|
||||
version=0.1.0
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for MCP41xxx and MCP42xxx SPI based digital potentiometers.
|
||||
paragraph=MCP41010, MCP41050, MCP41100, MCP42010, MCP42050, MCP42100
|
||||
category=Sensors
|
||||
url=https://github.com/RobTillaart/MCP_POT
|
||||
architectures=*
|
||||
includes=MCP_POT.h
|
||||
depends=
|
156
libraries/MCP_POT/test/unit_test_001.cpp
Normal file
156
libraries/MCP_POT/test/unit_test_001.cpp
Normal file
@ -0,0 +1,156 @@
|
||||
//
|
||||
// FILE: unit_test_001.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2021-01-01
|
||||
// PURPOSE: unit tests for the MCP_POT
|
||||
// https://github.com/RobTillaart/MCP_POT
|
||||
// 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 "MCP_POT.h"
|
||||
|
||||
|
||||
unittest_setup()
|
||||
{
|
||||
fprintf(stderr, "MCP_POT_LIB_VERSION: %s\n", (char *) MCP_POT_LIB_VERSION);
|
||||
}
|
||||
|
||||
|
||||
unittest_teardown()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constants)
|
||||
{
|
||||
assertEqual(128, MCP_POT_MIDDLE_VALUE);
|
||||
assertEqual(255, MCP_POT_MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
unittest(test_constructor)
|
||||
{
|
||||
MCP_POT MCP1(9, 10, 11, 6, 7); // SW SPI
|
||||
MCP_POT MCP2(9, 10, 11, &SPI); // HW SPI
|
||||
|
||||
MCP41010 MCP41_1(9, 10, 11, 6, 7); // SW SPI
|
||||
MCP41050 MCP41_2(9, 10, 11, 6, 7); // SW SPI
|
||||
MCP41100 MCP41_3(9, 10, 11, 6, 7); // SW SPI
|
||||
|
||||
MCP42010 MCP42_1(9, 10, 11, &SPI); // HW SPI
|
||||
MCP42050 MCP42_2(9, 10, 11, &SPI); // HW SPI
|
||||
MCP42100 MCP42_3(9, 10, 11, &SPI); // HW SPI
|
||||
|
||||
MCP1.begin();
|
||||
MCP2.begin();
|
||||
|
||||
assertFalse(MCP1.usesHWSPI());
|
||||
assertTrue(MCP2.usesHWSPI());
|
||||
|
||||
MCP41_1.begin();
|
||||
MCP41_2.begin();
|
||||
MCP41_3.begin();
|
||||
|
||||
MCP42_1.begin();
|
||||
MCP42_2.begin();
|
||||
MCP42_3.begin();
|
||||
|
||||
assertEqual(2, MCP1.pmCount());
|
||||
assertEqual(2, MCP2.pmCount());
|
||||
|
||||
assertEqual(1, MCP41_1.pmCount());
|
||||
assertEqual(1, MCP41_1.pmCount());
|
||||
assertEqual(1, MCP41_1.pmCount());
|
||||
|
||||
assertEqual(2, MCP42_1.pmCount());
|
||||
assertEqual(2, MCP42_2.pmCount());
|
||||
assertEqual(2, MCP42_3.pmCount());
|
||||
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP1.getValue());
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP2.getValue());
|
||||
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP41_1.getValue());
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP41_1.getValue());
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP41_1.getValue());
|
||||
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP42_1.getValue());
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP42_2.getValue());
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP42_3.getValue());
|
||||
}
|
||||
|
||||
|
||||
unittest(test_setValue_both)
|
||||
{
|
||||
MCP_POT MCP1(9, 10, 11, 6, 7); // SW SPI
|
||||
|
||||
MCP1.begin();
|
||||
|
||||
for (int i = 0; i < 256; i+= 33)
|
||||
{
|
||||
MCP1.setValue(i);
|
||||
assertEqual(i, MCP1.getValue(0));
|
||||
assertEqual(i, MCP1.getValue(1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unittest(test_setValue_0)
|
||||
{
|
||||
MCP_POT MCP1(9, 10, 11, 6, 7); // SW SPI
|
||||
|
||||
MCP1.begin();
|
||||
|
||||
for (int i = 0; i < 256; i+= 33)
|
||||
{
|
||||
MCP1.setValue(0, i);
|
||||
assertEqual(i, MCP1.getValue(0));
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP1.getValue(1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unittest(test_setValue_1)
|
||||
{
|
||||
MCP_POT MCP1(9, 10, 11, 6, 7); // SW SPI
|
||||
|
||||
MCP1.begin();
|
||||
|
||||
for (int i = 0; i < 256; i+= 33)
|
||||
{
|
||||
MCP1.setValue(1, i);
|
||||
assertEqual(i, MCP1.getValue(1));
|
||||
assertEqual(MCP_POT_MIDDLE_VALUE, MCP1.getValue(0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
unittest_main()
|
||||
|
||||
|
||||
// -- END OF FILE --
|
Loading…
x
Reference in New Issue
Block a user