Skip to content

Commit 97d14fe

Browse files
committed
Add NAU7802 external calibration (true Zero / Tare).
1 parent 4a079af commit 97d14fe

File tree

4 files changed

+46
-18
lines changed

4 files changed

+46
-18
lines changed

Firmware/OpenLog_Artemis/autoDetect.ino

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,11 +748,16 @@ void configureDevice(node * temp)
748748

749749
delay(sensor->getLDORampDelay()); // Wait for LDO to ramp
750750

751-
if (sensorSetting->useCalibrationInternal)
751+
if (sensorSetting->calibrationMode == 1) //Internal calibration
752752
{
753753
sensor->getWeight(true, 10); //Flush
754754

755-
sensor->calibrateAFE(); //Recalibrate after changing gain / sample rate
755+
sensor->calibrateAFE(NAU7802_CALMOD_INTERNAL); //Recalibrate after changing gain / sample rate
756+
}
757+
else if (sensorSetting->calibrationMode == 2) //Use saved or default external calibration
758+
{
759+
sensor->set24BitRegister(NAU7802_OCAL1_B2, sensorSetting->offsetReg);
760+
sensor->set32BitRegister(NAU7802_GCAL1_B3, sensorSetting->gainReg);
756761
}
757762

758763
sensor->getWeight(true, 10); //Flush

Firmware/OpenLog_Artemis/menuAttachedDevices.ino

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ void menuConfigure_NAU7802(void *configPtr)
937937
SerialPrintf2("\r\nScale calibration factor: %s\r\n", tempStr);
938938

939939
SerialPrintf2("Scale zero offset: %d\r\n", sensorConfig->zeroOffset);
940+
SerialPrintf2("Scale offset register: %d\r\n", sensor->get24BitRegister(NAU7802_OCAL1_B2));
940941

941942
sensor->getWeight(true, 10); //Flush
942943
olaftoa(sensor->getWeight(true, sensorConfig->averageAmount), tempStr, sensorConfig->decimalPlaces, sizeof(tempStr) / sizeof(char));
@@ -1024,9 +1025,10 @@ void menuConfigure_NAU7802(void *configPtr)
10241025
olaftoa(LDO, tempStr, 1, sizeof(tempStr) / sizeof(char));
10251026
SerialPrintf2("9) LDO voltage: %s\r\n", tempStr);
10261027
}
1027-
SerialPrint(F("10) Use internal calibration: "));
1028-
if (sensorConfig->useCalibrationInternal == true) SerialPrintln(F("Enabled"));
1029-
else SerialPrintln(F("Disabled"));
1028+
SerialPrint(F("10) Calibration mode: "));
1029+
if (sensorConfig->calibrationMode == 0) SerialPrintln(F("None"));
1030+
else if (sensorConfig->calibrationMode == 1) SerialPrintln(F("Internal"));
1031+
else SerialPrintln(F("External"));
10301032

10311033
SerialPrintln(F("x) Exit"));
10321034

@@ -1049,6 +1051,16 @@ void menuConfigure_NAU7802(void *configPtr)
10491051

10501052
sensor->getWeight(true, 10); //Flush
10511053

1054+
if (sensorConfig->calibrationMode == 2) //External calibration
1055+
{
1056+
sensor->calibrateAFE(NAU7802_CALMOD_OFFSET); //External offset calibration
1057+
1058+
sensorConfig->offsetReg = sensor->get24BitRegister(NAU7802_OCAL1_B2); // Save new offset
1059+
sensorConfig->gainReg = sensor->get32BitRegister(NAU7802_GCAL1_B3); // This should not have changed, but read it anyway
1060+
1061+
sensor->getWeight(true, 10); //Flush
1062+
}
1063+
10521064
sensor->calculateZeroOffset(sensorConfig->averageAmount); //Zero or Tare the scale
10531065

10541066
sensorConfig->zeroOffset = sensor->getZeroOffset();
@@ -1132,11 +1144,11 @@ void menuConfigure_NAU7802(void *configPtr)
11321144

11331145
sensor->setGain(sensorConfig->gain);
11341146

1135-
if (sensorConfig->useCalibrationInternal)
1147+
if (sensorConfig->calibrationMode == 1) //Internal calibration
11361148
{
11371149
sensor->getWeight(true, 10); //Flush
11381150

1139-
sensor->calibrateAFE(); //Recalibrate after changing gain / sample rate
1151+
sensor->calibrateAFE(NAU7802_CALMOD_INTERNAL); //Recalibrate after changing gain / sample rate
11401152
}
11411153

11421154
SerialPrintln(F("\r\n\r\nGain updated. Please zero and calibrate the scale\r\n\r\n"));
@@ -1151,13 +1163,14 @@ void menuConfigure_NAU7802(void *configPtr)
11511163

11521164
sensor->setSampleRate(sensorConfig->sampleRate);
11531165

1154-
if (sensorConfig->useCalibrationInternal)
1166+
if (sensorConfig->calibrationMode == 1) //Internal calibration
11551167
{
11561168
sensor->getWeight(true, 10); //Flush
11571169

1158-
sensor->calibrateAFE(); //Recalibrate after changing gain / sample rate
1170+
sensor->calibrateAFE(NAU7802_CALMOD_INTERNAL); //Recalibrate after changing gain / sample rate
11591171
}
11601172

