a15 Prepaid Energy Meter

Published on January 2017 | Categories: Documents | Downloads: 33 | Comments: 0 | Views: 274
of 68
Download PDF   Embed   Report

Comments

Content


qwertyuiopasdfghjklzxcvbnmqwerty
uiopasdfghjklzxcvbnmqwertyuiopasd
fghjklzxcvbnmqwertyuiopasdfghjklzx
cvbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmqwertyuiopasdfghjklzxc
vbnmqwertyuiopasdfghjklzxcvbnmq
wertyuiopasdfghjklzxcvbnmqwertyui
opasdfghjklzxcvbnmqwertyuiopasdfg
hjklzxcvbnmrtyuiopasdfghjklzxcvbn
mqwertyuiopasdfghjklzxcvbnmqwert
yuiopasdfghjklzxcvbnmqwertyuiopas
dfghjklzxcvbnmqwertyuiopasdfghjklz







PREPAID ENERGY METER

www.BEProjectReport.com


VISIT US, CHOOSE THE PROJECT YOU LIKE AND
CLICK THE DOWNLOAD BUTTON

PREPAID ENERGY METER

www.BEProjectReport.com Page 2

CHAPTER 1
INTRODUCTION

1.1 EMBEDDED SYSTEMS

An embedded system is a combination of software and hardware to perform a dedicated task.
One of the most critical needs of an embedded system is to decrease power consumption and
space. Some of the main devices used in embedded products are Microprocessors and
Microcontrollers. Microprocessors are commonly referred to as general purpose processors as they
simply accept the inputs, process it and give the output. These microprocessors contain no RAM, no
ROM, and no I/O ports on the chip itself .For this reason, they are commonly referred to as general-
purpose microprocessors. In contrast, a microcontroller has a CPU (microprocessor) in addition to a
fixed amount of RAM, ROM, I/O ports, and a timer all are embedded together on one chip. A
microcontroller not only accepts the data as inputs but also manipulates it, interfaces the data with
various devices, controls the data and thus finally gives the result.

1.2 MICROCONTROLLERS:
1.2.1 INTRODUCTION:
A Micro controller is a computer-on-a-chip or a single-chip computer. „Micro‟
suggests that the device is small and „Controller‟ tells that the device might be used to control
objects, processes or events.

The core of many specialized computers is the micro controller. The computer‟s program is
typically stored permanently in semiconductor memory such as ROM or EPROM. The interfaces
between the micro controller and the outside world vary with application, and may include a small
display, a keypad or switches, sensors, relays, motors and so on. These small, special purpose
computers are sometimes called Single Board Computers or SBCs.

A micro controller is similar to the microprocessor inside a personal computer. Examples
are Intel‟s 8086, Zilog‟s Z80. Both microprocessors and micro controllers contain CPU. The CPU
executes instructions that perform the basic logic, math, and data-moving functions of a computer.
To make a complete computer, a microprocessor requires memory for storing data and programs, and
I/O interfaces for connecting external devices like keyboard and displays. In contrast, micro
controllers are a single-chip computer because it contains memory and I/O interfaces in addition to
the CPU. It tends to limit the amount of memory and interfaces that can fit on single chip, micro
controllers tend to be used in smaller system. Examples of popular micro controllers are Intel‟s 8052,
89C052, AT89s52, Motorola‟s 68HC11 and Zilog‟s Z80.
PREPAID ENERGY METER

www.BEProjectReport.com Page 3

Microcontrollers are little more than a carefully designed array of logic gates and
memory cells, but modern fabrication processes allow thousands of these to fit on a single chip. By
using a microcontroller can reduce the number or components and thus the amount of design work
and wiring required for the project.

1.2.2 FEATURES:
Microcontrollers are “special purpose computers”. These are a number of
common characteristics that define microcontrollers:

 Microcontrollers are “embedded” inside some other device (often a consumer product) so that
they can control the features or actions of the product. Another name for a microcontroller,
therefore, is “embedded controller”.
 Microcontrollers are dedicated to one task and run one specific program. The program is
stored in ROM (Read Only Memory) and generally does not change.
 Microcontrollers are often low-power devices. A battery-operated microcontroller might
consume 50 milli watts.
 A microcontroller has a dedicated input device and often has a small LED or LCD display for
output. A microcontroller also takes input from the device it is controlling and controls the
device by sending signals to different components in the device.
 A microcontroller is often small and low cost. The components are chosen to minimize size
and to be as inexpensive as possible.

Basic functions of microcontroller include performing arithmetic and logic
operations, data moving and program branching functions. Control circuits often require reading or
changing single bits of input or output rather than reading and writing a byte at a time. For example
the MC might use 8 bits of the output is required to switch power to 8 sockets. If each socket must
operate independently of the others, a way is needed to change each bit without affecting the others.
Many microcontrollers use bit manipulation op-codes that allow programs to set, clear, compare,
copy or perform other logic operations on single bits of data, rather than a byte at a time.

1.2.3 ADVANTAGE OF MICROCONTROLLERS
We go for Microcontroller instead of microprocessor because in addition to parts
of microprocessors, microcontroller has ROM, RAM, I/O ports integrated on it. 8051
Microcontroller ATMEL 89s52 is a low power, high performance CMOS 8-bit microcontroller with
8Kbytes of Flash programmable (1000 Write/Erase Cycles) and electrically erasable read only
memory (EEPROM). This device is compatible with the industry standard 8051 instruction set and
pin out. The on-chip Flash allows the program memory to be quickly reprogrammed using a
nonvolatile memory programmer such as the PG302 (with the ADT87 adapter). By combining an
industry standard 8-bit CPU with Flash on a monolithic chip, the 89S52 is a powerful microcomputer
which provides a highly flexible and cost effective solution to many embedded control applications.

PREPAID ENERGY METER

www.BEProjectReport.com Page 4

1.2.4 APPLICATIONS OF MICROCONTROLLERS:
Microcontrollers are widely used in embedded system products nowadays a
microcontroller inside a device can measure, controls, stores or displays information. The largest use
of microcontroller is engine control and additional systems. In desktop computers we may find
microcontrollers inside keyboards, printers, modems and other peripherals. In test equipment,
microcontrollers make things easier to store measurement, and to display messages and waveforms.

1.3 PREPAID ENERGY SYSTEM:
In this system the user has to purchase an EEPROM based recharge card and it should be
inserted in the slot provided on prepaid energy meter kit. After inserting the recharge card into the
system, the user should press RECHARGE key to start recharge. Then the system will be loaded
with specific units as per the recharge card value. A 16X2 LCD is provided to read units available.
Here the system is connected with lamp loads. Microcontroller counts the pulses from the
optocoupler of the energy meter which depends on the energy consumption. Whenever the count
value reaches specific value which depends on the energy meter constant, 1unit is decremented and
these values are displayed on LCD.

1.4 ORGANIZATION OF PROJECT:
In our project a microcontroller named AT89S52 is used to count the pulses from the
optocoupler, to display message and number of units on LCD, and to trip the relay. An EEPROM
named AT24C02 is provided on the board to store the updated recharge units and energy meter pulse
count. At every instant the count value and units values are stored in EEPROM so that the data will
not be lost even in power failure cases. When 1 unit is decremented from EEPROM the system will
give a beep sound. When the recharged units become zero on power consumption, the system
shutdown all the loads connected to it by giving a continuous beep sound. To use the system again
the user has to reload the units by recharging the EEPROM.

fig 1.1 Block diagram of prepaid energy meter
PREPAID ENERGY METER

www.BEProjectReport.com Page 5

To recharge the system the user has to place the EEPROM based recharge card in slot
provided. After pressing the recharge key, the system will be loaded with the units corresponding to
that recharge value. After successful recharge, the load automatically gets ON.

This system uses 5V regulated power supply for microcontroller, LCD, EEPROM and driver
IC and 12V supply for relay which is supplied form a step down transformer.



PREPAID ENERGY METER

www.BEProjectReport.com Page 6

CHAPTER 2
COMPONENTS USED IN THE MODEL

AT89S52 MICROCONTROLLER

2.1 MICROCONTROLLER (AT89S52)
2.1.1 INTRODUCTION:
Microprocessors and microcontrollers are widely used in embedded systems products.
Microcontroller is a programmable device. A microcontroller has a CPU in addition to a fixed
amount of RAM, ROM, I/O ports and a timer embedded all on a single chip. The fixed amount of
on-chip ROM, RAM and number of I/O ports in microcontrollers makes them ideal for many
applications in which cost and space are critical.

The Intel 8051 is Harvard architecture, single chip microcontroller (µC) which was
developed by Intel in 1980 for use in embedded systems. It was popular in the 1980s and early
1990s, but today it has largely been superseded by a vast range of enhanced devices with 8051-
compatible processor cores that are manufactured by more than 20 independent manufacturers
including Atmel, Infineon Technologies and Maxim Integrated Products.
PREPAID ENERGY METER

www.BEProjectReport.com Page 7


Fig 2.1 AT89S52 Microcontroller
8051 is an 8-bit processor, meaning that the CPU can work on only 8 bits of data at a time.
Data larger than 8 bits has to be broken into 8-bit pieces to be processed by the CPU. 8051 is
available in different memory types such as UV-EPROM, Flash and NV-RAM.

The present project is implemented on Keil Vision. In order to program the device, proload
tool has been used to burn the program onto the microcontroller.

The features, pin description of the microcontroller and the software tools used are discussed
in the following sections.

Why AT89S52? :
The system requirements and control specifications clearly rule out the use of 16, 32 or 64 bit
micro controllers or microprocessors. Systems using these may be earlier to implement due to large
number of internal features. They are also faster and more reliable but, the above application is
satisfactorily served by 8-bit micro controller. Using an inexpensive 8-bit Microcontroller will doom
the 32-bit product failure in any competitive market place.

Coming to the question of why to use AT89S52 of all the 8-bit Microcontroller available in
the market the main answer would be because it has 64 KB Flash and 1024 bytes of data RAM. . The
Flash program memory supports both parallel programming and in Serial In-System Programming
(ISP). The AT89S52 is also In-Application Programmable (IAP), allowing the Flash program
memory to be reconfigured even while the application is running.



PREPAID ENERGY METER

www.BEProjectReport.com Page 8

2.1.2 FEATURES OF AT89S52:
 8K Bytes of in-system Programmable Flash Memory.
 4V to 5.5V Operating Range.
 Fully Static Operation: 0 Hz to 33 MHz
 Three-level Program Memory Lock.
 256 x 8-bit Internal RAM.
 32 Programmable I/O Lines.
 Three 16-bit Timer/Counters.
 Eight Interrupt Sources.
 Fully Duplex UART serial Channel.
 Low-power Idle and Power-down Modes.
 Fast programming time
