Displaying an accurate byte value on a DVM using just one PWM pin

AllyCat

Senior Member
Hi,

The attached program (too large to post inline) was inspired by this thread where a byte value is used to drive a budget 3-digit DVM to give a reasonable indication of the value of the byte (providing the supply rail is stable). In that thread I showed from post #9 onwards how CALIBADC10 can be used to compensate the PWM for variations of the supply rail voltage, but that code (and probably the economy DVM itself) is unlikely to achieve an exact indication of all byte values. However, with close attention to detail (and probably a "better" DVM / multimeter) it does seem practical to get very close to a precise digital display of the actual byte value. Of course a calibration stage is required, at least for the "Fixed Voltage Reference" inside the specific PICaxe, and probably for the display meter as well.

The program can be compiled in two versions, firstly an "Interactive" version which permits calibration (using the PE's terminal and keyboard interface) of the PICaxe's internal FVR, the Temperature-sensing diodes, and two-point calibration of the PWM / Meter combination. The alternative "Fixed" version (delete the INTERACT declaration) is a demonstration mode which alternates the meter display between the PICaxe's internal temperature and the Vdd supply rail (assuming the meter is connected directly to the PWM output pin). The temperature subroutine is a "preview" of more general code that I'm developing; this particular version shows degrees Fahrenheit, because the numerical values are much more compatible with a 0 to 255 display than is the Centigrade scale.

If the supply rail is as high as 5 volts, then the PWM duty cycle will be only just over 500 for an output of "255" (2.55 volts) which makes accurate mapping / calibration very difficult. A better supply range is about 2.7 to 3.7 volts, or alternatively a resistive divider can be connected on the PWM pin such that about 3 volts is presented to the meter at 100% duty cycle. To accommodate this latter case, multiply the PWM Calibration Constant by a factor = (Rp + Rs) / Rp, where Rs is the resistance between the PICaxe output and the meter and Rp is the resistance in parallel with the meter (combined with the meter resistance itself, if significant).

Even with an optimum supply voltage, the PWM resolution still spans much of the available "exact digit" tolerance, so all other factors need to be as accurate as possible, say better than 1/8 of one LS Bit calibration range and 12 bits of mathematical accuracy. The BIAS and CAL_PWM calibration constants can be created to any desired resolution so 1/16 of the Least Significant Bit and 1/8000 of full-scale gradient have been chosen here. Similar resolutions can be achieved by using special subroutine versions for "CALIBADC" and "double-word" division, which I have previously posted in the code snippets section. Surprisingly, these large calibration values actually simplify the calculation slightly, compared with my post linked above; if the numbers being multiplied are large enough (e.g. > 16000) then the ** operator alone can give a result with sufficient accuracy. More details of the calculation are given in the text associated with these two subroutines from the main program.

The Vdd measurement routine is described in this code snippet and I hope to post full details of an improved READINTERNALTEMP routine in due course. The Vdd output in the "Alternating" demo simply connects the supply rail directly to the output pin (i.e. 100% duty cycle) so that the external meter can be used to calibrate the FVR (and thus report the correct voltage to the serial terminal). I won't go into more detail of the calibration and calculations here, most should be covered by the program comments and the threads linked above. I did consider storing the calibration constants in the PICaxe's EEPROM, but the overall program was already becoming rather complex.

The "Interactive" part of the program has a simple code structure, so users can change the default values or general operation as required. Currently, it allows all the calibration constants to be increased or decreased by a "Step" value (default 10) with Upper Case keyboard characters increasing a value and Lower-Case decreasing it. "Z" adjusts the bias (zero) PWM level (in units of Step/10 to be more consistent with the multiplier values), "X" sets the PWM multiplier (for higher output levels), "C" calibrates the temperature offset and "V" the Vdd (FVR) supply rail value. "B" and "S" permit direct entry of the PWM Byte and Step values (don't forget the required non-digit terminator character). "D" and "F" give instant entry of some preset PWM calibration levels and "W" and "E" preset Step values (1, 10, 100, 1000). "A" toggles between the Alternating and PWM Calibration modes and finally "Q" (Quit) facilitates downloading of a new program without recourse to a Hard Reset. All constants are reported to the terminal when any are changed (or before the first pass of the Fixed version), in a rather concatenated format (to be compatible with the PE5 terminal window).

View attachment PWMoutput1.bas

Cheers, Alan.
 
Last edited:
Top