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
}