Added user-friendty VCC readings adjustment #featureadd

Minor fixes to i2c #bugfix
This commit is contained in:
Vsevolod Merenkov 2020-05-07 21:55:27 +03:00
parent 2f4e42d34a
commit 65dbe60c87
9 changed files with 55 additions and 34 deletions

View file

@ -1,6 +1,7 @@
#include "EEPROM.h" #include "EEPROM.h"
#include "pP_config.h" #include "pP_config.h"
#include "pP_function.h" #include "pP_function.h"
#include "pP_volatile.h"
/*------------------------------------------------*/ /*------------------------------------------------*/
@ -88,6 +89,13 @@ void updateConstant(long value) {
} }
} }
void adjustConstant(int value) {
if (value > 0 && Vin > 0) {
voltMeterConstant = (long)(1.1 * value / Vin * 1023 * 1000);
EEPROM.put(VM_CONST_ADDRESS, voltMeterConstant);
}
}
/*------------------------------------------------*/ /*------------------------------------------------*/
void updateDebug(int value) { void updateDebug(int value) {

View file

@ -11,6 +11,7 @@ void updateLogic(int value);
void updatePzDet(int value); void updatePzDet(int value);
void updateVccSwitch(int value); void updateVccSwitch(int value);
void updateConstant(long value); void updateConstant(long value);
void adjustConstant(int value);
void updateDebug(int value); void updateDebug(int value);
#endif // PP_CMD_H #endif // PP_CMD_H

View file

@ -5,7 +5,7 @@
#include <Wire1.h> #include <Wire1.h>
uint8_t command; uint8_t command;
uint16_t value; uint32_t value;
void i2cWrite(uint8_t *buffer, int offset, int data) { void i2cWrite(uint8_t *buffer, int offset, int data) {
buffer[offset] = (uint8_t)(data >> 8); buffer[offset] = (uint8_t)(data >> 8);
@ -33,8 +33,9 @@ void i2cReportConfig() {
i2cWrite(buffer, 10, Hyst); i2cWrite(buffer, 10, Hyst);
i2cWrite(buffer, 12, LOGIC); i2cWrite(buffer, 12, LOGIC);
i2cWrite(buffer, 14, PZDET); i2cWrite(buffer, 14, PZDET);
i2cWrite(buffer, 16, voltMeterConstant); i2cWrite(buffer, 16, VCCSW);
memcpy(buffer + 20, PP_VERSION, length - 20); i2cWrite(buffer, 18, voltMeterConstant);
memcpy(buffer + 22, PP_VERSION, length - 22);
Wire1.write(buffer, length); Wire1.write(buffer, length);
} }
@ -50,8 +51,6 @@ void i2cReportState() {
} }
void i2cReply() { void i2cReply() {
Serial.print("Requested ");
Serial.println(command);
switch (command) { switch (command) {
case CMD_CONFIG: case CMD_CONFIG:
case CMD_ERASE: case CMD_ERASE:
@ -72,18 +71,11 @@ void i2cInput(int bytesReceived) {
command = Wire1.read(); command = Wire1.read();
} else if (a == 1) { } else if (a == 1) {
value = Wire1.read(); value = Wire1.read();
} else if (a == 2) {
value = value << 8 | Wire1.read();
} else { } else {
Wire1.read(); // value = value << 8 | Wire1.read();
} }
} }
Serial.print("Command ");
Serial.print(command);
Serial.print(" ");
Serial.println(value);
// Parse commands and apply changes or actions // Parse commands and apply changes or actions
switch (command) { switch (command) {
case CMD_GAIN_F: case CMD_GAIN_F:
@ -122,6 +114,10 @@ void i2cInput(int bytesReceived) {
break; break;
case CMD_VCCSW: case CMD_VCCSW:
updateVccSwitch(value); updateVccSwitch(value);
break;
case CMD_VCCADJUST:
adjustConstant(value);
break;
default: default:
return; return;
} }

View file

@ -4,21 +4,6 @@
#include "pP_config.h" #include "pP_config.h"
#include "stdint.h" #include "stdint.h"
#define status_Offset 0x00 // Status register
#define senseInt_Offset 0x01 // Integer of sense threshold in millivolts
#define compInt_Offset 0x02 // Integer of comparator threshold in millivolts
#define gainFactor_Offset 0x03 // Gain adjustment factor. 0=3x, 1=3.5x, 2=4.33x, 3=6x, 4=11x
#define hysteresis_Offset 0x04 // Hysteresis value for ADC measurements
#define loopDuration_Offset 0x05 // duration of time between ADC checks and other loop functions
#define logicLevel_Offset 0x06
#define piezoDetect_Offset 0x07
#define triggerDuration_Offset 0x08 // duration of the Z-axis pulse sent, in ms
#define debugEnable_Offset 0x09
#define voltMeterLong_Offset 0x0a // For fine-tuning the input volt master
#define versionRegister_Offset 0x0b
#define configRegister_Offset 0x0c
#define identRegister_Offset 0x0d
void i2cInit(); void i2cInit();
#endif // _pP_i2c_h_ #endif // _pP_i2c_h_

View file

@ -11,3 +11,4 @@
#define CMD_ERASE 0x0a #define CMD_ERASE 0x0a
#define CMD_STATE 0x0b #define CMD_STATE 0x0b
#define CMD_VCCSW 0x0c #define CMD_VCCSW 0x0c
#define CMD_VCCADJUST 0x0d

View file

@ -150,6 +150,8 @@ void updateParams() {
updateVccSwitch(serialLong); updateVccSwitch(serialLong);
} else if (strcmp(serialMessageIn, "CONST") == 0) { } else if (strcmp(serialMessageIn, "CONST") == 0) {
updateConstant(serialLong); updateConstant(serialLong);
} else if (strcmp(serialMessageIn, "VCCADJUST") == 0) {
adjustConstant(serialLong);
} else if (strcmp(serialMessageIn, "DEBUG") == 0) { } else if (strcmp(serialMessageIn, "DEBUG") == 0) {
updateDebug(serialLong); updateDebug(serialLong);
} else if (strcmp(serialMessageIn, "CONFIG") == 0) { } else if (strcmp(serialMessageIn, "CONFIG") == 0) {
@ -173,6 +175,7 @@ void updateParams() {
Serial.println("To change the main voltage of the circuit: VCCSW [0|1]"); Serial.println("To change the main voltage of the circuit: VCCSW [0|1]");
Serial.println(" (0 for 3.3v, 1 for 5v)"); Serial.println(" (0 for 3.3v, 1 for 5v)");
Serial.println("To change ADC hysteresis value: HYST [integer in millivolts]"); Serial.println("To change ADC hysteresis value: HYST [integer in millivolts]");
Serial.println("To adjust VCC voltage readings: VCCADJUST [integer in millivolts, use value from multimeter]");
Serial.println("To enable or disable debug output: DEBUG [0|1]"); Serial.println("To enable or disable debug output: DEBUG [0|1]");
Serial.println("To print current config: CONFIG"); Serial.println("To print current config: CONFIG");
Serial.println("To set config to defaults: ERASE"); Serial.println("To set config to defaults: ERASE");

View file

@ -3,11 +3,17 @@
#include "Wire.h" #include "Wire.h"
uint16_t read16() { uint16_t read16() {
return Wire.read() << 8 | Wire.read(); uint8_t value = Wire.read();
uint8_t value1 = Wire.read();
return ((uint16_t)value) << 8 | value1;
} }
uint32_t read32() { uint32_t read32() {
return (uint32_t)Wire.read() << 24 | (uint32_t)Wire.read() << 16 | Wire.read() << 8 | Wire.read(); uint8_t value = Wire.read();
uint8_t value1 = Wire.read();
uint8_t value2 = Wire.read();
uint8_t value3 = Wire.read();
return ((uint32_t)value) << 24 | ((uint32_t)value1) << 16 | ((uint16_t)value2) << 8 | value3;
} }
void write(uint8_t cmd) { void write(uint8_t cmd) {
@ -40,7 +46,6 @@ config_t requestConfig() {
Wire.endTransmission(); Wire.endTransmission();
uint8_t bytes = Wire.requestFrom(ADDRESS, 255); uint8_t bytes = Wire.requestFrom(ADDRESS, 255);
Serial.println(bytes);
config_t config; config_t config;
config.GAIN_FACTOR = read16(); config.GAIN_FACTOR = read16();
@ -51,6 +56,7 @@ config_t requestConfig() {
config.Hyst = read16(); config.Hyst = read16();
config.LOGIC = read16(); config.LOGIC = read16();
config.PZDET = read16(); config.PZDET = read16();
config.VCCSW = read16();
config.voltMeterConstant = read32(); config.voltMeterConstant = read32();
config.version = Wire.readString(); config.version = Wire.readString();
@ -70,4 +76,6 @@ state_t requestState() {
state.VFol = read16(); state.VFol = read16();
state.ERR_STATE = read16(); state.ERR_STATE = read16();
state.PZ_STATE = read16(); state.PZ_STATE = read16();
return state;
} }

View file

@ -16,6 +16,7 @@
#define CMD_ERASE 0x0a #define CMD_ERASE 0x0a
#define CMD_STATE 0x0b #define CMD_STATE 0x0b
#define CMD_VCCSW 0x0c #define CMD_VCCSW 0x0c
#define CMD_VCCADJUST 0x0d
#include "WString.h" #include "WString.h"
@ -28,6 +29,7 @@ typedef struct {
uint16_t Hyst; uint16_t Hyst;
uint16_t LOGIC; uint16_t LOGIC;
uint16_t PZDET; uint16_t PZDET;
uint16_t VCCSW;
uint32_t voltMeterConstant; uint32_t voltMeterConstant;
String version; String version;
} config_t; } config_t;

View file

@ -90,6 +90,20 @@ void serialPrintConfig() {
Serial.print("PZDET "); Serial.print("PZDET ");
Serial.println(config.PZDET); Serial.println(config.PZDET);
Serial.print("VCCSW ");
Serial.print(config.VCCSW);
switch (config.VCCSW) {
case 0:
Serial.println(" 3.3v");
break;
case 1:
Serial.println(" 5v");
break;
default:
Serial.println(" INVALID");
break;
}
Serial.print("VM_CONST "); Serial.print("VM_CONST ");
Serial.println(config.voltMeterConstant); Serial.println(config.voltMeterConstant);
@ -107,11 +121,11 @@ void serialPrintState() {
Serial.print(","); Serial.print(",");
Serial.print("\"VComp\":"); Serial.print("\"VComp\":");
Serial.print((uint32_t)state.VComp * state.Vin / 1023); Serial.print(state.VComp);
Serial.print(","); Serial.print(",");
Serial.print("\"VFol\":"); Serial.print("\"VFol\":");
Serial.print((uint32_t)state.VFol * state.Vin / 1023); Serial.print(state.VFol);
Serial.print(","); Serial.print(",");
Serial.print("\"Err\":"); Serial.print("\"Err\":");
@ -147,6 +161,8 @@ void updateParams() {
write(CMD_VCCSW, (uint16_t)serialLong); write(CMD_VCCSW, (uint16_t)serialLong);
} else if (strcmp(serialMessageIn, "CONST") == 0) { } else if (strcmp(serialMessageIn, "CONST") == 0) {
write(CMD_CONST, serialLong); write(CMD_CONST, serialLong);
} else if (strcmp(serialMessageIn, "VCCADJUST") == 0) {
write(CMD_VCCADJUST, (uint16_t)serialLong);
} else if (strcmp(serialMessageIn, "CONFIG") == 0) { } else if (strcmp(serialMessageIn, "CONFIG") == 0) {
serialPrintConfig(); serialPrintConfig();
} else if (strcmp(serialMessageIn, "ERASE") == 0) { } else if (strcmp(serialMessageIn, "ERASE") == 0) {
@ -168,6 +184,7 @@ void updateParams() {
Serial.println("To change the main voltage of the circuit: VCCSW [0|1]"); Serial.println("To change the main voltage of the circuit: VCCSW [0|1]");
Serial.println(" (0 for 3.3v, 1 for 5v)"); Serial.println(" (0 for 3.3v, 1 for 5v)");
Serial.println("To change ADC hysteresis value: HYST [integer in millivolts]"); Serial.println("To change ADC hysteresis value: HYST [integer in millivolts]");
Serial.println("To adjust VCC voltage readings: VCCADJUST [integer in millivolts, use value from multimeter]");
Serial.println("To enable or disable debug output: DEBUG [0|1]"); Serial.println("To enable or disable debug output: DEBUG [0|1]");
Serial.println("To print current config: CONFIG"); Serial.println("To print current config: CONFIG");
Serial.println("To set config to defaults: ERASE"); Serial.println("To set config to defaults: ERASE");