# Low Cost, Low Speed A/D conversion for Embedded Systems

Walter Banks, President

Byte Craft Limited

I have often used the converter circuit shown here in embedded applications, to fill the need for a low-cost variable-resolution A/D converter. The circuit is simple to implement; it requires only two resistors and a capacitor.

Unlike most embedded A/D converters, this circuit has variable conversion resolution as a trade off with conversion time. With design care, the A/D converter will provide results pre-scaled in desired engineering units. The A/D converter assumes that the input impedance of an embedded microprocessor port is relatively high, and the switch point remains constant with little hysterics.

Assume that the junction between Ri and Rf is a current summing junction, with the Capacitor C1 integrating the error current. The microprocessor has the ability to modulate the current through Rf by sending a pulse stream out of the Pf port bit. The ratio of the total number of ones to total bits emitted is a function of the average voltage on Pf. This mode is actually using the microcomputer as a high gain operational amplifier, that attempts to keep the voltage at the summing junction at the threshold of the low-to-high sense voltage of Pi. The voltage Vi through the input resistance Ri is directly balanced by the average current from the port pin Pf through Rf.

To get an idea of the input range of the A/D converter, run the following code on the embedded micro and scope pin Pf. Vary the voltage on the input pin Vi and see the results. The scope will show a pulse stream whose duty cycle will vary with input voltage on Vi. The ratio of zeros on the scope trace to the total time is a direct function of input voltage. It is this ratio that we ultimately want to measure in software.

```void main (void)
{
// Set up Pf for output
// Set up Pi for input
while(1)
{ Pf = ~Pi }
}
```

The range of the input voltage that can be measured is dependent on the sense voltage (Vs) of the input port, the output voltage of Pf in high and low states (Vh and Vl) and the value of the resistors Ri and Rf. The following equations will determine the minimum and maximum input voltage that can be read by the A/D converter.

```Vmin = Vs - ((Vh - Vs) * (Ri / Rf))
Vmax = Vs - ((Vl - Vs) * (Ri / Rf))
```

The value of Vmin occurs when Pi is always just at the sense threshold, and the processor is always feeding back a 1 to the Pf pin. At an input of Vmax a 0 is always being fed back from Pf. The A/D value is linear, scaled between Vmin and Vmax, and is determined from the ratio of 1's read on Pi (N1) to the total tests in a sample. The accuracy of the systems is a linear function of test sample size (N). Vi can be calculated from the following relationship:

```Vi = (N1 / N) * (Vmax - Vmin)
```

The value of C1 is not critical and is used to control the slew rate and noise immunity of the system. For a typical system measuring an input from 0 to 5 volts start with 47K resistors and a .01 to .1 micro-farad capacitor.

Finally, ratiometric measuring systems like this one have the following properties:

• Conversion accuracy is a function of conversion time.

• Results can be easily scaled by changing the sample size, to eliminate conversion multiplies and divides.