View Course Path

Interfacing 8051 with ADC 0808 – Stepwise tutorial

The physical parameters that any microcontroller processes come from sensors. Sensors are transducers that convert a physical parameter like temperature into electrical signals that the microcontroller can understand. But here is the issue—analog sensors output data in an analog format which a microcontroller cannot understand. Therefore, to convert this analog data to a digital format, Analog to Digital converters or ADCs are used.

What is an ADC?

An analog to digital converter or ADC, as the name suggests, converts an analog signal to a digital signal. An analog signal has a continuously changing amplitude with respect to time. A digital signal, on the contrary, is a stream of 0s and 1s. An ADC maps analog signals to their binary equivalents. To do this, ADCs use various methods like Flash conversion, slope integration, or successive approximation.

To understand the ADC in a better way, let us look at an example. Let us say we have an input signal which varies from 0 to 8 volt, and we use a 3-bit ADC to convert this signal to binary data. A 3-bit ADC can represent 2^3 or 8 different voltage levels using 3 bits of data. How convenient! In this case, the ADC maps the data in the following manner.

 

Input voltage Binary equivalent
0-1 volt 000B
1-2 volt 001B
2-3volt 010B
3-4 volt 011B
4-5 volt 100B
5-6 volt 101B
6-7 volt 110B
7-8 volt 111B

 

If you look at the table above, you will understand how the ADC maps analog data to digital values. In the case mentioned above, we can see that the tiniest change we can detect is that of 1 volt. If the change is smaller than 1 volt, the ADC can’t detect it. This minimum change that an ADC can detect is known as the step size of the ADC. To calculate it, we can use the formula:

Step size=Vmax-Vmin/2n (where n is the number of bits(resolution) of an ADC)

The step size of an ADC is inversely proportional to the number of bits of an ADC. So using an ADC with higher bits can detect smaller changes, but this increases the cost of production. Due to this reason, most on-chip ADCs’ have an 8-bit/10-bit resolution. Given below is the resolution vs. step size for various configurations with a range of 0-5v input signal.

 

Number of bits Number of steps step size(mV)
8 256 5/256=19.53
10 1024 5/1024=4.88
12 4096 5/4096=1.2
16 65536 0.076 (precise conversion)

Where is an Analog-to-Digital Converter (ADC) used?

ADCs’ are everywhere. You are using one when you talk on the phone or when you listen to music. An ADC is required whenever a computer needs to understand physical parameters like sound, pressure, velocity using a transducer.

Are external ADCs still used?

With the advancement in technology, most modern microcontrollers have built-in ADCs. You might wonder then as to what is the point of learning how to interface an external ADC.

Well, for starters, it will help you understand how an ADC works with a microcontroller. Additionally, some IoT applications require an external ADC as they provide better accuracy and higher speeds compared to internal ADCs.

Now that we have a basic idea of what an ADC is and where they are used, we will dive into interfacing the 0808 ADC with 8051.

ADC 0808 

The ADC 0808 is a popular 8-bit ADC with a step size of 19.53 millivolts. It does not have an internal clock. Therefore, it requires a clock signal from an external source. It has eight input pins, but only one of them can be selected at a time because it has eight digital output pins. It uses the principle of successive approximation for calculating digital values, which is very accurate for performing 8-bit analog to digital conversions. Let us look at the pin description to get more insights into ADC 0808.

Input pins (INT0-INT7)

The ADC 0808 has eight input analog pins. These pins are multiplexed together, and only one of them can be selected using three select lines.

Select lines and ALE

It has three select lines, namely A, B, and C, that are used to select the desired input lines. The ALE pin also needs to be activated by a low to high pulse to select a particular input. The input lines are selected as follows:

 

A B C Selected analog channel ALE pin
0 0 0 INT0 Low to High pulse
0 0 1 INT1 Low to High pulse
0 1 0 INT2 Low to High pulse
0 1 1 INT3 Low to High pulse
1 0 0 INT4 Low to High pulse
1 0 1 INT5 Low to High pulse
1 1 0 INT6 Low to High pulse
1 1 1 INT7 Low to High pulse

Output pins (D0-D7)

The ADC has eight output pins that give the binary equivalent of a given analog value.

VCC and Ground

These two pins are used to provide the required voltage to power the microcontroller. In most cases, the ADC uses 5V DC to power up.

Clock

As mentioned earlier, the 0808 does not have an internal clock and needs an external clock signal to operate. It uses a clock frequency of 20Mhz, and using this clock frequency it can perform one conversion in 100 microseconds.

VREF (+) and VREF (-)

These two pins are used to provide the upper and the lower limit of voltages which determine the step size for the conversion. Here Vref(+) has a higher voltage, and Vref(-) has the lower voltage. If Vref(+) has an input voltage 5v and Vref(-) has a voltage of 0v then the step size will be 5v-0v/28= 15.53 mv.

Start conversion

This pin is used to tell the ADC to start the conversion. When the ADC receives a low to high pulse on this pin, it starts converting the analog voltage on the selected pin to its 8-bit digital equivalent.

End of conversion

Once the conversion is complete, the ADC sends low to high signal to tell a microcontroller that the conversion is complete and that it can extract the data from the 8 data pins.

Output enable

This pin is used to extract the data from the ADC. A microcontroller sends a low to high pulse to the ADC to extract the data from its data buffers

Components required

 

Interfacing 8051 with 0808

Most modern microcontrollers with 8051 IP cores have an inbuilt ADC. Older versions of 8051 like the MCS-51 and A789C51 do not have an on-chip ADC. Therefore to connect these microcontrollers to analog sensors like temperature sensors, the microcontroller needs to be hooked to an ADC. It converts the analog values to digital values, which the microcontroller can process and understand. Here is how we can interface the 8051 with 0808.

