Changing the PWM signal to dc
You can generate a variable dc reference voltage with a DAC or with a PWM (pulsewidthmodulation) signal from your controller (Figure 1). The accuracy of the voltage source that the PWM/lowpassanalogfilter combination generates is as accurate as your onboard timer, filter operational amplifier, and powersupply voltage. If your controller has 64 divisions available for the PWM function, this function's accuracy can be as low as 78 mV in a 5V system.
With a microcontroller PWM generator, the clock sets the fundamental frequency. You then adjust the duty cycle by changing the ratio of T_{ON} to T_{OFF}. T_{ON} is the high time of the PWM signal, and T_{OFF} is the low time. T_{ON}+T_{OFF}=T, where T is the time of one cycle (or period) from the PWM.

The number of divisions (K) that your clock can produce during the PWM period determines one part of the accuracy and granularity of your PWM reference. The highest granularity that you will get out of your adjustable voltage reference is 1/K of your fullscale range. Based on T, the number of time divisions in the period, the ideal number of bits, or the resolution, of this DAC is: DAC resolution=log (K)/log (2) in bits.
Having an analog filter after the PWM pulse produces a dc voltage, V_{REF}. This voltage's value depends on the ratio of T_{ON} to T_{OFF} and the powersupply voltage. If the PWM signal is on more than it is off, the output voltage after the filter in an inverting configuration will be below midscale, where midscale equals V_{DD}/2.
If you properly filter the PWM signal on the controller's output port, the errors in this system are the quantization error from the controller clock, the I/O gate's outputswing range, the lowpass filter's ripple rejection, and any offset errors and outputswing limitations of the lowpassfilter amplifier circuit. In Figure 1, the FFT plot separates the PWM's squarewave response into its equivalent frequencies. Figure 1 also shows a lowpass filter's frequency response.
The calculation of the analog filter's singlepole corner frequency for this circuit is:
If you need your voltage reference to remain stable under transient conditions, you may want to increase the filter corner frequency or filter order. In this case, a higher order filter is a good alternative, because you already have an amplifier in the circuit. Designing active analog filters is easy if you use the free lowpassfilter software from various opamp manufacturers.
With the design equations in this column, a PWM, and an op amp, you can design a DAC that generates a dc reference voltage. This design's frequencylimiting factors are the clock speed of your controller's fundamental PWM signal and the analog lowpass filter's cutoff frequency. If you want to improve this system's frequency response, you can use a faster clock without compromising the PWM/DAC, or you can use a standalone DAC. Using a standalone DAC may be attractive if your application requires precision.
Complete the simulation of your ADC with IBIS
Simulating the frontend of your ADC
Painless reduction of analog filter noise
The inner workings of the threeopamp INA
Will the right voltage reference stand up?
Measuring amplifier DC offset voltage, PSRR, CMRR, and openloop gain
PCB signal coupling can be a problem
Phantom voltage dividers on your PCB
Accidental engineering: 10 mistakes turned into innovation
That 60Wequivalent LED: What you don’t know, and what no one will tell you…
6 famous people you may not know are engineers
DC distribution in your house and 42V cars
10 tips for a successful engineering resume
The 5 greatest engineers of all time
Higgs Pt. 9: What makes King Carl XVI Gustaf think it’s the Higgs Boson?
10 things you may not know about Tesla
Analog Fundamentals: Instrumentation for impedance measurement