1173+
// Limit averageAmount (to prevent getWeight timing out after 1s)
11611174
if ((sensorConfig->sampleRate) == 0 && (sensorConfig->averageAmount > 9))
11621175
sensorConfig->averageAmount = 9;
11631176
else if ((sensorConfig->sampleRate) == 1 && (sensorConfig->averageAmount > 19))
@@ -1179,20 +1192,22 @@ void menuConfigure_NAU7802(void *configPtr)
11791192

11801193
sensor->setLDO(sensorConfig->LDO);
11811194

1182-
if (sensorConfig->useCalibrationInternal)
1195+
if (sensorConfig->calibrationMode == 1) //Internal calibration
11831196
{
11841197
delay(sensor->getLDORampDelay()); // Wait for LDO to ramp before attempting calibrateAFE
11851198

11861199
sensor->getWeight(true, 10); //Flush
11871200

1188-
sensor->calibrateAFE(); //Recalibrate after changing gain / sample rate
1201+
sensor->calibrateAFE(NAU7802_CALMOD_INTERNAL); //Recalibrate after changing gain / sample rate
11891202
}
11901203

11911204
SerialPrintln(F("\r\n\r\nLDO updated. Please zero and calibrate the scale\r\n\r\n"));
11921205
}
11931206
else if (incoming == 10)
11941207
{
1195-
sensorConfig->useCalibrationInternal ^= 1;
1208+
sensorConfig->calibrationMode += 1;
1209+
if (sensorConfig->calibrationMode == 3)
1210+
sensorConfig->calibrationMode = 0;
11961211

11971212
sensor->reset();
11981213
sensor->powerUp();
@@ -1210,11 +1225,11 @@ void menuConfigure_NAU7802(void *configPtr)
12101225

12111226
delay(sensor->getLDORampDelay()); // Wait for LDO to ramp before attempting calibrateAFE
12121227

1213-
if (sensorConfig->useCalibrationInternal)
1228+
if (sensorConfig->calibrationMode == 1) //Internal calibration
12141229
{
12151230
sensor->getWeight(true, 10); //Flush
12161231

1217-
sensor->calibrateAFE(); //Recalibrate after changing gain / sample rate
1232+
sensor->calibrateAFE(NAU7802_CALMOD_INTERNAL); //Recalibrate after changing gain / sample rate
12181233
}
12191234

12201235
sensor->getWeight(true, 10); //Flush

Firmware/OpenLog_Artemis/nvm.ino

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,9 @@ void recordDeviceSettingsToFile()
527527
settingsFile.println((String)base + "sampleRate=" + nodeSetting->sampleRate);
528528
settingsFile.println((String)base + "gain=" + nodeSetting->gain);
529529
settingsFile.println((String)base + "LDO=" + nodeSetting->LDO);
530-
settingsFile.println((String)base + "useCalibrationInternal=" + nodeSetting->useCalibrationInternal);
530+
settingsFile.println((String)base + "calibrationMode=" + nodeSetting->calibrationMode);
531+
settingsFile.println((String)base + "offsetReg=" + nodeSetting->offsetReg);
532+
settingsFile.println((String)base + "gainReg=" + nodeSetting->gainReg);
531533
}
532534
break;
533535
case DEVICE_DISTANCE_VL53L1X:
@@ -1039,8 +1041,12 @@ bool parseDeviceLine(char* str) {
10391041
nodeSetting->gain = d;
10401042
else if (strcmp(deviceSettingName, "LDO") == 0)
10411043
nodeSetting->LDO = d;
1042-
else if (strcmp(deviceSettingName, "useCalibrationInternal") == 0)
1043-
nodeSetting->useCalibrationInternal = d;
1044+
else if (strcmp(deviceSettingName, "calibrationMode") == 0)
1045+
nodeSetting->calibrationMode = d;
1046+
else if (strcmp(deviceSettingName, "offsetReg") == 0)
1047+
nodeSetting->offsetReg = d;
1048+
else if (strcmp(deviceSettingName, "gainReg") == 0)
1049+
nodeSetting->gainReg = d;
10441050
else
10451051
SerialPrintf2("Unknown device setting: %s\r\n", deviceSettingName);
10461052
}

Firmware/OpenLog_Artemis/settings.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ struct struct_NAU7802 {
112112
int sampleRate = 3; //Library says possible values are: 10(0), 20(1), 40(2), 80(3), 320(7)
113113
int gain = 7; //Library says possible values are: 1(0), 2(1), 4(2), 8(3), 16(4), 32(5), 64(6), 128(7)
114114
int LDO = 5; //LDO voltage. Only 3.3(4), 3.0(5), 2.7(6), 2.4(7) make sense here
115-
bool useCalibrationInternal = true; //Use CALMOD / CALS (calibrateAFE)
115+
int calibrationMode = 1; //0: None; 1: Use CALMOD / CALS (calibrateAFE) Internal; 2: Use CALMOD / CALS (calibrateAFE) External
116+
int32_t offsetReg = 0; //Value for the NAU7802 offset register
117+
uint32_t gainReg = 0x00800000; //Value for the NAU7802 gain register
116118
unsigned long powerOnDelayMillis = minimumQwiicPowerOnDelay; // Wait for at least this many millis before communicating with this device. Increase if required!
117119
};
118120

0 commit comments

Comments
 (0)