2.1.3 DESCRIPTION:
The AT89S52 is a low-power, high-performance CMOS 8-bit microcontroller with 8K bytes
of in-system programmable Flash memory. The device is manufactured using Atmel‟s high-density
nonvolatile memory technology and is compatible with the industry- standard 80C51 instruction set
and pin out. The on-chip Flash allows the program memory to be reprogrammed in-system or by a
conventional nonvolatile memory programmer. By combining a versatile 8-bit CPU with in-system
programmable Flash on a monolithic chip, the Atmel AT89S52 is a powerful microcontroller which
provides a highly-flexible and cost-effective solution to many embedded control applications.

Block diagram of AT89S52 microcontroller is given in fig 2.1 and Pin diagram in fig
2.2
Micro controller has 4 ports namely
1. Port 0
2. Port 1
3. Port 2
4. Port3

The AT89S52 provides the following standard features: 8K bytes of Flash, 256 bytes of
RAM, 32 I/O lines, Watchdog timer, two data pointers, three 16-bit timer/counters, a six-vector two-
level interrupt architecture, a full duplex serial port, on-chip oscillator, and clock circuitry
PREPAID ENERGY METER

www.BEProjectReport.com Page 9


Fig 2.2 Block diagram of 8051 microcontroller

. In addition, the AT89S52 is designed with static logic for operation down to zero frequency
and supports two software selectable power saving modes. The Idle Mode stops the CPU while
allowing the RAM, timer/counters, serial port, and interrupt system to continue functioning. The
Power-down mode saves the RAM contents but freezes the oscillator, disabling all other chip
functions until the next interrupt or hardware reset.
PREPAID ENERGY METER

www.BEProjectReport.com Page 10












Fig 2.3 Pin diagram of AT89S52 Microcontroller
VCC:
Pin 40 provides supply voltage to the chip. The voltage source is +5V.
GND:
Pin 20 is the ground.
XTAL1 and XTAL2
XTAL1 and XTAL2 are the input and output, respectively, of an inverting amplifier that can
be configured for use as an on-chip oscillator, as shown in Figure 11. Either a quartz crystal or
ceramic resonator may be used. To drive the device from an external clock source, XTAL2 should be
left unconnected while XTAL1 is driven, as shown in the below figure. There are no requirements on
the duty cycle of the external clock signal, since the input to the internal clocking circuitry is through
a divide-by-two flip-flop, but minimum and maximum voltage, high and low time specifications
must be observed.
PREPAID ENERGY METER

www.BEProjectReport.com Page 11


Fig 2.4 Oscillator Connections

C1, C2 = 30 pF ± 10 pF for Crystals
= 40 pF ± 10 pF for Ceramic Resonators

RESET
Pin9 is the reset pin. It is an input and is active high. Upon applying a high pulse to this pin, the
microcontroller will reset and terminate all the activities. This is often referred to as a power-on
reset.
PSEN (Program store enable)
This is an output pin.
ALE (Address latch enable)
This is an output pin and is active high
PREPAID ENERGY METER

www.BEProjectReport.com Page 12


Fig 2.5 External Clock Drive Configuration

EA (External access)
Pin 31 is EA. It is an active low signal. It is an input pin and must be connected to either VCC or
GND but it cannot be left unconnected.
The 8051 family members all come with on-chip ROM to store programs. In such cases, the
EA pin is connected to VCC. If the code is stored on an external ROM, the EA pin must be
connected to GND to indicate that the code is stored externally.
Ports 0, 1, 2 and 3
The four ports P0, P1, P2 and P3 each use 8 pins, making them 8-bit ports. All the ports upon
RESET are configured as input, since P0-P3 have value FFH on them.
Port 0(P0)
Port 0 is also designated as AD0-AD7, allowing it to be used for both address and data. ALE
indicates if P0 has address or data. When ALE=0, it provides data D0-D7, but when ALE=1, it has
address A0-A7. Therefore, ALE is used for demultiplexing address and data with the help of an
internal latch.
When there is no external memory connection, the pins of P0 must be connected to a 10K-
ohm pull-up resistor. This is due to the fact that P0 is an open drain. With external pull-up resistors
connected to P0, it can be used as a simple I/O, just like P1 and P2. But the ports P1, P2 and P3 do
not need any pull-up resistors since they already have pull-up resistors internally. Upon reset, ports
P1, P2 and P3 are configured as input ports.
PREPAID ENERGY METER

www.BEProjectReport.com Page 13

Port 1 and Port 2
With no external memory connection, both P1 and P2 are used as simple I/O. With external memory
connections, port 2 must be used along with P0 to provide the 16-bit address for the external
memory. Port 2 is designated as A8-A15 indicating its dual function. While P0 provides the lower 8
bits via A0-A7, it is the job of P2 to provide bits A8-A15 of the address.
Port 3
Port 3 occupies a total of 8 pins, pins 10 through 17. It can be used as input or output. P3 does not
need any pull-up resistors, the same as port 1 and port 2. Port 3 has an additional function of
providing some extremely important signals such as interrupts.

Table 2.1 Port 3 Alternate Functions
Machine cycle for the 8051
The CPU takes a certain number of clock cycles to execute an instruction. In the 8051 family, these
clock cycles are referred to as machine cycles. The length of the machine cycle depends on the
frequency of the crystal oscillator. The crystal oscillator, along with on-chip circuitry, provides the
clock source for the 8051 CPU.
The frequency can vary from 4 MHz to 30 MHz, depending upon the chip rating and manufacturer.
But the exact frequency of 11.0592 MHz crystal oscillator is used to make the 8051 based system
compatible with the serial port of the IBM PC.
In the original version of 8051, one machine cycle lasts 12 oscillator periods. Therefore, to calculate
the machine cycle for the 8051, the calculation is made as 1/12 of the crystal frequency and its
inverse is taken.
PREPAID ENERGY METER

www.BEProjectReport.com Page 14

The assembly language program is written and this program has to be dumped into the
microcontroller for the hardware kit to function according to the software. The program dumped in
the microcontroller is stored in the Flash memory in the microcontroller. Before that, this Flash
memory has to be programmed and is discussed in the next section.

ALE/PROG
Address Latch Enable is an output pulse for latching the low byte of the address during
accesses to external memory. This pin is also the program pulse input (PROG) during Flash
programming. In normal operation, ALE is emitted at a constant rate of 1/6 the oscillator frequency
and may be used for external timing or clocking purposes. If desired, ALE operation can be disabled
by setting bit 0 of SFR location 8EH. With the bit set, ALE is active only during a MOVX or MOVC
instruction. Otherwise, the pin is weakly pulled high. Setting the ALE-disable bit has no effect if the
microcontroller is in external execution mode.

PSEN (Program Store Enable)
It is the read strobe to external program memory. When the AT89S8252 is executing code
from external program memory, PSEN is activated twice each machine cycle, except that two PSEN
activations are skipped during each access to external data memory.

EA/VPP (External Access Enable)
EA must be strapped to GND in order to enable the device to fetch code from external program
memory locations starting at 0000H up to FFFFH. Note, however, that if lock bit 1 is programmed,
EA will be internally latched on reset. EA should be strapped to VCC for internal program
executions. This pin also receives the 12-volt programming enable voltage (VPP) during Flash
programming when 12-volt programming is selected.

Ports 0, 1, 2 and 3
The four ports P0, P1, P2 and P3 each use 8 pins, making them 8-bit ports. All the ports upon
RESET are configured as input, since P0-P3 have value FFH on them.
Port 0(P0)
Port 0 is also designated as AD0-AD7, allowing it to be used for both address and data. ALE
indicates if P0 has address or data. When ALE=0, it provides data D0-D7, but when ALE=1, it has
address A0-A7. Therefore, ALE is used for demultiplexing address and data with the help of an
internal latch.
When there is no external memory connection, the pins of P0 must be connected to a 10K-ohm pull-
up resistor. This is due to the fact that P0 is an open drain. With external pull-up resistors connected
to P0, it can be used as a simple I/O, just like P1 and P2. But the ports P1, P2 and P3 do not need any
PREPAID ENERGY METER

www.BEProjectReport.com Page 15

pull-up resistors since they already have pull-up resistors internally. Upon reset, ports P1, P2 and P3
are configured as input ports.
Port 1
Port 1 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 1 output buffers can
sink/source four TTL inputs. When 1s are written to Port 1 pins, they are pulled high by the internal
pull-ups and can be used as inputs. As inputs, Port 1 pins that are externally being pulled low will
source current because of the internal pull-ups.

Some Port 1 pins provide additional functions. P1.0 and P1.1 can be configured to be the
timer/counter 2 external count input (P1.0/T2) and the timer/counter 2 trigger input (P1.1/T2EX),
respectively. Furthermore, P1.4, P1.5, P1.6, and P1.7 can be configured as the SPI slave port select,
data input/output and shift clock input/output pins. Port 1 also receives the low-order address bytes
during Flash programming and verification.

Table 2.2 Port1 Alternate functions
Programmable Clock Out:
A 50% duty cycle clock can be programmed to come out on P1.0. This pin, besides being a
regular I/0 pin, has two alternate functions. It can be programmed to input the external clock for
Timer/Counter 2 or to output a 50% duty cycle clock ranging from 61 Hz to 4 MHz (for a 16-MHz
operating frequency).

Port 2
With no external memory connection, P2 are used as simple I/O. With external memory
connections, port 2 must be used along with P0 to provide the 16-bit address for the external
memory. Port 2 is designated as A8-A15 indicating its dual function. While P0 provides the lower 8
bits via A0-A7, it is the job of P2 to provide bits A8-A15 of the address.

PREPAID ENERGY METER

www.BEProjectReport.com Page 16

Port 2 also receives the high-order address bits and some control signals during Flash
programming and verification.
Port 3
Port 3 is an 8-bit bi-directional I/O port with internal pull-ups. The Port 3 output buffers can
sink/source four TTL inputs. When 1s are written to Port 3 pins, they are pulled high by the internal
pull-ups and can be used as inputs. As inputs, Port 3 pins that are externally being pulled low will
source current because of the pull-ups. Port 3 receives some control signals for Flash programming
and verification.
Port 3 also serves the functions of various special features of the AT89S8252, as shown in
the following table.

Table 2.3 Port 3 Alternate functions
2.2 EEPROM
2.2.1 INTRODUCTION:
EEPROM has several advantages over other memory devices, such as the fact that its method
of erasure is electrical and therefore instant. In addition, in EEPROM one can select which byte to be
erased, in contrast to flash, in which the entire contents of ROM are erased. The main advantage of
EEPROM is that one can program and erase its contents while it is in system board. It does not
require physical removal of the memory chip from its socket. In general, the cost per bit for
EEPROM is much higher when compared to other devices.
This project requires the data such as the total number of available units and the pulse count
to be stored permanently and this data modifies upon the power consumption. Thus this data has to
be stored in such a location where it cannot be erased when power fails and also the data should be
allowed to make changes in it without the system interface i.e., there should be a provision in such a
way that the data should be accessed (or modified) while it is in system board but not external
PREPAID ENERGY METER

