From 30afcdd8b7be6f0fd13ab5e50c1eb692173c7a66 Mon Sep 17 00:00:00 2001 From: loredan13 Date: Thu, 6 Feb 2020 11:12:32 +0300 Subject: [PATCH 1/4] Added separate cmd file --- .../src/Pyr0_Piezo_Sensor_V2.x.x.cpp | 4 - .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h | 119 +++++++ .../src/pP_config.cpp | 109 +++++++ .../src/pP_function.h | 198 ++++-------- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_serial.h | 296 +++++++----------- .../src/pP_volatile.h | 7 + 6 files changed, 411 insertions(+), 322 deletions(-) create mode 100644 firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h 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 6eae9d1..4062f02 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 @@ -84,10 +84,6 @@ update the voltMeterConstant variable in pP_config.h with the correct value ------------------------------------------------------------*/ -/* Debug output verbose mode will continuously output sensor readings - rather than waiting for user input */ -#define VERBOSE true - // Headers, variables, and functions #include #include 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 new file mode 100644 index 0000000..9e4d335 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_cmd.h @@ -0,0 +1,119 @@ +#ifndef PP_CMD_H +#define PP_CMD_H + +#include "pP_config.h" +#include "pP_function.h" +#include "EEPROM.h" + +/*------------------------------------------------*/ + +void updateGainFactor(int value) +{ + if (value >= 0) + { + GAIN_FACTOR = value; + adjustGain(); + EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); + } +} + +/*------------------------------------------------*/ + +void updateVFol(int value) +{ + if (value >= 0) + { + followerThrs = value; + adjustFollow(); + EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); + } +} +/*------------------------------------------------*/ + +void updateVComp(int value) +{ + if (value >= 0) + { + compThrs = value; + adjustComp(); + EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); + } +} + +/*------------------------------------------------*/ + +void updateLoopDuration(int value) +{ + if (value >= 0) + { + LOOP_DUR = value; + EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); + } +} +/*------------------------------------------------*/ + +void updateTrigDuration(int value) +{ + if (value >= 0) + { + TRG_DUR = value; + EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); + } +} +/*------------------------------------------------*/ + +void updateHysteresis(int value) +{ + if (value >= 0) + { + Hyst = value; + EEPROM.put(HYST_ADDRESS, Hyst); + } +} +/*------------------------------------------------*/ + +void updateLogic(int value) +{ + if (value >= 0) + { + LOGIC = value; + EEPROM.put(LOGIC_ADDRESS, LOGIC); + pulse(); + } +} +/*------------------------------------------------*/ + +void updatePzDet(int value) +{ + if (value >= 0) + { + PZDET = value; + EEPROM.put(PZDET_ADDRESS, PZDET); + } +} +/*------------------------------------------------*/ + +void updateConstant(long value) +{ + if (value >= 0) + { + voltMeterConstant = value; + EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); + } +} + +/*------------------------------------------------*/ + +void updateDebug(int value) +{ + if (value > 0) + { + Debug = 1; + } + else if (value == 0) + { + Debug = 0; + } +} + +#endif //PP_CMD_H \ No newline at end of file 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 7c74583..f284b6e 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 @@ -1,4 +1,5 @@ #include "pP_config.h" +#include "pP_function.h" #include int GAIN_FACTOR = GAIN_FACTOR_DEFAULT; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x @@ -12,3 +13,111 @@ int PZDET = PZDET_DEFAULT; // Enable/disable piezo connection detection int Debug = 0; long voltMeterConstant = VM_CONST_DEFAULT; uint8_t pP_i2c_address = 0xa0; + +/*------------------------------------------------*/ +void eraseEEPROM() { + + setDefaultConfig(); + + EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); + EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); + EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); + EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); + EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); + EEPROM.put(HYST_ADDRESS, Hyst); + EEPROM.put(PZDET_ADDRESS, PZDET); + EEPROM.put(LOGIC_ADDRESS, LOGIC); + EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); +} + +// Restore config from EEPROM, otherwise erase config and write to EEPROM +void restoreConfig() { + int temp; + + bool erase = false; + + EEPROM.get(GAIN_FACTOR_ADDRESS, temp); + if (temp < 0 || temp > 4) { + erase = true; + } else { + GAIN_FACTOR = temp; + } + + EEPROM.get(FOLLOWER_THRESHOLD_ADDRESS, temp); + if (temp < 0 || temp > 5000) { + erase = true; + } else { + followerThrs = temp; + } + + EEPROM.get(COMP_THRESHOLD_ADDRESS, temp); + if (temp < 0 || temp > 5000) { + erase = true; + } else { + compThrs = temp; + } + + EEPROM.get(LOOP_DUR_ADDRESS, temp); + if (temp < 0 && temp > 1000) { + erase = true; + } else { + LOOP_DUR = temp; + } + + EEPROM.get(TRG_DUR_ADDRESS, temp); + if (temp < 0 || temp > 1000) { + erase = true; + } else { + TRG_DUR = temp; + } + + EEPROM.get(HYST_ADDRESS, temp); + if (temp < 0 || temp > 1000) { + erase = true; + } else { + Hyst = temp; + } + + EEPROM.get(PZDET_ADDRESS, temp); + if (temp < 0 || temp > 1) { + erase = true; + } else { + PZDET = temp; + } + + EEPROM.get(LOGIC_ADDRESS, temp); + if (temp < 0 || temp > 1) { + erase = true; + } else { + LOGIC = temp; + } + + long longTemp; + EEPROM.get(VM_CONST_ADDRESS, longTemp); + if (longTemp < 1000000L || longTemp > 1200000L) { + erase = true; + } else { + voltMeterConstant = longTemp; + } + + if (erase) { + eraseEEPROM(); + } + + adjustFollow(); + adjustComp(); +} + +void setDefaultConfig() { + GAIN_FACTOR = GAIN_FACTOR_DEFAULT; + followerThrs = FOLLOWER_THRESHOLD_DEFAULT; + compThrs = COMP_THRESHOLD_DEFAULT; + LOOP_DUR = LOOP_DUR_DEFAULT; + TRG_DUR = TRG_DUR_DEFAULT; + Hyst = HYST_DEFAULT; + PZDET = PZDET_DEFAULT; + LOGIC = LOGIC_DEFAULT; + voltMeterConstant = VM_CONST_DEFAULT; + adjustFollow(); + adjustComp(); +} \ No newline at end of file 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 d721f33..612d697 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 @@ -6,17 +6,29 @@ //#pragma once //#include "pP_function.h" -void digitalWriteFast(uint8_t pin, uint8_t x) { - if (pin / 8) { // pin >= 8 +#ifndef PP_FUNCTION_H +#define PP_FUNCTION_H + +#include "Arduino.h" +#include "pP_volatile.h" +#include "pP_pins.h" +#include "stdint.h" + +void digitalWriteFast(uint8_t pin, uint8_t x) +{ + if (pin / 8) + { // pin >= 8 PORTB ^= (-x ^ PORTB) & (1 << (pin % 8)); } - else { + else + { PORTD ^= (-x ^ PORTD) & (1 << (pin % 8)); } } int inline analogReadFast(byte ADCpin) -{ byte ADCSRAoriginal = ADCSRA; +{ + byte ADCSRAoriginal = ADCSRA; ADCSRA = (ADCSRA & B11111000) | 4; int adc = analogRead(ADCpin); ADCSRA = ADCSRAoriginal; @@ -25,13 +37,15 @@ int inline analogReadFast(byte ADCpin) /*------------------------------------------------*/ -void doubleFlash() { - BlinkCount = 4 ; +void doubleFlash() +{ + BlinkCount = 4; } /*------------------------------------------------*/ -void pulse() { +void pulse() +{ digitalWriteFast(TRG_OUT, LOGIC); sensorHReading = 1; delay(TRG_DUR); @@ -42,7 +56,8 @@ void pulse() { /*------------------------------------------------*/ -long readVcc() { +long readVcc() +{ // Read 1.1V reference against AVcc // Atmega's Secret Voltmeter setup: @@ -60,15 +75,16 @@ long readVcc() { delay(2); // Wait for vref to settle ADCSRA |= _BV(ADSC); // Start conversion - while (bit_is_set(ADCSRA,ADSC)); // measuring + while (bit_is_set(ADCSRA, ADSC)) + ; // measuring - uint8_t low = ADCL; // must read ADCL first - it then locks ADCH + uint8_t low = ADCL; // must read ADCL first - it then locks ADCH uint8_t high = ADCH; // unlocks both - long result = (high<<8) | low; + long result = (high << 8) | low; result = voltMeterConstant / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 - return result; // Vcc in millivolts + return result; // Vcc in millivolts } /*------------------------------------------------- @@ -88,26 +104,28 @@ If the scale_constant calculated is different from the default 1125300, update the voltMeterConstant variable in pP_config.h with the correct value --------------------------------------------------*/ - void readVin() { - VOld = Vin; - Vin = readVcc(); - followerLong = followerThrs * 1023L; - compLong = compThrs * 1023L; - followerInt = (long long) followerLong / Vin; - compInt = (long long) compLong / Vin; - followerInt = (int) followerInt; - compInt = (int) compInt; - } +void readVin() +{ + VOld = Vin; + Vin = readVcc(); + followerLong = followerThrs * 1023L; + compLong = compThrs * 1023L; + followerInt = (long long)followerLong / Vin; + compInt = (long long)compLong / Vin; + followerInt = (int)followerInt; + compInt = (int)compInt; +} /*------------------------------------------------*/ - void adjustFollow() { +void adjustFollow() +{ /* Compares diffs of threshold vs read value if positive, adjusts the follower to within the range set above*/ followerLong = followerThrs * 1023L; - followerInt = (long long) followerLong / Vin; - followerInt = (int) followerInt; + followerInt = (long long)followerLong / Vin; + followerInt = (int)followerInt; ADJ_FOLLOW = (followerInt / 4); // Analog output (PWM) of duty cycle @@ -116,25 +134,29 @@ update the voltMeterConstant variable in pP_config.h with the correct value /*------------------------------------------------*/ -void adjustComp() { +void adjustComp() +{ compLong = compThrs * 1023L; - compInt = (long long) compLong / Vin; - compInt = (int) compInt; + compInt = (long long)compLong / Vin; + compInt = (int)compInt; OCR1A = compInt; } /*------------------------------------------------*/ -void calibrateAlert() { +void calibrateAlert() +{ VLast = VOld - Vin; - if (VLast > Hyst || VLast < -Hyst ) { + if (VLast > Hyst || VLast < -Hyst) + { ERR_STATE = 1; } } /*------------------------------------------------*/ -void adjustGain() { +void adjustGain() +{ switch (GAIN_FACTOR) { case 4: @@ -184,118 +206,14 @@ void adjustGain() { /*------------------------------------------------*/ -void pzConCheck () { +void pzConCheck() +{ PZ_STATE = digitalRead(PZDET_PIN); - if (PZ_STATE == PZDET) { + if (PZ_STATE == PZDET) + { //digitalWriteFast(TRG_OUT, LOGIC); ERR_STATE = 1; } } -/*------------------------------------------------*/ -void eraseEEPROM() { - - setDefaultConfig(); - - EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); - EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); - EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); - EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); - EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); - EEPROM.put(HYST_ADDRESS, Hyst); - EEPROM.put(PZDET_ADDRESS, PZDET); - EEPROM.put(LOGIC_ADDRESS, LOGIC); - EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); -} - -// Restore config from EEPROM, otherwise erase config and write to EEPROM -void restoreConfig() { - int temp; - - bool erase = false; - - EEPROM.get(GAIN_FACTOR_ADDRESS, temp); - if (temp < 0 || temp > 4) { - erase = true; - } else { - GAIN_FACTOR = temp; - } - - EEPROM.get(FOLLOWER_THRESHOLD_ADDRESS, temp); - if (temp < 0 || temp > 5000) { - erase = true; - } else { - followerThrs = temp; - } - - EEPROM.get(COMP_THRESHOLD_ADDRESS, temp); - if (temp < 0 || temp > 5000) { - erase = true; - } else { - compThrs = temp; - } - - EEPROM.get(LOOP_DUR_ADDRESS, temp); - if (temp < 0 && temp > 1000) { - erase = true; - } else { - LOOP_DUR = temp; - } - - EEPROM.get(TRG_DUR_ADDRESS, temp); - if (temp < 0 || temp > 1000) { - erase = true; - } else { - TRG_DUR = temp; - } - - EEPROM.get(HYST_ADDRESS, temp); - if (temp < 0 || temp > 1000) { - erase = true; - } else { - Hyst = temp; - } - - EEPROM.get(PZDET_ADDRESS, temp); - if (temp < 0 || temp > 1) { - erase = true; - } else { - PZDET = temp; - } - - EEPROM.get(LOGIC_ADDRESS, temp); - if (temp < 0 || temp > 1) { - erase = true; - } else { - LOGIC = temp; - } - - long longTemp; - EEPROM.get(VM_CONST_ADDRESS, longTemp); - if (longTemp < 1000000L || longTemp > 1200000L) { - erase = true; - } else { - voltMeterConstant = longTemp; - } - - if (erase) { - eraseEEPROM(); - } - - adjustFollow(); - adjustComp(); -} - -void setDefaultConfig() { - GAIN_FACTOR = GAIN_FACTOR_DEFAULT; - followerThrs = FOLLOWER_THRESHOLD_DEFAULT; - compThrs = COMP_THRESHOLD_DEFAULT; - LOOP_DUR = LOOP_DUR_DEFAULT; - TRG_DUR = TRG_DUR_DEFAULT; - Hyst = HYST_DEFAULT; - PZDET = PZDET_DEFAULT; - LOGIC = LOGIC_DEFAULT; - voltMeterConstant = VM_CONST_DEFAULT; - adjustFollow(); - adjustComp(); -} +#endif //PP_FUNCTION_H \ 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 63af5e5..9f6b098 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 @@ -1,150 +1,73 @@ -void parseData() { +#include "pP_cmd.h" + +void parseData() +{ // split the data into its parts - + char *strtokIndx; // this is used by strtok() as an index - + strtokIndx = strtok(inputBuffer, " "); // get the first part - the string strcpy(serialMessageIn, strtokIndx); // copy it to serialMessageIn - - strtokIndx = strtok(NULL, " "); // this continues where the previous call left off - serialLong = atol(strtokIndx); // convert this part to an integer + strtokIndx = strtok(NULL, " "); // this continues where the previous call left off + serialLong = atol(strtokIndx); // convert this part to an integer } /*------------------------------------------------*/ -void identifyMarkers() { - - char x = Serial.read(); - #ifdef I2C_INPUT - char y = Wire.read(); - #endif // I2C_INPUT +void identifyMarkers() +{ - if (x == '\n' || x == '\r') { + char x = Serial.read(); +#ifdef I2C_INPUT + char y = Wire.read(); +#endif // I2C_INPUT + + if (x == '\n' || x == '\r') + { serialIncoming = true; inputBuffer[bytesRecvd] = 0; parseData(); bytesRecvd = 0; - } else { + } + else + { inputBuffer[bytesRecvd] = x; bytesRecvd++; - if (bytesRecvd == buffSize) { + if (bytesRecvd == buffSize) + { bytesRecvd = buffSize - 1; } } - #ifdef I2C_INPUT - if (y == '\n' || y == '\r') { - serialIncoming = true; - inputBuffer[bytesRecvd] = 0; - parseData(); - bytesRecvd = 0; - } else { - inputBuffer[bytesRecvd] = y; - bytesRecvd++; - if (bytesRecvd == buffSize) { - bytesRecvd = buffSize - 1; - } +#ifdef I2C_INPUT + if (y == '\n' || y == '\r') + { + serialIncoming = true; + inputBuffer[bytesRecvd] = 0; + parseData(); + bytesRecvd = 0; + } + else + { + inputBuffer[bytesRecvd] = y; + bytesRecvd++; + if (bytesRecvd == buffSize) + { + bytesRecvd = buffSize - 1; } - #endif + } +#endif } /*------------------------------------------------*/ -void updateGainFactor() +void serialPrintConfig() { - if (serialLong >= 0) { - GAIN_FACTOR = serialLong; - adjustGain(); - EEPROM.put(GAIN_FACTOR_ADDRESS, GAIN_FACTOR); - } -} - -/*------------------------------------------------*/ - -void updateVFol() { - if (serialLong >= 0) { - followerThrs = serialLong; - adjustFollow(); - EEPROM.put(FOLLOWER_THRESHOLD_ADDRESS, followerThrs); - } -} -/*------------------------------------------------*/ - -void updateVComp() { - if (serialLong >= 0) { - compThrs = serialLong; - adjustComp(); - EEPROM.put(COMP_THRESHOLD_ADDRESS, compThrs); - } -} - -/*------------------------------------------------*/ - -void updateLoopDuration() { - if (serialLong >= 0) { - LOOP_DUR = serialLong; - EEPROM.put(LOOP_DUR_ADDRESS, LOOP_DUR); - } -} -/*------------------------------------------------*/ - -void updateTrigDuration() { - if (serialLong >= 0) { - TRG_DUR = serialLong; - EEPROM.put(TRG_DUR_ADDRESS, TRG_DUR); - } -} -/*------------------------------------------------*/ - -void updateHysteresis() { - if (serialLong >= 0) { - Hyst = serialLong; - EEPROM.put(HYST_ADDRESS, Hyst); - } -} -/*------------------------------------------------*/ - -void updateLogic() { - if (serialLong >= 0) { - LOGIC = serialLong; - EEPROM.put(LOGIC_ADDRESS, LOGIC); - pulse(); - } -} -/*------------------------------------------------*/ - -void updatePzDet() { - if (serialLong >= 0) { - PZDET = serialLong; - EEPROM.put(PZDET_ADDRESS, PZDET); - } -} -/*------------------------------------------------*/ - -void updateConstant() { - if (serialLong >= 0) { - voltMeterConstant = (long) serialLong; - EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant); - } -} - -/*------------------------------------------------*/ - -void updateDebug() { - if (serialLong > 0) { - Debug = 1; - } else if (serialLong == 0) { - Debug = 0; - } -} - -/*------------------------------------------------*/ - -void serialPrintConfig() { Serial.print("GAIN_F "); Serial.print(GAIN_FACTOR); - switch (GAIN_FACTOR) { + switch (GAIN_FACTOR) + { case 0: Serial.println(" 3x"); break; @@ -188,12 +111,13 @@ void serialPrintConfig() { Serial.print("VM_CONST "); Serial.println(voltMeterConstant); - + Serial.print("Firmware Version "); Serial.println(PP_VERSION); } -void serialPrintState() { +void serialPrintState() +{ Serial.print("{"); Serial.print("\"Vcc\":"); @@ -201,11 +125,11 @@ void serialPrintState() { Serial.print(","); Serial.print("\"VComp\":"); - Serial.print((long) VComp * Vin / 1023); + Serial.print((long)VComp * Vin / 1023); Serial.print(","); Serial.print("\"VFol\":"); - Serial.print((long) VFol * Vin / 1023); + Serial.print((long)VFol * Vin / 1023); Serial.print(","); Serial.print("\"Err\":"); @@ -223,83 +147,99 @@ void serialPrintState() { Serial.println("}"); } -void updateParams() { +void updateParams() +{ serialIncoming = false; - if (strcmp(serialMessageIn, "GAIN_F") == 0) { - updateGainFactor(); + if (strcmp(serialMessageIn, "GAIN_F") == 0) + { + updateGainFactor(serialLong); } - else if (strcmp(serialMessageIn, "VFOL") == 0) { - updateVFol(); + else if (strcmp(serialMessageIn, "VFOL") == 0) + { + updateVFol(serialLong); } - else if (strcmp(serialMessageIn, "VCOMP") == 0) { - updateVComp(); + else if (strcmp(serialMessageIn, "VCOMP") == 0) + { + updateVComp(serialLong); } - else if (strcmp(serialMessageIn, "LOOP_D") == 0) { - updateLoopDuration(); + else if (strcmp(serialMessageIn, "LOOP_D") == 0) + { + updateLoopDuration(serialLong); } - else if (strcmp(serialMessageIn, "TRG_D") == 0) { - updateTrigDuration(); + else if (strcmp(serialMessageIn, "TRG_D") == 0) + { + updateTrigDuration(serialLong); } - else if (strcmp(serialMessageIn, "HYST") == 0) { - updateHysteresis(); + else if (strcmp(serialMessageIn, "HYST") == 0) + { + updateHysteresis(serialLong); } - else if (strcmp(serialMessageIn, "LOGIC") == 0) { - updateLogic(); + else if (strcmp(serialMessageIn, "LOGIC") == 0) + { + updateLogic(serialLong); } - else if (strcmp(serialMessageIn, "PZDET") == 0) { - updatePzDet(); + else if (strcmp(serialMessageIn, "PZDET") == 0) + { + updatePzDet(serialLong); } - else if (strcmp(serialMessageIn, "CONST") == 0) { - updateConstant(); + else if (strcmp(serialMessageIn, "CONST") == 0) + { + updateConstant(serialLong); } - else if (strcmp(serialMessageIn, "DEBUG") == 0) { - updateDebug(); + else if (strcmp(serialMessageIn, "DEBUG") == 0) + { + updateDebug(serialLong); } - else if (strcmp(serialMessageIn, "CONFIG") == 0) { + else if (strcmp(serialMessageIn, "CONFIG") == 0) + { serialPrintConfig(); } - else if (strcmp(serialMessageIn, "ERASE") == 0) { + else if (strcmp(serialMessageIn, "ERASE") == 0) + { eraseEEPROM(); serialPrintConfig(); } - else if (strcmp(serialMessageIn, "STATE") == 0) { + else if (strcmp(serialMessageIn, "STATE") == 0) + { serialPrintState(); } - else if (strcmp(serialMessageIn, "HELP") == 0) { - #if defined(ARDUINO_AVR_ATmega328PB) - Serial.println("To change gain factor: GAIN_F [integer for gain state - see note*]"); - Serial.println("To change voltage follower voltage (low threshold): VFOL [float value]"); - Serial.println("To change comparator voltage (high threshold): VCOMP [float value]"); - Serial.println("To change main loop period: LOOP_D [integer for milliseconds]"); - Serial.println("To change trigger active duration: TRG_D [integer for milliseconds]"); - Serial.println("To change the output logic: LOGIC [0|1]"); - 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 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"); - Serial.println("To set config to defaults: ERASE"); - Serial.println("To print current state: STATE"); - Serial.println(""); - Serial.println("Commands are entered in this format:"); - Serial.println("CMD VAL"); - Serial.println("Commands are confirmed with Enter key"); - Serial.println(""); - Serial.println("Examples:"); - Serial.println("GAIN_F 3 <~ set gain factor to index 3 (6x)"); - Serial.println("VFOL 2350 <~ set the vref floor to 2.35V"); - #else - Serial.println("Check docs.pyroballpcbs.com/config"); - #endif // defined(ARDUINO_AVR_ATmega328PB) + else if (strcmp(serialMessageIn, "HELP") == 0) + { +#if defined(ARDUINO_AVR_ATmega328PB) + Serial.println("To change gain factor: GAIN_F [integer for gain state - see note*]"); + Serial.println("To change voltage follower voltage (low threshold): VFOL [float value]"); + Serial.println("To change comparator voltage (high threshold): VCOMP [float value]"); + Serial.println("To change main loop period: LOOP_D [integer for milliseconds]"); + Serial.println("To change trigger active duration: TRG_D [integer for milliseconds]"); + Serial.println("To change the output logic: LOGIC [0|1]"); + 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 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"); + Serial.println("To set config to defaults: ERASE"); + Serial.println("To print current state: STATE"); + Serial.println(""); + Serial.println("Commands are entered in this format:"); + Serial.println("CMD VAL"); + Serial.println("Commands are confirmed with Enter key"); + Serial.println(""); + Serial.println("Examples:"); + Serial.println("GAIN_F 3 <~ set gain factor to index 3 (6x)"); + Serial.println("VFOL 2350 <~ set the vref floor to 2.35V"); +#else + Serial.println("Check docs.pyroballpcbs.com/config"); +#endif // defined(ARDUINO_AVR_ATmega328PB) } parseData(); -} +} - -void serialInput() { +void serialInput() +{ // receive data from Serial and save it into inputBuffer - if (Serial.available() > 0) { + if (Serial.available() > 0) + { // the order of these IF clauses is significant identifyMarkers(); diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h index a3f5e07..ac1c5eb 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h @@ -1,3 +1,8 @@ +#ifndef PP_VOLATILE_H +#define PP_VOLATILE_H + +#include "LightChrono.h" + // these variables will change on their own. Do not edit ANYTHING below this line volatile int sensorHReading = 0; // variable to store the value read from the sensor pin volatile int ADJ_FOLLOW = 0; // Variable for Follower adjustment @@ -41,3 +46,5 @@ long serialLong = 0; // Task scheduler instances LightChrono mainLoop; + +#endif //PP_VOLATILE_H \ No newline at end of file From 36b6402b2e1bf0a174e935cc42dd0559d939d87c Mon Sep 17 00:00:00 2001 From: loredan13 Date: Thu, 6 Feb 2020 19:06:46 +0300 Subject: [PATCH 2/4] Attempt at fixing compile error --- .../src/pP_config.cpp | 12 ----- .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h | 48 +++++-------------- .../src/pP_function.h | 1 + 3 files changed, 14 insertions(+), 47 deletions(-) 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 f284b6e..d7225ca 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 @@ -2,18 +2,6 @@ #include "pP_function.h" #include -int GAIN_FACTOR = GAIN_FACTOR_DEFAULT; // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x -int followerThrs = FOLLOWER_THRESHOLD_DEFAULT; -int compThrs = COMP_THRESHOLD_DEFAULT; -int LOOP_DUR = LOOP_DUR_DEFAULT; // duration of time between ADC checks and other loop functions -int TRG_DUR = TRG_DUR_DEFAULT; // duration of the Z-axis pulse sent, in ms -int Hyst = HYST_DEFAULT; // Hysteresis value for ADC measurements -int LOGIC = LOGIC_DEFAULT; // Trigger output logic (active low or active high) -int PZDET = PZDET_DEFAULT; // Enable/disable piezo connection detection -int Debug = 0; -long voltMeterConstant = VM_CONST_DEFAULT; -uint8_t pP_i2c_address = 0xa0; - /*------------------------------------------------*/ void eraseEEPROM() { 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 abe8a97..92d31e9 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 @@ -5,71 +5,49 @@ #define GAIN_FACTOR_DEFAULT 2 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x #define GAIN_FACTOR_ADDRESS 0 -#if !(defined(GAIN_FACTOR)) - extern int GAIN_FACTOR; -#endif +int GAIN_FACTOR = GAIN_FACTOR_DEFAULT; #define FOLLOWER_THRESHOLD_DEFAULT 1450 // Voltage follower default voltage in mV #define FOLLOWER_THRESHOLD_ADDRESS 4 -#if !(defined(followerThrs)) - extern int followerThrs; -#endif +int followerThrs = 1450; #define COMP_THRESHOLD_DEFAULT 2850 // Comparatore Vref default voltage in mV #define COMP_THRESHOLD_ADDRESS 8 -#if !(defined(compThrs)) - extern int compThrs; -#endif +int compThrs = COMP_THRESHOLD_DEFAULT; #ifndef InitCount - #define InitCount 6 // Number of times to blink the LED on start +#define InitCount 6 // Number of times to blink the LED on start #endif #define LOOP_DUR_DEFAULT 50 // duration of time between ADC checks and other loop functions #define LOOP_DUR_ADDRESS 12 -#if !(defined(LOOP_DUR)) - extern int LOOP_DUR; -#endif +int LOOP_DUR = LOOP_DUR_DEFAULT; #define TRG_DUR_DEFAULT 20 // duration of the Z-axis pulse sent, in ms #define TRG_DUR_ADDRESS 16 -#if !(defined(TRG_DUR)) - extern int TRG_DUR; -#endif +int TRG_DUR = TRG_DUR_DEFAULT; #define HYST_DEFAULT 20 #define HYST_ADDRESS 20 -#if !(defined(Hyst)) - extern int Hyst; // Hysteresis value for ADC measurements -#endif +int Hyst = HYST_DEFAULT; // Hysteresis value for ADC measurements #define LOGIC_DEFAULT 1 #define LOGIC_ADDRESS 32 -#if !(defined(LOGIC)) - extern int LOGIC; // Trigger logic scheme, Active LOW is default -#endif +int LOGIC = LOGIC_DEFAULT; // Trigger logic scheme, Active LOW is default #define PZDET_DEFAULT 0 #define PZDET_ADDRESS 26 -#if !(defined(PZDET)) - extern int PZDET; // Enable or disable piezo connection detection, default is off -#endif +int PZDET = PZDET_DEFAULT; // Enable or disable piezo connection detection, default is off -#if !(defined(Debug)) - extern int Debug; -#endif +int Debug = 0; #define VM_CONST_ADDRESS 28 #define VM_CONST_DEFAULT 1125300L -#if !(defined(voltMeterConstant)) - extern long voltMeterConstant; // For fine tuning input voltage sense -#endif +long voltMeterConstant = VM_CONST_DEFAULT; // For fine tuning input voltage sense #ifdef I2C_INPUT - #define I2C_SLAVE_ADDRESS 24 - #if !(defined(pP_i2c_address)) - extern uint8_t pP_i2c_address; // I2C Bus Address - #endif +#define I2C_SLAVE_ADDRESS 24 +uint8_t pP_i2c_address = 0xa0; // I2C Bus Address #endif // I2C_INPUT void eraseEEPROM(); 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 612d697..5bc2061 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 @@ -10,6 +10,7 @@ #define PP_FUNCTION_H #include "Arduino.h" +#include "pP_config.h" #include "pP_volatile.h" #include "pP_pins.h" #include "stdint.h" From bf800e99409aa2a77a68f65a6380e21cf10abf8a Mon Sep 17 00:00:00 2001 From: Vsevolod Merenkov Date: Wed, 19 Feb 2020 03:32:22 +0300 Subject: [PATCH 3/4] Separating declaration and definition - volatile --- .../src/Pyr0_Piezo_Sensor_V2.x.x.cpp | 2 +- .../src/pP_volatile.cpp | 36 ++++++++++++++++++ .../src/pP_volatile.h | 37 ++++++++++--------- 3 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp 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 4062f02..a5ddd49 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 @@ -89,7 +89,7 @@ update the voltMeterConstant variable in pP_config.h with the correct value #include #include "LightChrono.h" #include "pP_pins.h" -#include "pP_config.h" +// #include "pP_config.h" #include "pP_volatile.h" #include "pP_function.h" #include "pP_serial.h" diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp new file mode 100644 index 0000000..1906e14 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp @@ -0,0 +1,36 @@ +#include "pP_volatile.h" +#include "pP_config.h" +#include "stdint.h" + +// these variables will change on their own. Do not edit ANYTHING below this line +volatile int sensorHReading = 0; // variable to store the value read from the sensor pin +volatile int ADJ_FOLLOW = 0; // Variable for Follower adjustment +volatile int ADJ_COMP = 0; // Variable for Comparator adjustment +volatile int ERR_STATE = 0; +volatile int PZ_STATE = 0; + +int Vin = 5000; // input reference voltage in millivolts (multiply V by 1000) +int VOld = 5000; // Variable to store previous cycle's Vin +int VLast = 0; + +// Convert threshold values based on the input voltage + +long followerLong = followerThrs * 1023L; +long compLong = compThrs * 1023L; + +// Voltage Comparator Adjustment parameters +int VComp = 0; + +// Voltage Follower Adjustment parameters +int VFol = 0; + +// Error blink parameters + +int BlinkState = 0; +int BlinkCount = (InitCount * 2) + 1; // Multiply Blink count by 2 to handle toggle state, add one extra to make sure light is on after + +// Serial Input Parsing Variables +uint8_t bytesRecvd = 0; +bool serialIncoming = false; +char serialMessageIn[buffSize] = {0}; +long serialLong = 0; \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h index ac1c5eb..7dd9c27 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h @@ -2,44 +2,45 @@ #define PP_VOLATILE_H #include "LightChrono.h" +#include "stdint.h" // these variables will change on their own. Do not edit ANYTHING below this line -volatile int sensorHReading = 0; // variable to store the value read from the sensor pin -volatile int ADJ_FOLLOW = 0; // Variable for Follower adjustment -volatile int ADJ_COMP = 0; // Variable for Comparator adjustment -volatile int ERR_STATE = 0; -volatile int PZ_STATE = 0; +extern volatile int sensorHReading; // variable to store the value read from the sensor pin +extern volatile int ADJ_FOLLOW; // Variable for Follower adjustment +extern volatile int ADJ_COMP; // Variable for Comparator adjustment +extern volatile int ERR_STATE; +extern volatile int PZ_STATE; -int Vin = 5000; // input reference voltage in millivolts (multiply V by 1000) -int VOld = 5000; // Variable to store previous cycle's Vin -int VLast = 0; +extern int Vin; // input reference voltage in millivolts (multiply V by 1000) +extern int VOld; // Variable to store previous cycle's Vin +extern int VLast; // Convert threshold values based on the input voltage -long followerLong = followerThrs * 1023L; -long compLong = compThrs * 1023L; +extern long followerLong; +extern long compLong; long followerInt; long compInt; // Voltage Comparator Adjustment parameters -int VComp = 0; +extern int VComp; // Voltage Follower Adjustment parameters -int VFol = 0; +extern int VFol; // Error blink parameters -int BlinkState = 0; -int BlinkCount = (InitCount * 2) + 1; // Multiply Blink count by 2 to handle toggle state, add one extra to make sure light is on after +extern int BlinkState; +extern int BlinkCount; // Multiply Blink count by 2 to handle toggle state, add one extra to make sure light is on after // Serial Input Parsing Variables #define buffSize 40 char inputBuffer[buffSize]; #define endMarker '\n' -byte bytesRecvd = 0; -bool serialIncoming = false; -char serialMessageIn[buffSize] = {0}; -long serialLong = 0; +extern uint8_t bytesRecvd; +extern bool serialIncoming; +extern char serialMessageIn[buffSize]; +extern long serialLong; //#define LOW 0 //#define HIGH 1 From 60a26ad3f713238cdc536b0302aa6de5a44fa6f2 Mon Sep 17 00:00:00 2001 From: loredan13 Date: Wed, 19 Feb 2020 19:25:24 +0300 Subject: [PATCH 4/4] More separation of declaration and definition --- .../src/pP_config.cpp | 11 + .../Pyr0_Piezo_Sensor_v2.x.x/src/pP_config.h | 20 +- .../src/pP_function.cpp | 216 ++++++++++++++++++ .../src/pP_function.h | 189 +-------------- .../src/pP_volatile.cpp | 7 +- .../src/pP_volatile.h | 8 +- 6 files changed, 258 insertions(+), 193 deletions(-) create mode 100644 firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp 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 d7225ca..a1f426b 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 @@ -2,6 +2,17 @@ #include "pP_function.h" #include +int GAIN_FACTOR = GAIN_FACTOR_DEFAULT; +int followerThrs = FOLLOWER_THRESHOLD_DEFAULT; +int compThrs = COMP_THRESHOLD_DEFAULT; +int LOOP_DUR = LOOP_DUR_DEFAULT; +int TRG_DUR = TRG_DUR_DEFAULT; +int Hyst = HYST_DEFAULT; +int LOGIC = LOGIC_DEFAULT; +int PZDET = PZDET_DEFAULT; +int Debug = 0; +long voltMeterConstant = VM_CONST_DEFAULT; + /*------------------------------------------------*/ void eraseEEPROM() { 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 92d31e9..14ae5ee 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 @@ -5,15 +5,15 @@ #define GAIN_FACTOR_DEFAULT 2 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x #define GAIN_FACTOR_ADDRESS 0 -int GAIN_FACTOR = GAIN_FACTOR_DEFAULT; +extern int GAIN_FACTOR; #define FOLLOWER_THRESHOLD_DEFAULT 1450 // Voltage follower default voltage in mV #define FOLLOWER_THRESHOLD_ADDRESS 4 -int followerThrs = 1450; +extern int followerThrs; #define COMP_THRESHOLD_DEFAULT 2850 // Comparatore Vref default voltage in mV #define COMP_THRESHOLD_ADDRESS 8 -int compThrs = COMP_THRESHOLD_DEFAULT; +extern int compThrs; #ifndef InitCount #define InitCount 6 // Number of times to blink the LED on start @@ -21,29 +21,29 @@ int compThrs = COMP_THRESHOLD_DEFAULT; #define LOOP_DUR_DEFAULT 50 // duration of time between ADC checks and other loop functions #define LOOP_DUR_ADDRESS 12 -int LOOP_DUR = LOOP_DUR_DEFAULT; +extern int LOOP_DUR; #define TRG_DUR_DEFAULT 20 // duration of the Z-axis pulse sent, in ms #define TRG_DUR_ADDRESS 16 -int TRG_DUR = TRG_DUR_DEFAULT; +extern int TRG_DUR; #define HYST_DEFAULT 20 #define HYST_ADDRESS 20 -int Hyst = HYST_DEFAULT; // Hysteresis value for ADC measurements +extern int Hyst; // Hysteresis value for ADC measurements #define LOGIC_DEFAULT 1 #define LOGIC_ADDRESS 32 -int LOGIC = LOGIC_DEFAULT; // Trigger logic scheme, Active LOW is default +extern int LOGIC; // Trigger logic scheme, Active LOW is default #define PZDET_DEFAULT 0 #define PZDET_ADDRESS 26 -int PZDET = PZDET_DEFAULT; // Enable or disable piezo connection detection, default is off +extern int PZDET; // Enable or disable piezo connection detection, default is off -int Debug = 0; +extern int Debug; #define VM_CONST_ADDRESS 28 #define VM_CONST_DEFAULT 1125300L -long voltMeterConstant = VM_CONST_DEFAULT; // For fine tuning input voltage sense +extern long voltMeterConstant; // For fine tuning input voltage sense #ifdef I2C_INPUT #define I2C_SLAVE_ADDRESS 24 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 new file mode 100644 index 0000000..44e8332 --- /dev/null +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_function.cpp @@ -0,0 +1,216 @@ +/* + pyr0-piezo functions library + Created by Alan "pyr0ball" Weinstock 6/26/2019 +*/ + +//#pragma once +//#include "pP_function.h" + +#include "pP_function.h" +#include "Arduino.h" +#include "pP_config.h" +#include "pP_volatile.h" +#include "pP_pins.h" +#include "stdint.h" + +void digitalWriteFast(uint8_t pin, uint8_t x) +{ + if (pin / 8) + { // pin >= 8 + PORTB ^= (-x ^ PORTB) & (1 << (pin % 8)); + } + else + { + PORTD ^= (-x ^ PORTD) & (1 << (pin % 8)); + } +} + +int analogReadFast(uint8_t ADCpin) +{ + byte ADCSRAoriginal = ADCSRA; + ADCSRA = (ADCSRA & B11111000) | 4; + int adc = analogRead(ADCpin); + ADCSRA = ADCSRAoriginal; + return adc; +} + +/*------------------------------------------------*/ + +void doubleFlash() +{ + BlinkCount = 4; +} + +/*------------------------------------------------*/ + +void pulse() +{ + digitalWriteFast(TRG_OUT, LOGIC); + sensorHReading = 1; + delay(TRG_DUR); + digitalWriteFast(TRG_OUT, !LOGIC); + Serial.println("Trig'd!"); + doubleFlash(); +} + +/*------------------------------------------------*/ + +long readVcc() +{ + // Read 1.1V reference against AVcc + + // Atmega's Secret Voltmeter setup: + // set the reference to Vcc and the measurement to the internal 1.1V reference +#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) + ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); +#elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) + ADMUX = _BV(MUX5) | _BV(MUX0); +#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) + ADMUX = _BV(MUX3) | _BV(MUX2); +#else + ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); +#endif + + delay(2); // Wait for vref to settle + + ADCSRA |= _BV(ADSC); // Start conversion + while (bit_is_set(ADCSRA, ADSC)) + ; // measuring + + uint8_t low = ADCL; // must read ADCL first - it then locks ADCH + uint8_t high = ADCH; // unlocks both + + long result = (high << 8) | low; + + result = voltMeterConstant / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 + return result; // Vcc in millivolts +} + +/*------------------------------------------------- +The above 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 +--------------------------------------------------*/ + +void readVin() +{ + VOld = Vin; + Vin = readVcc(); + followerLong = followerThrs * 1023L; + compLong = compThrs * 1023L; + followerInt = (long long)followerLong / Vin; + compInt = (long long)compLong / Vin; + followerInt = (int)followerInt; + compInt = (int)compInt; +} + +/*------------------------------------------------*/ + +void adjustFollow() +{ + /* Compares diffs of threshold vs read value + if positive, adjusts the follower to within + the range set above*/ + followerLong = followerThrs * 1023L; + followerInt = (long long)followerLong / Vin; + followerInt = (int)followerInt; + ADJ_FOLLOW = (followerInt / 4); + + // Analog output (PWM) of duty cycle + OCR2B = ADJ_FOLLOW; +} + +/*------------------------------------------------*/ + +void adjustComp() +{ + compLong = compThrs * 1023L; + compInt = (long long)compLong / Vin; + compInt = (int)compInt; + OCR1A = compInt; +} + +/*------------------------------------------------*/ + +void calibrateAlert() +{ + VLast = VOld - Vin; + if (VLast > Hyst || VLast < -Hyst) + { + ERR_STATE = 1; + } +} + +/*------------------------------------------------*/ + +void adjustGain() +{ + switch (GAIN_FACTOR) + { + case 4: + pinMode(GADJ_R0, OUTPUT); + digitalWriteFast(GADJ_R0, LOW); + break; + case 3: + pinMode(GADJ_R1, OUTPUT); + digitalWriteFast(GADJ_R1, LOW); + pinMode(GADJ_R0, INPUT); + break; + case 2: + pinMode(GADJ_R2, OUTPUT); + digitalWriteFast(GADJ_R2, LOW); + pinMode(GADJ_R1, INPUT); + pinMode(GADJ_R0, INPUT); + break; + case 1: + pinMode(GADJ_R3, OUTPUT); + digitalWriteFast(GADJ_R3, LOW); + pinMode(GADJ_R2, INPUT); + pinMode(GADJ_R1, INPUT); + pinMode(GADJ_R0, INPUT); + break; + case 0: + default: + pinMode(GADJ_R3, INPUT); + pinMode(GADJ_R2, INPUT); + pinMode(GADJ_R1, INPUT); + pinMode(GADJ_R0, INPUT); + break; + } +} + +/*------------------------------------------------*/ + +//void checkError () { +// if (ERR_STATE == 1) { +// digitalWriteFast(ERR_LED, BlinkState); +// BlinkState = !BlinkState; +// } +// else if (ERR_STATE == 0) { +// BlinkState = LOW; +// digitalWriteFast(ERR_LED, BlinkState); +// } +//} + +/*------------------------------------------------*/ + +void pzConCheck() +{ + PZ_STATE = digitalRead(PZDET_PIN); + if (PZ_STATE == PZDET) + { + //digitalWriteFast(TRG_OUT, LOGIC); + ERR_STATE = 1; + } +} \ No newline at end of file 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 5bc2061..beecc0b 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 @@ -9,84 +9,13 @@ #ifndef PP_FUNCTION_H #define PP_FUNCTION_H -#include "Arduino.h" -#include "pP_config.h" -#include "pP_volatile.h" -#include "pP_pins.h" #include "stdint.h" -void digitalWriteFast(uint8_t pin, uint8_t x) -{ - if (pin / 8) - { // pin >= 8 - PORTB ^= (-x ^ PORTB) & (1 << (pin % 8)); - } - else - { - PORTD ^= (-x ^ PORTD) & (1 << (pin % 8)); - } -} - -int inline analogReadFast(byte ADCpin) -{ - byte ADCSRAoriginal = ADCSRA; - ADCSRA = (ADCSRA & B11111000) | 4; - int adc = analogRead(ADCpin); - ADCSRA = ADCSRAoriginal; - return adc; -} - -/*------------------------------------------------*/ - -void doubleFlash() -{ - BlinkCount = 4; -} - -/*------------------------------------------------*/ - -void pulse() -{ - digitalWriteFast(TRG_OUT, LOGIC); - sensorHReading = 1; - delay(TRG_DUR); - digitalWriteFast(TRG_OUT, !LOGIC); - Serial.println("Trig'd!"); - doubleFlash(); -} - -/*------------------------------------------------*/ - -long readVcc() -{ - // Read 1.1V reference against AVcc - - // Atmega's Secret Voltmeter setup: - // set the reference to Vcc and the measurement to the internal 1.1V reference -#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) - ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); -#elif defined(__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) - ADMUX = _BV(MUX5) | _BV(MUX0); -#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) - ADMUX = _BV(MUX3) | _BV(MUX2); -#else - ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); -#endif - - delay(2); // Wait for vref to settle - - ADCSRA |= _BV(ADSC); // Start conversion - while (bit_is_set(ADCSRA, ADSC)) - ; // measuring - - uint8_t low = ADCL; // must read ADCL first - it then locks ADCH - uint8_t high = ADCH; // unlocks both - - long result = (high << 8) | low; - - result = voltMeterConstant / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 - return result; // Vcc in millivolts -} +void digitalWriteFast(uint8_t pin, uint8_t x); +int analogReadFast(uint8_t ADCpin); +void doubleFlash(); +void pulse(); +long readVcc(); /*------------------------------------------------- The above function assumes an "ideal" multiplier constant. @@ -104,96 +33,11 @@ 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 --------------------------------------------------*/ - -void readVin() -{ - VOld = Vin; - Vin = readVcc(); - followerLong = followerThrs * 1023L; - compLong = compThrs * 1023L; - followerInt = (long long)followerLong / Vin; - compInt = (long long)compLong / Vin; - followerInt = (int)followerInt; - compInt = (int)compInt; -} - -/*------------------------------------------------*/ - -void adjustFollow() -{ - /* Compares diffs of threshold vs read value - if positive, adjusts the follower to within - the range set above*/ - followerLong = followerThrs * 1023L; - followerInt = (long long)followerLong / Vin; - followerInt = (int)followerInt; - ADJ_FOLLOW = (followerInt / 4); - - // Analog output (PWM) of duty cycle - OCR2B = ADJ_FOLLOW; -} - -/*------------------------------------------------*/ - -void adjustComp() -{ - compLong = compThrs * 1023L; - compInt = (long long)compLong / Vin; - compInt = (int)compInt; - OCR1A = compInt; -} - -/*------------------------------------------------*/ - -void calibrateAlert() -{ - VLast = VOld - Vin; - if (VLast > Hyst || VLast < -Hyst) - { - ERR_STATE = 1; - } -} - -/*------------------------------------------------*/ - -void adjustGain() -{ - switch (GAIN_FACTOR) - { - case 4: - pinMode(GADJ_R0, OUTPUT); - digitalWriteFast(GADJ_R0, LOW); - break; - case 3: - pinMode(GADJ_R1, OUTPUT); - digitalWriteFast(GADJ_R1, LOW); - pinMode(GADJ_R0, INPUT); - break; - case 2: - pinMode(GADJ_R2, OUTPUT); - digitalWriteFast(GADJ_R2, LOW); - pinMode(GADJ_R1, INPUT); - pinMode(GADJ_R0, INPUT); - break; - case 1: - pinMode(GADJ_R3, OUTPUT); - digitalWriteFast(GADJ_R3, LOW); - pinMode(GADJ_R2, INPUT); - pinMode(GADJ_R1, INPUT); - pinMode(GADJ_R0, INPUT); - break; - case 0: - default: - pinMode(GADJ_R3, INPUT); - pinMode(GADJ_R2, INPUT); - pinMode(GADJ_R1, INPUT); - pinMode(GADJ_R0, INPUT); - break; - } -} - -/*------------------------------------------------*/ - +void readVin(); +void adjustFollow(); +void adjustComp(); +void calibrateAlert(); +void adjustGain(); //void checkError () { // if (ERR_STATE == 1) { // digitalWriteFast(ERR_LED, BlinkState); @@ -204,17 +48,6 @@ void adjustGain() // digitalWriteFast(ERR_LED, BlinkState); // } //} - -/*------------------------------------------------*/ - -void pzConCheck() -{ - PZ_STATE = digitalRead(PZDET_PIN); - if (PZ_STATE == PZDET) - { - //digitalWriteFast(TRG_OUT, LOGIC); - ERR_STATE = 1; - } -} +void pzConCheck(); #endif //PP_FUNCTION_H \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp index 1906e14..c7ab7b1 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.cpp @@ -17,6 +17,8 @@ int VLast = 0; long followerLong = followerThrs * 1023L; long compLong = compThrs * 1023L; +long followerInt = 0; +long compInt = 0; // Voltage Comparator Adjustment parameters int VComp = 0; @@ -30,7 +32,10 @@ int BlinkState = 0; int BlinkCount = (InitCount * 2) + 1; // Multiply Blink count by 2 to handle toggle state, add one extra to make sure light is on after // Serial Input Parsing Variables +char inputBuffer[buffSize]; uint8_t bytesRecvd = 0; bool serialIncoming = false; char serialMessageIn[buffSize] = {0}; -long serialLong = 0; \ No newline at end of file +long serialLong = 0; + +LightChrono mainLoop; \ No newline at end of file diff --git a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h index 7dd9c27..8d2d9ad 100644 --- a/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h +++ b/firmware/AVR-Source/Pyr0_Piezo_Sensor_v2.x.x/src/pP_volatile.h @@ -19,8 +19,8 @@ extern int VLast; extern long followerLong; extern long compLong; -long followerInt; -long compInt; +extern long followerInt; +extern long compInt; // Voltage Comparator Adjustment parameters extern int VComp; @@ -35,7 +35,7 @@ extern int BlinkCount; // Multiply Blink count by 2 to handle toggle state, ad // Serial Input Parsing Variables #define buffSize 40 -char inputBuffer[buffSize]; +extern char inputBuffer[buffSize]; #define endMarker '\n' extern uint8_t bytesRecvd; extern bool serialIncoming; @@ -46,6 +46,6 @@ extern long serialLong; //#define HIGH 1 // Task scheduler instances -LightChrono mainLoop; +extern LightChrono mainLoop; #endif //PP_VOLATILE_H \ No newline at end of file