Source Code:
#include<reg51.h>
#define adc P2
sbit load = P1^6;
////0804_adc_pin_details/////
sbit soc = P1^0;//HIGH to LOW gets data from internal registe to pins in adc
sbit rd = P1^1;// LOW to HIGH pulse is given for start of conversion
sbit intr = P1^2;// pin turn LOW when conversion is done
void intserial();
void sendserial(unsigned char dat);
void delay(unsigned int count);
unsigned char get_adc_data();
unsigned char mydata;
/////////////////////////////////////////
void sendserial(unsigned char dat)
{
TI = 0;
SBUF = dat;
while(TI == 0);
}
//////////////////////////////////////////
void intserial()
{
TMOD = 0x21;// timer 1 and 2 @ auto reload mode
TL1 = 253; //9600 baud rate
TH1 = 253; //9600 baud rate
SCON = 0X50; // 8 bit serial communication
TR1 = 1;
}
////////////////////////////////////////////
unsigned char get_adc_data()
{
rd = 0;
soc = 1;
//delay(10);
rd = 1;
//waits untill adc conversion is over
soc = 0;
//mydata = adc;//copies adc data to mydata
return P2;
}
//////////////////////////////////////////////
void delay(unsigned int count)
{
int i,j;
for(i=0;i<count;i++)
for(j=0;j<1000;j++);
}
////////////////////////////////////////////////
void main()
{
intserial();
while(1) // repeats infinite
{
mydata=get_adc_data();
sendserial(mydata);
if(mydata>51)
load=0;
else
load=1;
}
}
///////////////////////////////////////////////