# Prepaid Energy Meter with GSM Modem using pic microcontroller

In this tutorial will learn how to interface ADC0804(Analog to Digital Converter) with 8051(89c51,89c52) microcontroller. 8051 microcontrollers are pretty old and don’t have a build in analog to digital converter in them unlike their new rivals(Arduino, Pic microcontroller and many more). Hence we can not directly measure any analog value (voltage, Temperature present in the atmosphere) with 8051 microcontrollers. So to measure an analog quantity with 8051 microcontroller we need an external device which can measure analog quantity and pass it to 8051 microcontroller. Since 8051 series microcontrollers works on digital data, the external device must convert the analog data to digital before passing it to 8051 microcontroller. Analog to digital converters are used for this purpose. ### Diy Project

I am going to measure temperature of room using 8051 microcontroller. I am going to use LM35 temperature sensor to measure room temperature. Lm35 temperature sensor outputs an analog signal(voltage) by measuring the temperature present in the atmosphere. ADC0804 is used to convert the analog reading from lm35 in to digital. After converting the reading to digital the value will be passed to 8051 microcontroller.
NOTE: The above tutorial on ADC0804 is very important in order to understand the code below.

### Project requirements

• 16×2 lcd             – For displaying 8-bit value from ADC0804.
• 89c51 or 89c52 Microcontroller .
• ADC0804           – For analog to digital conversions.
• Potentiometer    – To adjust contrast of the lcd.
• Bread board       – To design circuit.
• Crystal                – I used 11.0592 Hz you can use of your choice, but its better to use same as i used.
• Voltage supplier  – 5 volts.
• Connecting wires – For making connections.

### Project Circuit

To learn more about Lm35 temperature sensor working, pin out and formula to calculate temperature go through the below tutorial.

At last connect Port-1 of 89c51 microcontroller with 8-bit output of ADC0804, and connect Port-2 with 8-bit input of 16×2 lcd. Circuit diagram of the project is given below.

# ADC0804 with 8051(89c51,89c52) microcontroller CODE

Comming to the code first i included the header file reg51.h, you must include this header file in your every project in which you are using 8051(89c51,89c52) microcontroller. Then port three pins 5 and 6 are defined as rs(regester select) and en(enable) to be used for controlling lcd. If you dont know how to use 16×2 lcd then first take a small tutorial on

Port-3 pins 0, 1, 2 and 3 are used as cs(chip select) wr(write) rd(read) intr(interrupt) to control ADC0804. Project code is given below and each statement well explained.

 #include sbit rs=P3^5; //Register select (RS) sbit en=P3^6; //Enable (EN) pin sbit cs=P3^; sbit wr= P3^1; sbit rd= P3^2; sbit intr= P3^3; void delay(unsigned int time) //Time delay function { unsigned int i,j; for(i=;i< time;i++) for(j=;j<5;j++); } void lcdcmd(unsigned char value) //Function for sending values to the command register of LCD { P1=value; P3=0x40; delay(50); en=; delay(50); return; } void display(unsigned char value) //Function for sending values to the data register of LCD { P1=value; P3=0x60; delay(500); en=; delay(50); return; } void lcdint(void) //function to initialize the registers and pins of LCD { P1=0x00; P2=0x00; P3=0x00; delay(15000); display(0x30); delay(4500); display(0x30); delay(300); display(0x30); delay(650); lcdcmd(0x38); lcdcmd(0x0F); lcdcmd(0x01); lcdcmd(0x06); lcdcmd(0x80); } void main() { unsigned int Adcvalue,Adcvalue1; char ch1,ch2,ch3; P1=0x00; //Declared as Output port P3=0x00; //Output port P2=0xFF; //Input port lcdint(); while (1){ delay(10000); cs=; //chipselect is on now –It is active low pin wr=; // write is enabled –It is also active low delay(10); wr=1; //start conversion analog to digital rd=1; while(intr==1); //The looop runs until intr==1 and //when intr==0 it jumps to next iteration rd=; //read the digital output from adc Adcvalue=P2; ch1=Adcvalue/100; if(ch1!=) display(ch1+0x30); delay(100); Adcvalue1=Adcvalue%100; ch2=Adcvalue1/10; display(ch2+0x30); ch3=Adcvalue1-(ch2*10); display(ch3); delay(10000); lcdcmd(0x01); }}
view rawadc0804-with-8051-microcontroller.c hosted with by GitHub
More advance project on adc0804 is measure room temperature using 89c52 microcontroller, 16×2 lcd and adc0804. Tutorial contains free source code and circuit diagram of the project. Below button will take you to the project.

#### Current Project / Post can also be found using:

• gsm based energy meter pic microcontroller code 