www.BEProjectReport.com Page 17

erasure and programming. The flash memory inbuilt in the microcontroller can erase the entire
contents in less than a second and the erasure method is electrical.

Fig2.6 .EEPROM
But the major drawback of Flash memory is that when flash memory‟s contents are erased, the entire
device will be erased but not a desired section or byte. For this purpose, we prefer EEPROM in our
project. The EEPROM used in this project is 24C04 type.
2.2.2 FEATURES OF 24C04 EEPROM:
 1 million erase/write cycles with 40 years data retention.
 Single supply voltage:
3v to 5.5v for ST24X04 versions.
 Hardware write control versions:
ST24W04 and ST25W04.
 Programmable write protection.
 Two wire serial interface, fully i2c bus compatible.
 Byte and multibyte write (up to 4 bytes).
 Page write (up to 8 bytes).
 Byte, random and sequential read modes
 Self timed programming cycle
 Automatic address incrementing
PREPAID ENERGY METER

www.BEProjectReport.com Page 18

 Enhanced ESD/Latch up performances
Fig 2.7 Pin diagram and Signal names

Fig 2.8 Logic Diagram



PREPAID ENERGY METER

www.BEProjectReport.com Page 19

2.2.3 DESCRIPTION
The 24C04 is a 4Kbit electrically erasable programmable memory (EEPROM), organized as
2 blocks of 256 x8 bits. They are manufactured in ST Microelectronics‟ Hi-Endurance Advanced
CMOS technology which guarantees an endurance of one million erase/write cycles with data
retention of 40 years. Both Plastic Dual-in-Line and Plastic Small Outline packages are available.
The memories are compatible with the I2C standard, two wire serial interface which uses a bi-
directional data bus and serial clock. The memories carry a built-in 4 bit, unique device identification
code (1010) corresponding to the I2C bus definition. This is used together with 2 chip enable inputs
(E2, E1) so that up to 4 x 4K devices may be attached to the I2C bus and selected individually. The
memories behave as a slave device in the I2C protocol with all memory operations synchronized by
the serial clock. Read and write operations are initiated by a START condition generated by the bus
master. The START condition is followed by a stream of 7 bits (identification code 1010), plus one
read/write bit and terminated by an acknowledge bit. When writing data to the memory it responds to
the 8 bits received by asserting an acknowledge bit during the 9th bit time. When data is read by the
bus master, it acknowledges the receipt of the data bytes in the same way. Data transfers are
terminated with a STOP condition.
Power on Reset: VCC locks out write protect.
In order to prevent data corruption and inadvertent write operations during power up, a Power on
Reset (POR) circuit is implemented. Until the VCC voltage has reached the POR threshold value, the
internal reset is active, all operations are disabled and the device will not respond to any command.
In the same way, when VCC drops down from the operating voltage to below the POR threshold
value, all operations are disabled and the device will not respond to any command. A stable VCC
must be applied before applying any logic signal.
SIGNAL DESCRIPTIONS
Serial Clock (SCL).
The SCL input pin is used to synchronize all data in and out of the memory. A resistor can be
connected from the SCL line to VCC to act as a pull up.
Serial Data (SDA).
The SDA pin is bi-directional and is used to transfer data in or out of the memory. It is an open drain
output that may be wire-OR‟ed with other open drain or open collector signals on the bus. A resistor
must be connected from the SDA bus line to VCC to act as pull up.
Chip Enable (E1 - E2).
These chip enable inputs are used to set the 2 least significant bits (b2, b3) of the 7 bit device select
code. These inputs may be driven dynamically or tied to VCC or VSS to establish the device select
code.
PREPAID ENERGY METER

www.BEProjectReport.com Page 20

Protect Enable (PRE).
The PRE input pin, in addition to the status of the Block Address Pointer bit (b2, location 1FFh as in
below figure), sets the PRE write protection active.

Fig: 2.9 Memory Protection
Mode (MODE).
The MODE input is available on pin 7 and may be driven dynamically. It must be at VIL or VIH for
the Byte Write mode, VIH for Multibyte Write mode or VIL for Page Write mode. When
unconnected, the MODE input is internally read as VIH (Multibyte Write mode).
Write Control (WC).
A hardware Write Control feature (WC) is offered only for ST24W04 and ST25W04 versions on pin
7. This feature is useful to protect the contents of the memory from any erroneous erase/write cycle.
The Write Control signal is used to enable (WC = VIH) or disable (WC =VIL) the internal write
protection. When unconnected, the WC input is internally read as VIL and the memory area is not
write protected.
2.3 RELAY
A relay is an electrically controllable switch widely used in industrial controls, automobiles and
appliances.
The relay allows the isolation of two separate sections of a system with two different voltage
sources i.e., a small amount of voltage current on one side can handle a large amount of voltage
current on the other side but there is no chance that these two voltages mix up.
PREPAID ENERGY METER

www.BEProjectReport.com Page 21

Inductor

Fig: 2.10 Circuit symbol of a relay
2.3.1 OPERATION
When a current flow through the coil, a magnetic field is created around the coil i.e., the coil is
energized. This causes the armature to be attracted to the coil. The armature‟s contact acts like a
switch and closes or opens the circuit. When the coil is not energized, a spring pulls the armature to
its normal state of open or closed. There are all types of relays for all kinds of applications.
Transistors and ICs must be protected from the brief high voltage 'spike' produced when the relay
coil is switched off. The above diagram shows how a signal diode (e.g. 1N4148) is connected across
the relay coil to provide this protection. The diode is connected 'backwards' so that it will normally
not conduct. Conduction occurs only when the relay coil is switched off, at this moment the current
tries to flow continuously through the coil and it is safely diverted through the diode. Without the
diode no current could flow and the coil would produce a damaging high voltage 'spike' in its attempt
to keep the current flowing.

Fig: 2.11 Relay Operation and use of protection diodes
In choosing a relay, the following characteristics need to be considered:
PREPAID ENERGY METER

www.BEProjectReport.com Page 22

1. The contacts can be normally open (NO) or normally closed (NC). In the NC type, the contacts are
closed when the coil is not energized. In the NO type, the contacts are closed when the coil is
energized.
2. There can be one or more contacts. i.e. different types like SPST (single pole single throw), SPDT
(single pole double throw) and DPDT (double pole double throw) relays.
3. The voltage and current required to energize the coil. The voltage can vary from a few volts to 50
volts, while the current can be from a few milliamps to 20milliamps. The relay has a minimum
voltage, below which the coil will not be energized. This minimum voltage is called the “pull-in”
voltage.
4. The minimum DC/AC voltage and current that can be handled by the contacts. This is in the range
of a few volts to hundreds of volts, while the current can be from a few amps to 40A or more,
depending on the relay.
An SPDT relay consists of five pins, two for the magnetic coil, one as the common terminal
and the last pins as normally connected pin and normally closed pin. When the current flows through
this coil, the coil gets energized. Initially when the coil is not energized, there will be a connection
between the common terminal and normally closed pin. But when the coil is energized, this
connection breaks and a new connection between the common terminal and normally open pin will
be established.
Thus when there is an input from the microcontroller to the relay, the relay will be switched
on. Thus when the relay is on, it can drive the loads connected between the common terminal and
normally open pin. Therefore, the relay takes 5V from the microcontroller and drives the loads which
consume high currents. Thus the relay acts as an isolation device.
Digital systems and microcontroller pins lack sufficient current to drive the relay. While the
relay‟s coil needs around 10milli amps to be energized, the microcontroller‟s pin can provide a
maximum of 1-2milli amps current. For this reason, a driver such as a power transistor is placed in
between the microcontroller and the relay.






PREPAID ENERGY METER

www.BEProjectReport.com Page 23

2.3.2 RELAY INTERFACING WITH THE MICROCONTROLLER:

Fig.2.12
The operation of this circuit is as follows:

The input to the base of the transistor is applied from the microcontroller port pin P1.0. The
transistor will be switched on when the base to emitter voltage is greater than 0.7V (cut-in voltage).
Thus when the voltage applied to the pin P1.0 is high i.e., P1.0=1 (>0.7V), the transistor will be
switched on and thus the relay will be ON and the load will be operated.

When the voltage at the pin P1.0 is low i.e., P1.0=0 (<0.7V) the transistor will be in off state
and the relay will be OFF. Thus the transistor acts like a current driver to operate the relay
accordingly.









DRIVER
CIRCUIT


RELAY
LOAD




AT 89C51




P1.0
PREPAID ENERGY METER

www.BEProjectReport.com Page 24

2.4 LIQUID CRYSTAL DISPLAY
LCD stands for Liquid Crystal Display. LCD is finding wide spread use replacing LEDs
(seven segment LEDs or other multi segment LEDs) because of the following reasons:
1. The declining prices of LCDs.
2. The ability to display numbers, characters and graphics. This is in contrast to LEDs, which
are limited to numbers and a few characters.
3. 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.
4. Ease of programming for characters and graphics.
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.



A model described here is for its low price and great possibilities most frequently used in
practice. It is based on the HD44780 microcontroller (Hitachi) and can display messages in two lines
with 16 characters each. It displays all the alphabets, Greek letters, punctuation marks, mathematical
symbols etc. In addition, it is possible to display symbols that user makes up on its own. Automatic
shifting message on display (shift left and right), appearance of the pointer, backlight etc. are
considered as useful characteristics.




PREPAID ENERGY METER

www.BEProjectReport.com Page 25

2.4.1 PINS FUNCTIONS
There are pins along one side of the small printed board used for connection to the
microcontroller. There are total of 14 pins marked with numbers (16 in case the background light is
built in). Their function is described in the table below:




























Function
Pin
Number
Name
Logic
State
Description
Ground 1 VSS - 0V
Power supply 2 Vdd - +5V
Contrast 3 Vee - 0 - Vdd
Control of
operating
4 RS
0
1
D0 – D7 are interpreted as
commands
D0 – D7 are interpreted as data
5 R/W
0
1
Write data (from controller to
LCD)
Read data (from LCD to
controller)
6 E
0
1
From 1 to
0
Access to LCD disabled
Normal operating
Data/commands are transferred to
LCD
Data / commands
7 D0 0/1 Bit 0 LSB
8 D1 0/1 Bit 1
9 D2 0/1 Bit 2
10 D3 0/1 Bit 3
11 D4 0/1 Bit 4
12 D5 0/1 Bit 5
13 D6 0/1 Bit 6
14 D7 0/1 Bit 7 MSB
PREPAID ENERGY METER

www.BEProjectReport.com Page 26

2.4.2 LCD SCREEN:
LCD screen consists of two lines with 16 characters each. Each character consists of 5X7 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).

2.4.3 LCD BASIC COMMANDS
All data transferred to LCD through outputs D0-D7 will be interpreted as commands or as
data, which depends on logic state on pin RS:
RS = 1 - Bits D0 - D7 are addresses of characters that should be displayed. Built in processor
addresses built in “map of characters” and displays corresponding symbols. Displaying position is
determined by DDRAM address. This address is either previously defined or the address of
previously transferred character is automatically incremented.

