ole00 / afterburner

GAL chip programmer for Arduino
155 stars 43 forks source link

Redesigned algorithm for VPP measurement #54

Open hubertushirsch opened 7 months ago

hubertushirsch commented 7 months ago

I propose a simplified form of voltage measurement in the Afterburner. This affects the varVppMeasureVpp() function in aftb_vpp.h and some definitions. The mathematical basics:

ADC = return value of analogRead() Vin = voltage at the analog input res = resolution - 2^10 = 1024 (10 bit resolution) Vpp = programming voltage VPP Vref = reference voltage of the A/D converter at pin AREF

Based on the basic formula

(1) ADC = Vin * res / Vref

After converting formula (1) we get

(2) Vin = ADC * Vref / res

Vin results from the voltage divider R5/R6

Vpp ---+
       |
       -
      | | R5
      | |
       -
       |
       o---- Vin to analog input
       |
       -
      | | R6
      | |
       -
       |
      ___ GND

(3) Vpp / (R5 + R6) = Vin / R6

Switched to Vpp

(4) Vpp = Vin * (R5 + R6) / R6

Vin substituted by (2):

(5) Vpp = (ADC * Vref * (R5 + R6)) / (R6 * res)

If you calculate with floating point, you can measure the Vref of your specific afterburner with a good multimeter and specify it precisely, e.g.

#define VREF (3.28)      // Vref in Volts as float

This also applies to the resistors R5 and R6, which can be measured beforehand.

#define ADC_R5 (99.96)   // R5 in kOhm as float
#define ADC_R6 (20.01)   // R6 in kOhm as float

I put the definitions of the parameters in a separate header file aftb_adcparms.h, which is included in aftb_vpp.h. This makes it easy to adapt to your own hardware without having to spend a long time searching between the lines of code. The varVppMeasureVpp() function has been completely rewritten.

Measuring using this method provides a significantly better agreement between the displayed VPP values compared to a multimeter. The difference between the Arduino measurement and the multimeter was a maximum of 0.02V (Arduino NANO, Vref = internal 1.1V, R5 = 20k, R6 = 1.3k).

fredcwbr commented 7 months ago

I'm following this with great interest., ..

hubertushirsch commented 7 months ago

I'm following this with great interest., ..

I would be very grateful if you could test the suggested code change. See pull request https://github.com/ole00/afterburner/pull/55

fredcwbr commented 7 months ago

I'll look into it closely, at the time i can get my hands in a 4131..

While waiting, did try another approach with a a handmade DAC R2R , and it worked, so i opened a discussion to share my thoughts ...