質問編集履歴
3
自己解決
test
CHANGED
File without changes
|
test
CHANGED
@@ -177,3 +177,129 @@
|
|
177
177
|
}
|
178
178
|
```
|
179
179
|
[参考にしたHP](https://www.denshi.club/cookbook/adda/adc/a-d210spi-mcp3008.html)。こちらのプログラムは動作することを確認しました。
|
180
|
+
|
181
|
+
#### 解決
|
182
|
+
皆様のご指摘を参考にしつつ、それ以外のバグも見つけ解決しました。
|
183
|
+
|
184
|
+
```cpp:mcp3008.h
|
185
|
+
#ifndef __MCP3009_H__
|
186
|
+
#define __MCP3008_H__
|
187
|
+
#include <arduino.h>
|
188
|
+
#include <stdint.h>
|
189
|
+
#include <SPI.h>
|
190
|
+
|
191
|
+
class ADC_MCP3008
|
192
|
+
{
|
193
|
+
public:
|
194
|
+
ADC_MCP3008(uint8_t CSPinNo, uint32_t clock, float vref)
|
195
|
+
: m_ADC_MCP3008Setting(clock, MSBFIRST, SPI_MODE0)
|
196
|
+
, m_CSPinNumber(CSPinNo)
|
197
|
+
, ADC_VREF(vref)
|
198
|
+
{
|
199
|
+
SPI.begin();
|
200
|
+
pinMode(m_CSPinNumber, OUTPUT);
|
201
|
+
digitalWrite(m_CSPinNumber, m_CSPIN_NEGATE);
|
202
|
+
}
|
203
|
+
virtual ~ADC_MCP3008()
|
204
|
+
{
|
205
|
+
}
|
206
|
+
uint16_t readSingleMode(uint8_t ch);
|
207
|
+
uint16_t readDifferentialMode(uint8_t ch);
|
208
|
+
void readBurstSingleMode(uint16_t * buffer);
|
209
|
+
void readBurstDifferentialMode(uint16_t * buffer);
|
210
|
+
float convertBinToVoltage(uint16_t bin);
|
211
|
+
const uint8_t ADC_SINGLE_CHNUM = 8;
|
212
|
+
const uint8_t ADC_DUAL_CHNUM = 4;
|
213
|
+
const float ADC_VREF;
|
214
|
+
private:
|
215
|
+
const SPISettings m_ADC_MCP3008Setting;
|
216
|
+
const uint8_t m_CSPinNumber;
|
217
|
+
const uint8_t m_CSPIN_ASSERT = LOW;
|
218
|
+
const uint8_t m_CSPIN_NEGATE = HIGH;
|
219
|
+
const uint16_t m_ADC_MASK = 0x03FF; // 10bit
|
220
|
+
const uint8_t m_ADC_STARTBIT = 0x01; // 0b0000 0001
|
221
|
+
const uint8_t m_ADC_SINGLMODE = 0x80; // 0b1000 0000
|
222
|
+
const uint8_t m_ADC_DUALMODE = 0x00; // 0b0000 0000
|
223
|
+
uint32_t _readRaw(uint8_t startByte, uint8_t highByte, uint8_t lowByte);
|
224
|
+
};
|
225
|
+
|
226
|
+
#endif /* __MCP300x_H__ */
|
227
|
+
```
|
228
|
+
|
229
|
+
```cpp:mcp3008.cpp
|
230
|
+
#include <arduino.h>
|
231
|
+
#include <stdint.h>
|
232
|
+
#include <SPI.h>
|
233
|
+
#include "mcp3008.h"
|
234
|
+
|
235
|
+
uint32_t ADC_MCP3008::_readRaw(uint8_t startByte, uint8_t highByte, uint8_t lowByte)
|
236
|
+
{
|
237
|
+
volatile uint8_t rcvs[3];
|
238
|
+
digitalWrite(m_CSPinNumber, m_CSPIN_ASSERT);
|
239
|
+
rcvs[0] = SPI.transfer(startByte);
|
240
|
+
rcvs[1] = SPI.transfer(highByte);
|
241
|
+
rcvs[2] = SPI.transfer(lowByte);
|
242
|
+
digitalWrite(m_CSPinNumber, m_CSPIN_NEGATE);
|
243
|
+
return (uint32_t)((rcvs[0] << 16) | (rcvs[1] << 8) | (rcvs[2]));
|
244
|
+
}
|
245
|
+
|
246
|
+
uint16_t ADC_MCP3008::readSingleMode(uint8_t ch)
|
247
|
+
{
|
248
|
+
uint16_t rcv;
|
249
|
+
if (ch < ADC_SINGLE_CHNUM)
|
250
|
+
{
|
251
|
+
SPI.beginTransaction(m_ADC_MCP3008Setting);
|
252
|
+
rcv = (uint16_t)( _readRaw(m_ADC_STARTBIT, (uint8_t)( m_ADC_SINGLMODE | (ch << 4)), 0x00) & m_ADC_MASK);
|
253
|
+
SPI.endTransaction();
|
254
|
+
}
|
255
|
+
else
|
256
|
+
{
|
257
|
+
rcv = 0xFFFF;
|
258
|
+
}
|
259
|
+
return rcv;
|
260
|
+
}
|
261
|
+
|
262
|
+
uint16_t ADC_MCP3008::readDifferentialMode(uint8_t ch)
|
263
|
+
{
|
264
|
+
uint16_t rcv;
|
265
|
+
if (ch < ADC_DUAL_CHNUM)
|
266
|
+
{
|
267
|
+
SPI.beginTransaction(m_ADC_MCP3008Setting);
|
268
|
+
rcv = (uint16_t)(_readRaw(m_ADC_STARTBIT, (uint8_t)( m_ADC_DUALMODE| (ch << 4)), 0x00)) & m_ADC_MASK;
|
269
|
+
SPI.endTransaction();
|
270
|
+
}
|
271
|
+
else
|
272
|
+
{
|
273
|
+
rcv = 0xFFFF;
|
274
|
+
}
|
275
|
+
return rcv;
|
276
|
+
}
|
277
|
+
|
278
|
+
void ADC_MCP3008::readBurstSingleMode(uint16_t * buffer)
|
279
|
+
{
|
280
|
+
int cnt;
|
281
|
+
SPI.beginTransaction(m_ADC_MCP3008Setting);
|
282
|
+
for (cnt = 0; cnt < ADC_SINGLE_CHNUM; cnt++, buffer++)
|
283
|
+
{
|
284
|
+
*buffer = (uint16_t)(_readRaw(m_ADC_STARTBIT, (uint8_t)( m_ADC_SINGLMODE | (cnt << 4)), 0x00)) & m_ADC_MASK;
|
285
|
+
}
|
286
|
+
SPI.endTransaction();
|
287
|
+
}
|
288
|
+
|
289
|
+
void ADC_MCP3008::readBurstDifferentialMode(uint16_t * buffer)
|
290
|
+
{
|
291
|
+
int cnt;
|
292
|
+
SPI.beginTransaction(m_ADC_MCP3008Setting);
|
293
|
+
for (cnt = 0; cnt < ADC_DUAL_CHNUM; cnt++, buffer++)
|
294
|
+
{
|
295
|
+
*buffer = (uint16_t)(_readRaw(m_ADC_STARTBIT, (uint8_t)( m_ADC_DUALMODE| (cnt << 4)), 0x00)) & m_ADC_MASK;
|
296
|
+
}
|
297
|
+
SPI.endTransaction();
|
298
|
+
}
|
299
|
+
|
300
|
+
float ADC_MCP3008::convertBinToVoltage(uint16_t bin)
|
301
|
+
{
|
302
|
+
return (float)bin * ADC_VREF / m_ADC_MASK;
|
303
|
+
}
|
304
|
+
```
|
305
|
+
|
2
beginTransaction/endTransactionの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -97,7 +97,9 @@
|
|
97
97
|
|
98
98
|
uint32_t ADC_MCP3008::readTest(void)
|
99
99
|
{
|
100
|
+
SPI.beginTransaction(m_ADC_MCP3008Setting);
|
100
101
|
return _readRaw(0x01, 0x80, 0x00);
|
102
|
+
SPI.endTransaction();
|
101
103
|
}
|
102
104
|
|
103
105
|
uint32_t ADC_MCP3008::privateRead(void)
|
1
初心者マーク
test
CHANGED
File without changes
|
test
CHANGED
@@ -84,12 +84,10 @@
|
|
84
84
|
|
85
85
|
uint16_t ADC_MCP3008::readDifferentialMode(uint8_t ch)
|
86
86
|
{
|
87
|
-
|
88
87
|
}
|
89
88
|
|
90
89
|
void ADC_MCP3008::readBurstSingleMode(uint16_t * buffer)
|
91
90
|
{
|
92
|
-
|
93
91
|
}
|
94
92
|
|
95
93
|
void ADC_MCP3008::readBurstDifferentialMode(uint16_t * buffer)
|