Skip to content

Commit fb8e834

Browse files
committed
0.2.1 MCP_ADC
1 parent 87bbbfd commit fb8e834

File tree

9 files changed

+125
-35
lines changed

9 files changed

+125
-35
lines changed

libraries/MCP_ADC/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.2.1] - 2023-09-08
10+
- fix #13 support **MCP3201**
11+
- add **MCP3001** support
12+
- fix startup pulse for **select** pin.
13+
- update readme.md
14+
- fix version number in .cpp
15+
- update keywords.txt
16+
17+
918
## [0.2.0] - 2023-08-15
1019
- add analogReadMultiple(channels\[], numChannels, readings\[])
1120
- Kudo's to Alex Uta.

libraries/MCP_ADC/MCP_ADC.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//
22
// FILE: MCP_ADC.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.9
4+
// VERSION: 0.2.1
55
// DATE: 2019-10-24
6-
// PURPOSE: Arduino library for MCP3002, MCP3004, MCP3008, MCP3202, MCP3204, MCP3208
6+
// PURPOSE: Arduino library for MCP3001, MCP3002, MCP3004, MCP3008, MCP3201, MCP3202, MCP3204, MCP3208
77
// URL: https://github.com/RobTillaart/MCP_ADC
88

99

@@ -25,6 +25,8 @@ void MCP_ADC::begin(uint8_t select)
2525
_select = select;
2626
pinMode(_select, OUTPUT);
2727
digitalWrite(_select, HIGH);
28+
digitalWrite(_select, LOW); // force communication See datasheet)
29+
digitalWrite(_select, HIGH);
2830

2931
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
3032

@@ -153,7 +155,16 @@ int16_t MCP_ADC::readADC(uint8_t channel, bool single)
153155
}
154156
digitalWrite(_select, HIGH);
155157

156-
if (bytes == 2) return ((256 * data[0] + data[1]) & _maxValue);
158+
if (bytes == 2)
159+
{
160+
// combine bytes
161+
int16_t raw = 256 * data[0] + data[1];
162+
// patch bit pattern MCP3001
163+
if ((_channels == 1) && (_maxValue == 1023)) raw >>= 3;
164+
// patch bit pattern MCP3201
165+
if ((_channels == 1) && (_maxValue == 4095)) raw >>= 1;
166+
return raw & _maxValue;
167+
}
157168
// data[0]?
158169
return ((256 * data[1] + data[2]) & _maxValue);
159170
}
@@ -185,7 +196,13 @@ void MCP_ADC::readADCMultiple(uint8_t channels[], uint8_t numChannels, int16_t r
185196
}
186197

187198
if (bytes == 2) {
188-
readings[i] = ((256 * data[0] + data[1]) & _maxValue);
199+
// combine bytes
200+
int16_t raw = 256 * data[0] + data[1];
201+
// patch bit pattern MCP3001
202+
if ((_channels == 1) && (_maxValue == 1023)) raw >>= 3;
203+
// patch bit pattern MCP3201
204+
if ((_channels == 1) && (_maxValue == 4095)) raw >>= 1;
205+
readings[i] = raw & _maxValue;
189206
} else {
190207
readings[i] = ((256 * data[1] + data[2]) & _maxValue);
191208
}
@@ -219,6 +236,28 @@ uint8_t MCP_ADC::swSPI_transfer(uint8_t val)
219236

220237

221238

