From 12f86c971b6423e21b550d0ae3377014a091d48b Mon Sep 17 00:00:00 2001 From: pyr0ball Date: Thu, 27 Feb 2020 12:34:10 -0800 Subject: [PATCH] Added code for switching between 3.3v and 5v signal output #featureadd - Fixes #67 --- .../src/Pyr0_Piezo_Sensor_V2.x.x.cpp | 15 +++++++++---- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h | 10 +++++++++ .../src/pP_config.cpp | 10 +++++++++ .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h | 6 +++++- .../src/pP_function.cpp | 17 +++++++++++++++ .../src/pP_function.h | 1 + .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h | 3 ++- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h | 21 +++++++++++++++++++ 8 files changed, 77 insertions(+), 6 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 a5ddd49..5cf11c7 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 @@ -11,10 +11,11 @@ * PC0 ADC0 (Voltage Reference Check 'A0') * PC1 ADC1 (Sensitivity Adjustment Check 'A1') * PD4 PCINT20 (Error feedback LED 'D4') - * PB6 PCINT6 (Voltage Adjustment Resistor 0 'D20') - * PB7 PCINT7 (Voltage Adjustment Resistor 1 'D21') - * PD5 T1 (Voltage Adjustment Resistor 2 'D5') - * PD6 PCINT22 (Voltage Adjustment Resistor 3 'D6') + * PB6 PCINT6 (Gain Adjustment Resistor 0 'D20') + * PB7 PCINT7 (Gain Adjustment Resistor 1 'D21') + * PD5 T1 (Gain Adjustment Resistor 2 'D5') + * PD6 PCINT22 (Gain Adjustment Resistor 3 'D6') + * PB0 PCINT0 (VCC Adjustment Resistors 'D8') * PB1 OC1A (Comparator VRef PWM Out 'D9') * PD3 OC2B (Voltage Follower VRef PWM Out 'D3') @@ -36,6 +37,7 @@ To change trigger active duration: TRG_D [integer for milliseconds] To change gain factor: GAIN_F [integer for gain state - see note*] To change the output logic: LOGIC [0|1] (0 for active low, 1 for active high) To enable piezo plugged detection: PZDET [0|1] (0 for disabled, 1 for enabled) +To set the sensor's output voltage: VCCSW [0|1] (0 for 3.3v, 1 for 5v) To change ADC hysteresis value: HYST [integer in millivolts] To change sensor input pullup vRef low threshold: VFOL [integer in millivolts] To change comparator trigger high threshold: VCOMP [integer in millivolts] @@ -128,6 +130,8 @@ void setup() { adjustGain(); + adjustVcc(); + digitalWriteFast(TRG_OUT, !LOGIC); } @@ -155,6 +159,9 @@ void loop() { // Set the amplification gain factor adjustGain(); + // Set the VCC input switch + adjustVcc(); + // Check voltage of first and second stages and compare against thresholds readVin(); VComp = analogReadFast(VCOMP_SENSE_PIN); diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h index 9e4d335..a183a2e 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h @@ -93,6 +93,16 @@ void updatePzDet(int value) } /*------------------------------------------------*/ +void updateVccSwitch(int value) +{ + if (value >= 0) + { + VCCSW = value; + EEPROM.put(VCCSW_ADDRESS, VCCSW); + } +} +/*------------------------------------------------*/ + void updateConstant(long value) { if (value >= 0) diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp index a1f426b..a44357d 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.cpp @@ -10,6 +10,7 @@ int TRG_DUR = TRG_DUR_DEFAULT; int Hyst = HYST_DEFAULT; int LOGIC = LOGIC_DEFAULT; int PZDET = PZDET_DEFAULT; +int VCCSW = VCCSW_DEFAULT; int Debug = 0; long voltMeterConstant = VM_CONST_DEFAULT; @@ -26,6 +27,7 @@ void eraseEEPROM() { EEPROM.put(HYST_ADDRESS, Hyst); EEPROM.put(PZDET_ADDRESS, PZDET); EEPROM.put(LOGIC_ADDRESS, LOGIC); + EEPROM.put(VCCSW_ADDRESS, VCCSW); EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); } @@ -91,6 +93,13 @@ void restoreConfig() { LOGIC = temp; } + EEPROM.get(VCCSW_ADDRESS, temp); + if (temp < 0 || temp > 1) { + erase = true; + } else { + VCCSW = temp; + } + long longTemp; EEPROM.get(VM_CONST_ADDRESS, longTemp); if (longTemp < 1000000L || longTemp > 1200000L) { @@ -116,6 +125,7 @@ void setDefaultConfig() { Hyst = HYST_DEFAULT; PZDET = PZDET_DEFAULT; LOGIC = LOGIC_DEFAULT; + VCCSW = VCCSW_DEFAULT; voltMeterConstant = VM_CONST_DEFAULT; adjustFollow(); adjustComp(); 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 14ae5ee..4538923 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 @@ -39,9 +39,13 @@ extern int LOGIC; // Trigger logic scheme, Active LOW is default #define PZDET_ADDRESS 26 extern int PZDET; // Enable or disable piezo connection detection, default is off +#define VCCSW_DEFAULT 0 +#define VCCSW_ADDRESS 28 +extern int VCCSW; // Enable or disable piezo connection detection, default is off + extern int Debug; -#define VM_CONST_ADDRESS 28 +#define VM_CONST_ADDRESS 30 #define VM_CONST_DEFAULT 1125300L extern long voltMeterConstant; // For fine tuning input voltage sense diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp index 44e8332..434b4e7 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp @@ -192,6 +192,23 @@ void adjustGain() /*------------------------------------------------*/ +void adjustVcc() +{ + switch (VCCSW) + { + case 0: + pinMode(VCCSW_PIN, OUTPUT); + digitalWriteFast(VCCSW_PIN, LOW); + break; + case 1: + default: + pinMode(VCCSW_PIN, INPUT); + break; + } +} + +/*------------------------------------------------*/ + //void checkError () { // if (ERR_STATE == 1) { // digitalWriteFast(ERR_LED, BlinkState); diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h index beecc0b..e686308 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.h @@ -36,6 +36,7 @@ update the voltMeterConstant variable in pP_config.h with the correct value void readVin(); void adjustFollow(); void adjustComp(); +void adjustVcc(); void calibrateAlert(); void adjustGain(); //void checkError () { diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h index bd103cb..dba3e74 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_pins.h @@ -30,4 +30,5 @@ Default pins (based on Rev.2.x.xPCB layout) #define GADJ_R3 6 // " #define V_FOL_PWM 3 // PWM analog output pin for voltage follower adjustment #define VCOMP_PWM 9 // PWM analog output pin for comparator adjustment -#define PZDET_PIN 16 // Digital input pin for detecting piezo connection \ No newline at end of file +#define PZDET_PIN 16 // Digital input pin for detecting piezo connection +#define VCCSW_PIN 8 // VCC variable regulator switch pin \ No newline at end of file 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 9f6b098..c2f4185 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 @@ -108,6 +108,21 @@ void serialPrintConfig() Serial.print("PZDET "); Serial.println(PZDET); + + Serial.print("VCCSW "); + Serial.print(VCCSW); + switch (VCCSW) + { + case 0: + Serial.println(" 3.3v"); + break; + case 1: + Serial.println(" 5v"); + break; + default: + Serial.println(" INVALID"); + break; + } Serial.print("VM_CONST "); Serial.println(voltMeterConstant); @@ -182,6 +197,10 @@ void updateParams() { updatePzDet(serialLong); } + else if (strcmp(serialMessageIn, "VCCSW") == 0) + { + updateVccSwitch(serialLong); + } else if (strcmp(serialMessageIn, "CONST") == 0) { updateConstant(serialLong); @@ -215,6 +234,8 @@ void updateParams() Serial.println(" (0 for active low, 1 for active high)"); Serial.println("To enable piezo plugged detection: PZDET [0|1]"); Serial.println(" (0 for disabled, 1 for enabled)"); + Serial.println("To change the main voltage of the circuit: VCCSW [0|1]") + Serial.println(" (0 for 3.3v, 1 for 5v)"); Serial.println("To change ADC hysteresis value: HYST [integer in millivolts]"); Serial.println("To enable or disable debug output: DEBUG [0|1]"); Serial.println("To print current config: CONFIG");