Interfacing ADC 0809 with 8051



In this post, I am going to show you how can you use ADC (Analog to Digital converter) with 8051 microcontrollers. the IC for ADC conversion I have used is ADC0809 you can also download its datasheet from Here

Image result for 0809

 Pin configuration on ADC0809 


we have to connect it to 8051, we should also connect an LCD display to check the value of ADC you can make the connection as per C code pin definition below.

Code for ADC Interface.
#include<reg51.h>

//////////// pin definition ///////////////
#define DATA P1
#define ADC P3
sbit RS=P2^0;
sbit E=P2^1;
sbit ALE=P0^2;
sbit SOC=P0^1;
sbit OE=P0^0;
sbit EOC=P0^3;
sbit SET0=P0^5;
sbit SET1=P0^6;
sbit SET2=P0^7;
sbit CLOCK=P0^4;
//////////////////////////////////////////
#include"delay.h"
#include"lcd.h"
#include"adc.h"

unsigned char adc_val;

void main(void)
{
  init_lcd();
string_lcd("ADC Test");
delay(30000);
  while(1)
  {
    adc_val = adc(7);
cmd_lcd(0xc0);
number_lcd(adc_val,3);
delay(1000);
   }
 }

void clock(void)
{
 int a,b;
 for(b=0;b<=1000;b++)
 {
  for(a=0;a<30;a++);
  CLOCK=~CLOCK;
 }
}

unsigned char adc(char s)
{
unsigned char dat;
if(s & (1<<0)) SET0=1;
if(s & (1<<1)) SET1=1;
if(s & (1<<2)) SET2=1;
ALE=1;    SOC=1;    clock();
  ALE=0;    SOC=0;    clock();
  while(EOC==0);
  OE = 1;
dat = ADC;
return(dat);
}
void cmd_lcd (unsigned char dat)    // function to write command at lcd port 
{
DATA=dat;
RS=0;   //clear RS (ie. RS=0) to write command
E=1;   // send  H-L pulse at E pin
delay(100);
E=0;
delay(100);
}

void data_lcd (unsigned char dat) // function to write data at lcd port 
{
DATA=dat;
RS=1; // set RS=1 to write data
E=1;   // send  H-L pulse at E pin
delay(100);
E=0;
delay(100);   
}

void init_lcd()         // function to initialize the LCD at power on time
{
cmd_lcd(0x38); // 2x16 display select
delay(50000);
cmd_lcd(0x0c); // display on cursor off command
delay(1000);
cmd_lcd(0x06); // automatic cursor movement to right
delay(1000);
cmd_lcd(0x01); // lcd clear command 
delay(5000);
}

void string_lcd(unsigned char *str)    // function to display string to lcd
{
while(*str!='\0')   // '\0' is null char as last char of pointer is null
{
data_lcd(*str);
str++;
}
 }

void number_lcd(unsigned int dat, unsigned int siz)      // function to display any  decimal value to lcd
{
unsigned int temp,temparr[10];

for(temp=siz;temp>0;temp--)
        {
                temparr[temp-1]=dat%10;
                dat=dat/10;
        }
for(temp=0;temp<siz;temp++)
        {
                data_lcd(temparr[temp]+0x30);
        }        
}

void delay(int num)
{
int a;
for(a=0;a<=num;a++);
}

Pin connection(microcontroller to LCD and ADC) should be made as per the Pin definition in code .
.