RS = 0 - Bits D0 - D7 are commands which determine display mode.
I/D 1 = Increment (by 1) R/L 1 = Shift right
0 = Decrement (by 1) 0 = Shift left
S 1 = Display shift on DL 1 = 8-bit interface
0 = Display shift off 0 = 4-bit interface
D 1 = Display on N 1 = Display in two lines
PREPAID ENERGY METER

www.BEProjectReport.com Page 27

0 = Display off 0 = Display in one line
U 1 = Cursor on F 1 = Character format 5x10 dots
0 = Cursor off 0 = Character format 5x7 dots
B 1 = Cursor blink on D/C 1 = Display shift
0 = Cursor blink off 0 = Cursor shift

2.4.3 LCD INTERFACING WITH THE MICROCONTROLLER:
To send commands we simply need to select the command register. Everything is same as we
have done in the initialization routine. But we will summarize the common steps and put them in a
single subroutine. Following are the steps:
 Move data to LCD port
 select command register
 select write operation
 send enable signal
 wait for LCD to process the command

PREPAID ENERGY METER

www.BEProjectReport.com Page 28


Fig. 2.13 Sending Commands to LCD









Vcc
Gnd
PRESET
(CONTRAST
CONTROL)
Vcc
FOR
BACKLIGHT
PURPOSE

P2.0
P2.1
P2.2






89C51 P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7







4 (RS) 1
5 (R/W) 2
6(EN) 3


LCD



D0
D1
D2
D3
D4
D5 15
D6 16
D7
Gnd
PREPAID ENERGY METER

www.BEProjectReport.com Page 29

2.5 ULN 2003 DRIVER
The relays are interfaced with microcontroller using their respective drivers which are used
for current amplification. The output current of µc (500mA) is amplified to 600mA at the output of
the drivers. Thus the driver for relay is ULN2003.
If a number of output devices are being controlled it may be necessary to use a number
of output transistors. In this case it will often be more convenient to use a ULN2003 Darlington
driver IC. This is simply a 16 pin „chip‟ that contains 7 Darlington transistors. The „chip‟ also
contains internal back emf suppression diodes and so no external 1N4001 diodes are required.

The ULN2001A, ULN2002A, ULN2003
and ULN2004A are high voltage, high current
Darlington arrays each containing seven open
collector Darlington pairs with common emitters.
Each channel is rated at 500mA and can withstand
peak currents of 600mA. They have series input
resistors selected for operation directly with 5v
TTL. These devices will handle numerous interface
needs- particularly those beyond the capabilities of
standard logic buffers.

They are standard Darlington arrays. The outputs are capable of sinking 500mA and
will withstand at least 50v in the OFF state. Outputs may be paralleled for higher load current
capability. These versatile devices are useful for driving a wide range of load including solenoids,
relays, DC motors, LED displays, filament lamps, and high power buffers. ULN2003 is supplied I 16
pin plastic DIP packages with a copper load frame to reduce thermal resistance.
If a number of output devices are being controlled it may be necessary to use a
number of output transistors. In this case it will often be more convenient to use a ULN2003
Darlington driver IC. This is simply a 16 pin „chip‟ that contains 7 Darlington transistors. The „chip‟
also contains internal back emf suppression diodes and so no external 1N4001 diodes are required.
PREPAID ENERGY METER

www.BEProjectReport.com Page 30


Fig – 2.15 ULN2003-IC with relay and MC


2.5.1 FEATURES OF ULN2003:
1. Dual In-line Plastic Package or Small-Outline IC Package.
2. Seven Darlington‟s per package.
3. Output current 500mA per Driver (800mA PEAK).
4. Output voltage 50v.
5. Internal Suppression diodes for inductive loads.
6. Outputs can be paralleled for higher current.
7. TTL/CMS/DTL compatible inputs.







PREPAID ENERGY METER

www.BEProjectReport.com Page 31

2.5.2 PIN DIAGRAM OF ULN2003:

Fig – 2.16 Pin diagram of ULN2003
2.5.3 BLOCK DIAGRAM OF ULN2003:


Fig – 2.17 Block diagram of ULN2003.
PREPAID ENERGY METER

www.BEProjectReport.com Page 32

The Darlington driver IC ULN2003 can be used to provide both the NOT and Darlington driver
circuits. It also contains the back emf suppression diodes so no external diodes are required. The
complete circuit is shown above.

Before programming, there is another pattern to notice in the stepping sequence. Look at
this table, which just shows coil 1 and coil 3.

Table 4.1 Truth Table
Notice the change from step 1 to step 2, just coil 3 changes. Then look at the next change - just
coil 1 change. In fact the two coils take it „in turns‟ to change from high to low and back again. This
high-low-high changing can be described as „toggling‟ state. This makes the programming very
simple by using the BASIC toggle command.





PREPAID ENERGY METER

www.BEProjectReport.com Page 33

2.6 POWER SUPPLY DESIGN
2.6.1 DESCRIPTION

Most digital logic circuits and processors need a 5 volt power supply. To use these parts we
need to build a regulated 5 volt source. Usually you start with an unregulated power supply ranging
from 9 volts to 24 volts DC. To make a 5 volt power supply, we use a LM7805 voltage regulator IC.

FIG-2.18 Voltage Regulator-LM7805
The LM7805 is simple to use. You simply connect the positive lead of your unregulated DC
power supply (anything from 9VDC to 24VDC) to the Input pin, connect the negative lead to the
Common pin and then when you turn on the power, you get a 5 volt supply from the Output pin.
2.6.2 BASIC POWER SUPPLY CIRCUIT
Below is the circuit of a basic unregulated dc power supply. A bridge rectifier D1 to D4
rectifies the ac from the transformer secondary, which may also be a block rectifier such as WO4 or
even four individual diodes such as 1N4004 types. (See later re rectifier ratings).
The principal advantage of a bridge rectifier is it does not need a centre tap on the secondary
of the transformer. A further but significant advantage is that the ripple frequency at the output is
twice the line frequency (i.e. 50 Hz or 60 Hz) and makes filtering somewhat easier.


PREPAID ENERGY METER

www.BEProjectReport.com Page 34

BLOCK DIAGRAM

FIG-2.19 Block Diagram of Power Supply

FIG-2.20 Circuit Diagram of Power Supply
For the positive half cycle of the input ac voltage, diodes D1 and D3 conduct, whereas diodes
D2 and D4 remain in the OFF state. The conducting diodes will be in series with the load resistance
R
L
and hence the load current flows through R
L.

For the negative half cycle of the input ac voltage, diodes D2 and D4 conduct whereas, D1
and D3 remain OFF. The conducting diodes D2 and D4 will be in series with the load resistance
PREPAID ENERGY METER

www.BEProjectReport.com Page 35

R
L
and hence the current flows through R
L
in the same direction as in the previous half cycle. Thus a
bi-directional wave is converted into a unidirectional wave.
FILTER:
Capacitive filter is used in this project. It removes the ripples from the output of rectifier and
smoothens the D.C. Output received from this filter is constant until the mains voltage and load is
maintained constant. However, if either of the two is varied, D.C. voltage received at this point
changes. Therefore a regulator is applied at the output stage.











PREPAID ENERGY METER

www.BEProjectReport.com Page 36

CHAPTER 3
SOFTWARE REQUIREMENTS

SOFTWARE TOOLS
Software‟s used in our project are
1. Kiel µvision
2. Pro load
4.1 KEIL SOFTWARE:
Keil compiler is software used where the machine language code is written and compiled.
After compilation, the machine source code is converted into hex code which is to be dumped into
the microcontroller for further processing. Keil compiler also supports C language code.
WHAT IS µVISION3?
µVision3 is an IDE (Integrated Development Environment) that helps you write, compile,
and debug embedded programs. It encapsulates the following components:
 A project manager.
 A make facility.
 Tool configuration.
 Editor.
 A powerful debugger.


PREPAID ENERGY METER

www.BEProjectReport.com Page 37

4.1.1 BUILDING AN APPLICATION IN µVISION
To build (compile, assemble, and link) an application in µVision2, you must:
1. Select Project - (for example, 166\EXAMPLES\HELLO\HELLO.UV2).
2. Select Project - Rebuild all target files or Build target.
µVision2 compiles, assembles, and links the files in your project.
Creating Your Own Application in µVision2
1. Select Project - New Project.
2. Select a directory and enter the name of the project file.
3. Select Project - Select Device and select an 8051, 251, or C16x/ST10 device from the Device
Database™.
4. Create source files to add to the project.
5. Select Project - Targets, Groups, and Files. Add/Files, select Source Group1, and add the
source files to the project.
6. Select Project - Options and set the tool options. Note when you select the target device from
the Device Database™ all special options are set automatically. You typically only need to
configure the memory map of your target hardware. Default memory model settings are
optimal for most applications.
7. Select Project - Rebuild all target files or Build target.

4.1.2 SOURCE CODE

1. Click on the Keil uVision Icon on Desktop
2. Click on the Project menu from the title bar
3. Then Click on New Project


Keil u Vision Window 1
PREPAID ENERGY METER

www.BEProjectReport.com Page 38

4. Save the Project by typing suitable project name with no extension in u r own folder


keil u vision Window 2
5. Then Click on Save button above.
6. Select the component for u r project. i.e. Atmel……
7. Select AT89C51 as shown below


Keil u Vision Window 3
8. Then Click on “OK”
9. Then Click either YES or NO………mostly “NO”
10. Now double click on the Target1, you would get another option
PREPAID ENERGY METER

www.BEProjectReport.com Page 39

11. Click on the file option from menu bar and select “new”.
12. Now start writing program in either in “C” or “ASM”
13. For a program written in Assembly, then save it with extension “. asm” and for “C”
based program save it with extension “ .C”

Keil u Vision Window 7
14. Now right click on Source group 1 and click on “Add files to Group Source”

Keil u Vision Window 8

15. Now you will get another window, on which by default “C” files will appear.
16. Now select as per your file extension given while saving the file
17. Click only one time on option “ADD”
PREPAID ENERGY METER

www.BEProjectReport.com Page 40

18. Now Press function key F7 to compile. Any error will appear if so happen.

Keil u Vision Window 9

19. If the file contains no error, then press Control+F5 simultaneously.
20. The new window is as follows



Keil u Vision Window 10

21. Then Click “OK”
22. Now click on the Peripherals from menu bar.
23. Drag the port a side and click in the program file.
24. Now keep Pressing function key “F11” slowly and observe.
25. You are running your program successfully
PREPAID ENERGY METER

www.BEProjectReport.com Page 41

