From e416f1407251516217271dc6d09cad009e482987 Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Wed, 25 Sep 2019 14:43:53 -0700 Subject: [PATCH] Added input parameter for vref constant #featureadd Added an option to change the vref constant over serial --- .../src/Pyr0_Piezo_Sensor_V2.x.x.cpp | 26 +++++++++++++++---- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h | 2 ++ .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h | 16 ++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp index 36a1804..7c51801 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/Pyr0_Piezo_Sensor_V2.x.x.cpp @@ -27,9 +27,10 @@ by Alan "pyr0ball" Weinstock This code is in the public domain. -*/ +------------------------------------------------------------*/ -/* To set the below parameters using serial input, use the following: +/*----------------------------------------------------------- +To set the below parameters using serial input, use the following: To change trigger active duration: TRG_D [integer for milliseconds] To change gain factor: GAIN_F [integer for gain state - see note*] @@ -37,6 +38,7 @@ To change ADC hysteresis value: HYST [integer] To change sensor input pullup vRef low threshold: VFOL [integer in millivolts] To change comparator trigger high threshold: VCOMP [integer in millivolts] To change the duration between ADC measurements: LOOP_D [integer in milliseconds] +To update the internal vRef constant value **(see notes below): CONST [long value] You can also enable or disable DEBUG output with: DEBUG [0|1] @@ -45,7 +47,6 @@ You can query the current state (including ADC measurements) with: STATE To reset all settings to defaults, use: RESET - These commands should be wrapped in this format: CMD INT @@ -62,9 +63,24 @@ The gain STATE is representative of these values: 2 = 4.33x 3 = 6x 4 = 11x -*/ -/*------------------------------------------------------------*/ +**Note for calibrating internal 1.1v vRef: +The ADC reading function assumes an "ideal" multiplier constant. Each Atmega +chip is slightly different, so it won't be completely accurate without tuning. +Most of the time this won't be necessary, so don't mess with this if you don't +know what you're doing! +The reading can be fine-tuned by using a multimeter, and this equation: + +scale_constant = internal1.1Ref * 1023 * 1000 + +where + +internal1.1Ref = 1.1 * Vcc1 (per voltmeter) / Vcc2 (per readVcc() function) + +If the scale_constant calculated is different from the default 1125300, +update the voltMeterConstant variable in pP_config.h with the correct value + +------------------------------------------------------------*/ // Debug output toggle. Uncomment to enable #define DEBUG true diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h index 2949d6e..657cbf0 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h @@ -47,11 +47,13 @@ extern int Debug; #endif +#define VM_CONST_ADDRESS 28 #if !(defined(voltMeterConstant)) extern long voltMeterConstant = 1125300L; // For fine tuning input voltage sense #endif #ifdef I2C_INPUT + #define I2C_SLAVE_ADDRESS 24 #if !(defined(pP_i2c_address)) extern byte pP_i2c_address = 0xa0; // I2C Bus Address #endif diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h index bbb7443..0868637 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h @@ -118,6 +118,16 @@ void updateHysteresis() { } /*------------------------------------------------*/ +void updateConstant() { + if (serialInt >= 0) + { + voltMeterConstant = serialInt; + EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); + } +} + +/*------------------------------------------------*/ + void updateDebug() { if (serialInt > 0) { Debug = 1; @@ -168,6 +178,9 @@ void serialPrintConfig() Serial.print("HYST "); Serial.println(Hyst); + + Serial.print("VM_CONST "); + Serial.println(voltMeterConstant); } void serialPrintState() @@ -212,6 +225,9 @@ void updateParams() { else if (strcmp(serialMessageIn, "HYST") == 0) { updateHysteresis(); } + else if (strcmp(serialMessageIn, "CONST") == 0) { + updateConstant(); + } else if (strcmp(serialMessageIn, "DEBUG") == 0) { updateDebug(); }