Interfacing_8051_with_ADC0808 - circuit diagram schematic

To interface the ADC to 8051, follow these steps. In our case, we are using Proteus as the simulation software and the AT89C51 microcontroller.

  • Connect the oscillator circuit to pins 19 and 20. This includes a crystal oscillator and two capacitors of 22uF each. Connect them to the pins, as shown in the diagram.
  • Connect one end of the capacitor to the EA’ pin and the other to the resister. Connect this resistor to the RST pin, as shown in the diagram.
  • We are using port 1 as the input port, so we have connected the output ports of the ADC to port 1.
  • As mentioned earlier, the 0808 does not have an internal clock; therefore, we have to connect an external clock. Connect the external clock to pin 10.
  • Connect Vref (+) to a voltage source according to the step size you need.
  • Ground Vref (-) and connect the analog sensor to any one of the analog input pins on the ADC. We have connected a variable resistor to INT2 for getting a variable voltage at the pin.
  • Connect ADD A, ADD B, ADD C, and ALE pins to the microcontroller for selecting the input analog port. We have connected ADD A- P2.0; ADD B- P2.1; ADD C- P2.2 and the ALE pin to port 2.4.
  • Connect the control pins Start, OE, and Start to the microcontroller. These pins are connected as follows in our case Start-Port-2.6; OE-Port-2.5 and EOC-Port-2.7.

With this, you have successfully interfaced the 8051 to the ADC. Now let us look at the logic to use the ADC with the microcontroller.

Logic to communicate between 8051 and ADC 0808

Several control signals need to be sent to the ADC to extract the required data from it.

  • Step 1: Set the port you connected to the output lines of the ADC as an input port. You can learn more about the Ports in 8051 here.
  • Step 2: Make the Port connected to EOC pin high. The reason for doing this is that the ADC sends a high to low signal when the conversion of data is complete. So this line needs to be high so that the microcontroller can detect the change.
  • Step 3: Clear the data lines which are connected to pins ALE, START, and OE as all these pins require a Low to High pulse to get activated.
  • Step 4: Select the data lines according to the input port you want to select. To do this, select the data lines and send a High to Low pulse at the ALE pin to select the address.
  • Step 5: Now that we have selected the analog input pin, we can tell the ADC to start the conversion by sending a pulse to the START pin.
  • Step 6: Wait for the High to low signal by polling the EOC pin.
  • Step 7: Wait for the signal to get high again.
  • Step 8: Extract the converted data by sending a High to low signal to the OE pin.

Assembly language program to interface ADC 0808 with 8051

Here is how the assembly code for the same looks like

ORG 0000H; Starting address
MOV P1,#0FFH; Makes port 1 input port
SETB P2.7; Makes EOC pin high
CLR P2.4; Clears ALE pin
CLR P2.6; Clears Start pin
CLR P2.5; Clears OE pin
BACK: CLR P2.2; Clears ADD C
SETB P2.1; Sets ADD B
CLR P2.0; Clears ADD A (this selects the second address line)
ACALL DELAY
SETB P2.4; Sets ALE high
ACALL DELAY
SETB P2.6; sends a command to start conversion
ACALL DELAY
CLR P2.4; makes ALE low
CLR P2.6; makes Start pin low
HERE: JB P2.7,HERE; waits for low pulse at EOC
HERE1: JNB P2.7,HERE1; waits for low pulse to finish
SETB P2.5; enables OE pin to extract data from ADC
ACALL DELAY
MOV A,P1; moves acquired data to accumulator
CLR P2.5; clears OE
SJMP BACK; repeatedly gets data from ADC
DELAY: MOV R3,#50
HERE2: MOV R4,#255
HERE3: DJNZ R4,HERE3
DJNZ R3,HERE2
RET
END

Now that we have a basic understanding of how to interface an ADC with the 8051, let us look at an example in which we connect LEDs to 8051 to see the data conversion.

C program to interface ADC 0808 with 8051

Seeing data conversion using LEDs

To see the data conversion of an ADC, we will extract the data using the code shown above. Then we will transfer the binary data to port 3 to see the data.

LEDs and ADC interfacing with 8051 - circuit diagram schematic

The C code for the same is given below:

#include <reg51.h>
sbit ALE = P2^4;
sbit OE = P2^5;
sbit SC = P2^6;
sbit EOC = P2^7;
sbit ADDR_A = P2^0;
sbit ADDR_B = P2^1;
sbit ADDR_C = P2^2;
sfr MYDATA =P1;
sfr SENDDATA =P3;
void MSDelay(unsighned int) // Function to generate time delay
{
unsighned int i,j;
for(i=0;i<delay;i++)
for(j=0;j<1275;j++);
}
void main()
{
unsigned char value;
MYDATA = 0xFF;
EOC = 1; 
ALE = 0;
OE = 0; 
SC = 0;
while(1)
{
ADDR_C = 0;
ADDR_B = 0;
ADDR_A = 0;
MSDelay(1);
ALE = 1;
MSDelay(1);
SC = 1;
MSDelay(1);
ALE = 0;
SC = 0;
while(EOC==1);
while(EOC==0);
OE=1;
MSDelay(1);
value = MYDATA;
SENDDATA = value;
OE = 0 ;
}
}

We hope that reading this article helped you understand how to interface an ADC with 8051. If you have any doubts, feel free to ask them in the comments section, and we will get back to you.

2 thoughts on “Interfacing 8051 with ADC 0808 – Stepwise tutorial

  1. I have a doubt. In the C prog, j takes the value of 1272, which is greater than 255(unsigned 8 bit). How is this possible

    1. unsigned ints have a range of 0 to 65,535. At the very least. You are thinking of an unsigned char. Hope that helps!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.