4.2 PROLOAD:
Proload is software which accepts only hex files. Once the machine code is converted into
hex code, that hex code has to be dumped into the microcontroller placed in the programmer kit and
this is done by the Proload. Programmer kit contains a microcontroller on it other than the one which
is to be programmed. This microcontroller has a program in it written in such a way that it accepts
the hex file from the keil compiler and dumps this hex file into the microcontroller which is to be
programmed. As this programmer kit requires power supply to be operated, this power supply is
given from the power supply circuit designed above. It should be noted that this programmer kit
contains a power supply section in the board itself but in order to switch on that power supply, a
source is required. Thus this is accomplished from the power supply board with an output of 12volts
or from an adapter connected to 230 V AC.
1. Install the Proload Software in the PC.
2. Now connect the Programmer kit to the PC (CPU) through serial cable.
3. Power up the programmer kit from the ac supply through adapter.
4. Now place the microcontroller in the GIF socket provided in the programmer kit.
5. Click on the proload icon in the PC. A window appears providing the information like
Hardware model, com port, device type, Flash size etc. Click on browse option to select the
hex file to be dumped into the microcontroller and then click on “Auto program” to program
the microcontroller with that particular hex file.
6. The status of the microcontroller can be seen in the small status window in the bottom of the
page. After this process is completed, remove the microcontroller from the programmer kit
and place it in your system board. Now the system board behaves according to the program
written in the microcontroller.









PREPAID ENERGY METER

www.BEProjectReport.com Page 42

CHAPTER 4
WORKING PROCEDURE OF PROJECT

WORKING PROCEDURE:
The working of this project starts when the user tries to consume the power i.e. when he
switches on any of the electrical appliances in his house. When these electrical appliances are
switched on, they consume some power. The meter fixed outside the house will display the number
of consumed units.

The main concept of the project lies in buying the energy card from the electrical department,
inserting it into the energy card fixed in the house; consume it according to the number of units
available in the card. The product that we developed uses 89S52 Microcontroller to control all the
functions.

This project consists of two EEPROMs, one to store the no. of units consumed and second is to load
the units into EEPROM just as similar to recharge card. Initially the units in EEPROM are zero. The
system will be in OFF state until and unless the user recharges. Here the system is connected to a
lamp load. Now, as the power consumption increases the rate of pulses from the optocoupler output
of the energy meter increases and the microcontroller counts these pulses , when these pulses reaches
a specific number which depends on the meter constant of energy meter one unit is decremented
from the total units stored in EEPROM and these values are displayed on 16X2 LCD. In this process
at every instant the count value and units values are stored in EEPROM. Under POWER OFF or
RESET conditions these values are not lost and can be regained by the EEPROM and the count starts
from the updated value only. On decrementing each and every unit the system gives a beep sound
indicating that the unit value has been decremented. System will give a continuous beep sound as the
unit value reaches to zero. When the number of units becomes zero the relay operates and interrupts
supply.






PREPAID ENERGY METER

www.BEProjectReport.com Page 43

CIRCUIT DIAGRAM

PREPAID ENERGY METER

www.BEProjectReport.com Page 44

At this instant the user has to purchase another EEPROM based recharge card or the
user has to recharge by inserting the EEPROM based recharge card in the slots provided for the
recharge in Recharging unit. Here recharging means loading a new unit‟s value to the EEPROM
based recharge card. After recharge, the user has to place the EEPROM based recharge card in the
main system slots, if the recharge card is valid then a message is displayed as Recharge successful
and the system automatically turns ON. If it is Invalid then a message is displayed as Invalid card
and gives a continuous beep sound.
















PREPAID ENERGY METER

www.BEProjectReport.com Page 45

CHAPTER 5
CONCLUSION

CONCLUSION
In this project “Prepaid Energy Meter”, energy consumption calculation based on the
counting of pulses is designed and implemented using Atmel 89S52 MCU in embedded system
domain. An LCD is provided to display the number of units remaining, so controlled usage of energy
is possible and this system eliminates burden of electricity billing and saves money and time for
electricity department and consumers respectively.
Presence of every module has been reasoned out and placed carefully thus
contributing to the best working of the unit. Secondly, using highly advanced IC‟s and with the help
of growing technology the project has been successfully implemented.
This system can be replaced with the GSM Modems, by which tracking of the consumers
load on a timely basis is possible, which will help in tracking maximum demand, detect online theft
and more over instead of recharging the chip, the readily available recharge cards (smart cards) used
in cell phones can be introduced. Using these other mechanisms, consumers can recharge their
meters at their convenience and making the system much more user friendly.











PREPAID ENERGY METER

www.BEProjectReport.com Page 46

CHAPTER 6
SOURCE CODE


INCLUDE REG_51.PDF
RB0 EQU 000H ; Select Register Bank 0
RB1 EQU 008H ; Select Register Bank 1 ...poke to PSW to use

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
; PORT DECLERATION
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
SDA1 EQU P2.1 ;SDA=PIN5
SCL1 EQU P2.0 ;SCL=PIN6

WTCMD EQU 10100110B ;WRITE DATA COMMAND Note 3
RDCMD EQU 10100111B ;READ DATA COMMAND Note 3

WTCMD1 EQU 10100000B ;WRITE DATA COMMAND Note 3
RDCMD1 EQU 10100001B ;READ DATA COMMAND Note 3

RELAY EQU P2.7
BUZZER EQU P2.4
; ***LCD CONTROL***
LCD_RS EQU P0.0 ;LCD REGISTER SELECT LINE
LCD_E EQU P0.1 ;LCD ENABLE LINE
LCD_DB4 EQU P0.2 ;PORT 1 IS USED FOR DATA
LCD_DB5 EQU P0.3 ;USED FOR DATA
LCD_DB6 EQU P0.4 ;FOR DATA
LCD_DB7 EQU P0.5 ;FOR DATA
; ***CURSOR CONTROL INSTRUCTIONS***

OFFCUR EQU 0CH
BLINKCUR EQU 0DH

; ***DISPLAY CONTROL INSTRUCTIONS***

CLRDSP EQU 01H
ONDSP EQU 0CH

; ***SYSTEM INSTRUCTIONS***

CONFIG EQU 28H ; 4-BIT DATA,2 LINES,5X7 MATRIX LCD
ENTRYMODE EQU 6 ; INCREMENT CURSOR DON'T SHIFT DISPLAY

DSEG ; This is internal data memory
ORG 20H ; Bit adressable memory

FLAGS1: DS 1
BCDCARRY BIT FLAGS1.0
CARRY BIT FLAGS1.1
PREPAID ENERGY METER

www.BEProjectReport.com Page 47

TBIT BIT FLAGS1.2
TBIT1 BIT FLAGS1.3

READING: DS 2
AMOUNT: DS 3
COUNTER: DS 2
TEMP: DS 1
PRICE: DS 2
BALANCE: DS 1
BUZZ_COUNT: DS 1
READ_BYTE: DS 3
F1: DS 1
F2: DS 1
F3: DS 1

STACK: DS 1
CSEG ; Code begins here

; ---------==========----------==========---------=========---------
; Main routine. Program execution starts here.
; ---------==========----------==========---------=========---------
ORG 00H ; Reset
AJMP MAIN

ORG 0003H
PUSH PSW
PUSH ACC
MOV PSW,#RB1 ; Select register bank 0
CALL INC_COUNTER
POP ACC
POP PSW
RETI
; ---------==========----------==========---------=========---------

MAIN:
MOV SP,#50H
MOV PSW,#RB0 ; Select register bank 0
MOV IE,#10000001B
CALL RESETLCD4
CALL TITLE1
CLR BUZZER
SETB RELAY
CLR TBIT1
MOV BUZZ_COUNT,#00H

CALL READ_COUNTER
MOV A,COUNTER
CJNE A,#0FFH,BYPASS
CALL RESET_READING
CALL RESET_AMT
CALL RESET_COUNTER
CALL RESET_PRICE
CALL RESET_BALANCE ;RELAY ON/OFF BYTE
PREPAID ENERGY METER

www.BEProjectReport.com Page 48

CALL SYSTEM_RESET
CALL DELAYYS
BYPASS:

CALL READ_COUNTER
CALL READ_PRICE
CALL READ_BALANCE

MAINS: CALL TITLE1
CALL DELAYY

MOV A,BALANCE
CJNE A,#00H,FG1
CLR RELAY
CALL RECHAGRE
CALL DELAYY
SETB BUZZER
AJMP MAINS

FG1: SETB RELAY
MOV A,BUZZ_COUNT ;CHK TO SWITCH OFF THE BUZZER
CJNE A,#00H,AZX1
CLR BUZZER
AJMP AZX2
AZX1: DEC BUZZ_COUNT
AZX2:
MOV R1,#READING ;GET DATA IN
BYTES(RAM)
MOV R4,#05H ;DATA ADDRESS IN
EEPROM
MOV R6,#2 ;NUMBER OF BYTES
CALL READ_EEPROM
CALL DISP_READING
MOV TEMP,READING
CALL SEP_DISP
MOV TEMP,READING+1
CALL SEP_DISP

CALL DELAYY

MOV R1,#AMOUNT ;GET DATA IN
BYTES(RAM)
MOV R4,#0AH ;DATA ADDRESS IN
EEPROM
MOV R6,#3 ;NUMBER OF BYTES
CALL READ_EEPROM
CALL AMT_READING
MOV TEMP,AMOUNT
CALL SEP_DISP
MOV TEMP,AMOUNT+1
CALL SEP_DISP
MOV R4,#'.'
CALL WRLCDDATA
PREPAID ENERGY METER

www.BEProjectReport.com Page 49

CALL MDELAY
MOV TEMP,AMOUNT+2
CALL SEP_DISP


CALL DELAYY
MOV R1,#COUNTER ;GET DATA IN
BYTES(RAM)
MOV R4,#0EH ;DATA ADDRESS IN
EEPROM
MOV R6,#2 ;NUMBER OF BYTES
CALL READ_EEPROM
CALL COUNT_READING
; MOV TEMP,COUNTER
; CALL SEP_DISP
MOV TEMP,COUNTER+1
CALL SEP_DISP

CALL DELAYY
MOV R1,#PRICE ;GET DATA IN
BYTES(RAM)
MOV R4,#10H ;DATA ADDRESS IN
EEPROM
MOV R6,#2 ;NUMBER OF BYTES
CALL READ_EEPROM


CALL READ_PRICE
CALL UNIT_PRICE
MOV A,PRICE
ADD A,#30h
MOV R4,A
CALL WRLCDDATA
CALL MDELAY
MOV R4,#'.'
CALL WRLCDDATA
CALL MDELAY
MOV TEMP,PRICE+1
CALL SEP_DISP




CALL DELAYY
AJMP MAINS
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
; INCREMENT COUNTER BY 1
; IF COUNT=3200 THEN INCREMENT READING
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
INC_COUNTER:
MOV A,COUNTER+1
ADD A,#01
DA A
PREPAID ENERGY METER

www.BEProjectReport.com Page 50

