From 2e88c0324dcc4bfc297f1cbd36a73cfc45884e89 Mon Sep 17 00:00:00 2001 From: HubertH <51236138+hubertushirsch@users.noreply.github.com> Date: Wed, 14 Feb 2024 22:58:19 +0100 Subject: [PATCH] Update aftb_vpp.h for new VPP measurement --- aftb_vpp.h | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/aftb_vpp.h b/aftb_vpp.h index 519f4f6..857944b 100644 --- a/aftb_vpp.h +++ b/aftb_vpp.h @@ -2,6 +2,7 @@ * Variable voltage functions for Afterburner GAL project. * * 2024-02-02 Minor changes in varVppInit() + * 2024-02-14 Redesign of VPP measurement (function varVppMeasureVpp()) */ #ifndef __AFTB_VPP_H__ #define __AFTB_VPP_H__ @@ -15,6 +16,7 @@ #define VPP A0 #include "aftb_mcp4131.h" +#include "aftb_adcparms.h" #ifndef FAIL #define FAIL 0 #define OK 1 @@ -41,19 +43,14 @@ #define MAX_WIPER 16 -#define VPP_VERBOSE 0 - -#ifdef EXTERNAL -#define ANALOG_REF_EXTERNAL EXTERNAL -#else -#define ANALOG_REF_EXTERNAL AR_EXTERNAL -#endif +#define VPP_VERBOSE 1 +/* No longer necessary due to new measurement algorithm //UNO R4 Minima or Wifi (Aref internally pulled down by 130kOhm, AVR Uno R3 pulled down by 32kOhm) #ifdef _RENESAS_RA_ #define AREF_IS_3V2 #endif - +*/ //pot wiper indices for the voltages uint8_t vppWiper[MAX_WIPER] = {0}; @@ -79,7 +76,7 @@ static void varVppReadCalib(void) { calOffset = (int8_t) EEPROM.read(2); for (i = 0; i < MAX_WIPER; i++) { vppWiper[i] = EEPROM.read(i + 3); -#if 0 +#if 1 Serial.print(F("Calib ")); Serial.print(i); Serial.print(F(":")); @@ -149,6 +146,31 @@ static void varVppSet(uint8_t value) { varVppSetVppIndex(vppWiper[value]); } +// New VPP measurement algorithm +#define MCOUNT (14) // Number of added measurements as integer +#define ADCRES (10) // Analog Read Resolution in bits as integer + +static int16_t varVppMeasureVpp(int8_t printValue) { + int adcsum = 0; // Sum MCOUNT measurements here + int loops = 0; // Counter for measure loop + float vpp; // Vpp result as float + // Precalculate constant parts of the formula + float divisor = ADC_R6 * float(1 << ADCRES); + float dividend = VREF * (ADC_R5 + ADC_R6); + + // Measure MCOUNT times and add results + do { + adcsum += analogRead(VPP); // Sum MCOUNT measurements here + } while (++loops < MCOUNT); + // Now calculate the VPP + vpp = (float(adcsum) * dividend) / divisor / float(MCOUNT); + vpp += float(calOffset) / 100.0; + if (printValue) { + Serial.println(vpp); + } + return int16_t(vpp * 100.0); +} +/* No longer necessary due to new measurement algorithm // UNO R4/Minima - Renesas IC (significant ADC gain errors measured) #ifdef AREF_IS_3V2 #define SAMPLE_CNT 16 @@ -201,7 +223,7 @@ static int16_t varVppMeasureVpp(int8_t printValue) { } return r1; } - +*/ // Returns 1 on Success, 0 on Failure static uint8_t varVppCalibrateVpp(void) { uint8_t vppIndex = 0; @@ -305,7 +327,7 @@ static void varVppStoreWiperCalib() { //return 1 on success (variable VPP functionality present), 0 on failure (VPP not detected on board) static int8_t varVppInit(void) { - analogReference(ANALOG_REF_EXTERNAL); //use 3V3 external reference + analogReference(AREF_SOURCE); //use analog reference depending on settings in aftp_adcparms.h analogRead(VPP); // Perform a dummy conversion referring to the datasheet wiperStat = 0; //wiper disabled