ADC0808

Photo of author

By Jackson Taylor

As an Amazon Associate we earn from qualifying purchases.

In this program 8051 is interfaced with adc adc0808

/*
MICROCONTROLLER -AT89S51/52
COMPILER ---MIKROC
BY---EMERGING TECHNOLOGIES --WWW.EMERGINGTECHS.ORG
*/
#define AN0 0
#define AN1 1
#define AN2 2
#define AN3 3
#define AN4 4
#define AN5 5
#define AN6 6
#define AN7 7
/*
MICROCONTROLLER -AT89S51/52
COMPILER ---MIKROC
BY---EMERGING TECHNOLOGIES --WWW.EMERGINGTECHS.ORG
*/
#define Data_Bus   P0
#define HalfCycleDelay 10
void InitADC(void);
unsigned char ReadADC(unsigned char);
    unsigned char ADC_Value = 0; // To capture ADC value
unsigned char Digit[3] = { 0,0,0 }; // To make digits to display on LCD
sbit LCD_RS at P1_1_bit;
sbit LCD_EN at P1_0_bit;
sbit LCD_D4 at P1_4_bit;
sbit LCD_D5 at P1_5_bit;
sbit LCD_D6 at P1_6_bit;
sbit LCD_D7 at P1_7_bit;
// End Lcd module connections
sbit switch1 at P3_0_bit;
sbit led1 at P3_1_bit;
sbit ADD_A at P2_0_bit;   // Address pins for selecting input channels.
sbit ADD_B at P2_1_bit;
sbit ADD_C at P2_2_bit;
sbit ale at P2_3_bit;     //address latch enable
sbit eoc at P2_4_bit;     //end of conversion
sbit oe at P2_5_bit;      //output enable
sbit start at P2_6_bit;   //start conversion
sbit clk at P2_7_bit;     // clock
//sfr input_port=0x80;//P0 port
void main(){
    Lcd_Init();                     // Initialize Lcd
    Lcd_Cmd(_LCD_CLEAR);            // Clear display
    Lcd_Cmd(_LCD_CURSOR_OFF);       // Cursor off
    Lcd_Out(1,6,"hai");             // Write text in first row
    delay_ms(1000);
    InitADC();
    delay_ms(1000);
    Lcd_Cmd(_LCD_CLEAR);
    while(1)
    {
        ADC_Value = ReadADC(AN1);
        Digit[2] = (unsigned char)( ADC_Value/100);   // Find out first digit
        Digit[1] = (unsigned char)( ADC_Value/10) - Digit[2]*10; // Find out second digit
        Digit[0] = ADC_Value - Digit[2]*100 - Digit[1]*10; // Find out third digit
        Lcd_Cmd(_LCD_CLEAR); // Clear LCD
        Lcd_Out(1,6,"temp= "); // Display string
        Lcd_Chr_cp(Digit[2]+0x30); // Display first digit
        Lcd_Chr_cp(Digit[1]+0x30); // Display second digit
        Lcd_Chr_cp(Digit[0]+0x30); // Display third digit
        if( switch1 ==0)   { led1=1; Lcd_Out(2,1,"gas found caution"); }
        else   { led1=0;   Lcd_Out(2,1,"                "); }
        delay_ms(100);
    }
}
unsigned char ReadADC(unsigned char Channel)
{
    unsigned int i = 0;
    unsigned int ADC_value = 0;
    // Select Channel
    switch(Channel)
    {
        case AN0: Add_C = 0;   Add_B = 0;   Add_A = 0; break;
        case AN1: Add_C = 0;   Add_B = 0;   Add_A = 1; break;
        case AN2: Add_C = 0;   Add_B = 1;   Add_A = 0; break;
        case AN3: Add_C = 0;   Add_B = 1;   Add_A = 1; break;
        case AN4: Add_C = 1;   Add_B = 0;   Add_A = 0; break;
        case AN5: Add_C = 1;   Add_B = 0;   Add_A = 1; break;
        case AN6: Add_C = 1;   Add_B = 1;   Add_A = 0; break;
        case AN7: Add_C = 1;   Add_B = 1;   Add_A = 1; break;
    }
    delay_us(HalfCycleDelay); // 250kHz Frequency
    ALE = 1; // Enable Address Latch
    CLK = 1; // Make CLK High
    delay_us(HalfCycleDelay); // 250kHz Frequency
    CLK = 0; // Make CLK Low
    START = 1; // Start ADC Conversion
    delay_us(HalfCycleDelay); // 250kHz Frequency
    CLK = 1; // Make CLK High
    ALE = 0; // Disable Address Latch
    delay_us(HalfCycleDelay); // 250kHz Frequency
    CLK = 0; // Make CLK Low
    START = 0; // Complete the start pulse
    for(i=0;i<2000;i++)
    {
        CLK = !CLK; // Toggle Clock
        delay_us(HalfCycleDelay); // 250kHz Frequency
        if(!EOC)   // Wait for EOC to be low
            break;
    }
    for(i=0;i<2000;i++)
    {
        CLK = !CLK; // Toggle Clock
        delay_us(HalfCycleDelay); // 250kHz Frequency
        if(EOC)   // Wait for EOC to be High
            break;
    }
    CLK = 0; // Make CLK Low
    OE = 1; // Enable Output
    delay_us(HalfCycleDelay); // 250kHz Frequency
    CLK = 1; // Make CLK High
    delay_us(HalfCycleDelay); // 250kHz Frequency
    CLK = 0; // Make CLK Low
    delay_us(HalfCycleDelay); // 250kHz Frequency
    CLK = 1; // Make CLK High
    ADC_value = Data_Bus; // Read value
    delay_us(HalfCycleDelay); // 250kHz Frequency
    OE = 0; // Disable Output
    CLK = 0; // Make CLK Low
    delay_us(HalfCycleDelay); // 250kHz Frequency
    return ADC_value; // Return ADC value
}
void InitADC(void)
{
    Add_A = 0;   // Make output
    Add_B = 0;   // Make output
    Add_C = 0;   // Make output
    ALE   = 0;   // Make output
    EOC   = 1;   // Make input
    OE    = 0;   // Make output
    START = 0;   // Make output
    CLK   = 0;   // Make output
    Data_Bus = 0xFF;   // Make Inputs
}

