# Interfacing DAC with 8051 Microcontroller

In this section we will see how DAC (Digital to Analog Converter) using Intel 8051 Microcontroller. We will also see the sinewave generation using DAC.

The Digital to Analog converter (DAC) is a device, that is widely used for converting digital pulses to analog signals. There are two methods of converting digital signals to analog signals. These two methods are binary weighted method and R/2R ladder method. In this article we will use the MC1408 (DAC0808) Digital to Analog Converter. This chip uses R/2R ladder method. This method can achieve a much higher degree of precision. DACs are judged by its resolution. The resolution is a function of the number of binary inputs. The most common input counts are 8, 10, 12 etc. Number of data inputs decides the resolution of DAC. So if there are n digital input pin, there are 2n analog levels. So 8 input DAC has 256 discrete voltage levels.

## The MC1408 DAC (or DAC0808)

In this chip the digital inputs are converted to current. The output current is known as Iout by connecting a resistor to the output to convert into voltage. The total current provided by the Iout pin is basically a function of the binary numbers at the input pins D- D(D0 is the LSB and  D7 is the MSB) of DAC0808 and the reference current Iref. The following formula is showing the function of Iout

$$I_{Out}=I_{ref}\lgroup\frac{D7}{2}+\frac{D6}{4}+\frac{D5}{8}+\frac{D4}{16}+\frac{D3}{32}+\frac{D2}{64}+\frac{D1}{128}+\frac{D0}{256}\rgroup$$

The Iref is the input current. This must be provided into the pin 14. Generally 2.0mA is used as Iref

We connect the Iout pin to the resistor to convert the current to voltage. But in real life it may cause inaccuracy since the input resistance of the load will also affect the output voltage. So practically Iref current input is isolated by connecting it to an Op-Amp with Rf = 5KΩ as feedback resistor. The feedback resistor value can be changed as per requirement.

## Generating Sinewave using DAC and 8051 Microcontroller

For generating sinewave, at first we need a look-up table to represent the magnitude of the sine value of angles between 0° to 360°. The sine function varies from -1 to +1. In the table only integer values are applicable for DAC input. In this example we will consider 30° increments and calculate the values from degree to DAC input. We are assuming full-scale voltage of 10V for DAC output. We can follow this formula to get the voltage ranges.

Vout = 5V + (5 ×sinθ)

Let us see the lookup table according to the angle and other parameters for DAC.

Angle(in θ )sinθVout (Voltage Magnitude)Values sent to DAC
005128
300.57.5192
600.8669.33238
901.010255
1200.8669.33238
1500.57.5192
18005128
210-0.52.564
240-0.8660.66917
270-1.000
300-0.8660.66917
330-0.52.564
36005128

Circuit Diagram −

## Source Code

#include<reg51.h>
sfr DAC = 0x80; //Port P0 address
void main(){
int sin_value[12] = {128,192,238,255,238,192,128,64,17,0,17,64};
int i;
while(1){
//infinite loop for LED blinking
for(i = 0; i<12; i++){
DAC = sin_value[i];
}
}
}

## Output

The output will look like this −

Updated on: 14-Sep-2023

23K+ Views

##### Kickstart Your Career

Get certified by completing the course