forked from cristovaozr/hx711dual
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHX711Dual.cpp
236 lines (188 loc) · 4.57 KB
/
HX711Dual.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "HX711Dual.h"
#include <Arduino.h>
#include <stdint.h>
HX711Dual::HX711Dual(int DT, int SCK, ChannelGain_e ch_a_gain,
ChannelGain_e ch_b_gain)
: _dt_pin(DT), _sck_pin(SCK), _ch_a_offset(0.0f), _ch_b_offset(0.0f),
_ch_a_gain(ch_a_gain), _ch_b_gain(ch_b_gain)
{
if(this->_ch_b_gain != CHANNEL_B_GAIN_32) {
this->_ch_b_gain = CHANNEL_B_GAIN_32;
}
if(this->_ch_a_gain == CHANNEL_B_GAIN_32) {
this->_ch_a_gain = CHANNEL_A_GAIN_128;
}
pinMode(this->_dt_pin, INPUT);
pinMode(this->_sck_pin, OUTPUT);
digitalWrite(this->_sck_pin, HIGH);
delayMicroseconds(100);
digitalWrite(this->_sck_pin, LOW);
}
HX711Dual::~HX711Dual() {
pinMode(this->_dt_pin, INPUT);
pinMode(this->_sck_pin, INPUT);
}
void HX711Dual::_setReadingChannel(Channel_e ch)
{
int n_clk;
switch(ch){
case CHANNEL_A:
n_clk = this->_ch_a_gain;
break;
case CHANNEL_B:
n_clk = this->_ch_b_gain;
break;
default:
return;
}
while(digitalRead(this->_dt_pin));
for(int i = 0; i < n_clk; i++) {
digitalWrite(this->_sck_pin, HIGH);
digitalWrite(this->_sck_pin, LOW);
}
}
int32_t HX711Dual::_readChannel(Channel_e ch)
{
int32_t ret = 0;
while(digitalRead(this->_dt_pin));
for(int bit = 23; bit > 0; bit--) {
digitalWrite(this->_sck_pin, HIGH);
if(digitalRead(this->_dt_pin)) {
ret |= (1 << bit);
}
digitalWrite(this->_sck_pin, LOW);
}
digitalWrite(this->_sck_pin, HIGH);
digitalWrite(this->_sck_pin, LOW);
// ret ^= 0x00800000;
if(ret & 0x00800000) {
// negative number... extend signal over 32 bits
ret |= 0xff000000;
}
return ret;
}
void HX711Dual::setChannelAGain(ChannelGain_e new_gain)
{
if(new_gain == CHANNEL_B_GAIN_32) return ;
this->_ch_a_gain = new_gain;
}
void HX711Dual::setChannelBGain(ChannelGain_e new_gain)
{
if(new_gain != CHANNEL_B_GAIN_32) return;
this->_ch_b_gain = new_gain;
}
void HX711Dual::channelACalibrate(int sample_number)
{
int64_t sum = 0;
for(int i = 0; i < sample_number; i++) {
_setReadingChannel(CHANNEL_A);
sum += _readChannel(CHANNEL_A);
}
this->_ch_a_offset = (sum / sample_number);
}
void HX711Dual::channelBCalibrate(int sample_number)
{
int64_t sum = 0;
for(int i = 0; i < sample_number; i++) {
_setReadingChannel(CHANNEL_B);
sum += _readChannel(CHANNEL_B);
}
this->_ch_b_offset = (sum / sample_number);
}
int32_t HX711Dual::readChannelARaw()
{
return readChannelRaw(CHANNEL_A);
}
int32_t HX711Dual::readChannelBRaw()
{
return readChannelRaw(CHANNEL_B);
}
int32_t HX711Dual::readChannelRaw(Channel_e ch)
{
_setReadingChannel(ch);
return _readChannel(ch);
}
float HX711Dual::readChannelA()
{
float v, gain;
_setReadingChannel(CHANNEL_A);
v = (float)(this->readChannelARaw() - this->_ch_a_offset);
switch(this->_ch_a_gain){
case CHANNEL_A_GAIN_128:
gain = 128.0f;
break;
case CHANNEL_A_GAIN_64:
gain = 64.0f;
break;
default:
gain = 1.0f;
break;
}
return v / gain;
}
float HX711Dual::readChannelB()
{
float v;
_setReadingChannel(CHANNEL_B);
v = (float)(this->readChannelBRaw() - this->_ch_b_offset);
return v / 32.0f;
}
float HX711Dual::readChannel(Channel_e ch)
{
float v, gain;
_setReadingChannel(ch);
v = (float)this->readChannelRaw(ch);
if(ch == CHANNEL_A) {
v -= (float)this->_ch_a_offset;
} else {
v -= (float)this->_ch_b_offset;
}
switch(ch) {
case CHANNEL_A:
switch(this->_ch_a_gain){
case CHANNEL_A_GAIN_128:
gain = 128.0f;
break;
case CHANNEL_A_GAIN_64:
gain = 64.0f;
break;
default:
gain = 1.0f;
break;
}
break;
case CHANNEL_B:
gain = 32.0f;
break;
default:
gain = 1.0f;
}
return v / gain;
}
float averageChannelA(uint8_t averageFactor = 16)
{
float ret;
for (uint8_t i = 0; i < averageFactor; i++)
{
ret += readChannelA();
}
return (ret / averageFactor);
}
float averageChannelB(uint8_t averageFactor = 16)
{
float ret;
for (uint8_t i = 0; i < averageFactor; i++)
{
ret += readChannelB();
}
return (ret / averageFactor);
}
float averageChannel(Channel_e ch, uint8_t averageFactor = 16)
{
float ret;
for (uint8_t i = 0; i < averageFactor; i++)
{
ret += readChannel(ch);
}
return (ret / averageFactor);
}