Skip to content

Commit bfdc680

Browse files
authored
Merge pull request #22 from caternuson/two_chan
Updates for dual channel support
2 parents ed7d09c + 21029dd commit bfdc680

File tree

4 files changed

+177
-7
lines changed

4 files changed

+177
-7
lines changed

Adafruit_NAU7802.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,6 @@ bool Adafruit_NAU7802::begin(TwoWire *theWire) {
8787
if (!ldomode.write(0))
8888
return false;
8989

90-
// PGA stabilizer cap on output
91-
Adafruit_I2CRegister pwr_reg = Adafruit_I2CRegister(i2c_dev, NAU7802_POWER);
92-
Adafruit_I2CRegisterBits capen =
93-
Adafruit_I2CRegisterBits(&pwr_reg, 1, 7); // # bits, bit_shift
94-
if (!capen.write(1))
95-
return false;
96-
9790
return true;
9891
}
9992

@@ -327,6 +320,23 @@ NAU7802_SampleRate Adafruit_NAU7802::getRate(void) {
327320
return (NAU7802_SampleRate)rate_select.read();
328321
}
329322

323+
/**************************************************************************/
324+
/*!
325+
@brief Enable or disable optional PGA filters. NOTE - this should only
326+
be used for single channel operation.
327+
@param enable Use true to enable or false to disable.
328+
@returns False if any I2C error occured
329+
*/
330+
/**************************************************************************/
331+
bool Adafruit_NAU7802::setPGACap(bool enable) {
332+
Adafruit_I2CRegister pwr_reg = Adafruit_I2CRegister(i2c_dev, NAU7802_POWER);
333+
Adafruit_I2CRegisterBits capen =
334+
Adafruit_I2CRegisterBits(&pwr_reg, 1, 7); // # bits, bit_shift
335+
if (!capen.write(enable ? 1 : 0))
336+
return false;
337+
return true;
338+
}
339+
330340
/**************************************************************************/
331341
/*!
332342
@brief Perform the internal calibration procedure

Adafruit_NAU7802.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ class Adafruit_NAU7802 {
9292
NAU7802_Gain getGain(void);
9393
bool setRate(NAU7802_SampleRate gain);
9494
NAU7802_SampleRate getRate(void);
95+
bool setPGACap(bool enable);
9596
bool calibrate(NAU7802_Calibration mode);
9697

9798
private:
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <Adafruit_NAU7802.h>
2+
3+
Adafruit_NAU7802 nau;
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
Serial.println("NAU7802");
8+
if (! nau.begin()) {
9+
Serial.println("Failed to find NAU7802");
10+
while (1) delay(10); // Don't proceed.
11+
}
12+
Serial.println("Found NAU7802");
13+
14+
nau.setLDO(NAU7802_3V0);
15+
Serial.print("LDO voltage set to ");
16+
switch (nau.getLDO()) {
17+
case NAU7802_4V5: Serial.println("4.5V"); break;
18+
case NAU7802_4V2: Serial.println("4.2V"); break;
19+
case NAU7802_3V9: Serial.println("3.9V"); break;
20+
case NAU7802_3V6: Serial.println("3.6V"); break;
21+
case NAU7802_3V3: Serial.println("3.3V"); break;
22+
case NAU7802_3V0: Serial.println("3.0V"); break;
23+
case NAU7802_2V7: Serial.println("2.7V"); break;
24+
case NAU7802_2V4: Serial.println("2.4V"); break;
25+
case NAU7802_EXTERNAL: Serial.println("External"); break;
26+
}
27+
28+
nau.setGain(NAU7802_GAIN_128);
29+
Serial.print("Gain set to ");
30+
switch (nau.getGain()) {
31+
case NAU7802_GAIN_1: Serial.println("1x"); break;
32+
case NAU7802_GAIN_2: Serial.println("2x"); break;
33+
case NAU7802_GAIN_4: Serial.println("4x"); break;
34+
case NAU7802_GAIN_8: Serial.println("8x"); break;
35+
case NAU7802_GAIN_16: Serial.println("16x"); break;
36+
case NAU7802_GAIN_32: Serial.println("32x"); break;
37+
case NAU7802_GAIN_64: Serial.println("64x"); break;
38+
case NAU7802_GAIN_128: Serial.println("128x"); break;
39+
}
40+
41+
nau.setRate(NAU7802_RATE_320SPS);
42+
Serial.print("Conversion rate set to ");
43+
switch (nau.getRate()) {
44+
case NAU7802_RATE_10SPS: Serial.println("10 SPS"); break;
45+
case NAU7802_RATE_20SPS: Serial.println("20 SPS"); break;
46+
case NAU7802_RATE_40SPS: Serial.println("40 SPS"); break;
47+
case NAU7802_RATE_80SPS: Serial.println("80 SPS"); break;
48+
case NAU7802_RATE_320SPS: Serial.println("320 SPS"); break;
49+
}
50+
}
51+
52+
void loop() {
53+
int32_t chan0, chan1;
54+
55+
//--------------
56+
// CHAN 0
57+
//--------------
58+
// Switch channel
59+
nau.setChannel(0);
60+
// Take 10 readings to flush out readings
61+
for (uint8_t i=0; i<10; i++) {
62+
while (! nau.available()) delay(1);
63+
nau.read();
64+
}
65+
// Take actual reading
66+
while (! nau.available()) {
67+
delay(1);
68+
}
69+
chan0 = nau.read();
70+
71+
//--------------
72+
// CHAN 1
73+
//--------------
74+
// Switch channel
75+
nau.setChannel(1);
76+
// Take 10 readings to flush out readings
77+
for (uint8_t i=0; i<10; i++) {
78+
while (! nau.available()) delay(1);
79+
nau.read();
80+
}
81+
// Take actual reading
82+
while (! nau.available()) {
83+
delay(1);
84+
}
85+
chan1 = nau.read();
86+
87+
//--------------
88+
// results
89+
//--------------
90+
Serial.print(chan0); Serial.print(","); Serial.println(chan1);
91+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <Adafruit_NAU7802.h>
2+
3+
Adafruit_NAU7802 nau;
4+
5+
void setup() {
6+
Serial.begin(115200);
7+
Serial.println("NAU7802");
8+
if (! nau.begin()) {
9+
Serial.println("Failed to find NAU7802");
10+
while (1) delay(10); // Don't proceed.
11+
}
12+
Serial.println("Found NAU7802");
13+
14+
nau.setLDO(NAU7802_3V0);
15+
Serial.print("LDO voltage set to ");
16+
switch (nau.getLDO()) {
17+
case NAU7802_4V5: Serial.println("4.5V"); break;
18+
case NAU7802_4V2: Serial.println("4.2V"); break;
19+
case NAU7802_3V9: Serial.println("3.9V"); break;
20+
case NAU7802_3V6: Serial.println("3.6V"); break;
21+
case NAU7802_3V3: Serial.println("3.3V"); break;
22+
case NAU7802_3V0: Serial.println("3.0V"); break;
23+
case NAU7802_2V7: Serial.println("2.7V"); break;
24+
case NAU7802_2V4: Serial.println("2.4V"); break;
25+
case NAU7802_EXTERNAL: Serial.println("External"); break;
26+
}
27+
28+
nau.setGain(NAU7802_GAIN_128);
29+
Serial.print("Gain set to ");
30+
switch (nau.getGain()) {
31+
case NAU7802_GAIN_1: Serial.println("1x"); break;
32+
case NAU7802_GAIN_2: Serial.println("2x"); break;
33+
case NAU7802_GAIN_4: Serial.println("4x"); break;
34+
case NAU7802_GAIN_8: Serial.println("8x"); break;
35+
case NAU7802_GAIN_16: Serial.println("16x"); break;
36+
case NAU7802_GAIN_32: Serial.println("32x"); break;
37+
case NAU7802_GAIN_64: Serial.println("64x"); break;
38+
case NAU7802_GAIN_128: Serial.println("128x"); break;
39+
}
40+
41+
nau.setRate(NAU7802_RATE_10SPS);
42+
Serial.print("Conversion rate set to ");
43+
switch (nau.getRate()) {
44+
case NAU7802_RATE_10SPS: Serial.println("10 SPS"); break;
45+
case NAU7802_RATE_20SPS: Serial.println("20 SPS"); break;
46+
case NAU7802_RATE_40SPS: Serial.println("40 SPS"); break;
47+
case NAU7802_RATE_80SPS: Serial.println("80 SPS"); break;
48+
case NAU7802_RATE_320SPS: Serial.println("320 SPS"); break;
49+
}
50+
51+
// Take 10 readings to flush out readings
52+
for (uint8_t i=0; i<10; i++) {
53+
while (! nau.available()) delay(1);
54+
nau.read();
55+
}
56+
57+
// SINGLE CHANNEL ONLY!!!
58+
// enable use of PGA stabilizer caps (Cfilter) on VIN2
59+
nau.setPGACap(true);
60+
}
61+
62+
void loop() {
63+
while (! nau.available()) {
64+
delay(1);
65+
}
66+
int32_t val = nau.read();
67+
Serial.print("Read "); Serial.println(val);
68+
}

0 commit comments

Comments
 (0)