MOV COUNTER+1,A
CJNE A,#01H,REPPA1
AJMP DCV2
REPPA1: CJNE A,#02H,REPPA2
AJMP DCV2
REPPA2: CJNE A,#03H,REPPA3
AJMP DCV2
REPPA3: CJNE A,#04H,REPPA4
AJMP DCV2
REPPA4: CJNE A,#05H,REPPA
AJMP DCV2
REPPA: MOV COUNTER,#00H
MOV COUNTER+1,#01H
MOV R1,#COUNTER ;store COUNT
MOV R4,#0EH ;Starting Address IN EEPROM
MOV R6,#2 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY
AJMP DVB1

DCV2: MOV R1,#COUNTER ;store COUNT
MOV R4,#0EH ;Starting Address IN EEPROM
MOV R6,#2 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY
RET

DVB1: MOV A,READING+1 ;INCREMENT READING BY 1
ADD A,#01
DA A
MOV READING+1,A
JNC DCS1
MOV A,READING
ADD A,#01
DA A
MOV READING,A
DCS1: MOV R1,#READING ;store READING
MOV R4,#05H ;Starting Address IN EEPROM
MOV R6,#2 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY

MOV A,AMOUNT+2 ;SUBTRACT AMT0 FROM
TOTAL0
CLR C
SUBB A,PRICE+1
CALL BCD_CONV
MOV AMOUNT+2,A
MOV A,AMOUNT+1 ;SUBTRACT AMT1 FROM
TOTAL1
SUBB A,PRICE
CALL BCD_CONV
MOV AMOUNT+1,A
PREPAID ENERGY METER

www.BEProjectReport.com Page 51

MOV A,AMOUNT ;SUBTRACT AMT2 FROM
TOTAL2
SUBB A,#00h
CALL BCD_CONV
MOV AMOUNT,A

MOV R1,#AMOUNT ;store AMOUNT
MOV R4,#0AH ;Starting Address IN EEPROM
MOV R6,#3 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY

MOV A,AMOUNT+1
CJNE A,#40H,FCX1
MOV BUZZ_COUNT,#02H
SETB BUZZER
FCX1: CJNE A,#38H,FAX1
MOV BUZZ_COUNT,#02H
SETB BUZZER
FAX1: CJNE A,#41H,FAAX1
MOV BUZZ_COUNT,#02H
SETB BUZZER
FAAX1: CJNE A,#20H,FCX2
MOV BUZZ_COUNT,#03H
SETB BUZZER
FCX2: CJNE A,#19H,FAX2
MOV BUZZ_COUNT,#03H
SETB BUZZER
FAX2: CJNE A,#21H,FAAX2
MOV BUZZ_COUNT,#03H
SETB BUZZER
FAAX2: CJNE A,#10H,FCX3
MOV BUZZ_COUNT,#04H
SETB BUZZER
FCX3: CJNE A,#11H,FCX4
MOV BUZZ_COUNT,#04H
SETB BUZZER
FCX4: CJNE A,#09H,FAX4
MOV BUZZ_COUNT,#04H
SETB BUZZER
FAX4:

MOV A,AMOUNT+2 ;SUBTRACT AMT0 FROM
TOTAL0
CLR C
SUBB A,PRICE+1
CALL BCD_CONV
MOV A,AMOUNT+1 ;SUBTRACT AMT1 FROM
TOTAL1
SUBB A,PRICE
MOV A,AMOUNT
CLR TBIT
JNC POP1
PREPAID ENERGY METER

www.BEProjectReport.com Page 52


SETB TBIT
POP1: CJNE A,#00H,BACK
JNB TBIT, BACK
MOV BALANCE,#00H
MOV R1,#BALANCE ;store COUNT
MOV R4,#15H ;Starting Address IN EEPROM
MOV R6,#1 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY
CLR RELAY
SETB BUZZER
BACK: RET
;&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
BCD_CONV:
CLR BCDCARRY
CLR CARRY
JNC LOP2
SETB CARRY
LOP2: JNB AC,LOP1
SETB BCDCARRY
CLR C
SUBB A,#06H
LOP1: JNB CARRY,LOP3
CLR C
SUBB A,#60H
LOP3: CLR C
JNB CARRY,LOP4
SETB C
LOP4: RET
;#################################################
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
; READ PULSE COUNTER FROM MEMORY
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

READ_BALANCE:
MOV R1,#BALANCE ;GET DATA IN
BYTES(RAM)
MOV R4,#15H ;DATA ADDRESS IN
EEPROM
MOV R6,#1 ;NUMBER OF BYTES
CALL READ_EEPROM
RET
READ_COUNTER:
MOV R1,#COUNTER ;GET DATA IN
BYTES(RAM)
MOV R4,#0EH ;DATA ADDRESS IN
EEPROM
MOV R6,#2 ;NUMBER OF BYTES
CALL READ_EEPROM
RET
READ_PRICE:
PREPAID ENERGY METER

www.BEProjectReport.com Page 53

MOV R1,#PRICE ;GET DATA IN
BYTES(RAM)
MOV R4,#10H ;DATA ADDRESS IN
EEPROM
MOV R6,#2 ;NUMBER OF BYTES
CALL READ_EEPROM
RET
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
SEP_DISP1:
MOV A,AMOUNT
ANL A,#0F0H
SWAP A
CJNE A,#00H,DAP1
MOV A,AMOUNT
ANL A,#0FH
AJMP DAP3

DAP1: ADD A,#30H ;BOTH NOT EQUAL TO ZERO
MOV R4,A
CALL WRLCDDATA
CALL MDELAY
DAP2: MOV A,AMOUNT
ANL A,#0FH
ADD A,#30H
MOV R4,A
CALL WRLCDDATA
CALL MDELAY
DAP4: MOV A,AMOUNT+1
ANL A,#0F0H
SWAP A
ADD A,#30H
MOV R4,A
CALL WRLCDDATA
CALL MDELAY
DAP5: MOV A,AMOUNT+1
ANL A,#0FH
ADD A,#30H
MOV R4,A
CALL WRLCDDATA
CALL MDELAY
MOV R4,#'.'
CALL WRLCDDATA
CALL MDELAY
MOV A,AMOUNT+2
ANL A,#0F0H
SWAP A
ADD A,#30H
MOV R4,A
CALL WRLCDDATA
CALL MDELAY
MOV A,AMOUNT+2
ANL A,#0FH
ADD A,#30H
PREPAID ENERGY METER

www.BEProjectReport.com Page 54

MOV R4,A
CALL WRLCDDATA
CALL MDELAY
RET

DAP3: CJNE A,#00H,DAP2 ;CHK 2 DIGIT
MOV A,AMOUNT+1
ANL A,#0F0H
SWAP A
CJNE A,#00H,DAP4 ;CHK 3 DIGIT
AJMP DAP5


SEP_DISP:
MOV A,TEMP
ANL A,#0F0H
SWAP A
ADD A,#30H
MOV R4,A
CALL WRLCDDATA
CALL MDELAY
MOV A,TEMP
ANL A,#0FH
ADD A,#30H
MOV R4,A
CALL WRLCDDATA
CALL MDELAY
RET
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
AMT_RECHARGE:
MOV READ_BYTE,#01H
MOV READ_BYTE+1,#00H
MOV READ_BYTE+2,#10H
MOV R1,#READ_BYTE ;store COUNT
MOV R6,#3 ;STORE 2 BYTES
MOV A,#WTCMD1 ;LOAD WRITE COMMAND
CALL OUTS ;SEND IT
MOV A,#20H ;GET LOW BYTE ADDRESS
CALL OUT ;SEND IT
BXLP: MOV A,@R1 ;GET DATA
CALL OUT ;SEND IT
INC R1 ;INCREMENT DATA POINTER
DJNZ R6,BXLP ;LOOP TILL DONE
CALL STOP ;SEND STOP CONDITION
CALL DELAY
RET
STORE_UNIT_PRICE:
MOV READ_BYTE,#00H
MOV READ_BYTE+1,#01H
MOV READ_BYTE+2,#00H
MOV R1,#READ_BYTE ;store COUNT
MOV R6,#3 ;STORE 2 BYTES
MOV A,#WTCMD1 ;LOAD WRITE COMMAND
PREPAID ENERGY METER

www.BEProjectReport.com Page 55

CALL OUTS ;SEND IT
MOV A,#20H ;GET LOW BYTE ADDRESS
CALL OUT ;SEND IT
BALP: MOV A,@R1 ;GET DATA
CALL OUT ;SEND IT
INC R1 ;INCREMENT DATA POINTER
DJNZ R6,BALP ;LOOP TILL DONE
CALL STOP ;SEND STOP CONDITION
CALL DELAY
RET

RESET_BALANCE:
MOV BALANCE,#0FFH
MOV R1,#BALANCE ;store COUNT
MOV R4,#15H ;Starting Address IN EEPROM
MOV R6,#1 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY
RET
RESET_PRICE:
MOV PRICE,#01H
MOV PRICE+1,#80H
MOV R1,#PRICE ;store COUNT
MOV R4,#10H ;Starting Address IN EEPROM
MOV R6,#2 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY
RET

RESET_COUNTER:
MOV COUNTER,#00H
MOV COUNTER+1,#03H
MOV R1,#COUNTER ;store COUNT
MOV R4,#0EH ;Starting Address IN EEPROM
MOV R6,#2 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY
RET

RESET_AMT:
MOV AMOUNT,#00H ;
MOV AMOUNT+1,#05H
MOV AMOUNT+2,#00H
MOV R1,#AMOUNT ;store READING
MOV R4,#0AH ;Starting Address IN EEPROM
MOV R6,#3 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY
RET
RESET_READING:
MOV READING,#00H
MOV READING+1,#13H
MOV R1,#READING ;store READING
PREPAID ENERGY METER

www.BEProjectReport.com Page 56

MOV R4,#05H ;Starting Address IN EEPROM
MOV R6,#2 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAY
RET
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DELAYY:
MOV F1,#0FH
SEP3: MOV F2,#0fFH
SEP2: MOV F3,#0FFH
SEP1: DJNZ F3,SEP1
DJNZ F2,SEP2
CALL CARD_READ
MOV A,READ_BYTE
CJNE A,#0FFH,DSP1
CLR TBIT1
DSP3A:DJNZ F1,SEP3
RET

DSP1: JB TBIT1,DSP3A
CALL TITLE3
CALL DELAYS
CALL DELAYS
CALL CARD_READ
MOV A,READ_BYTE
CJNE A,#00H,DSP2
CALL TITLE4 ; NEW UNIT PRICE
MOV PRICE,READ_BYTE+1
MOV PRICE+1,READ_BYTE+2
MOV R1,#PRICE ;store COUNT
MOV R4,#10H ;Starting Address IN EEPROM
MOV R6,#2 ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAYS
SETB TBIT1
AJMP RESETX_CHIP

