In the application of electronics and automation technology, microcontrollers and DAC (digital-to-analog converter) are often used at the same time. However, many microcontrollers do not have integrated DAC. Even if some microcontrollers have integrated DAC, the accuracy of DAC is often not high. In high-precision applications, an external DAC is still required, which increases the cost. However, almost all microcontrollers provide timer or PWM output functions. If the PWM output of the microcontroller can be applied (or the PWM output can be achieved through a timer and software), the DAC can be realized through a simple conversion circuit, which will greatly reduce the cost of electronic equipment, reduce the volume, and easily improve the accuracy. Based on the theoretical analysis of the PWM to DAC conversion relationship, this paper designs a DAC with an output voltage of 0 to 5V.1 Theoretical Analysis of DAC Using PWM PWM is a square wave signal with a fixed period and a modulated duty cycle of high and low levels. Figure 1 is a PWM wave often encountered in circuits. The high and low levels of the PWM are VH and VL respectively. Ideally, VL is equal to 0, but in practice it is generally not equal to 0, which is often a major cause of error in applications. The PWM waveform of Figure 1 can be expressed by a piecewise function as formula (1): Where: T is the basic cycle of the counting pulse in the single-chip microcomputer, that is, the single-chip microcomputer counts once every T time (the value of the counter increases or decreases by 1), N is the number of counting pulses in one cycle of the PWM wave, n is the number of high-level counting pulses in one cycle of the PWM wave, VH and VL are the high and low-level voltage values in the PWM wave, k is the harmonic order, and t is time. Expand the function represented by formula (1) into a Fourier series [1], and get formula (2): It can be seen from formula (2) that the first square bracket is the DC component, the second item is the 1st harmonic component, and the third item is the higher harmonic component greater than 1st. The DC component in formula (2) is linearly related to n, and as n changes from 0 to N, the DC component changes from VL to VL+VH, which is exactly what the voltage output DAC needs. Therefore, if the harmonics except the DC component in formula (2) can be filtered out, the conversion from PWM wave to voltage output DAC can be obtained, that is: the PWM wave can be demodulated by a low-pass filter. The amplitude and phase angle of the second item in formula (2) are related to n, and the frequency is 1/(NT), which is the basis for designing the low-pass filter. If the 1st harmonic can be filtered out well, the higher harmonics should basically not exist. Based on the above analysis, we can get the signal processing block diagram from PWM to DAC output as shown in Figure 2. According to this block diagram, there are many circuit implementation methods. In the application of single-chip microcomputer, precision adjustment and error further correction can be performed through software methods. In DAC applications, resolution is a very important parameter. The resolution calculation in Figure 1 is directly related to the possible changes of N and n. The calculation formula is as follows: Table 1 shows the resolution for different N and n. From Table 1 and formula (3), it can be seen that the larger N is, the higher the resolution of the DAC is, but the larger NT is, that is, the larger the PWM period or the first harmonic period in formula (2), which is equivalent to the lower frequency of the first harmonic. A low-pass filter with a very low cutoff frequency is required, and the lag of the DAC output will also increase. One solution is to reduce T, that is, to reduce the counting pulse width of the microcontroller (this often requires increasing the operating frequency of the microcontroller) to achieve the premise of not reducing the first harmonic frequency. In practice, the reduction of T is limited by the switching characteristics of the microcontroller clock and the subsequent PWM circuit. If a microsecond T is required in practice, the subsequent circuit needs to select a device with better switching characteristics to reduce the distortion of the PWM waveform, such as the electronic switch T1 (IRF530) in Figure 4. 2PWM to DAC voltage output circuit implementation According to the structure of Figure 2, Figure 3 is the simplest implementation method. In Figure 3, the PWM wave is directly output from the PWM pin of the MCU. The circuit has no reference voltage and only obtains the output voltage of the DAC through simple RC filtering. The specific parameters of R1 and C1 can be selected according to the first harmonic frequency of the second part of formula (2). In practical applications, the cutoff frequency of the RC filter in Figure 2 is generally selected to be about 1/4 of the fundamental frequency of formula (2). The VH and VL of the PWM wave in Figure 3 are limited by the high and low levels of the MCU output. Generally, VL is not equal to 0 V, and VH is not equal to VCC. For example, for the single-chip microcomputer AT89C52 [2,3], when VCC is +5 V, VH and VL are about 4.5 V and 0.45 V respectively, and this value changes with the load current and temperature. According to the DC component of formula (2), it can be seen that the DAC voltage output can only change between 0.45 and 4.5 V, and it is difficult to guarantee the accuracy as it changes with the load current and ambient temperature. Since the accuracy of the changing part of this circuit is not high, it is not necessary to use high-resolution PWM output, and 8 bits are sufficient. In addition, the load capacity of the DAC output in Figure 2 is also relatively poor, and it is only suitable for connection with subsequent circuits with high input impedance. Therefore, the circuit in Figure 3 can only be used in situations where the DAC output accuracy is not high and the load is very small. In situations where the accuracy and load capacity are required to be high, the circuit in Figure 3 needs to be improved by adding circuits such as reference voltage and load drive. The circuit of Figure 4 adds a switch tube T1, a reference voltage source LM3365, and an output amplifier TLV2472 to the circuit of Figure 3. The PWM wave output by the MCU from point A drives the gate of T1, and T1 switches according to the period and duty cycle of PWM. T1 is a switch tube with low on-resistance and good switching characteristics, such as IRF530[4], whose typical on-resistance is less than 0.16 Ω, while its off-resistance is very large. The reference voltage LM3365 is connected in parallel with T1. Point B of Figure 4 will obtain an ideal PWM waveform, that is: VH=5 V, VL=0 V, and the waveform is a square wave. The PWM wave at point A is shaped to obtain the ideal PWM wave at point B. The PWM wave at point B is then filtered by two stages of resistors and capacitors to obtain a DC component at point C, that is, the modulated PWM wave output by the MCU is demodulated at point C, realizing the DAC function. According to formula (2), the voltage at point C is (5 ×n/N)V, which is a voltage between 0 and 5 V. Since the input impedance of amplifier A1 is very large, the effect of secondary RC filtering is very good, and the voltage ripple at point C is extremely small, meeting the high-precision requirements. The output amplifier uses TLV2472, which works in voltage follower mode. It is a Rail to Rail amplifier. Its output voltage span is almost equal to the power supply voltage amplitude, so it can get a 0 V voltage output, which overcomes the disadvantage that the output voltage span of general amplifiers (such as LM324, TL071, etc.) is about 1 V smaller than the power supply voltage range. Another important difference between Figure 4 and Figure 3 is that the power supply voltage of Figure 4 is 6 V, while that of Figure 3 is 5 V. In Figure 4, a diode is connected in series when the MCU is connected to the power supply voltage, which plays a role in reducing the voltage, because the general MCU working power supply range is between 4.5 and 5.5 V [*-)][*-)]. The power supply voltage of 6 V in Figure 4 is used to ensure that LM336 5 can work normally. The circuit and capacitors used in the circuit of Figure 4 have no special requirements and are easy to debug. Since the PWM wave can be easily controlled by the MCU software, even if the circuit has a slight system error, it can be easily corrected by software. Therefore, the circuit of Figure 4 can obtain a high-precision DAC output. 3 Conclusion Based on the theoretical analysis of PWM waveform composition, this paper proposes that the DAC modulation signal in PWM can be demodulated through a low-pass filter to realize DAC. The paper analyzes the causes of errors in the implementation of DAC and designs two sets of DAC circuit implementation methods, which are suitable for different application occasions. The implementation method of Figure 4 can obtain a high-precision DAC through simple and cheap electronic components, reducing the cost of the equipment. The circuit is powered by a single power supply and is very suitable for use in embedded systems based on single-chip microcomputers. |