ACC : Accumulator
B : B Register
PSW : Program Status Word
SP : Stack Pointer
DPTR : Data Pointer
DPL : Low byte
DPH : High byte
P0 : Port 0
P1 : Port 1
P2 : Port 2
P3 : Port 3
IE : Interrupt Enable control
IP : Interrupt Priority control
TMOD : Timer/counter Mode control
TCON : Timer/counter control
T2CON : Timer/counter 2 control
T2MOD : Timer/counter mode2 control
TH0 : Timer/counter 0high byte
TL0 : Timer /counter 0low byte
TH1 : Timer/counter 1high byte
TL1 : Timer/counter 1low byte
TH2 : Timer/counter 2 high byte
TL2 : Timer/counter 2 low byte
SCON : Serial control
SBUF : Serial data buffer
PCON : Power control
IR : Infra Red
INDEX
ACKNOWLEDGE ------------------------------------ 6
ABSTRACT ------------------------------------ 7
I.INTRODUCTION 9
II. LITERATURE SURVEY
2.1 Motivation 11
2.2 Background 11
2.3 Aim 11
2.4 Requirement Analysis 12
2.4.1 Hardware Requirements 12
2.4.2 Software Requirements 12
2.5 Scope 12
2.6 Advantages 12
III.DESIGN METHODOLOGY
3.1 Hardware system design 14
3.1.1 Block level design of Smart Energy Meter 14
3.1.2 Selection of Hardware 15
3.1.3 Design consideration of Microcontroller 15
3.1.3.1 8051 15
3.1.3.2 Internal architecture of P89C51RD2FN 16
3.1.3.2.1 I/O ports 17
3.1.3.2.2 Interrupt controls 18
3.1.3.2.3 Bus controllers 19
3.1.3.2.4 Memory organization 19
3.1.3.2.5 Registers in 8051 20
3.1.3.2.6 Oscillator 22
3.1.3.3 Features 23
3.1.4 Serial communication 24
3.1.4.1 Introduction 24
3.1.4.2 Baud rate 25
3.1.5 Hardware design of LCD 25
3.1.5.1 LCD screen 26
3.1.5.2 Features 29
3.1.5.3 Pin configuration 29
3.1.5.4 Specifications 30
3.1.5.5 Functionality of LCD in project 32
3.1.6 MAX232 32
3.1.6.1 Pin configuration 33
3.1.7 RS232 (Female port) 33
3.1.7.1 Voltage levels 33
3.1.7.2 Pin configuration 34
3.1.7.3 DB9 interfacing microcontroller using MAX232 34
3.1.8 Serial port connector 35
3.1.9 Design of KEYPAD 35
3.1.10 IR sensors and IC NE555 Timer 37
3.1.10.1 Photo transmitter 37
3.1.10.2 Principle of operation 38
3.1.10.3 Application 39
3.1.10.4 Features 39
3.1.10.5 IR receivers 39
3.1.10.6 Photo transistor 39
3.1.10.7 Principle of operation 39
3.1.11 IC NE555 timer 40
3.1.12 Resistors 41
3.1.13 Capacitors 41
3.1.14 Crystal oscillators 42
3.2 Software design 43
3.2.1 Liquid Crystal Display 43
3.2.1.1 Initialization of LCD 43
3.2.1.2 Checking busy state of LCD 43
3.2.2 KEYPAD 47
3.2.2.1 Flow chart of keyboard scanning algorithm 47
IV. IMPLEMENTATION
4.1 Hardware implementation 49
4.1.1 Complete Schematic of Smart Energy Meter 50
4.1.2 Connections of P89C51RD2FN 51
4.1.3 Pin connections of LCD 52
4.1.4 Keypad connections 53
4.1.5 MAX232 and DB9 connections 53
4.1.6 IC555 timer and IR transmitter connections 54
V. SOFTWARE IMPLEMENTATION
5.1 JHD162A LCD interfacing 56
5.1.1 Initialization of LCD 56
5.1.2 Initialization sequence code 56
5.1.3 Checking the busy state of LCD 56
5.1.4 Writing the command to display 57
5.1.5 Writing data to display 57
5.1.6 Displaying the data into LCD 57
5.1.7 4*4 matrix Keypad interfacing 58
5.1.8 Sensors 58
Saving energy is high on the agenda for consumers and businesses, but with most of
the electrical devices today, it‘s difficult to know how much energy we are actually
using at any given point in time. Smart Energy Meter is a meter which helps the
consumers to know their day to day power consumption to better control their usage
and producers to manage production. This meter records consumption of electric
energy in intervals of hour or less. Smart meters enable two-way communication
between the meter and the central system. The proposed project comprises of
hardware design using a low-cost 8-bit ARM microcontroller and the complete
hardware design will be proposed . The system software driver is also developed
using embedded-C programming language in Keil µVision 4 IDE. . Smart meters are
also believed to be a less costly alternative to traditional interval or time-of-use meters
and are intended to be used on a wide scale with all customer classes, including
residential customers.
2.1 MOTIVATION
In the present scenario, the use of advanced technologies such as
digital metering has become extremely necessary to achieve greater efficiency, theft
reduction to reduce AT & C losses and to improve revenue collection. The utilities
and planners should now focus on best use of electronic technology to develop a full
‘smart’ system, which is capable of offering long term benefits and comprehensive
solutions in addition to theft reduction. In undeveloped and under developing
countries proper distribution of power has to be done. So it’s the duty of us, engineers
to develop the equipments to reduce the power losses and power thefts.
2.2 BACKGROUND
An electricity meter or energy meter is a device that measures the
amount of electric energy consumed by a residence, business, or an electrically
powered device. Electricity meters are typically calibrated in billing units, the most
common one being the kilowatt hour. Periodic readings of electric meters establish
billing cycles and energy used during a cycle. In settings when energy savings during
certain periods are desired, meters may measure demand, the maximum use of power
in some interval. In some areas, the electric rates are higher during certain times of
day, to encourage reduction in use. The billing of the electricity consumption in these
present days are done with human observation, but this project deals with the
communication for the proper billing through GSM without any human involvement.
Due to theft of electricity leads to power shut down in many of the rural areas in
India.
2.3 AIM:
To design and implement the smart energy meter by using 8051 micro
controller coded in embedded c program.
2.4 Requirement Analysis:
2.4.1 Hardware requirements
The components those are required for “Smart Energy Meter” project
is given below.
1. Micro Controller
2. MAX 232.
3. DB9 or RS232 connector.
4. Power supply.
5. 4*4 matrix keypad.
6. LCD display.
7. 555 timer IC
8. IR sensors.
2.5 SCOPE:
The scope of the project work is to introduce advanced technology in
converting dc voltage in to ac voltage and introducing smart energy metering concept.
In future this project can be used to measuring natural gas or water
consumption. These meters can be connected to GSM module and data (i.e.
consumption) can be transmitted over GSM networks and the bills can be
automatically issued to the particular customer through SMS. By making small
modifications in the program (code) we can break the connection if user does not pay
the bills in time. There is no need for the electricity officials to visit the spot to
disconnect the connections i.e., everything can be controlled over the GSM module.
The user can also sell the electricity to the government which is created in his home
using solar cells. These meters can also be used as prepaid energy meters by slightly
modifying them.
2.6 Advantages:
More accurate bills.
Lower bills.
Track of energy usage.
Sell energy back to the grid.
Flexible tariffs.
No more meter readings.
CHAPTER-3
CHAPTER - III
DESIGN METHODOLOGY
Potential LCD
Sensor
ARM
Current Interface
Sensor module
Power Load
Supply
In our project, we use a JHD162A LCD Display which has 2 rows and 16
characters. It contains internal 1 byte latch. It has a better contrast and a wider
viewing angle. To develop a protocol to interface this LCD with 89C51 first we have
to understand how they functions. These displays contain two internal byte-wide
registers, one for command and second for characters to be displayed. There are three
control signals called R/W, RS and EN. Select By making RS signal 0 you can send
different commands to display. These commands are used to initialize LCD, to
display pattern, to shift cursor or screen etc. You can see the markings right next to
1st and 16th pins. The 16by2 LCD with connections is as given below
LCD screen:-
LCD screen consists of two lines with 16 characters each. Each character
consists of 5*7 dot matrix. Contrast on display depends on the power supply voltage
and whether messages are displayed in one or two lines. For that reason, variable
voltage 0-Vdd is applied on pin marked as VEE. Trimmer potentiometer is usually
used for that purpose. Some versions of displays have built in backlight (blue or green
diodes). When used during operating, a resistor for current limitation should be used
(like with any LE diode)
The main control pins on JHD162A are data lines, read or write and enable.
LCD is finding wide spread use replacing LEDs (seven segment LEDs or
other multi segment LEDs) because of the following reasons:
1. The ability to display numbers, characters and graphics. This is in contrast to
LEDs, which are limited to numbers and a few characters.
2. Incorporation of a refreshing controller into the LCD, thereby relieving the
CPU of the task of refreshing the LCD. In contrast, the LED must be refreshed
by the CPU to keep displaying the data.
3. Ease of programming for characters and graphics.
4. These components are “specialized” for being used with the microcontrollers,
which means that they cannot be activated by standard IC circuits. They are
used for writing different messages on a miniature LCD.
Fig 3.5: LCD Display
1. Transistor
2. TCRT5000 IR transmitter
3. TCRT5000 IR receiver
4. 100ohms , 220ohms resistor (near transmitter)
5. 22ohms,4.7ohms resister (near receiver)
3.1.10.3 Applications:
Photo diodes are used in
Consumer electronic devices such as compact disc players, smoke
detectors, and the receivers for remote controls in VCRs and television.
Accurate measurement of light intensity.
Detectors for computed tomography (coupled with scintillators) or
instruments to analyze samples (immunoassay), pulse oximeters.
Optical communications and in lighting regulation.
Astronomy, spectroscopy, night vision equipment and laser range finding.
3.1.10.4 FEATURES:
λ= 940 nm
Chip material =GaAs with AlGaAs window
Medium Emission Angle, 40°
High Output Power
Package material and color: Clear, untinted, plastic
Ideal for remote control applications
3.1.10.5 IR Receiver:
IR receiver is used to receive the signals transmitted by the IR transmitter.IR
receiver is similar to a N-P-N transistor. It is a three terminal device but looks like a
two terminal device a base is connected internally. It is a nothing but a
phototransistor.
3.1.12 Resistors:
A resistor is a two-terminal passive electronic component that implements
electrical resistance as a circuit element. When a voltage V is applied across the
terminals of a resistor, a current I will flow through the resistor in direct proportion to
that voltage. This constant of proportionality is called conductance, G. The reciprocal
of the conductance is known as the resistance R, since, with a given voltage V, a
larger value of R further "resists" the flow of current I as given by Ohm's law:
3.1.13 Capacitors:
A capacitor (formerly known as condenser) is a device for storing electric
charge. The forms of practical capacitors vary widely, but all contain at least two
conductors separated by a non-conductor. Capacitors used as parts of electrical
systems, for example, consist of metal foils separated by a layer of insulating film.
Capacitors are widely used in electronic circuits for blocking direct current
while allowing alternating current to pass, in filter networks, for smoothing the output
of power supplies, in the resonant circuits that tune radios to particular frequencies
and for many other purposes.
The capacitor is a reasonably general model for electric fields within electric
circuits. An ideal capacitor is wholly characterized by a constant capacitance C,
defined as the ratio of charge ±Q on each conductor to the voltage V between them:
RS=0
E=1 RS=0
Delay
E=1
E=0
Delay
LCD
Busy
P0=command
LCD
Busy
3.2.2 KEYPAD:
3.2.2.1 FLOW CHART OF KEY BOARD SCANNING ALGORITHM
The 1st and 2nd pins of JHD162A LCD are connected to ground and high
voltage VCC respectively.
3rd pin of LCD is connected to the centre pin of the potentiometer or variable
resistor so as to adjust the contrast of LCD.
The 4th, 5th, 6th pins are connected to 26th (P 2.5), 27th (P 2.6), 28th (P 2.7) pins
of the microcontroller respectively.
The 7th to 14th pins are data pins and are connected to the 39th (P 0.0) to 32nd
(P 0.7) pins of the microcontroller respectively.
The 15th and 16th pins are used for backlight purpose. 15th pin is connected to
VCC and 16th pin to ground.
Fig 4.3: connection of LCD with P89C51RD2FN.
Capacitor C10 of capacitance 1Uf is connected across 1st and 3rd pins of
MAX232 and C9 of capacitance 1Uf is connected in between 4th and 5th pins.
Charge pump capacitors are required for the MAX232 to work it as voltage
level shifter. The charge pump capacitors used here are C7 and C8 whose
capacitance is 1Uf. C7 is connected between 6th pin and ground, while C8 is
connected across 2nd pin of MAX232 and Vcc.
12th and 11th pins of MAX232 are connected to the 10th and 11th pins of
P89C51RD2FN controller respectively. These acts as a transmitter and
receiver for the data flow.
To connect the MAX232 to the PC host we require a medium named as DB9
connector. The 2nd and 3rd pin of the DB9 connector should be connected to
the 14th and 13th pins of MAX232 respectively. While the 5th pin is grounded.
4.1.6 IC555 TIMER and IR transmitter connections:
5.1.8 SENSORS:
Chapter – 6
DEBUGGING TECHNIQUES
6.1.2 Features
The Keil 8051 Development Tools are designed to solve the complex problems
facing embedded software developers.
When starting a new project, simply select the microcontroller you use from the
Device Database and the µvision IDE sets all compiler, assembler, linker, and
memory options for you.
Numerous example programs are included to help you get started with most
popular embedded 8051 devices.
The Keil µVision Debugger accurately simulates on-chip peripherals (CAN,
UART, SPI, Interrupts, I/O Ports, A/D Convertor, D/A convertor, and PWM
Modules) of your 8051 device. Simulation helps you understand hardware
configurations and avoids time wasted on setup problems. Additionally, with
simulation, you can write and test applications before target hardware is
available.
When you are ready to begin testing your software application with target
hardware, use the MON51, MONADI, or FlashMON51 Target Monitors, the
ISD51 In-system Debugger, or the ULINK USB-JTAG Adapter to download
and test program code on your target system.
1. Install Keil Micro Vision in your PC, Then after Click on that “Keil UVision” icon.
After opening the window go to toolbar and select Project Tab then close previous
project.
2. Next select New Project from Project Tab.
3. Then it will open “Create New Project” window. Select the path where you want to
save project and edit project name.
4. Next it opens “Select Device for Target” window, it shows list of companies and
here you can select the device manufacturer company.
5. For an example, for your project purpose you can select the chip as 89c51rd2xx
from Philips Group. Next Click OK Button, it appears empty window here you can
observe left side a small window i.e., “Project Window”. Next create a new file.
6. From the Main tool bar Menu select “File” Tab and go to New, then it will open a
window, there you can edit the program.
7. Here you can edit the program as which language will you prefer either Assembly
or C.
8. After editing the program save the file with extension as “.c” or “.asm”, if you write
a program in Assembly Language save as “.asm” or if you write a program in C
Language save as “.c” in the selected path.
9. Then after saving the file, compile the program. For compilation go to project
window select “source group” and right click on that and go to “Add files to
Group”.
10. Here it will ask which file has to add. For an example here you can add “test.c” as
you saved before
11. After adding the file, again go to Project Window and right click on your “c file”
then select “Build target” for compilation. If there is any “Errors or Warnings” in
your program you can check in “Output Window” that is shown bottom of the Keil
window.
12. Here in this step you can observe the output window for “errors and warnings
13. If you make any mistake in your program you can check in this slide for which
error and where the error is by clicking on that error
14. After compilation then next go to Debug Session. In Tool Bar menu go to
“Debug” tab and select “Start/Stop Debug Session”.
15. Write a program for “Leds Blinking”. LEDS are connected to PORT-1. you can
observe the output in that port.
16. To see the Ports and other Peripheral Features go to main toolbar menu and select
peripherals.
17. In this slide see the selected port i.e., PORT-1.
18. Start to trace the program in sequence manner i.e, step by step execution and
observe the output in port window
19. After completion of Debug Session Create an Hex file for Burning the Processor.
Here to create a Hex file goes to project window and right click on Target next
select “Option for Target”.
20. It appears one window; here in “target tab” modify the crystal frequency as you
connected to your microcontroller.
21. Next go to “Output’ tab. In that Output tab click on “Create HEX File” and then
click OK.
22. Finally Once again compile your program. The Created Hex File will appear in
your path folder
6.2.1 Features
1. From the Windows Start menu, choose All Programs, and then
choose Accessories.
2. Choose Communications, and then choose HyperTerminal.
3. In the Connection Description dialog box, in the Name box, type a name for
the connection to your SDB.
4. From the Icon list, choose an icon to represent your connection, and then
choose OK.
5. In the Connect To dialog box, in the Connect using box, choose the
communications (COM) port on the development workstation through which
you want to receive messages from the SDB.
The COM port that you choose must be the COM port on the development
workstation to which you attach the null modem cable.
6. Choose OK.
7. In the COM<Port Number> Properties dialog box, modify the settings for
your connection so that the settings are correct for your BSP.
While considering the microcontroller, the voltage level at 30th pin must be
1.6V because of the following reason
1. The voltage supply to the kit or mainly processor is 5V.
2. The duty cycle of the microcontroller is 33%
Duty Cycle = T on / (T on+T off)
Where T off = 2* T on
DC = 1/ (1+2)
DC= 0.333
Hence the voltage at 30th pin should be product of Duty Cycle and voltage
supply VCC. Therefore the voltage should be 1.6V.
3. The voltage levels at 2nd and 6th pins of MAX232 should be -10V and
+10V respectively.
CONCLUSION
RESULTS
Now the most awaited part of the program is final result which can be seen
by the following steps once the kit is ready.
Give 5V Power supply and connect the pc with serial connector to DB9
connector
Now ON the circuit.
We can see on the LCD display “WELCOME RMCE” “SMART ENERGY
METER” and press enter (E) in keypad.
The LCD asks for user id and password. Once they are correct, it shows
“AUTHENTICATED”.
Now main menu is displayed having options:1.Readings
2. Settings
7. BIBILOGRAPHY:
Janci Gillespie Mazidi, Muhammad Ali Mazidi, The 8051 micro controller and
embedded systems, PHI Prentice hall India, Eastern Economy Edition,
www.phindia.com.
http://en.wikipedia.org/wiki/Level_s
APPENDICES
15. Click on the file option from menu bar and select “new”.
16. The next screen will be as shown in next page, and just maximize it by
double clicking on its blue boarder.
20. Now you will get another window, on which by default “EMBEDDED C” files will
appear.
21. Now select as per your file extension given while saving the file
22. Click only one time on option “ADD”.
23. Now Press function key F7 to compile. Any error will appear if so happen.
29. Now keep Pressing function key “F11” slowly and observe.
30. You are running your program successfully.
FLASH MAGIC:
Flash Magic is a PC tool for programming flash based microcontrollers from
NXP using a serial or Ethernet protocol while in the target hardware.
6) Then by browsing the required HEX file we can dump the code into controller by
pressing “START” button.
Setup of HyperTerminal
Go to
1. Start Menu>> Programs >> Accessories >> Communications >> Hyper Terminal
After that HyperTerminal window will open. If it prompt for checking of default
program as shown in fig, then say yes to it.
2. Click Yes
3. Type Connection Name as 8051 (or as per your Choice)
4. Select COM PORT (1, 2, 3 etc) whichever is available. (NOTE: Very Less USB-
TO Serial Converters as Reliable)
So use Standard Serial Port
7. Now burn the Hex File (Given) and Switch on the Circuit after Connecting
Specified Serial Port .You will see output as Follows:
If everything works fine you will see Hello World on the screen. Otherwise check
your setup.
COMPLETE CODE:
The complete source code for the project entitled as “Smart Energy Meter” using
Hyper Terminal is given below.
#include <reg51f.h>
sbit r0=P1^0;
sbit r1=P1^1;
sbit r2=P1^2;
sbit r3=P1^3; //rows
sbit c0=P1^4;
sbit c1=P1^5;
sbit c2=P1^6;
sbit c3=P1^7; //columns
sbit sense=P3^2; //energy meter
sfr ldata=0x80;//port 0 address
sbit rs=P2^5;
sbit rw=P2^6;
sbit en=P2^7; //control pins of lcd
sbit busy=P0^7; //busy pin of lcd
void UpdateService(void);
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void lcdready(void);
void MainMenu(void);
void MSDelay(unsigned int);
void WriteString(unsigned char,unsigned char *);
void serialinit(void);
void sertx(unsigned char);
void sendintdata(unsigned int);
void SendString(unsigned char,unsigned char * );
unsigned char KeyTest(void);
unsigned char key;
unsigned char mode;
void Start(void);
void LCDClear(void);
unsigned char MY_PWD[4]={'1','2','3','4'};
unsigned int Count;
unsigned char Flag;
//void convertanddisplay(unsigned char value);
void _Up() interrupt 0
{
Flag=sense;
if (Flag==1)
{
Count++;
Flag=0;
}
}
void lcdcmd (unsigned char value)
{
lcdready();
ldata=value;
rs=0;
rw=0;
en=1;
MSDelay(1);
en=0;
}
void lcddata (unsigned char value)
{
lcdready();
ldata=value;
rs=1;
rw=0;
en=1;
MSDelay(1);
en=0;
}
void lcdready(void)
{
busy=1;
rs=0;
rw=1;
while(busy==1)
{
en=0;
MSDelay(1);
en=1;
}
}