DSP2: CJNE A,#01H,DSP3
CALL TITLE5 ; NEW RECHARGE
; MOV R1,#AMOUNT ;GET DATA IN BYTES(RAM)
; MOV R4,#0AH ;DATA ADDRESS IN EEPROM
; MOV R6,#03h ;NUMBER OF BYTES
; CALL READ_EEPROM

MOV A,AMOUNT
ADD A,READ_BYTE+1
DA A
MOV AMOUNT,A
MOV A,AMOUNT+1
ADDC A,READ_BYTE+2
DA A
MOV AMOUNT+1,A
MOV R1,#AMOUNT ;store READING
PREPAID ENERGY METER

www.BEProjectReport.com Page 57

MOV R4,#0AH ;Starting Address IN EEPROM
MOV R6,#03h ;STORE 2 BYTES
CALL STORE_EEPROM
CALL DELAYS
SETB TBIT1
CALL RESET_BALANCE
RESETX_CHIP:

MOV READ_BYTE,#0AAH ;ERASE AMOUNT
MOV READ_BYTE+1,#0FFH
MOV READ_BYTE+2,#0FFH
MOV R1,#READ_BYTE ;store COUNT
MOV R6,#3 ;STORE 2 BYTES
MOV A,#WTCMD1 ;LOAD WRITE COMMAND
CALL OUTS ;SEND IT
MOV A,#20H ;GET LOW BYTE ADDRESS
CALL OUT ;SEND IT
BBLP: MOV A,@R1 ;GET DATA
CALL OUT ;SEND IT
INC R1 ;INCREMENT DATA POINTER
DJNZ R6,BBLP ;LOOP TILL DONE
CALL STOP ;SEND STOP CONDITION
CALL DELAY
RET

DSP3: CJNE A,#0AAH,DSP4
CALL TITLE6 ; NEW RECHARGE
CALL DELAYS
SETB TBIT1
DSP4: RET
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DELAY:
MOV R6,#0FFH
RE1: MOV R7,#0FFH
RE: NOP
DJNZ R7,RE
DJNZ R6,RE1
RET
;**********************************************************
CARD_READ:
MOV R1,#READ_BYTE ;GET DATA IN
BYTES(RAM)
MOV R6,#3 ;NUMBER OF BYTES
MOV A,#WTCMD1 ;LOAD WRITE COMMAND TO SEND ADDRESS
CALL OUTS ;SEND IT
MOV A,#20H ;GET LOW BYTE ADDRESS
CALL OUT ;SEND IT
MOV A,#RDCMD1 ;LOAD READ COMMAND
CALL OUTS ;SEND IT
BXDLP: CALL IN ;READ DATA
MOV @R1,a ;STORE DATA
INC R1 ;INCREMENT DATA POINTER
DJNZ R6,AXLP ;DECREMENT LOOP COUNTER
PREPAID ENERGY METER

www.BEProjectReport.com Page 58

CALL STOP ;IF DONE, ISSUE STOP CONDITION
RET ;DONE, EXIT ROUTINE
AXLP: CLR SDA1 ;NOT DONE, ISSUE ACK
SETB SCL1
NOP ;NOTE 1
NOP
NOP
NOP ;NOTE 2
NOP
CLR SCL1
JMP BXDLP ;CONTINUE WITH READS
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
; READ DATA FROM EEPROM
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

READ_EEPROM:
MOV A,#WTCMD ;LOAD WRITE COMMAND TO SEND ADDRESS
CALL OUTS ;SEND IT
MOV A,R4 ;GET LOW BYTE ADDRESS
CALL OUT ;SEND IT
MOV A,#RDCMD ;LOAD READ COMMAND
CALL OUTS ;SEND IT
BRDLP: CALL IN ;READ DATA
MOV @R1,a ;STORE DATA
INC R1 ;INCREMENT DATA POINTER
DJNZ R6,AKLP ;DECREMENT LOOP COUNTER
CALL STOP ;IF DONE, ISSUE STOP CONDITION
RET ;DONE, EXIT ROUTINE
AKLP: CLR SDA1 ;NOT DONE, ISSUE ACK
SETB SCL1
NOP ;NOTE 1
NOP
NOP
NOP ;NOTE 2
NOP
CLR SCL1
JMP BRDLP ;CONTINUE WITH READS
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
; STORE DATA IN EEPROM
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

STORE_EEPROM:
MOV A,#WTCMD ;LOAD WRITE COMMAND
CALL OUTS ;SEND IT
MOV A,R4 ;GET LOW BYTE ADDRESS
CALL OUT ;SEND IT
BTLP: MOV A,@R1 ;GET DATA
CALL OUT ;SEND IT
INC R1 ;INCREMENT DATA POINTER
DJNZ R6,BTLP ;LOOP TILL DONE
CALL STOP ;SEND STOP CONDITION
RET
PREPAID ENERGY METER

www.BEProjectReport.com Page 59

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



;##########################################################
; DISPLAY ROUTINES
;##########################################################
TITLE1:
MOV DPTR,#MSAG1
CALL LCD_MSG
RET
MSAG1:
DB 1H,84H,'PREPAID',0C2H,'ENERGY METER',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DISP_READING:
MOV DPTR,#MSAG2
CALL LCD_MSG
RET
MSAG2:
DB 1H,82H,'METER READING',0C6H,00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AMT_READING:
MOV DPTR,#MSAG3
CALL LCD_MSG
RET
MSAG3:
DB 1H,81H,'BALANCE AMOUNT',0C3H,'Rs.',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
COUNT_READING:
MOV DPTR,#MSAG4
CALL LCD_MSG
RET
MSAG4:
DB 1H,82H,'PULSE COUNT',0C6H,00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
UNIT_PRICE:
MOV DPTR,#MSAG14
CALL LCD_MSG
RET
MSAG14:
DB 1H,83H,'UNIT PRICE',0C4H,'Rs ',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
RECHAGRE:
MOV DPTR,#MSAG5
CALL LCD_MSG
RET
MSAG5:
DB 1H,80H,'Please Recharge',0C2H,'your Account',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TITLE3:
MOV DPTR,#MSAG6
CALL LCD_MSG
RET
PREPAID ENERGY METER

www.BEProjectReport.com Page 60

MSAG6:
DB 1H,84H,'New Card',0C1H,'** DETECTED **',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TITLE4:
MOV DPTR,#MSAG7
CALL LCD_MSG
RET
MSAG7:
DB 1H,81H,'NEW UNIT PRICE',0C1H,'** STORED **',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TITLE5:
MOV DPTR,#MSAG8
CALL LCD_MSG
RET
MSAG8:
DB 1H,83H,'NEW AMOUNT',0C1H,'** RECHARGED **',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TITLE6:
MOV DPTR,#MSAG9
CALL LCD_MSG
RET
MSAG9:
DB 1H,82H,'INVALID CARD',0C0H,'****************',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SYSTEM_RESET:
MOV DPTR,#MSAG91
CALL LCD_MSG
RET
MSAG91:
DB 1H,80H,'System Restored',0C0H,'****************',00H
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;**********************************************************
; INITIALIZE THE LCD 4-BIT MODE
;**********************************************************
INITLCD4:
CLR LCD_RS ; LCD REGISTER SELECT LINE
CLR LCD_E ; ENABLE LINE
MOV R4, #CONFIG; FUNCTION SET - DATA BITS,
; LINES, FONTS
CALL WRLCDCOM4
MOV R4, #ONDSP ; DISPLAY ON
CALL WRLCDCOM4
MOV R4, #ENTRYMODE ; SET ENTRY MODE
CALL WRLCDCOM4 ; INCREMENT CURSOR RIGHT, NO SHIFT
MOV R4, #CLRDSP; CLEAR DISPLAY, HOME CURSOR
CALL WRLCDCOM4
RET
; **********************************************************
; SOFTWARE VERSION OF THE POWER ON RESET
; **********************************************************
RESETLCD4:
CLR LCD_RS ; LCD REGISTER SELECT LINE
CLR LCD_E ; ENABLE LINE
PREPAID ENERGY METER

www.BEProjectReport.com Page 61

CLR LCD_DB7 ; SET BIT PATTERN FOR...
CLR LCD_DB6 ; ... POWER-ON-RESET
SETB LCD_DB5
SETB LCD_DB4
SETB LCD_E ; START ENABLE PULSE
CLR LCD_E ; END ENABLE PULSE
MOV A, #4 ; DELAY 4 MILLISECONDS
CALL MDELAY
SETB LCD_E ; START ENABLE PULSE
CLR LCD_E ; END ENABLE PULSE
MOV A, #1 ; DELAY 1 MILLISECOND
CALL MDELAY
SETB LCD_E ; START ENABLE PULSE
CLR LCD_E ; END ENABLE PULSE
MOV A, #1 ; DELAY 1 MILLISECOND
CALL MDELAY
CLR LCD_DB4 ; SPECIFY 4-BIT OPERATION
SETB LCD_E ; START ENABLE PULSE
CLR LCD_E ; END ENABLE PULSE
MOV A, #1 ; DELAY 1 MILLISECOND
CALL MDELAY
MOV R4, #CONFIG; FUNCTION SET
CALL WRLCDCOM4
MOV R4, #08H ; DISPLAY OFF
CALL WRLCDCOM4
MOV R4, #1 ; CLEAR DISPLAY, HOME CURSOR
CALL WRLCDCOM4
MOV R4,#ENTRYMODE ; SET ENTRY MODE
ACALL WRLCDCOM4
JMP INITLCD4

; **********************************************************
; SUB RECEIVES A COMMAND WORD TO THE LCD
; COMMAND MUST BE PLACED IN R4 BY CALLING PROGRAM
; **********************************************************
WRLCDCOM4:
CLR LCD_E
CLR LCD_RS ; SELECT READ COMMAND
PUSH ACC ; SAVE ACCUMULATOR
MOV A, R4 ; PUT DATA BYTE IN ACC
MOV C, ACC.4 ; LOAD HIGH NIBBLE ON DATA BUS
MOV LCD_DB4, C ; ONE BIT AT A TIME USING...
MOV C, ACC.5 ; BIT MOVE OPERATOINS
MOV LCD_DB5, C
MOV C, ACC.6
MOV LCD_DB6, C
MOV C, ACC.7
MOV LCD_DB7, C
SETB LCD_E ; PULSE THE ENABLE LINE
CLR LCD_E
MOV C, ACC.0 ; SIMILARLY, LOAD LOW NIBBLE
MOV LCD_DB4, C
MOV C, ACC.1
PREPAID ENERGY METER

www.BEProjectReport.com Page 62

