/
MCP48xx.h
186 lines (136 loc) · 3.89 KB
/
MCP48xx.h
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
#ifndef MCP48XX_LIB_MCP48XX_H
#define MCP48XX_LIB_MCP48XX_H
#include <Arduino.h>
#include <SPI.h>
template<uint8_t BITS_RES>
class MCP48xx {
private:
const uint8_t cs;
uint16_t command[2] = {0};
bool isAActive = false;
bool isBActive = false;
public:
enum Channel : uint8_t {
A = 0u, B = 1u
};
enum Gain : uint8_t {
High = 0u, Low = 1u
};
explicit MCP48xx(uint8_t cs);
~MCP48xx();
void init();
void setVoltage(uint16_t value, Channel channel);
void setVoltageA(uint16_t value);
void setVoltageB(uint16_t value);
void shutdownChannel(Channel channel);
void shutdownChannelA();
void shutdownChannelB();
void turnOnChannel(Channel channel);
void turnOnChannelA();
void turnOnChannelB();
void setGain(Gain gain, Channel channel);
void setGainA(Gain gain);
void setGainB(Gain gain);
void updateDAC();
};
typedef MCP48xx<12> MCP4822;
typedef MCP48xx<10> MCP4812;
typedef MCP48xx<8> MCP4802;
/* Implementation */
template<uint8_t BITS_RES>
MCP48xx<BITS_RES>::MCP48xx(uint8_t cs) : cs(cs) {
/* Setting channel bits*/
command[Channel::A] = command[Channel::A] | (0u << 15u);
command[Channel::B] = command[Channel::B] | (1u << 15u);
SPI.begin();
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::init() {
pinMode(cs, OUTPUT);
digitalWrite(cs, HIGH);
}
template<uint8_t BITS_RES>
MCP48xx<BITS_RES>::~MCP48xx() {
SPI.end();
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::setVoltage(uint16_t value, Channel channel) {
if (value > (1u << BITS_RES) - 1) {
value = (1u << BITS_RES) - 1;
} else {
value = value << (12u - BITS_RES);
}
command[channel] = command[channel] & 0xF000u;
command[channel] = command[channel] | value;
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::setVoltageA(uint16_t value) {
setVoltage(value, Channel::A);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::setVoltageB(uint16_t value) {
setVoltage(value, Channel::B);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::shutdownChannel(Channel channel) {
command[channel] = command[channel] | (0u << 12u);
if (channel == Channel::A) {
isAActive = false;
} else {
isBActive = false;
}
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::shutdownChannelA() {
shutdownChannel(Channel::A);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::shutdownChannelB() {
shutdownChannel(Channel::B);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::turnOnChannel(Channel channel) {
command[channel] = command[channel] | (1u << 12u);
if (channel == Channel::A) {
isAActive = true;
} else {
isBActive = true;
}
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::turnOnChannelA() {
turnOnChannel(Channel::A);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::turnOnChannelB() {
turnOnChannel(Channel::B);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::setGain(Gain gain, Channel channel) {
command[channel] = command[channel] | (gain << 13u);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::setGainA(Gain gain) {
setGain(gain, Channel::A);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::setGainB(Gain gain) {
setGain(gain, Channel::B);
}
template<uint8_t BITS_RES>
void MCP48xx<BITS_RES>::updateDAC() {
/* begin transaction using maximum clock frequency of 20MHz */
SPI.beginTransaction(SPISettings(20000000, MSBFIRST, SPI_MODE0));
if (isAActive) {
digitalWrite(cs, LOW); //select device
SPI.transfer16(command[Channel::A]); // sent command for the A channel
digitalWrite(cs, HIGH); //deselect device
}
if (isBActive) {
digitalWrite(cs, LOW);
SPI.transfer16(command[Channel::B]); // sent command for the B channel
digitalWrite(cs, HIGH);
}
SPI.endTransaction();
}
#endif //MCP48XX_LIB_MCP48XX_H