-
Notifications
You must be signed in to change notification settings - Fork 31
/
MCP4728.cpp
214 lines (182 loc) · 5.96 KB
/
MCP4728.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// LIBRARY FOR MCP4728
// Link: https://github.com/hideakitai/MCP4728
// Author: Hideaki Tai
// License: MIT (https://github.com/hideakitai/MCP4728/blob/master/LICENSE)
// Extended by Joe Seggiola to include optional LDAC
#pragma once
#ifndef MCP4728_H
#define MCP4728_H
#include "Arduino.h"
#include <Wire.h>
class MCP4728 {
public:
enum class CMD {
FAST_WRITE = 0x00,
MULTI_WRITE = 0x40,
SINGLE_WRITE = 0x58,
SEQ_WRITE = 0x50,
SELECT_VREF = 0x80,
SELECT_GAIN = 0xC0,
SELECT_PWRDOWN = 0xA0
};
enum class VREF { VDD, INTERNAL_2_8V };
enum class PWR_DOWN { NORMAL, GND_1KOHM, GND_100KOHM, GND_500KOHM };
enum class GAIN { X1, X2 };
void init(TwoWire& w, uint8_t addr = 0, int8_t pin = -1) {
wire_ = &w;
addr_ = I2C_ADDR + addr;
pin_ldac_ = pin;
if (pin_ldac_ > -1) {
pinMode(pin_ldac_, OUTPUT);
enable(false);
}
readRegisters();
}
void enable(bool b) {
if (pin_ldac_ > -1) {
digitalWrite(pin_ldac_, !b);
}
}
uint8_t analogWrite(uint8_t ch, uint16_t data, bool b_eep = false) {
if (b_eep) {
eep_[ch].data = data > 0xFFF ? 0xFFF : data;
return singleWrite(ch);
} else {
reg_[ch].data = data > 0xFFF ? 0xFFF : data;
return fastWrite();
}
}
uint8_t analogWrite(uint16_t a, uint16_t b, uint16_t c, uint16_t d, bool b_eep = false) {
if (b_eep) {
reg_[0].data = eep_[0].data = a > 0xFFF ? 0xFFF : a;
reg_[1].data = eep_[1].data = b > 0xFFF ? 0xFFF : b;
reg_[2].data = eep_[2].data = c > 0xFFF ? 0xFFF : c;
reg_[3].data = eep_[3].data = d > 0xFFF ? 0xFFF : d;
return seqWrite();
} else {
reg_[0].data = a > 0xFFF ? 0xFFF : a;
reg_[1].data = b > 0xFFF ? 0xFFF : b;
reg_[2].data = c > 0xFFF ? 0xFFF : c;
reg_[3].data = d > 0xFFF ? 0xFFF : d;
return fastWrite();
}
}
uint8_t selectVref(VREF a, VREF b, VREF c, VREF d) {
reg_[0].vref = a;
reg_[1].vref = b;
reg_[2].vref = c;
reg_[3].vref = d;
uint8_t data = (uint8_t)CMD::SELECT_VREF;
for (uint8_t i = 0; i < 4; ++i) bitWrite(data, 3 - i, (uint8_t)reg_[i].vref);
wire_->beginTransmission(addr_);
wire_->write(data);
return wire_->endTransmission();
}
uint8_t selectPowerDown(PWR_DOWN a, PWR_DOWN b, PWR_DOWN c, PWR_DOWN d) {
reg_[0].pd = a;
reg_[1].pd = b;
reg_[2].pd = c;
reg_[3].pd = d;
uint8_t h = ((uint8_t)CMD::SELECT_PWRDOWN) | ((uint8_t)a << 2) | (uint8_t)b;
uint8_t l = 0 | ((uint8_t)c << 6) | ((uint8_t)d << 4);
wire_->beginTransmission(addr_);
wire_->write(h);
wire_->write(l);
return wire_->endTransmission();
}
uint8_t selectGain(GAIN a, GAIN b, GAIN c, GAIN d) {
reg_[0].gain = a;
reg_[1].gain = b;
reg_[2].gain = c;
reg_[3].gain = d;
uint8_t data = (uint8_t)CMD::SELECT_GAIN;
for (uint8_t i = 0; i < 4; ++i) bitWrite(data, 3 - i, (uint8_t)reg_[i].gain);
wire_->beginTransmission(addr_);
wire_->write(data);
return wire_->endTransmission();
}
void readRegisters() {
wire_->requestFrom((int)addr_, 24);
if (wire_->available() == 24) {
for (uint8_t i = 0; i < 8; ++i) {
uint8_t data[3];
bool isEeprom = i % 2;
for (uint8_t i = 0; i < 3; ++i) data[i] = wire_->read();
uint8_t ch = (data[0] & 0x30) >> 4;
if (isEeprom) {
read_eep_[ch].vref = (VREF) ((data[1] & 0b10000000) >> 7);
read_eep_[ch].pd = (PWR_DOWN)((data[1] & 0b01100000) >> 5);
read_eep_[ch].gain = (GAIN) ((data[1] & 0b00010000) >> 4);
read_eep_[ch].data = (uint16_t)((data[1] & 0b00001111) << 8 | data[2]);
} else {
read_reg_[ch].vref = (VREF) ((data[1] & 0b10000000) >> 7);
read_reg_[ch].pd = (PWR_DOWN)((data[1] & 0b01100000) >> 5);
read_reg_[ch].gain = (GAIN) ((data[1] & 0b00010000) >> 4);
read_reg_[ch].data = (uint16_t)((data[1] & 0b00001111) << 8 | data[2]);
}
}
}
}
uint8_t getVref(uint8_t ch, bool b_eep = false) {
return b_eep ? (uint8_t)read_eep_[ch].vref : (uint8_t)read_reg_[ch].vref;
}
uint8_t getGain(uint8_t ch, bool b_eep = false) {
return b_eep ? (uint8_t)read_eep_[ch].gain: (uint8_t)read_reg_[ch].gain;
}
uint8_t getPowerDown(uint8_t ch, bool b_eep = false) {
return b_eep ? (uint8_t)read_eep_[ch].pd : (uint8_t)read_reg_[ch].pd;
}
uint16_t getDACData(uint8_t ch, bool b_eep = false) {
return b_eep ? (uint16_t)read_eep_[ch].data : (uint16_t)read_reg_[ch].data;
}
private:
uint8_t fastWrite() {
wire_->beginTransmission(addr_);
for (uint8_t i = 0; i < 4; ++i) {
wire_->write((uint8_t)CMD::FAST_WRITE | highByte(reg_[i].data));
wire_->write(lowByte(reg_[i].data));
}
return wire_->endTransmission();
}
uint8_t multiWrite() {
wire_->beginTransmission(addr_);
for (uint8_t i = 0; i < 4; ++i) {
wire_->write((uint8_t)CMD::MULTI_WRITE | (i << 1));
wire_->write(((uint8_t)reg_[i].vref << 7) | ((uint8_t)reg_[i].pd << 5) | ((uint8_t)reg_[i].gain << 4) | highByte(reg_[i].data));
wire_->write(lowByte(reg_[i].data));
}
return wire_->endTransmission();
}
uint8_t seqWrite() {
wire_->beginTransmission(addr_);
wire_->write((uint8_t)CMD::SEQ_WRITE);
for (uint8_t i = 0; i < 4; ++i) {
wire_->write(((uint8_t)eep_[i].vref << 7) | ((uint8_t)eep_[i].pd << 5) | ((uint8_t)eep_[i].gain << 4) | highByte(eep_[i].data));
wire_->write(lowByte(eep_[i].data));
}
return wire_->endTransmission();
}
uint8_t singleWrite(uint8_t ch) {
wire_->beginTransmission(addr_);
wire_->write((uint8_t)CMD::SINGLE_WRITE | (ch << 1));
wire_->write(((uint8_t)eep_[ch].vref << 7) | ((uint8_t)eep_[ch].pd << 5) | ((uint8_t)eep_[ch].gain << 4) | highByte(eep_[ch].data));
wire_->write(lowByte(eep_[ch].data));
return wire_->endTransmission();
}
private:
struct DACInputData {
VREF vref;
PWR_DOWN pd;
GAIN gain;
uint16_t data;
};
const uint8_t I2C_ADDR {0x60};
uint8_t addr_ {I2C_ADDR};
int8_t pin_ldac_;
DACInputData reg_[4];
DACInputData eep_[4];
DACInputData read_reg_[4];
DACInputData read_eep_[4];
TwoWire* wire_;
};
#endif