few microcontrollers include a DAC. Although you can easily find a inexpensive DAC to control from your microcontroller, you can use, unused peripherals inste Ad of adding parts. Fortunately, you can convert a microcontroller ' s ADC channel along with a GPIO (general-purpose input/output) pin to a D AC. You can make a DAC by charging a capacitor to an analog level by driving it high. You can also discharge the capacitor by driving it's voltage by tristating it (figure 1 ). At first glance, this approach seems like a crude a-D DAC. The technique becomes more plausible, however if you use a PID (proportional-integral-derivative) algorithm and monitor The voltage with the microcontroller ' s ADC. you can use the PID algorithm to compare the output voltage with the desired value and CALCU Late the error. If the error value is zero and then the I/O control block tristates the GPIO pin. If the error signal is positive or negative and then the I/O control block turns the I/O pin to a high state to charge the CA Pacitor or to a low state to discharge it. Your microcontroller code should load the error value into a timer to generate a timed pulse. The Error-value sign determines the Charge/discharge cycle, and its magnitude determines the duration of the pulse. Once the cycle is complete, you can set the I/O pin to a tristate mode, which holds the value. The algorithm can run as a software loop. You can call it based on another timer interrupt. To minimize the response time, make sure that this algorithm runs at the desired output value slightly longer than 2.2RC. You need the extra time to completely charge or discharge the capacitor through resistor R1.
the DAC ' s resolution depends on several factors, the foremost of which are ADC resolution. The DAC ' s resolution never exceeds that of the ADC. Variable Selection and timer resolution also affect DAC resolution. To implement a 10-bit DACs, you need a 16-bit timer and 16-bit variables for the PID algorithm. You can use a lower-resolution timer, but you must more frequently call the algorithm. That action results in longer settling times and higher CPU usage.by adjusting the algorithm ' s PID variables, you can achieve surprisingly good output settling times with little change To the DAC ' s output after settling. The stability of the ADC ' s voltage reference limits temperature stability. Neither the temperature stability of R1 and C1 nor the leakage of C1 has a adverse effect on the DAC ' s stability. The selection of R1 and C1 depends on the application, and your should select them based on settling time. For relatively slow-acting DACs, you can increase the update rate by running the algorithm faster than the 2.2RC period an D using an 8-bit timer. To buffer the DAC output, with an op amp as a voltage follower or use a common emitter follower. You can use a noninverting amplifier to amplify the output and feed just a portion of it to the ADC through a voltage divi Der.
Create a DAC from a microcontroller ' s ADC