MOV LCD_DB5, C
MOV C, ACC.2
MOV LCD_DB6, C
MOV C, ACC.3
MOV LCD_DB7, C
CLR LCD_E
SETB LCD_E ; PULSE THE ENABLE LINE
CLR LCD_E
CALL MADELAY
POP ACC
RET
; **********************************************************
; SUB TO RECEIVE A DATA WORD TO THE LCD
; DATA MUST BE PLACED IN R4 BY CALLING PROGRAM
; **********************************************************
WRLCDDATA:
CLR LCD_E
SETB LCD_RS ; SELECT READ DATA
PUSH ACC ; SAVE ACCUMULATOR
MOV A, R4 ; PUT DATA BYTE IN ACC
MOV C, ACC.4 ; LOAD HIGH NIBBLE ON DATA BUS
MOV LCD_DB4, C ; ONE BIT AT A TIME USING...
MOV C, ACC.5 ; BIT MOVE OPERATOINS
MOV LCD_DB5, C
MOV C, ACC.6
MOV LCD_DB6, C
MOV C, ACC.7
MOV LCD_DB7, C
SETB LCD_E ; PULSE THE ENABLE LINE
CLR LCD_E
MOV C, ACC.0 ; SIMILARLY, LOAD LOW NIBBLE
MOV LCD_DB4, C
MOV C, ACC.1
MOV LCD_DB5, C
MOV C, ACC.2
MOV LCD_DB6, C
MOV C, ACC.3
MOV LCD_DB7, C
CLR LCD_E
SETB LCD_E ; PULSE THE ENABLE LINE
CLR LCD_E
NOP
NOP
POP ACC
RET

; **********************************************************
; SUB TAKES THE STRING IMMEDIATELY FOLLOWING THE CALL AND
; DISPLAYS ON THE LCD. STRING MUST BE TERMINATED WITH A
; NULL (0).
; **********************************************************
LCD_MSG:
CLR A ; Clear Index
PREPAID ENERGY METER

www.BEProjectReport.com Page 63

MOVC A,@A+DPTR ; Get byte pointed by Dptr
INC DPTR ; Point to the next byte
JZ LCD_Msg9 ; Return if found the zero (end of stringz)
CJNE A,#01H,Lcd_Msg1 ; Check if is a Clear Command
MOV R4,A
CALL WRLCDCOM4 ;If yes, RECEIVE it as command to LCD
JMP LCD_MSG ;Go get next byte from stringz

Lcd_Msg1: CJNE A,#0FFH,FLL ;Check for displaying full character
MOV R4,A
CALL WRLCDDATA
JMP LCD_MSG
FLL: CJNE A,#080h,$+3 ; Data or Address? If => 80h then is address.
JC Lcd_Msg_Data ; Carry will be set if A < 80h (Data)
MOV R4,A
CALL WRLCDCOM4 ; Carry not set if A=>80, it is address
JMP Lcd_Msg ; Go get next byte from stringz

Lcd_Msg_Data: ;
MOV R4,A
CALL WRLCDDATA ; It was data, RECEIVE it to Lcd
JMP Lcd_Msg ; Go get next byte from stringz
Lcd_Msg9:

RET ; Return to Caller

; **********************************************************
; 1 MILLISECOND DELAY ROUTINE
; **********************************************************

MDELAY:
PUSH ACC
MOV A,#0A6H
MD_OLP:
INC A
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
JNZ MD_OLP
NOP
POP ACC
RET
MADELAY:
PUSH ACC
MOV A,#036H
MAD_OLP:
INC A
NOP
PREPAID ENERGY METER

www.BEProjectReport.com Page 64

NOP
NOP
NOP
NOP
NOP
NOP
NOP
JNZ MAD_OLP
NOP
POP ACC
RET
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
DELAYS: ;One second delay routine
MOV R6, #00H ;put 0 in register R6 (R6 = 0)
MOV R5, #04H ;put 5 in register R5 (R5 = 4)
LOOPB:
INC R6 ;increase R6 by one (R6 = R6 +1)
ACALL DELAYMS ;call the routine above. It will run and return to
here.
MOV A, R6 ;move value in R6 to A
JNZ LOOPB ;if A is not 0, go to LOOPB
DEC R5 ;decrease R5 by one. (R5 = R5 -1)
MOV A, R5 ;move value in R5 to A
JNZ LOOPB ;if A is not 0 then go to LOOPB.
RET
;**************************************************************************
DELAYMS: ;millisecond delay routine
; ;
MOV R7,#00H ;put value of 0 in register R7
LOOPA:
INC R7 ;increase R7 by one (R7 = R7 +1)
MOV A,R7 ;move value in R7 to Accumlator (also known as A)
CJNE A,#0FFH,LOOPA ;compare A to FF hex (256). If not equal go to
LOOPA
RET ;return to the point that this routine was called
from
;**************************************************************************

;***********************************************************************
; THIS ROUTINE SENDS OUT CONTENTS OF THE ACCUMULATOR
; to the EEPROM and includes START condition. Refer to the data sheets
; for discussion of START and STOP conditions.
;***********************************************************************

OUTS: MOV R2,#8 ;LOOP COUNT -- EQUAL TO BIT COUNT
SETB SDA1 ;INSURE DATA IS HI
SETB SCL1 ;INSURE CLOCK IS HI
NOP ;NOTE 1
NOP
NOP
CLR SDA1 ;START CONDITION -- DATA = 0
NOP ;NOTE 1
NOP
PREPAID ENERGY METER

www.BEProjectReport.com Page 65

NOP
CLR SCL1 ;CLOCK = 0
OTSLP: RLC A ;SHIFT BIT
JNC BITLS
SETB SDA1 ;DATA = 1
JMP OTSL1 ;CONTINUE
BITLS: CLR SDA1 ;DATA = 0
OTSL1: SETB SCL1 ;CLOCK HI
NOP ;NOTE 1
NOP
NOP

CLR SCL1 ;CLOCK LOW
DJNZ R2,OTSLP ;DECREMENT COUNTER
SETB SDA1 ;TURN PIN INTO INPUT
NOP ;NOTE 1

SETB SCL1 ;CLOCK ACK
NOP ;NOTE 1
NOP
NOP

CLR SCL1
RET

;**********************************************************************
; THIS ROUTINE SENDS OUT CONTENTS OF ACCUMLATOR TO EEPROM
; without sending a START condition.
;**********************************************************************

OUT: MOV R2,#8 ;LOOP COUNT -- EQUAL TO BIT COUNT
OTLP: RLC A ;SHIFT BIT
JNC BITL
SETB SDA1 ;DATA = 1
JMP OTL1 ;CONTINUE
BITL: CLR SDA1 ;DATA = 0
OTL1: SETB SCL1 ;CLOCK HI
NOP ;NOTE 1
NOP
NOP

CLR SCL1 ;CLOCK LOW
DJNZ R2,OTLP ;DECREMENT COUNTER
SETB SDA1 ;TURN PIN INTO INPUT
NOP ;NOTE 1

SETB SCL1 ;CLOCK ACK
NOP ;NOTE 1
NOP
NOP

CLR SCL1
RET
PREPAID ENERGY METER

www.BEProjectReport.com Page 66



STOP: CLR SDA1 ;STOP CONDITION SET DATA LOW
NOP ;NOTE 1
NOP
NOP

SETB SCL1 ;SET CLOCK HI
NOP ;NOTE 1
NOP
NOP

SETB SDA1 ;SET DATA HIGH
RET
;*******************************************************************
; THIS ROUTINE READS A BYTE OF DATA FROM EEPROM
; From EEPROM current address pointer.
; Returns the data byte in R1
;*******************************************************************
CREAD: MOV A,#RDCMD ;LOAD READ COMMAND
CALL OUTS ;SEND IT
CALL IN ;READ DATA
MOV R1,A ;STORE DATA
CALL STOP ;SEND STOP CONDITION
RET

;**********************************************************************
; THIS ROUTINE READS IN A BYTE FROM THE EEPROM
; and stores it in the accumulator
;**********************************************************************

IN: MOV R2,#8 ;LOOP COUNT
SETB SDA1 ;SET DATA BIT HIGH FOR INPUT
INLP: CLR SCL1 ;CLOCK LOW
NOP ;NOTE 1
NOP
NOP
NOP

SETB SCL1 ;CLOCK HIGH
CLR C ;CLEAR CARRY
JNB SDA1,INL1 ;JUMP IF DATA = 0
CPL C ;SET CARRY IF DATA = 1
INL1: RLC A ;ROTATE DATA INTO ACCUMULATOR
DJNZ R2,INLP ;DECREMENT COUNTER
CLR SCL1 ;CLOCK LOW
RET

;*********************************************************************
; This routine test for WRITE DONE condition
; by testing for an ACK.
; This routine can be run as soon as a STOP condition
; has been generated after the last data byte has been sent
PREPAID ENERGY METER

www.BEProjectReport.com Page 67

; to the EEPROM. The routine loops until an ACK is received from
; the EEPROM. No ACK will be received until the EEPROM is done with
; the write operation.
;*********************************************************************
ACKTST: MOV A,#WTCMD ;LOAD WRITE COMMAND TO SEND ADDRESS
MOV R2,#8 ;LOOP COUNT -- EQUAL TO BIT COUNT
CLR SDA1 ;START CONDITION -- DATA = 0
NOP ;NOTE 1
NOP
NOP

CLR SCL1 ;CLOCK = 0
AKTLP: RLC A ;SHIFT BIT
JNC AKTLS
SETB SDA1 ;DATA = 1
JMP AKTL1 ;CONTINUE
AKTLS: CLR SDA1 ;DATA = 0
AKTL1: SETB SCL1 ;CLOCK HI
NOP ;NOTE 1
NOP
NOP

CLR SCL1 ;CLOCK LOW
DJNZ R2,AKTLP ;DECREMENT COUNTER
SETB SDA1 ;TURN PIN INTO INPUT
NOP ;NOTE 1

SETB SCL1 ;CLOCK ACK
NOP ;NOTE 1
NOP
NOP

JNB SDA1,EXIT ;EXIT IF ACK (WRITE DONE)
JMP ACKTST ;START OVER
EXIT: CLR SCL1 ;CLOCK LOW
CLR SDA1 ;DATA LOW
NOP ;NOTE 1
NOP
NOP

SETB SCL1 ;CLOCK HIGH
NOP
NOP
SETB SDA1 ;STOP CONDITION
RET
;*********************************************************************
END







PREPAID ENERGY METER

www.BEProjectReport.com Page 68

BIBLIOGRAPHY

 Mazidi.Mohammed Ali, Mazidi.Janice Gillespie, 1999. 8051 Microcontroller and Embedded
systems, second edition, Prentice Hall Publications.
 Loss.P.A.V, Lamego.M.M and Vieira.J.L.F, 1998. A single phase Microcontroller based
energy meter, IEEE Instrumentation and Measurements
 Embedded system by raj kamal
 Kenneth j. Ayala – “8051 microcontroller”.

WEBSITES:
 www.8051projects.com/projects/prepaidenergymeter
 www.datasheetcatalog.com/datasheets_pdf/A/T/8/9/AT89S52.shtml
 http://www.datasheetcatalog.org/datasheet/atmel/doc1919.pdf
 www.howstuffworks.com
 www.keil.com
 www.alldatasheets.com
 www.atmel.databook.com



Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close