Tutorial Microcontroller MCS-51 ATMEL ISP
 

 

Super MCS51 Trainer

Standart MCS51 Trainer

Standart AVR Trainer

Programmer USBASP

Standart ARM Trainer

 

 

5. Analog to Digital Converter

The easiest way to do analog to digital conversion is to use an IC such as the ADC0804 that does the work for you. The analog voltage is applied to pin 6 and the result is available at pins 11 through 18. We will connect pin 1 and 2 (Chip Select and Read) to ground so that the chip is always enabled. (If you wanted to use more than one ADC you could use this pin to control which chip is currently enabled).
Connect pin 7 (Vin - ) to ground. The ADC0804 includes an internal oscillator which requires an external capacitor and resistor to operate. Connect the 150 pF capacitor from pin 4 (CLOCK IN) to ground and the 10k ohm resistor from pin 4 to pin 19 (CLOCK R).

Figure 2.12 Typical connection of free running mode

For example: 
 The input range of ADC is 0 - 5 volt
 then we must setting Vref = 0.5 Vin maximum or = 2.5 volt
 because this ADC0804 is 8 bit then 
V resolution = 5/255 = 0.02 volt
this is mean that ADC will respon each 0,02V increasing
Vin
(volt)
D0
D1
D2
D3
D4
D5
D6
D7
Des
0
0
0
0
0
0
0
0
0
0
0.02
0
0
0
0
0
0
0
1
1
0.04
0
0
0
0
0
0
1
0
2
0.06
0
0
0
0
0
0
1
1
3
0.08
0
0
0
0
0
1
0
0
4
0.10
0
0
0
0
0
1
0
1
5
0.12
0
0
0
0
0
1
1
0
6
:
:
5
1
1
1
1
1
1
1
1
255

Stage 1)
to operate this ADC, we use a free running mode, by connecting WR to INT

Stage 2)
When the conversion process is complete, pin 5 (Interrupt) will go low and this signal is used to convert ADC again.

Stage 3)
Next we read the values into the 89s51 Port 0.

5.1. Interfacing ADC0804 output LED

#include <AT89X52.h>
unsigned char dataADC,data_;
void convertADC()
{
           P3_4=0;
           P2_4=1;
           P3_3=0;
           P3_3=1;
           P2_4=0;
 while (P3_2==1)
 {
 }
 dataADC=P1;
 P2_4=1;
}
main()
{
 while (1)
 {
  convertADC();
  P0=dataADC;
 }
}
         

5.2. Interfacing ADC0804 display 7 segmen

#include <AT89X52.h>
unsigned char dataADC,data_;
void wait_ms()
{
 unsigned int x;
 for (x=0;x<200;x++);
}
void convert7seg()
{
           if (data_==0){data_=0xc0;}
           if (data_==1){data_=0xf9;}
           if (data_==2){data_=0xa4;}
           if (data_==3){data_=0xb0;}
           if (data_==4){data_=0x99;}
           if (data_==5){data_=0x92;}
           if (data_==6){data_=0x82;}
           if (data_==7){data_=0xf8;}
           if (data_==8){data_=0x80;}
           if (data_==9){data_=0x90;}
} 
void display7segmen()
{ 
           unsigned char first,second,third; 
           first = dataADC % 10; // sat = sisa dari data dibagi 10
           second = dataADC / 10;
           second = second % 10; 
           third = dataADC / 100;
           third = third % 10; 
           //
           data_=third;
           convert7seg();
           P0=data_;
           P3_5=0;
           P3_6=0;
           P3_7=0;
           wait_ms(); 
           //
           data_=second;
           convert7seg();
           P0=data_;
           P3_5=1;
           P3_6=0;
           P3_7=0;
           wait_ms(); 
           //
           data_=first;
           convert7seg();
           P0=data_;
           P3_5=0;
           P3_6=1;
           P3_7=0;
           wait_ms(); 
           // 
}
void convertADC()
{
           P3_4=0;
           P2_4=1;
           P3_3=0;
           P3_3=1;
           P2_4=0;
 while (P3_2==1)
 {
 }
 dataADC=P1;
 P2_4=1;
}
main()
{
 while (1)
 {
  convertADC();
  display7segmen();
 }
}

5.3. Interfacing ADC0804 with floating point Display 7 segmen

#include <AT89X52.h>
unsigned char dataADC,data_;
unsigned int suhu;
void wait_ms()
{
           unsigned int x;
           for (x=0;x<200;x++);
}
void convert7seg()
{
           if (data_==0){data_=0xc0;}
           if (data_==1){data_=0xf9;}
           if (data_==2){data_=0xa4;}
           if (data_==3){data_=0xb0;}
           if (data_==4){data_=0x99;}
           if (data_==5){data_=0x92;}
           if (data_==6){data_=0x82;}
           if (data_==7){data_=0xf8;}
           if (data_==8){data_=0x80;}
           if (data_==9){data_=0x90;}
} 
void display7segmen()
{ 
           unsigned char first,second,third,fourth; 
           suhu = (float)(dataADC*19.6078)+1;
           //
           fourth= suhu/1000;
           //
           third= suhu%1000;
           third= third/100;
           //
           second= suhu%1000;
           second= second%100;
           second= second/10;
           //
           first= suhu%1000;
           first= first%100;
           first= first%10;
           //
           data_=fourth;
           convert7seg();
           P0=data_;
           P3_5=0;
           P3_6=0;
           P3_7=0;
           wait_ms(); 
           //
           data_=third;
           convert7seg();
           P0=data_;
           P3_5=1;
           P3_6=0;
           P3_7=0;
           wait_ms(); 
           //
           data_=second;
           convert7seg();
           P0=data_;
           P3_5=0;
           P3_6=1;
           P3_7=0;
           wait_ms(); 
           //
           data_=first;
           convert7seg();
           P0=data_;
           P3_5=1;
           P3_6=1;
           P3_7=0;
           wait_ms(); 
           // 
}
void convertADC()
{
           P3_4=0;
           P2_4=1;
           P3_3=0;
           P3_3=1;
           P2_4=0;
 while (P3_2==1)
 {
 }
 dataADC=P1;
 P2_4=1;
}
main()
{
 while (1)
 {
  convertADC();
  display7segmen();
 }
}