kaka919
Newbie level 5

Adc0808 is giving output 00 for 0v and 255 for 5v which is correct, however for analog input between 0 and 5v, it's giving random values.
Clock to adc is given via timer of 8051. Using timer 0 interrupt, I;m generating a 100 Khz frequency which is given to clock pin of ADC 0808.
Can anyone please help me with this? Thank You
Clock to adc is given via timer of 8051. Using timer 0 interrupt, I;m generating a 100 Khz frequency which is given to clock pin of ADC 0808.
Can anyone please help me with this? Thank You
Code:
#define start_conv P3_0
#define end_conv P3_1
#define output_enable P3_2
#define clock_output P3_3
#define address_latch P3_4
#define ADDA P3_5
#define ADDB P3_6
#define ADDC P3_7
#define buzzer P0_3
#define data_bus P1
unsigned char val=0;
char buffer[33];
void mydelay(unsigned int temp)
{
while(temp--);
}
//ISR sequence for the timer 0
void timer0() interrupt 1
{
if(clock_output==1) //conditional statements for the toggle procedure
clock_output=0;//clock =0
else
clock_output=1;//clock = 1
buzzer=!buzzer;// toggle buzzer just to check
TF0=0;
TL0=0XFB;//count for 100 khz which is given as clock to adc
TH0=0XFF;//count
TR0=1;
}
//ADC conversion function
unsigned char ADC_conversion()
{
unsigned char converted_data=0; //variable to store the A-to-D converted data
ADDA=0;
ADDB=0;
ADDC=0;
mydelay(2);
//data_bus=ADC_ch_no; //sending the address to the Port 1 to select the appropriate channel
address_latch=1; //low to high transition to the ALE pin of ADC0808 to latch the address
mydelay(2);
start_conv=1;
mydelay(2);
address_latch=0;
start_conv=0;
while(end_conv==1);
while(end_conv==0);
output_enable=1; //low to high transition on the OE pin to enable the output(to enable extraction of the converted data from the ADC)
//data_bus=0xFF; //declaring the Port1 as input port
mydelay(2);
converted_data=data_bus; //storing the data converted into the variable
output_enable=0; //reset the OE pin
//start_conv=0; //reset the SC pin
return converted_data; //return the converted value
}
void adc_init()
{
ADDA=0;
ADDB=0;
ADDC=0;
start_conv=0;
output_enable=0;
clock_output=0;
address_latch=0;
end_conv=1;
data_bus=0XFF;
}
void timer_init()
{
TMOD=0X01;
TL0=0XFB;
TH0=0XFF;
IEN0=0X82;
TR0=1;
}
//Main function
void main()
{
buzzer=0;
lcd_init();
timer_init();
adc_init();
//infinite loop for constant polling the ADC channel for analog values to be captured and converted
while(1)
{
val=(ADC_conversion()); //passing the converted value to port 2
sprintf(buffer,"%u",val);
lcd_commandsend(0x06);
DELAY(5);
lcd_write(buffer);
DELAY(5);
//lcd_commandsend(0x01);
//and the negation(~) is done considering
//the LED connections on the controller board
} //end of the while loop
} //end of the main function