239+
/////////////////////////////////////////////////////////////////////////////
240+
//
241+
// MCP3001
242+
//
243+
MCP3001::MCP3001(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
244+
:MCP_ADC(dataIn, dataOut, clock)
245+
{
246+
_channels = 1;
247+
_maxValue = 1023;
248+
}
249+
250+
uint8_t MCP3001::buildRequest(uint8_t channel, bool single, uint8_t * data)
251+
{
252+
// P16 fig 6.1 MCP3001
253+
// no specific data needed
254+
// keep build CI compiler (ESP32) happy next statement
255+
data[0] = data[1] = 0;
256+
if ((channel == 0) || (single == false)) return 2;
257+
return 2;
258+
}
259+
260+
222261
/////////////////////////////////////////////////////////////////////////////
223262
//
224263
// MCP3002
@@ -297,7 +336,8 @@ uint8_t MCP3201::buildRequest(uint8_t channel, bool single, uint8_t * data)
297336
{
298337
// P21 fig 6.1 MCP3201
299338
// no specific data needed
300-
// keep build CI compiler (ESP32) happy next statement
339+
// keep build CI compiler (ESP32) happy next statements
340+
data[0] = data[1] = 0;
301341
if ((channel == 0) || (single == false)) return 2;
302342
return 2;
303343
}

libraries/MCP_ADC/MCP_ADC.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
//
33
// FILE: MCP_ADC.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.0
5+
// VERSION: 0.2.1
66
// DATE: 2019-10-24
7-
// PURPOSE: Arduino library for MCP_ADC
7+
// PURPOSE: Arduino library for MCP3001, MCP3002, MCP3004, MCP3008, MCP3201, MCP3202, MCP3204, MCP3208
88
// URL: https://github.com/RobTillaart/MCP_ADC
99
//
1010

@@ -13,7 +13,7 @@
1313
#include "SPI.h"
1414

1515

16-
#define MCP_ADC_LIB_VERSION (F("0.2.0"))
16+
#define MCP_ADC_LIB_VERSION (F("0.2.1"))
1717

1818

1919
class MCP_ADC
@@ -84,6 +84,14 @@ class MCP_ADC
8484
//
8585
// DERIVED CLASSES
8686
//
87+
class MCP3001 : public MCP_ADC
88+
{
89+
public:
90+
MCP3001(uint8_t dataIn = 255, uint8_t dataOut = 255, uint8_t clock = 255);
91+
uint8_t buildRequest(uint8_t channel, bool single, uint8_t * data);
92+
};
93+
94+
8795
class MCP3002 : public MCP_ADC
8896
{
8997
public:

libraries/MCP_ADC/README.md

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
[![Arduino CI](https://github.com/RobTillaart/MCP_ADC/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
33
[![Arduino-lint](https://github.com/RobTillaart/MCP_ADC/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/MCP_ADC/actions/workflows/arduino-lint.yml)
44
[![JSON check](https://github.com/RobTillaart/MCP_ADC/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/MCP_ADC/actions/workflows/jsoncheck.yml)
5+
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/MCP_ADC.svg)](https://github.com/RobTillaart/MCP_ADC/issues)
6+
57
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/MCP_ADC/blob/master/LICENSE)
68
[![GitHub release](https://img.shields.io/github/release/RobTillaart/MCP_ADC.svg?maxAge=3600)](https://github.com/RobTillaart/MCP_ADC/releases)
9+
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/MCP_ADC.svg)](https://registry.platformio.org/libraries/robtillaart/MCP_ADC)
710

811

912
# MCP_ADC
1013

11-
Arduino library for MCP3002 MCP3004 MCP3008 MCP3201 MCP3202 MCP3204 MCP3208 and compatibles.
14+
Arduino library for MCP3001 MCP3002 MCP3004 MCP3008 MCP3201 MCP3202 MCP3204 MCP3208 and compatibles.
1215

1316

1417
## Description
@@ -19,10 +22,11 @@ The chips are communicates with SPI and support both hardware SPI or optional so
1922

2023
| type | bits | chan | notes |
2124
|:----------|:------:|:------:|:--------|
25+
| MCP3001 | 10 | 1 | not tested yet.
2226
| MCP3002 | 10 | 2 |
2327
| MCP3004 | 10 | 4 |
2428
| MCP3008 | 10 | 8 |
25-
| MCP3201 | 12 | 1 | not tested yet.
29+
| MCP3201 | 12 | 1 | test, see #13
2630
| MCP3202 | 12 | 2 |
2731
| MCP3204 | 12 | 4 |
2832
| MCP3208 | 12 | 8 |
@@ -39,17 +43,29 @@ Build into the library is a delta mode which is a software enhanced differential
3943
This delta mode can return negative values too.
4044

4145

46+
#### Related
47+
48+
- https://gammon.com.au/adc tutorial about ADC's (UNO specific)
49+
- https://github.com/RobTillaart/ADS1x15 (12 & 16 bit ADC, I2C, slow)
50+
- https://github.com/RobTillaart/PCF8591 (8 bit ADC + 1 bit DAC)
51+
52+
53+
- https://github.com/RobTillaart/MCP_DAC
54+
55+
56+
4257
## Interface
4358

4459
```cpp
4560
#include "MCP_ADC.h"
4661
```
4762

48-
#### Base
63+
#### Constructors
4964

5065
If the pins are not set in the constructor, the class will automatically
5166
use the hardware SPI, otherwise it will use software SPI.
5267

68+
- **MCP3001(uint8_t dataIn, uint8_t dataOut, uint8_t clock)** constructor 10 bit ADC 1 channel.
5369
- **MCP3002(uint8_t dataIn, uint8_t dataOut, uint8_t clock)** constructor 10 bit ADC 2 channel.
5470
- **MCP3004(uint8_t dataIn, uint8_t dataOut, uint8_t clock)** constructor 10 bit ADC 4 channel.
5571
- **MCP3008(uint8_t dataIn, uint8_t dataOut, uint8_t clock)** constructor 10 bit ADC 8 channel.
@@ -59,8 +75,12 @@ use the hardware SPI, otherwise it will use software SPI.
5975
- **MCP3208(uint8_t dataIn, uint8_t dataOut, uint8_t clock)** constructor 12 bit ADC 8 channel.
6076
- **void begin(uint8_t select)** set select pin.
6177
- **uint8_t channels()** returns the number of channels.
62-
- **int16_t maxValue()** returns maxReading of adc, e.g. 1023.
78+
- **int16_t maxValue()** returns maxReading of ADC, typical 1023 or 4095.
6379
This makes it easy to calculate relative measurements.
80+
81+
82+
#### Base
83+
6484
- **int16_t analogRead(uint8_t channel)** reads the value of a single channel.
6585
- **void analogReadMultiple(uint8_t channels[], uint8_t numChannels, int16_t readings[])**
6686
reads multiple channels in one call. See section below.
@@ -87,6 +107,8 @@ of the ADC first to get optimal speed.
87107
| 6 | 6 | 7 | 3x08 |
88108
| 7 | 7 | 6 | 3x08 |
89109

110+
Note: the MCP3x01 are not included in this table, not investigated yet.
111+
90112

91113
### Debug
92114

@@ -108,8 +130,10 @@ BEFORE the **begin()** function.
108130
#### setGPIOpins() experimental
109131

110132
- **void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)**
111-
overrule GPIO pins of ESP32 for hardware SPI. needs to be called AFTER the **begin()** function.
133+
overrule GPIO pins of ESP32 for hardware SPI.
134+
This function needs to be called AFTER the **begin()** function.
112135

136+
in code:
113137
```cpp
114138
void setup()
115139
{
@@ -165,16 +189,15 @@ Finally **analogReadMultiple()** can be used to read only one channel too
165189
by using numChannels = 1.
166190

167191

168-
## MCP3201 experimental
169-
170-
Since 0.2.0 code for the MCP3201 has been added however this 12 bit single
171-
channel device has not been tested yet.
192+
## MCP3001, MCP3201 experimental
172193

173-
As the SPI transfer looked quite a bit like the MCP3202 it is expected to work
174-
but the proof is in the hardware test.
194+
Since 0.2.0 code for the MCP3201 has been added.
195+
The first tests are done (see #13) which showed that the 0.2.0 implementation
196+
was not correct.
197+
This has been fixed in the 0.2.1 version.
175198

176-
Note that not all function calls make sense for the MCP3201 as this device only
177-
has one channel. So use the library carefully.
199+
Note that not all function calls make sense for the MCP3201 and MCP3001 as these
200+
devices only have one channel. So use the library carefully.
178201

179202
Feedback is as always welcome.
180203

@@ -202,3 +225,12 @@ Feedback is as always welcome.
202225
it actually does float mapping. As it implies the same mapping for all it might
203226
not be that useful => check multiMap library.
204227

228+
229+
## Support
230+
231+
If you appreciate my libraries, you can support the development and maintenance.
232+
Improve the quality of the libraries by providing issues and Pull Requests, or
233+
donate through PayPal or GitHub sponsors.
234+
235+
Thank you,
236+

libraries/MCP_ADC/examples/MCP3208_performance/performance_0.1.9.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ Max clock frequency tested 16 MHz, ESP32 WROVER Module
88

99
### Library version: 0.1.9
1010

11-
| Action | HW 1 MHz | HW 2 MHz | HW 4 MHz | HW 8 MHz | HW 10 MHz | HW 12 MHz | HW 14 MHz | HW 16 MHz | notes |
12-
|:------------------------------|---------:|---------:|---------:|---------:|:---------:|:---------:|:---------:|:---------:|
13-
| mcp28.analogRead() | 397.0 | 243.0 | 188.0 | 158.00 | 153.0 | 209.0 | 208.0 | 147.0 |
14-
| mcp28.analogReadMultiple() | 271.0 | 154.0 | 99.00 | 70.00 | 64.00 | 61.00 | 59.0 | 55.00 |
15-
| single / multiple ratio | 1.0 | 1.0 | 1.0 | 2.0 | 2.0 | 3.0 | 3.0 | 2.0 |
16-
| | | | | | | | | |
17-
| mcp28.differentialRead() | 359.0 | 241.0 | 186.0 | 157.0 | 151.0 | 148.0 | 146.0 | 142.0 |
18-
| mcp28.deltaRead() | 363.0 | 304.0 | 234.0 | 198.0 | 191.0 | 187.0 | 185.0 | 145.0 |
11+
| Action | HW 1 MHz | HW 2 MHz | HW 4 MHz | HW 8 MHz | HW 10 MHz | HW 12 MHz | HW 14 MHz | HW 16 MHz | notes |
12+
|:------------------------------|---------:|---------:|---------:|----------:|----------:|-----------:|----------:|----------:|:----------|
13+
| mcp28.analogRead() | 397.0 | 243.0 | 188.0 | 158.00 | 153.0 | 209.0 | 208.0 | 147.0 |
14+
| mcp28.analogReadMultiple() | 271.0 | 154.0 | 99.00 | 70.00 | 64.00 | 61.00 | 59.0 | 55.00 |
15+
| single / multiple ratio | 1.0 | 1.0 | 1.0 | 2.0 | 2.0 | 3.0 | 3.0 | 2.0 |
16+
| | | | | | | | | |
17+
| mcp28.differentialRead() | 359.0 | 241.0 | 186.0 | 157.0 | 151.0 | 148.0 | 146.0 | 142.0 |
18+
| mcp28.deltaRead() | 363.0 | 304.0 | 234.0 | 198.0 | 191.0 | 187.0 | 185.0 | 145.0 |
1919

2020
### Notes
2121

libraries/MCP_ADC/examples/MCP3208_performance/readADC_vs_readADCMultiple.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
- source https://github.com/RobTillaart/MCP_ADC/pull/11#issuecomment-1676461195
66
- figures are indicative and platform dependent.
77

8-
| function | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | <- channels
9-
|:--------------------:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
8+
| function | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | <- channels |
9+
|:--------------------:|------:|------:|------:|------:|------:|------:|------:|------:|:-------------:|
1010
| analogRead | | 42 | 59 | 78 | 98 | 118 | 137 | 157 | (us)
1111
| analogReadMultiple | | 27 | 34 | 41 | 49 | 55 | 62 | 70 | (us)
1212
| ratio | | 1.56 | 1.73 | 1.90 | 2.00 | 2.15 | 2.21 | 2.24 |

libraries/MCP_ADC/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Syntax Colouring Map For MCP_ADC
22

33
# Data types (KEYWORD1)
4+
MCP3001 KEYWORD1
45
MCP3002 KEYWORD1
56
MCP3004 KEYWORD1
67
MCP3008 KEYWORD1

libraries/MCP_ADC/library.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "MCP_ADC",
3-
"keywords": "MCP3002, MCP3004, MCP3008, MCP3201, MCP3202, MCP3204, MCP3208",
3+
"keywords": "MCP3001, MCP3002, MCP3004, MCP3008, MCP3201, MCP3202, MCP3204, MCP3208",
44
"description": "Arduino library for MCP_ADC, e.g. MCP3008 SPI 10 bit, 8 channel ADC",
55
"authors":
66
[
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/MCP_ADC.git"
1717
},
18-
"version": "0.2.0",
18+
"version": "0.2.1",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/MCP_ADC/library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=MCP_ADC
2-
version=0.2.0
2+
version=0.2.1
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for MCP_ADC, e.g. MCP3008 SPI 10 bit, 8 channel ADC
6-
paragraph=MCP3002, MCP3004, MCP3008, MCP3201, MCP3202, MCP3204, MCP3208
6+
paragraph=MCP3001, MCP3002, MCP3004, MCP3008, MCP3201, MCP3202, MCP3204, MCP3208
77
category=Sensors
88
url=https://github.com/RobTillaart/MCP_ADC
99
architectures=*

0 commit comments

Comments
 (0)