Skip to content

Commit b2e8735

Browse files
committed
0.1.0 MCP330X
1 parent 51b3963 commit b2e8735

21 files changed

+1416
-0
lines changed

libraries/MCP330X/.arduino-ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
platforms:
2+
rpipico:
3+
board: rp2040:rp2040:rpipico
4+
package: rp2040:rp2040
5+
gcc:
6+
features:
7+
defines:
8+
- ARDUINO_ARCH_RP2040
9+
warnings:
10+
flags:
11+
12+
packages:
13+
rp2040:rp2040:
14+
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
15+
16+
compile:
17+
# Choosing to run compilation tests on 2 different Arduino platforms
18+
platforms:
19+
- uno
20+
# - due
21+
# - zero
22+
# - leonardo
23+
- m4
24+
- esp32
25+
# - esp8266
26+
# - mega2560
27+
- rpipico

libraries/MCP330X/.github/FUNDING.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# These are supported funding model platforms
2+
3+
github: RobTillaart
4+
custom: "https://www.paypal.me/robtillaart"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: Arduino-lint
2+
3+
on: [push, pull_request]
4+
jobs:
5+
lint:
6+
runs-on: ubuntu-latest
7+
timeout-minutes: 5
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: arduino/arduino-lint-action@v1
11+
with:
12+
library-manager: update
13+
compliance: strict
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: Arduino CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
runTest:
7+
runs-on: ubuntu-latest
8+
timeout-minutes: 20
9+
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: ruby/setup-ruby@v1
13+
with:
14+
ruby-version: 2.6
15+
- run: |
16+
gem install arduino_ci
17+
arduino_ci.rb
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: JSON check
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.json'
7+
pull_request:
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 5
13+
steps:
14+
- uses: actions/checkout@v4
15+
- name: json-syntax-check
16+
uses: limitusus/json-syntax-check@v2
17+
with:
18+
pattern: "\\.json$"

libraries/MCP330X/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Change Log MCP330X
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
9+
## [0.1.0] - 2025-04-16
10+
- initial version.
11+

libraries/MCP330X/LICENSE

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