How the ADC0808 Code Works and Common Problems

The ADC0808 is an 8-bit analog-to-digital converter with eight multiplexed input channels. It has no internal clock, so the 8051 must supply one. That is why the ReadADC function toggles the CLK pin by hand with short delays. Each reading follows the same sequence:

  1. Put the channel number on the ADD_A, ADD_B and ADD_C pins (the switch block in the code).
  2. Pulse ALE to latch the address, then pulse START to begin the conversion.
  3. Keep clocking the chip while waiting for EOC to go low and then high again. This means the conversion is finished.
  4. Set OE high so the chip puts the result on the data lines, then read Port 0.
See also
SUPER CAPACITOR

The result is a number from 0 to 255. With a 5 V reference, each step is about 19.5 mV, so the reading is roughly input voltage divided by reference voltage, multiplied by 256. The program shows this raw number on the LCD under the label “temp=”. If you connect a temperature sensor, you still need to convert the raw value to degrees using that sensor’s own scale.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

Things to check if it does not work

  • Name case: MikroC is case sensitive. The pins are declared as ADD_A, ale, eoc, oe, start and clk, but the functions use Add_A, ALE, EOC and so on. Make the names match, or the code will not compile.
  • Clock speed: The delays are meant to give a clock of about 250 kHz. If you change the crystal frequency, the delays change too, so adjust HalfCycleDelay to compensate.
  • Reference voltage: Make sure the reference pins are wired correctly. A wrong or noisy reference gives wrong readings.
  • Unused channels: The code reads channel AN1. Connect your sensor to that input, or change the channel in the call to ReadADC.
  • Port 0: Writing 0xFF to P0 in InitADC sets it up as an input. Check your wiring if the values look random.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works