libraries/MCP330X/MCP330X.cpp

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
//
2+
// FILE: MCP330X.cpp
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.1.0
5+
// DATE: 2015-04-16
6+
// PURPOSE: Arduino library for MCP3302 and MCP3304 13-Bit Differential ADC, SPI
7+
// URL: https://github.com/RobTillaart/MCP330X
8+
//
9+
10+
11+
#include "MCP330X.h"
12+
13+
14+
// HARDWARE SPI
15+
MCP330X::MCP330X(__SPI_CLASS__ * mySPI)
16+
{
17+
_dataIn = 255;
18+
_dataOut= 255;
19+
_clock = 255;
20+
_select = 255;
21+
_hwSPI = true;
22+
_mySPI = mySPI;
23+
}
24+
25+
26+
// SOFTWARE SPI
27+
MCP330X::MCP330X(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
28+
{
29+
_dataIn = dataIn;
30+
_dataOut = dataOut;
31+
_clock = clock;
32+
_select = 255;
33+
_hwSPI = false;
34+
_mySPI = NULL;
35+
}
36+
37+
38+
void MCP330X::begin(uint8_t select)
39+
{
40+
_select = select;
41+
pinMode(_select, OUTPUT);
42+
digitalWrite(_select, HIGH);
43+
digitalWrite(_select, LOW); // force communication See datasheet)
44+
digitalWrite(_select, HIGH);
45+
46+
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
47+
48+
if (_hwSPI)
49+
{
50+
// _mySPI->end();
51+
// _mySPI->begin();
52+
}
53+
else // software SPI
54+
{
55+
pinMode(_dataIn, INPUT);
56+
pinMode(_dataOut, OUTPUT);
57+
pinMode(_clock, OUTPUT);
58+
digitalWrite(_dataOut, LOW);
59+
digitalWrite(_clock, LOW);
60+
}
61+
}
62+
63+
64+
uint8_t MCP330X::channels()
65+
{
66+
return _channels;
67+
}
68+
69+
70+
int16_t MCP330X::maxValue()
71+
{
72+
return _maxValue;
73+
}
74+
75+
76+
uint32_t MCP330X::count()
77+
{
78+
uint32_t cnt = _count;
79+
_count = 0;
80+
return cnt;
81+
}
82+
83+
84+
int16_t MCP330X::read(uint8_t channel)
85+
{
86+
if (channel >= _channels) return 0;
87+
return readADC(channel, true);
88+
}
89+
90+
91+
int16_t MCP330X::differentialRead(uint8_t channel)
92+
{
93+
if (channel >= _channels) return 0;
94+
return readADC(channel, false);
95+
}
96+
97+
98+
//
99+
// SPI
100+
//
101+
void MCP330X::setSPIspeed(uint32_t speed)
102+
{
103+
_SPIspeed = speed;
104+
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
105+
}
106+
107+
108+
uint32_t MCP330X::getSPIspeed()
109+
{
110+
return _SPIspeed;
111+
}
112+
113+
114+
bool MCP330X::usesHWSPI()
115+
{
116+
return _hwSPI;
117+
}
118+
119+
120+
/////////////////////////////////////////////////////////////////////////////
121+
//
122+
// PROTECTED
123+
//
124+
int16_t MCP330X::readADC(uint8_t channel, bool single)
125+
{
126+
if (channel >= _channels) return 0;
127+
128+
_count++;
129+
130+
// datasheet figure 6.4 page 27
131+
uint8_t data[3] = { 0, 0, 0 };
132+
data[0] = 0x08; // start bit
133+
if (single) data[0] |= 0x40; // single read | differential
134+
data[0] |= channel >> 1; // channel d2 and d1;
135+
if (channel & 0x01) data[1] = 0x80; // channel d0
136+
137+
// send request and read answer
138+
int bytes = 3;
139+
digitalWrite(_select, LOW);
140+
if (_hwSPI) // hardware SPI
141+
{
142+
_mySPI->beginTransaction(_spi_settings);
143+
for (int b = 0; b < bytes; b++)
144+
{
145+
data[b] = _mySPI->transfer(data[b]);
146+
}
147+
_mySPI->endTransaction();
148+
}
149+
else // software SPI
150+
{
151+
for (int b = 0; b < bytes; b++)
152+
{
153+
data[b] = swSPI_transfer(data[b]);
154+
}
155+
}
156+
digitalWrite(_select, HIGH);
157+
158+
// construct value
159+
// skip data[0]
160+
int16_t raw = 256 * data[1] + data[2];
161+
raw &= 0x1FFF; // clear all unknown bits.
162+
if (raw & 0x1000) // sign bit set?
163+
{
164+
// extend sign bits
165+
raw |= 0xF000; // absolute value
166+
return raw;
167+
}
168+
// positive value
169+
return raw;
170+
}
171+
172+
// MSBFIRST
173+
uint8_t MCP330X::swSPI_transfer(uint8_t val)
174+
{
175+
uint8_t clk = _clock;
176+
uint8_t dao = _dataOut;
177+
uint8_t dai = _dataIn;
178+
179+
uint8_t rv = 0;
180+
for (uint8_t mask = 0x80; mask; mask >>= 1)
181+
{
182+
digitalWrite(dao, (val & mask));
183+
digitalWrite(clk, HIGH);
184+
if (digitalRead(dai) == HIGH) rv |= mask;
185+
digitalWrite(clk, LOW);
186+
}
187+
return rv;
188+
}
189+
190+
191+
/////////////////////////////////////////////////////////////////////////////
192+
//
193+
// DERIVED CLASSES
194+
//
195+
196+
/////////////////////////////////////////////////////////////////////////////
197+
//
198+
// MCP3302
199+
//
200+
MCP3302::MCP3302(__SPI_CLASS__ * mySPI)
201+
:MCP330X(mySPI)
202+
{
203+
_channels = 4;
204+
_maxValue = 4095;
205+
}
206+
207+
MCP3302::MCP3302(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
208+
:MCP330X(dataIn, dataOut, clock)
209+
{
210+
_channels = 4;
211+
_maxValue = 4095;
212+
}
213+
214+
215+
216+
217+
/////////////////////////////////////////////////////////////////////////////
218+
//
219+
// MCP3304
220+
//
221+
MCP3304::MCP3304(__SPI_CLASS__ * mySPI)
222+
:MCP330X(mySPI)
223+
{
224+
_channels = 8;
225+
_maxValue = 4095;
226+
}
227+
228+
MCP3304::MCP3304(uint8_t dataIn, uint8_t dataOut, uint8_t clock)
229+
:MCP330X(dataIn, dataOut, clock)
230+
{
231+
_channels = 8;
232+
_maxValue = 4095;
233+
}
234+
235+
236+
// -- END OF FILE --
237+

0 commit comments

Comments
 (0)