Sie sind auf Seite 1von 53



To avoid railway accidents happening at unattended railway gates,

if implemented in spirit. This project utilizes two powerful IR transmitters and two receivers; one
pair of transmitter and receiver is fixed at up side (from where the train comes) at a level higher
than a human being in exact alignment and similarly the other pair is fixed at down side of the
train direction. Sensors are fixed at 1km on both sides of the gate. We call the sensor along the
train direction as ‘foreside sensor’ and the other as ‘aft side sensor’. When foreside receiver gets
activated, buzzer will on, red led on, the gate motor is turned on in one direction and the gate is
closed and stays closed until the train crosses the gate and reaches aft side sensors. When aft side
receiver gets activated motor turns in opposite direction and gate opens and motor stops.


In our country everyday we are hearing about the accidents in the level crossing. That may end
the human’s life. To prevent the accident we plan to go for the automatic railway gate controller
for the unmanned level crossing. The train, which is coming in either way, will be sensed in two
ways, one is by sensing the vibration and the one is by using the infrared ways. The best method
is by sensing the vibration. But we are going to develop the model by using the toy trains. IR
sensor is suitable for these types of trains.

We are planning to go for two pairs of IR sensors, one is at the one end of the gate on
track, and the other one is other side. This system is to install the sensor at least 0.5 KM from the
gate. When a train crossed the IR sensor, it will be sensed and given to the microcontroller, then
the microcontroller, which we are going to use, is 89c51 it is advanced version of 8051.


This is an era of automation where it is broadly defined as replacement of manual effort

by Electronics in all degrees of automation. The operation remains an essential part of the system
although with changing demands on physical input as the degree of mechanization is increased.

Degrees of automation are of two types, viz.

Full automation.
Semi automation.
In semi automation a combination of manual effort and mechanical power is required
whereas in full automation human participation is very negligible. In our project is fully
automation one.

In our country everyday we are hearing about the accidents in the level crossing. That may end
the human’s life. To prevent the accident we plan to go for the automatic railway gate controller
for the unmanned level crossing. The train, which is coming in either way, will be sensed in two
ways, one is by sensing the vibration and the one is by using the infrared ways. The best method
is by sensing the vibration. But we are going to develop the model by using the toy trains. IR
sensor is suitable for these types of trains.

We are planning to go for two pairs of IR sensors, one is at the one end of the gate on
track, and the other one is other side. This system is to install the sensor at least 1 KM from the
gate. When a train crossed the IR sensor, it will be sensed and given to the microcontroller, then
the microcontroller, which we are going to use, is 89c52 it is advanced version of 8052.


Automation can be achieved through computers, hydraulics, pneumatics, robotics, etc., of

these sources, form an attractive medium for low cost automation. The main advantages of all
automation systems are economy, accuracy and simplicity. Automation plays an important role
in all industries.

 To achieve Quick Response

 To reduce man power


 To increase the system efficiency

 To reduce the work load
 To achieve grater accuracy
 To reduce the time of operation
 To reduce the fatigue of workers
 To achieve good system quality
 Less Maintenance







IR1 and IR2 are used to know the entry and exit aspects of the trains. Here, in this concept entry
and exit of the train can be realized by using only two IRS. If train enters, automatically buzzer
will on, after 5 seconds, led will on, after 5 more seconds, motor will on to close the gate.
Whenever train leaves, gate will open.


Microcontroller is a general purpose device, which integrates a number of the

components of a microprocessor system on to single chip. It has inbuilt CPU, memory and
peripherals to make it as a mini computer. A microcontroller combines on to the same microchip:

 The CPU core

 Memory(both ROM and RAM)
 Some parallel digital i/o
Microcontrollers will combine other devices such as:

 A timer module to allow the microcontroller to perform tasks for certain time periods.
 A serial I/O port to allow data to flow between the controller and other devices such
as a PIC or another microcontroller.
 An ADC to allow the microcontroller to accept analogue input data for processing.
Microcontrollers are:

 Smaller in size
 Consumes less power
 Inexpensive
Micro controller is a stand alone unit, which can perform functions on its own without
any requirement for additional hardware like I/O ports and external memory.

The heart of the microcontroller is the CPU core. In the past, this has traditionally been based on
a 8-bit microprocessor unit. For example Motorola uses a basic 6800 microprocessor core in
their 6805/6808 microcontroller devices.

In the recent years, microcontrollers have been developed around specifically designed
CPU cores, for example the microchip PIC range of microcontrollers.

AT89S52 is the 40 pins, 8 bit Microcontroller manufactured by Atmel group. It is the

flash type reprogrammable memory. Advantage of this flash memory is we can erase the
program with in few minutes. It has 4kb on chip ROM and 128 bytes internal RAM and 32 I/O
pin as arranged as port 0 to port 3 each has 8 bit bin .Port 0 contain 8 data line(D0-D7) as well
as low order address line(AO-A7).

Port 2 contain higher order address line (A8-A15). Port 3 contains special purpose
register such as serial input receiver register SBUF, interrupt INT0,INT1 and timers T0 , T1
many of the pins have multi functions which can be used as general purpose I/O pins (or) Special
purpose function can be decided by the programmer itself.

• 8K Bytes of In-System Reprogrammable Flash Memory

Endurance: 1,000 Write/Erase Cycles

• Fully Static Operation: 0 Hz to 24 MHz

• Three-Level Program Memory Lock

• 256 x 8-Bit Internal RAM

• 32 Programmable I/O Lines

• Two 16-Bit Timer/Counters

• Six Interrupt Sources

• Programmable Serial Channel

• Low Power Idle and Power Down Modes

The AT89S52 is a low-power, high-performance CMOS 8-bit microcomputer with 4K

bytes of Flash Programmable and Erasable Read Only Memory (PEROM). The device is
manufactured using Atmel is high density nonvolatile memory technology and is compatible
with the industry standard MCS-51™ instruction set and pinout.

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 Flash
on a monolithic chip, the Atmel AT89S52 is a powerful microcomputer which provides a highly
flexible and cost effective solution to many embedded control applications.


The 8052 consist of:

 Eight-bit CPU with registers A (the accumulator) and B

 Program counter (PC)
 Data pointer (DPTR)
 Flags and the Program Status Word (PSW)
 Eight-bit stack pointer (SP)
 Internal ROM or EPROM or FLASH ROM
 Internal RAM of 256bytes(128bytes general purpose):
 Four register banks, each containing eight registers
 Two 16-bit timer / counter: T0 and T1
 Full duplex serial data receiver/transmitter; SBUF
 Interrupts
 Oscillator and clock circuits
 Thirty-two input/output pins arranged as four 8-bit ports P0-P3


1 40

2 39

3 38

4 37

5 36

6 35

7 34

8 33

9 32

10 31

11 30

12 29

13 28

14 27

15 26

16 25

17 24

18 23
19 22

20 21


The 8052 contain 34 general-purpose, or working, registers. Two of these,

registers A and B hold results of many instructions, particularly for arithmetical and logical
operations. The other 32 are arranged as part of internal RAM in four banks, Bank0-Bank3, of
eight registers each.


The 8052 contain two 16-bit registers: the program counter (PC) and the data pointer
(DPTR). Each is used to hold the address of a word in memory. Program instruction bytes are
fetched from locations in memory that are addressed by the PC. Program ROM may be on the
chip at addresses 000h to FFFh, external to the chip for address that exceed FFFh, or totally
external for all address from 0000h to FFFFh. The PC is automatically incremented after every
instruction byte is fetched and may also be altered by certain instructions. The PC is the only
register that does not have an internal address.

The Program Counter (PC) is a 2-byte address that tells the 8052 where the next
instruction to execute is found in memory. When the 8052 is initialized PC always starts at
0000h and is incremented each time an instruction is executed. It is important to note that PC
isn’t always incremented by one. Since some instructions require 2 or 3 bytes the PC will be
incremented by 2 or 3 in these cases. There is no way to directly modify it’s value. The Program
Counter is special in that. That is to say, you can’t do something like PC=2430h. On the other
hand, if you execute LJMP 2340h you’ve effectively accomplished the same thing. It is also
interesting to note that while you may change the value of PC (by executing a jump instruction,
etc.) there is no way to read the value of PC. That is to say, there is no way to ask the 8052
"What address are you about to org 00h


The DPTR register is made up of two 8-bit registers, named DPH and DPL, which are
used to furnish memory addresses for internal and external code access and external data access.
The DPTR is under the control of program instructions name, DPH and DPL. DPTR does not
have a single internal address; DPH and DPL are each assigned an address. The Data

Pointer (DPTR) is the 8052’s only user-accessible 16-bit (2-byte) register. DPTR, as the name
suggests, is used to point to address something like HL register pair in 8085 microprocessor. It is
used by a number of commands that allow the 8052 to access external memory and internal
memory. While DPTR is most often used to point to data in external memory, many
programmers often take advantage of the fact that it’s the only true 16-bit register available. It is
often used to store 2-byte values that have nothing to do with memory locations.

5. PROGRAM STATUS WORD (PSW) Flags are 1-bit registers provided to store the
results of certain program instructions. Other instructions can test the condition of the flags and
make decisions based on the flag states. In order that the flags may be conveniently addressed,
they are grouped inside the program status word (PSW) and the power control (PCON) registers.
The 8052 have four math flags that respond automatically to the outcomes of math operations
and three general-purpose user flags that can be set to 1 or cleared to 0 by the programmer as
desired. The math flags include Carry (CY), Auxiliary Carry (AC), Overflow (OV), and Parity
(P). User flag is named F0; this general-purpose flags that may be used by the programmer to
record some event in the program. Register bank selection may be done by the use of RS0
and RS1 Note that all of the flags can be set and cleared by the programmer at will. The math
flags, however, are also affected by math operations.

CY AC F0 RS1 RS0 OV -- P

CY PSW.7 Carry flag

AC PSW.6 Auxiliary carry


F0 PSW.5 Flag 0 (User


RS1 PSW.4 Register bank


selector bit1

RS0 PSW.3 Register bank

selector bit0

OV PSW.2 Overflow flag

-- PSW.1 Reserved for

future use

P PSW.0 Parity flag


RS1 RS0 Register Bank Address

0 0 0 00H-07H

0 1 1 08H-0FH

1 0 2 10H-17H

1 1 3 18H-1FH
program status word is shown above. The PSW contains the math flags, user program flag F0,
and the register select bits RS0, RS1 that identify which of the four general-purpose register
banks is currently in use by the program.


The stack refers to an area of internal RAM that is used in conjunction with certain
opcodes to store and retrieve data quickly. The 8-bit Stack Pointer (SP) register is used by the
8052 to hold an internal RAM addresses that is called the top of the stack. The address held in

the SP register is the location in internal RAM where the last byte of data was stored by a stack
operation. When data is to be placed on the stack, the SP increments before storing data on the
stack so that the stack grows up as data is stored. As data is retrieved from the stack, the byte is
read from the stack, and then the SP decrements to point to the next available byte of stored data.


Action Action

Store Get
SP = 0A Address 0A SP = 0A

data data

SP = 09 Address 09 SP = 09
Store Get

data data

Address 07
SP = 08 SP = 08
Store Get

data data

SP = 07 Address 08 SP = 07

Storing data on the stack internal RAM Getting Data from the stack
(Increment then store) (get then decrement)

Operation of the stack and the SP is shown above. The SP is set to 07h when the 8052 is reset
and can be changed to any internal RAM address by the programmer, I.e., default address
location of stack pointer is 07h. Using a data move command we can change the stack pointer
address. The stack is limited in height to the size of the internal RAM. The stack has the potential
(if the programmer is not careful to limit its growth) to over write valuable data in the register
banks, bit-addressable RAM, and general purpose (scratchpad) RAM areas. The programmer is
responsible for making sure the stack does not grow beyond predefined bounds!. The stack is
normally placed high in internal RAM, by an appropriate choice of the number placed in the SP
register, to avoid conflict with the register, bit and scratch-pad internal RAM areas. The Stack
Pointer, like all registers except DPTR and PC, may hold an 8-bit (1-byte) value. The Stack
Pointer is used to indicate where the next value to be removed from the stack should be taken
from. When you push a value onto the stack, the 8052 first increments the value of SP and then
stores the value at the resulting memory location. When you pop a value off the stack, the 8052
returns the value from the memory location indicated by SP, and then decrements the value of
SP. That is last in first out method (LIFO) This order of operation is important. When the 8052
is initialized SP will be initialized to 07h. If you immediately push a value onto the stack, the
value will be stored in Internal RAM address 08h. This makes sense taking into account what
were mentioned two paragraphs above: First the 8052 will increment the value of SP (from 07h
to 08h) and then will store the pushed value at that memory address (08h).

SP is modified directly by the 8052 by six instructions: PUSH, POP, ACALL, LCALL,
RET, and RETI. It is also used intrinsically whenever an interrupt is triggered. (More on
interrupts later).


A functioning computer must have memory for program codes, commonly in ROM, and
RAM memory for variable data that can be altered as the program runs. The 8052 has
internal RAM and ROM memory for these functions. Additional memory can be added
externally using suitable circuits.

Unlike Microcontrollers with Von Neumann architectures, which can use a single memory
address for either program code or data, but not for both, the 8052 has a Harvard architecture,

which uses the same address, in different memories, for code and data. Internal circuitry accesses
the correct memory based on the nature of the operation in progress. Harvard architecture uses
separate buses to fetch the data and the address

The 8052 is organized so that data memory and program code memory can be in two
entirely different physical memory entities. Each has the same address range. The structure of
the internal RAM will be discussed later. Generally 8052 microcontroller is available with 4Kilo
Bytes internal ROM. A corresponding block of internal program code, contained in an internal
ROM, occupies code address space 000h to FFFh. The Program Counter is ordinarily used to
address program code bytes from address 0000h to FFFFh. Program addresses higher than
0FFFh, which exceed the internal ROM capacity, will cause the 8052 to automatically fetch code
bytes from external program memory. Code bytes can also be fetched exclusively from an
external memory, address 0000h to FFFFh, by connecting the external access pin (EA pin no 31
on the DIP) to ground. The PC does not care where the code is, the circuit designer decides
whether the code is found totally in internal ROM, totally in external ROM, or in a combination
of internal and external ROM.

Each register bank address are

Bank 0 00h-07h

Bank 1 08h-0fh

Bank 2 10h-17h

Bank 3 18h-1fh


8052 microcontroller has 16-bit address bus and 8 bit data bus,
. with 16-bit address bus we can
address maximum of 64Kilobytes of external memory that is from 0000h to FFFF.
051 is available with 4kilobytes of internal ROM its derivatives 8751, 8951 are available with
EPROM, FLASH ROM respectively with 4kilobytes capacity.





As mentioned at the beginning of this chapter, the 8052 includes a certain amount of on-
chip memory. On-chip memory is really one of two types: Internal RAM and Special Function
Register (SFR) memory. The layout of the 8052's internal RAM is presented. As is illustrated in
this map, the 8052 has a bank of 128 bytes of Internal RAM. This Internal RAM is found on-chip
on the 8052 so it is the fastest RAM available, and it is also the most flexible in terms of reading,
writing, and modifying its contents. Internal RAM is volatile, so when the 8052 is switched off
this memory is cleared. The 128 bytes of internal ram is subdivided as shown on the memory
map. The first 8 bytes (00h - 07h) are "register bank 0". By manipulating certain SFRs, a
program may choose to use register banks 1, 2, or 3. These alternative register banks are located
in internal RAM in addresses 08h through 1Fh. So the registers are part of internal RAM. Bit
Memory also lives and is part of internal RAM. Bit memory actually resides in internal RAM,
from addresses 20h through 2Fh. It can be bit addressed from 00h to 7fh (totally 128 bits) The
80 bytes remaining of Internal RAM, from addresses 30h through 7Fh, may be used by user
variables that need to be accessed frequently or at high-speed. This area is also utilized by the
microcontroller as a storage area for the operating stack.


1F R7 7F
Bank 3 R4
17 R7
Bank 2 R4
0F R7 2F 7F 78
R6 F
77 70
R5 6F 68
R4 67 60
Bank 1
R3 5F 58
R2 57 50
R1 4F 48
08 R0 47 40
07 R7 3F 38
R6 37 30
R5 2F 28
Bank 0 R4 27 20
R3 1F 18
R2 17 10
R1 0F 08
00 R0 20 30
07 00

The 80 bytes remaining of Internal RAM, from addresses 30h through 7Fh, may be used
by user variables that need to be accessed frequently or at high-speed. This area is also utilized
by the microcontroller as a storage area for the operating stack. This fact severely limits the
8052’s stack since, as illustrated in the memory map, the area reserved for the stack is only 80
bytes and usually it is less since these 80 bytes has to be shared between the stack and user


The 8052 use 8 "R" registers, which are used in many of its instructions. These "R"
registers are numbered from 0 through 7 (R0, R1, R2, R3, R4, R5, R6, and R7). These registers
are generally used to assist in manipulating values and moving data from one memory location to
another. For example, to add the value of R4 to the Accumulator, we would execute the
following instruction:


Thus if the Accumulator (A) contained the value 3 and R4 contained the value 3, the
Accumulator would contain the value 6 after this instruction was executed.

However, as the memory map shows, the "R" Register R4 is really part of Internal RAM.
Specifically, R4 is address 04h. Thus the above instruction accomplishes the same thing as the
following operation:

ADD A, 04h

This instruction adds the value found in Internal RAM address 04h to the value of the
Accumulator, leaving the result in the Accumulator. Since R4 is really Internal RAM 04h, the
above instruction effectively accomplished the same thing.

But watch out! As the memory map shows, the 8052 has four distinct register banks. When the
8052 is first booted up, register bank 0 (addresses 00h through 07h) is used by default. However,
your program may instruct the 8052 to use one of the alternate register banks; i.e., register banks

1, 2, or 3. In this case, R4 will no longer be the same as Internal RAM address 04h. For example,
if your program instructs the 8052 to use register bank 3, "R" register R4 will now be
synonymous with Internal RAM address 1Ch. The concept of register banks adds a great level of
flexibility to the 8052, especially when dealing with interrupts (we'll talk about interrupts later).
However, always remember that the register banks really reside in the first 32 bytes of Internal
RAM. Register banks can be selected with the help of RS0, RS1 bits in the program status word
(PSW). If you only use the first register bank (i.e. bank 0), you may use Internal RAM locations
08h through 1Fh for your own use. But if you plan to use register banks 1, 2, or 3, be very
careful about using addresses below 20h as you may end up overwriting the value of your "R"


All four ports in the 8052 are bi-directional each contains a latch, an output driver and
input buffer. The output drivers of port0 and 2, and the input buffers of port 0 are used in access
to external memory. In this application port 0 is used as a lower byte of the external memory
address multiplexed with data bus and port 2 is used as a higher byte of the external memory
address when address is sixteen bit wide. Otherwise it can be used as general purpose I/O

P0 (Port 0, SFR Address 80h, Bit-Addressable): This is input/output port 0. Each bit of this
SFR corresponds to one of the pins on the microcontroller. For example, bit 0 of port 0 is pin
P0.0 i.e. pin number 39 in microcontroller bit 7 is pin P0.7 i.e., pin number 32 in the IC. Writing
a value of ‘1’ to a bit of this SFR will send a high level on the corresponding I/O pin whereas a
value of ’0’ will bring it to a low level. While the 8052 has four I/O port (P0, P1, P2, and P3), if
your hardware uses external RAM or external code memory (i.e., your program is stored in an
external ROM or EPROM chip or if you are using external RAM chips) or if your hardware
consists of any peripheral interface like ADC or DAC, you may not use P0 and P2). This is
because the 8052 use ports P0 and P2 to address the external memory. Thus if you are using
external RAM or code memory or peripheral interface you may only use ports P1 and P3 for
your own use.

P1 (Port 1, Address 90h, Bit-Addressable): This is input/output port 1. Each bit of this SFR
corresponds to one of the pins on the microcontroller. For example, bit 0 of port 1 is pin P1.0 i.e.,

pin number 1 bit 7 is pin P1.7 i.e., pin number 8. Writing a value of 1 to a bit of this SFR will
send a high level on the corresponding I/O pin whereas a value of 0 will bring it to a low level.

P2 (Port 2, Address A0h, Bit-Addressable): This is input/output port 2. Each bit of this SFR
corresponds to one of the pins on the Microcontroller. For example, bit 0 of port 2 is pin P2.0
i.e., pin number 21, bit 7 is pin P2.7 i.e., pin number 28. Writing a value of 1 to a bit of this SFR
will send a high level on the corresponding I/O pin whereas a value of 0 will being it to a low

While the 8052 has four I/O port (P0, P1, P2 and P3), if your hardware uses external RAM or
external code memory (i.e., your program is stored in an external ROM or EPROM chip or if you
are using external RAM chips) you may not use P0 or P2. This is because the 8052 uses ports P0
and P2 to address the external memory. Thus if you are using external RAM or code memory
you may only use ports P1 and P3 for your own use.

P3 (Port 3, Address B0h, Bit – Addressable): This is input/output port 3. Each bit of this SFR
corresponds to one of the pins on the microcontroller. For example, bit 0 of port 3 is pin P3.0 i.e.,
pin number 10, bit 7 is pin P3.7 i.e., pin number 17. Writing a value of 1 to a bit of this SFR will
send a high level on the corresponding I/O pin whereas a value of 0 will bring it to a low level.

While the 8052 has four I/O port (P0, P1, P2 and P3), if your hardware uses external RAM or
external code memory (i.e., your program is stored in an external ROM or EPROM chip or if you
are using external RAM chips) you may not use P0 or P2 and p3.6, p3.7. This is because the
8052 uses ports P0 and P2 to address the external memory and P3.6 and P3.7 for write and read
operations. Thus if you are using external RAM or code memory you may only use ports P1 and
P3 for your own use.

Port 3 can also used for timer/counter, interrupts, serial ports also. P3.0 is also used for serial
data reception, P3.1 is for serial data transmission, P3.2 &

P3.3 is for external interrupts, P3.4 & P3.5 is for timer0 and timer1 respectively. P3.6&P3.7 for
write and read operations respectively.


Many microcontroller applications require the counting of external events, such as the
frequency of a pulse train, or the generation or precise internal time delays between computer
actions. Both of these tasks can be accomplished using software techniques, but software loops
for counting or timing keep the processor occupied so that other, perhaps more important,
functions are not done. To relieve the processor of this burden, two 16-bit up counters, named T0
and T1, are provided for the general use of the programmer. Each counter may be programmed
to count internal clock pulses, acting as a timer, or programmed to count external pulses as a

The counters are divided into two-8 bit registers called the timer low (TL0, TL1) and high (TH0,
TH1) bytes. All counter action is controlled by bit states in the timer mode control register
(TMOD), the timer/counter control register (TCON), and certain program instructions.


Computers must be able to communicate with other computers in modern multiprocessor

distributed systems. One cost-effective way to communication is to send and receive data bits
serially. The 8052 has a serial data communication circuit that uses register SBUF to hold data.
Register SCON controls data communication, register PCON controls data rates, and pins RXD
(P3.0) and TXD (P3.1) connect to the serial data network.

SBUF is physically two registers. One is write only and is used to hold data to be transmitted out
of the 8052 via TXD. The other is read only and holds received data from external sources via
RXD. Both mutually exclusive registers use address 99h.

There are four programmable modes for serial data communication that are chosen by setting the
SMX bits in SCON. Baud rates are determined by the mode chose. We will discuss about serial
port later.


An interrupt is a special feature, which allows the 8052 to provide the illusion of "multi-
tasking," although in reality the 8052 is only doing one thing at a time. The word "interrupt" can
often be substituted with the word "event." An interrupt is triggered whenever a corresponding
event occurs. When the event occurs, the 8052 temporarily puts "on hold" the normal execution
of the program and executes a special section of code referred to as an interrupt handler. The
interrupt handler performs whatever special functions are required to handle the event and then
returns control to the 8052 at which point program execution continues as if it had never been
interrupted. The topic of interrupts is somewhat tricky and very important. For that reason, an
entire chapter will be dedicated to the topic. For now, suffice it to say that Interrupts can cause
program flow to change. A computer program has only two ways to determine the conditions
that exist in internal external circuits. One method uses software instructions that jump to
subroutines on the states of flags and port pins. The second method responds to hardware signals,
called interrupts that force the program to call a subroutine. Software techniques use up
processor time that could be devoted to other tasks; interrupts take processor time only when
action by the program is needed. Most applications of microcontrollers involve responding to
events quickly enough to control the environment that generates the events (generically termed
real-time programming).

Interrupts are often the only way in which real-time programming can be done
successfully. Interrupts may be generated by internal chip operations or provided by external
sources. Any interrupt can cause the 8052 to perform a hardware call to an interrupt, handling
subroutine that is located at a predetermined (by the 8052 designers) absolute address in program

Five interrupts are provided in the 8052. Three of these are generated automatically by internal
operations: Timer flag 0, Timer flag 1, and the serial port interrupt (RI or TI). Two interrupts are
triggered by external signals provided by circuitry that is connected to pins INT0 and INT1 (port
pins P3.2 and P3.3). After the Interrupt has been handled by the interrupt subroutine,
which is placed by the programmer at the interrupt location in program memory, the interrupt
program must resume operation at the instruction where the interrupt took place. Program

resumption is done by storing the interrupted PC address on the stack in RAM before changing
the PC to the interrupt address in ROM. The PC address will be restored from the stack after an
RETI instruction is executed at the end of the interrupt subroutine.


The heart of the 8052 is the circuitry that generates the clock pulse by which all internal
operations are synchronized. Pins XTAL1 and XTAL2 are provided for connecting a resonant
network to form an oscillator. Typically, a quartz crystal and capacitors are employed, as shown
in Figure 3.7 the crystal frequency is the basic internal clock frequency of the microcontroller.
The manufactures make available 8052 designs that can run at specified maximum and minimum
frequencies, typically 1 megahertz to 24 megahertz. Minimum frequencies imply that some
internal memories are dynamic and must always operate above a minimum frequency or data
will be lost. Serial data communication needs often state the frequency of the oscillator because
of the requirement that internal counters must divide the basic clock frequency is not divisible
without a remainder, and then the resulting communication frequency is not standard.





Oscillator Circuit

Ceramic resonators may be used as low-cost alternative to crystal resonators. However,

decreases in frequency stability data accuracy make the ceramic resonator a poor choice if high-
speed serial data communication with the systems, or critical timing, is to be done. The oscillator
formed by the crystal, capacitors, and an on-chip inverter microcontroller, called the pulse, P,
time. The smallest interval of time to accomplish any simple instruction, or part of a complex
instruction, however, is the machine cycle. The machine cycle is itself made up of six states. A
state is the basic time interval for discrete operations of the microcontroller such as fetching an
opcode byte, decoding an opcode, executing an opcode, or writing a data byte. Two oscillator
pulses define each state. Program instructions may require one, two, or four machine cycles to
the executed, depending on the type of instruction. Instructions are fetched and executed by the
microcontroller automatically, beginning with the instruction located by the microcontroller
automatically; beginning with the instruction located at ROM memory address 0000h at the time
the microcontroller is first reset.

To calculate the time any particular instruction will take to be executed, find the number
of cycles, C, The time to execute that instruction is then found by multiplying C by 12 and
dividing the product by the crystal frequency:

C x 12

T inst = --------------------------------------------

Crystal frequency

For example, if the crystal frequency is 16 megahertz, then the time to execute an ADD A, R1
one-cycle instructions is .75 microseconds. A 12-megahertz crystal yields the convenient time of
1 microsecond per cycle.

An 11.0592-megahertz crystal, although seemingly an odd value, yields a cycle frequency of

921.6 kilohertz, which can be divided evenly by the standard communication baud rates of
19200, 9600, 4800, 2400, 1200, and 300 hertz.

Oscillator frequency (f)

P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2

State 1 State 2 State 3 State 4 State 5 State 6

One machine cycle

Address Latch
Enable (ALE)
8052 Timing


There are two ALE pulse per machine cycle. The ALE pulse, which is primarily used
as a timing pulse for external memory access, indicates when every instruction byte is fetched.
Two bytes of a single instruction may thus be fetched, and executed, in one machine cycle.
Single byte instructions are nor executed in a half cycle, however, Single-byte instructions
"throw-away" the second byte (which is the first byte of the next instruction.)

Liquid Crystal Display

The LCD is used for the purpose of displaying the words which we are given in the
program code. This code will be executed on microcontroller chip. By following the instructions
in code the LCD display the related words. Fig.5.6 shows the LCD display.

Fig. : LCD Display


The LCD display consists of two lines, 20 characters per line that is interfaced with
the PIC16F73.The protocol (handshaking) for the display is as shown in Fig. The display
contains two internal byte-wide registers, one for commands (RS=0) and the second for
characters to be displayed (RS=1). It also contains a user-programmed RAM area (the
character RAM) that can be programmed to generate any desired character that can be formed
using a dot matrix. To distinguish between these two data areas, the hex command byte 80
will be used to signify that the display RAM address 00h will be chosen Port1 is used to
furnish the command or data type, and ports 3.2 to 3.4 furnish register select and read/write


A liquid crystal is a material (normally organic for LCDs) that will flow like a liquid but
whose molecular structure has some properties normally associated with solids. The Liquid
Crystal Display (LCD) is a low power device. The power requirement is typically in the order of
microwatts for the LCD. However, an LCD requires an external or internal light source. It is
limited to a temperature range of about 0°C to 60°C and lifetime is an area of concern, because
LCDs can chemically degrade.

There are two major types of LCDs which are:

1. Dynamic-scattering LCDs and

2. Field-effect LCDs
The turn-on and turn-off time is an important consideration in all displays. The response
time of LCDs is in the range of 100 to 300ms.The lifetime of LCDs is steadily increasing beyond
10,000+hours limit. Since the color generated by LCD units is dependent on the source of
illumination, there is a wide range of color choice.

Pin Diagram

The Pin diagram for LCD is shown in the following fig 5.7 and the pin description is
also explained in Table 5.1.


1 2 3 15 16

2x16 Liquid Crystal Display

RS R/w En
D0 D1 D2 D3 D4 D5 D6 D7
4 5 6
wow 76 Pin
Fig. 5.7: 8 Diagram
9 10 of11LCD12 13 14

Pins Description

1 "Vss" - Ground

2 "Vcc" - +5v power supply

3 "Vee" - Contrast Voltage

4 "R/S" - Instruction/Register Select

5 "R/W" - Read/Write LCD Registers

6 "E" - Enable

7 - 14 Data I/O Pins

Table 5.1: Pin Description of LCD


Pin Description:

Vcc, Vss, VEE

While Vcc and Vss provide +5v and ground, respectively, Vee is used for controlling
LCD contrast.

RS, Register Select

There are two very important registers inside the LCD. The RS pin is used for their
selection as follows.

If RS = 0, the instruction command code register is selected, allowing the user to send
as command code register is selected, allowing the user to send a command such as clear
display, cursor at home, etc.

If RS = 1 the data register is selected, allowing the user to send data to be displayed on
the LCD.

R/W, Read/Write

R/W input allows the user to write information to the LCD or read information from it.

R/W = 1 when reading;

R/W = 0 when writing.

E, Enable

The enable pin is used by the LCD to latch information presented to its data pins. When
data is supplied to data pins, a high - to - low pulse must be applied to this pin in order for the
LCD latch in the data pins. This pulse must be a minimum of 450 ns wide.

D0 - D7

The 8 - bit data pins, D0 - D7, are used to send information to the LCD or read the
contents of the LCD's internal registers.

To display letters and numbers, send ASCII codes for the letters A - Z, a - z, and numbers
0 - 9 to these pins while RS = 1. When RS = 0 to check the busy flag bit to see if the LCD is
ready to receive information. The busy flag is D7 and can be read when R/W = 1 and RS = 0, as
follows: if R/W = 1, RS = 0. When D7 =1 (busy flag = 1), the LCD is busy taking care of
internal operation and will not accept any new information. The description of the pins is shown
in Table 5.1.

LCD Commands Description

RS R/W D7 D6 D5 D4 D3 D2 D1 D0 Function

0 0 0 0 0 0 0 0 0 1 Clear LCD &memory,

Home cursor

0 0 0 0 0 0 0 0 1 0 Clear and home cursor only

0 0 0 0 0 0 0 1 1/0 S Screen action as display


Written S=1/0:shift screen/


0 0 0 0 0 0 1 D C B D=1/0: Screen on/off

C=1/0: Cursor on/off

B=1/0: Cursor blink/no

0 0 0 0 0 1 S/C R/L 0 0 S/C=1/0: Screen/Cursor

R/L==1/0: Shift one space


0 0 0 0 1 DL N F 0 0 DL=1/0: 8/4 bits per

character N=1/0: 2/1 Rows
of characters F=1/0:

0 0 0 1 Character address Write to character RAM

address after this

0 0 1 Display data address Write to display RAM

address after this

0 1 BF Current addresses BF=1/0:busy/Not busy

0 1 Character type Write byte to last RAM


Table 5.2: LCD Commands

Handling the EN Control Line

As mentioned above, the EN line is used to tell the LCD that it is ready to execute an
instruction that prepared on the data bus and on the other control lines. Note that the EN line
must be raised/lowered before/after each instruction sent to the LCD regardless of whether that
instruction is read or write, text or instruction. In short, someone must always manipulate EN
when communicating with the LCD. EN is the LCD's way of knowing that someone is talking to
it. If one doesn’t raise/lower EN, the LCD doesn't know that someone is talking to it on the other

Checking the Busy Status of the LCD

As previously mentioned, it takes a certain amount of time for each instruction to be

executed by the LCD. The delay varies depending on the frequency of the crystal attached to the
oscillator input of the controller as well as the instruction which is being executed. While it is
possible to write code that waits for a specific amount of time to allow the LCD to execute
instructions, this method of "waiting" is not very flexible. If the crystal frequency is changed, the
software will need to be modified. Additionally, if the LCD itself is changed for another LCD
which, although 44780 compatible, requires more time to perform its operations, the program
will not work until it is properly modified.

A more robust method of programming is to use the "Get LCD Status" command to
determine whether the LCD is still busy executing the last instruction received.

The "Get LCD Status" command will return to the user two bits of information; the
information that is useful to the user right now is found in DB7. In summary, when user issue the
"Get LCD Status" command the LCD will immediately raise DB7 if it's still busy executing a
command or lower DB7 to indicate that the LCD is no longer occupied. Thus the program can
query the LCD until DB7 goes low, indicating the LCD is no longer busy. At that point user is
free to continue and send the next command.

Initializing the LCD

LCD must be initialized and configured before using. This is accomplished by sending a
number of initialization instructions to the LCD.

The first instruction send must tell the LCD whether it is to be communicated with an 8-
bit or 4-bit data bus. 5x8 dot character font should also be selected. These two options are
selected by sending the command 38h to the LCD as a command.

a) Clearing the Display

When the LCD is first initialized, the screen should automatically be cleared by the

b) Writing Text into the LCD

The data to be displayed is send to the LCD through data bus.

c) Cursor Positioning

The cursor positioning in a LCD can be done in the right entry mode or left entry mode.
As left entry mode is flexible it is implemented.

The circuit diagram of Check post Control System using RFID Module and its
components description is described in next chapter.

Power supply

Block diagram:

The ac voltage, typically 220V rms, is connected to a transformer, which steps that ac
voltage down to the level of the desired dc output. A diode rectifier then provides a full-wave
rectified voltage that is initially filtered by a simple capacitor filter to produce a dc voltage. This
resulting dc voltage usually has some ripple or ac voltage variation.

A regulator circuit removes the ripples and also remains the same dc value even if the
input dc voltage varies, or the load connected to the output dc voltage changes. This voltage
regulation is usually obtained using one of the popular voltage regulator IC units.


Fig: Block diagram of Power Supply

Working principle


The potential transformer will step down the power supply voltage (0-230V) to (0-6V)
level. Then the secondary of the potential transformer will be connected to the precision rectifier,
which is constructed with the help of op–amp. The advantages of using precision rectifier are it
will give peak voltage output as DC, rest of the circuits will give only RMS output.

Bridge rectifier

When four diodes are connected as shown in figure, the circuit is called as bridge
rectifier. The input to the circuit is applied to the diagonally opposite corners of the network, and
the output is taken from the remaining two corners.

Let us assume that the transformer is working properly and there is a positive potential, at
point A and a negative potential at point B. the positive potential at point A will forward bias D3
and reverse bias D4.

The negative potential at point B will forward bias D1 and reverse D2. At this time D3
and D1 are forward biased and will allow current flow to pass through them; D4 and D2 are
reverse biased and will block current flow.

The path for current flow is from point B through D1, up through RL, through D3,
through the secondary of the transformer back to point B. this path is indicated by the solid
arrows. Waveforms (1) and (2) can be observed across D1 and D3.

One-half cycle later the polarity across the secondary of the transformer reverse, forward
biasing D2 and D4 and reverse biasing D1 and D3. Current flow will now be from point A
through D4, up through RL, through D2, through the secondary of T1, and back to point A. This
path is indicated by the broken arrows. Waveforms (3) and (4) can be observed across D2 and
D4. The current flow through RL is always in the same direction. In flowing through RL this
current develops a voltage corresponding to that shown waveform (5). Since current flows
through the load (RL) during both half cycles of the applied voltage, this bridge rectifier is a full-
wave rectifier.

One advantage of a bridge rectifier over a conventional full-wave rectifier is that with a
given transformer the bridge rectifier produces a voltage output that is nearly twice that of the
conventional full-wave circuit.

This may be shown by assigning values to some of the components shown in views A
and B. assume that the same transformer is used in both circuits. The peak voltage developed
between points X and y is 1000 volts in both circuits. In the conventional full-wave circuit
shown—in view A, the peak voltage from the center tap to either X or Y is 500 volts. Since only
one diode can conduct at any instant, the maximum voltage that can be rectified at any instant is
500 volts.

The maximum voltage that appears across the load resistor is nearly-but never exceeds-
500 v0lts, as result of the small voltage drop across the diode. In the bridge rectifier shown in
view B, the maximum voltage that can be rectified is the full secondary voltage, which is 1000

volts. Therefore, the peak output voltage across the load resistor is nearly 1000 volts. With both
circuits using the same transformer, the bridge rectifier circuit produces a higher output voltage
than the conventional full-wave rectifier circuit.

IC voltage regulators

Voltage regulators comprise a class of widely used ICs. Regulator IC units

contain the circuitry for reference source, comparator amplifier, control device, and overload
protection all in a single IC. IC units provide regulation of either a fixed positive voltage, a fixed
negative voltage, or an adjustably set voltage. The regulators can be selected for operation with
load currents from hundreds of milli amperes to tens of amperes, corresponding to power ratings
from milli watts to tens of watts.

Fig : Circuit diagram (Power supply)


A fixed three-terminal voltage regulator has an unregulated dc input voltage, Vi, applied
to one input terminal, a regulated dc output voltage, Vo, from a second terminal, with the third
terminal connected to ground.

The series 78 regulators provide fixed positive regulated voltages from 5 to 24 volts.
Similarly, the series 79 regulators provide fixed negative regulated voltages from 5 to 24 volts.

 For ICs, microcontroller, LCD --------- 5 volts

 For alarm circuit, op-amp, relay circuits ---------- 12 volts

Infrared transmitter is one type of LED which emits infrared rays generally called as
IR Transmitter. Similarly IR Receiver is used to receive the IR rays transmitted by the
IR transmitter. One important point is both IR transmitter and receiver should be
placed straight line to each other.

 The transmitted signal is given to IR transmitter whenever the signal is high, the IR
transmitter LED is conducting it passes the IR rays to the receiver. The IR receiver is
connected with comparator. The comparator is constructed with LM 358 operational
amplifier. In the comparator circuit the reference voltage is given to inverting input
terminal. The non inverting input terminal is connected IR receiver. When interrupt
the IR rays between the IR transmitter and receiver, the IR receiver is not conducting.
So the comparator non inverting input terminal voltage is higher then inverting input.
Now the comparator output is in the range of +5V. This voltage is given to
microcontroller or PC and led so led will glow.
 When IR transmitter passes the rays to receiver, the IR receiver is conducting due to
that non inverting input voltage is lower than inverting input. Now the comparator
output is GND so the output is given to microcontroller or PC. This circuit is mainly
used to for counting application, intruder detector etc.


The piezo buzzer produces sound based on reverse of the piezoelectric effect. The generation of
pressure variation or strain by the application of electric potential across a piezoelectric material
is the underlying principle. These buzzers can be used alert a user of an event corresponding to a
switching action, counter signal or sensor input. They are also used in alarm circuits.

The buzzer produces a same noisy sound irrespective of the voltage variation applied to it. It
consists of piezo crystals between two conductors. When a potential is applied across these
crystals, they push on one conductor and pull on the other. This, push and pull action, results in a
sound wave. Most buzzers produce sound in the range of 2 to 4 kHz.
Piezo buzzer is an electronic device commonly used to produce sound. Light weight, simple
construction and low price make it usable in various applications like car/truck reversing
indicator, computers, call bells etc. Piezo buzzer is based on the inverse principle of piezo
electricity discovered in 1880 by Jacques and Pierre Curie. It is the phenomena of generating
electricity when mechanical pressure is applied to certain materials and the vice versa is also
true. Such materials are called piezo electric materials. Piezo electric materials are either
naturally available or manmade. Piezoceramic is class of manmade material, which poses piezo

electric effect and is widely used to make disc, the heart of piezo buzzer. When subjected to an
alternating electric field they stretch or compress, in accordance with the frequency of the signal
thereby producing sound
The Red lead is connected to the Input and the Black lead is connected to Ground.


This tutorial will cover those wonderful blinky things, LEDs. We're going to cover how to
calculate the current going through an LED and in the mean time introduce two important laws
of electronics, Kirchhoff's Voltage Law and Ohm's Law. We'll begin by performing
experiments that will demonstrate how voltage and resistance affects current and then prove
those results with a little math.

What is an LED?

Who doesn't love LEDs? They are bright and blinky, or soft and elegant. They're festive! They're
colorful! They're everywhere and they're a lot of fun. We love LEDs when we write tutorials
because most of electronics hackery is hidden in chips, or goes very fast and we can't see or
sense it without expensive equipment. But LEDs are easy to see for everyone - this way we can
visually identify what is going on inside our microcontroller.

LEDs are so common, they come in dozens of different shapes and sizes. The LEDs you are
most likely to use are the through hole LEDs with two legs. There are lots of LEDs that are
small and hard to solder but these are easy to use with a breadboard because they have long wires
we can stick in. The clear or clear-ish bulb is what protects the light emitter (thats where the
magic happens). In fact, the first two letters of LED stand for Light Emitting.

A really nice thing about LEDs is that they are very simple. Unlike some chips that have dozens
of pins with names and special uses, LEDs have only two wires. One wire is the anode (positive)
and another is the cathode (negative). The two wires have different names because LEDs only
work in one direction and we need to keep track of which pin is which. One goes to the positive

voltage and the other goes to the negative voltage. Electronic parts that only work in 'one
direction' like this are called Diodes, thats what the last letter of LED stands for.

 The longer lead goes to the more-positive voltage

 Current goes in one direction, from the anode (positive) to the cathode (negative)
 LEDs that are 'backwards' won't work - but they won't break either

It's all a little confusing - we often have to think about which is which. So to make it easy, there's
only one thing you need to remember and that's the LED wont light up if you put it in
backwards. If you're ever having LED problems where they are not lighting, just flip it around.
Its very hard to damage an LED by putting it in backwards so don't be scared if you do

If it helps, refer back to these photos and diagrams or print them out for your reference

All the different sizes and colors

5mm LEDs! Green, Red, Blue (in a clear case) and InfraRed (in a bluish case)

One of the best things about modern LEDs is all the colors they come in. It used to be that LEDs
were only red or maybe yellow and orange, which is why early electronics from the 70s and 80s

only had red LEDs. The color emitted from an LED has to do with what type of material they are
made of. So red, for example, is made with Gallium Arsenide. Since then, scientists have
experimented with many other materials and figured out how to make other colors such as green
and blue, as well as violet and white.

LEDs come in all sorts of sizes as well. Here is a photo showing a 3mm, 5mm and 10mm LED.
the "millimeter" size refers to the diameter of the LED. For example, if you need to drill a hole in
a box for your 5mm blinky LED, the hole size should be 5mm, and you'd need a 5mm drill bit to
make it. 5mm are the most common size you'll see, and they can be extremely bright!

Introduction to Micro vision Keil (IDE)

Keil is a cross compiler. So first we have to understand the concept of compilers and cross
compilers. After then we shall learn how to work with keil. Concept of compiler: -

Compilers are programs used to convert a High Level Language to object code. Desktop
compilers produce an output object code for the underlying microprocessor, but not for other
microprocessors. I.E the programs written in one of the HLL like ‘C’ will compile the code
to run on the system for a particular processor like x86 (underlying microprocessor in the
computer). For example compilers for Dos platform is different from the Compilers for Unix

So if one wants to define a compiler then compiler is a program that translates

source code into object code. The compiler derives its name from the way it works, looking
at the entire piece of source code and collecting and reorganizing the instruction. See there is
a bit little difference between compiler and an interpreter. Interpreter just interprets whole
program at a time while compiler analyzes and execute each line of source code in
succession, without looking at the entire program.

The advantage of interpreters is that they can execute a program immediately. Secondly
programs produced by compilers run much faster than the same programs executed by an
interpreter. However compilers require some time before an executable program emerges.

Now as compilers translate source code into object code, which is unique for each type of
computer, many compilers are available for the same language.

Concept of cross compiler: -

A cross compiler is similar to the compilers but we write a program for the target processor
(like 8051 and its derivatives) on the host processors (like computer of x86)

It means being in one environment you are writing a code for another environment is called
cross development. And the compiler used for cross development is called cross compiler

So the definition of cross compiler is a compiler that runs on one computer but
produces object code for a different type of computer. Cross compilers are used to generate
software that can run on computers with a new architecture or on special-purpose devices
that cannot host their own compilers. Cross compilers are very popular for embedded
development, where the target probably couldn't run a compiler. Typically an embedded
platform has restricted RAM, no hard disk, and limited I/O capability. Code can be edited
and compiled on a fast host machine (such as a PC or Unix workstation) and the resulting
executable code can then be downloaded to the target to be tested. Cross compilers are
beneficial whenever the host machine has more resources (memory, disk, I/O etc) than the
target. Keil C Compiler is one such compiler that supports a huge number of host and target
combinations. It supports as a target to 8 bit microcontrollers like Atmel and Motorola etc.

Why do we need cross compiler?

There are several advantages of using cross compiler. Some of them are described as follows

• By using this compilers not only can development of complex embedded systems be
completed in a fraction of the time, but reliability is improved, and maintenance is easy.

• Knowledge of the processor instruction set is not required.

• A rudimentary knowledge of the 8051’s memory architecture is desirable but not


• Register allocation and addressing mode details are managed by the compiler.

• The ability to combine variable selection with specific operations improves program

• Keywords and operational functions that more nearly resemble the human thought
process can be used.

• Program development and debugging times are dramatically reduced when compared
to assembly language programming.

• The library files that are supplied provide many standard routines (such as formatted
output, data conversions, and floating-point arithmetic) that may be incorporated into
your application.

• Existing routine can be reused in new programs by utilizing the modular programming
techniques available with C.

• The C language is very portable and very popular. C compilers are available for almost
all target systems. Existing software investments can be quickly and easily converted
from or adapted to other processors or environments.

Now after going through the concept of compiler and cross compilers lets we start with Keil C
cross compiler.

Keil C cross compiler: -

Keil is a German based Software development company. It provides several development tools

• IDE (Integrated Development environment)

• Project Manager

• Simulator

• Debugger

• C Cross Compiler , Cross Assembler, Locator/Linker

Keil Software provides you with software development tools for the 8051 family of
microcontrollers. With these tools, you can generate embedded applications for the multitude of
8051 derivatives. Keil provides following tools for 8051 development

1. C51 Optimizing C Cross Compiler,

2. A51 Macro Assembler,

3. 8051 Utilities (linker, object file converter, library manager),

4. Source-Level Debugger/Simulator,

5. µVision for Windows Integrated Development Environment.

The keil 8051 tool kit includes three main tools, assembler, compiler and linker.

An assembler is used to assemble your 8051 assembly program

A compiler is used to compile your C source code into an object file

A linker is used to create an absolute object module suitable for your in-circuit emulator.

8051 project development cycle: - these are the steps to develop 8051 project using keil

1. Create source files in C or assembly.

2. Compile or assemble source files.
3. Correct errors in source files.
4. Link object files from compiler and assembler.
5. Test linked application.

Now let us start how to work with keil.


working with keil: -

To open keil software click on start menu then program and then select keil2 (or any other
version keil3 etc. here the discussion is on keil2 only). Following window will appear on your

you can see three different windows in this screen. 1) project work space window 2) editing
window 3) output window.

Project workspace window is for showing all the related files connected with your project.

Editing window is the place where you will edit the code

Output window will show the output when you compile or build or run your project.

Now to start with new project follow the steps

 click on project menu and select new project


 you will be asked to create new project in specific directory

 just move to your desired directory and there create a new folder for your project named
"first". Here I am creating new project in d:\keil2\myprojects\first as shown in figure

 give the name of project as "test". By default it will be saved as *.v2 extension.
 now you will be asked to chose your target device for which you want to write the
 scroll down the cursor and select generic from list. expand the list and select 8051 (all

 when you click OK, you will be asked to add startup code and file to your project folder.
click yes. Now on your screen expand target1 list fully. You will see following window.

 now click on file menu and select new file. editor window will open. Now you can start
writing your code.
 as you start writing program in C, same way here also you have to first include the header
file. Because our target is 8051 our header file will be "reg51.h"
 after including this file. just right click on the file and select open document <reg51.h>.
The following window will appear

 If you scroll down cursor you will see that all the SFRs like P0-P3, TCON, TMOD, ACC,
bit registers and byte registers are already defined in this header file. so one can directly
use these register names in coding

 now you can write your program same as c language starting with void main()
 After completing the code save the file in project folder with ".c" extension.
 Now right click on "source group 1" in project workspace window. select "add files to
source group 1"
 select the C file you have created and click add button

 you will see that the c file has been added in source group
 Now to compile the program from project menu select "build target". In the output
window you will see the progress
 If there is any compilation error then target will not be created. Remove all the errors and
again build the target till you find "0 Error(s)"
 Now you are ready to run your program. from debug menu select "start/stop debug
 You will see your project workspace window now shows most of the SFRs as well as
GPRs r0-r7. Also one more window is now opened named "watches". in this window you
can see different variable values.

 to add variable in watch window go to "watch#1" tab. then type F2 to edit and type the
name of your variable

 If you want to see the output on ports go to peripheral menu and select I/O ports. Select
the desire port. you can give input to port pins by checking or unchecking any check box.
here the check mark means digit 1 and no check mark means 0. the output on the pin will
be shown in same manner
 to run the program you can use any of the option provided "go", "step by step", "step
forward", "step ove" etc.
 now after testing your program you need to down load this program on your target board
that is 8051. for this you have to create hax file
 to create hex file first stop debug session. Again you will be diverted to project workspace
 right click on "target 1" and select "option for target 1". Following window will appear.

 select output tag and check "create hex file" box


 now when you again build your program you will see the message in output window "hex
file is created".
 in your project folder you can see the hex file with same name of your project as
 this file you can directly load in 8051 target board and run the application on actual

So here I have described the procedure to create a project in keil for 8051 micro controller. To
see some sample programs for 8051 in keil just go through the link "sample programs in keil" so
that you can get the idea how to write a program for 8051 in keil C.




We have designed in our project automatic railway gate control system. Small
improvement in it. Automatic railway gate control system is more sensitive and also reduces
railway accident to provide more secure life for road users. This system provides very large
benefit for road user and railway management. Our system is completely automatic is suitable in
rare area and also forest area at that place no station master is available it reduce railway
accident. The circuit for our project was designed and set up in a breadboard. It is found to be
very reliable and stable. The circuit was able to control the railway gate precisely. The circuit
was tested in both direction and worked perfectly


[1] Krishna, ShashiYadav and Nidhi, “Automatic Railway Gate Control Using Microcontroller”,
Oriental Journal Of Computer Science & Technology, Vol.6, No.4, December 2013. [2] Ahmed
Salih Mahdi. Al-Zuhairi,“Automatic Railway Gate and Crossing Control based Sensors
&Microcontroller ”, IN International Journal of Computer Trends and Technology (IJCTT) –
Volume 4 Issue 7–July 2013 [3] J. Banuchandar, V. Kaliraj, P. Balasubramanian, S. Deepa, N.
Thamilarasi,“ Automated Unmanned Railway Level Crossing System”, in International Journal
of Modern Engineering Research (IJMER)Volume.2, Issue.1, Jan-Feb 2012 pp-458-463 [4] Fred
Coleman III, Young J. Moon (2011) Trapped Vehicle Detection System for Four Quadrant Gates
in High Speed Rail Corridors, Transportation Research Record 1648 [5] Fred Coleman III,
Young J. Moon (2010) Design of Gate Delay and Gate Interval Time for Four Quadrant Gate
System at Railroad-Highway Grade Crossings Transportation Research Record. [6] The 8051
Microcontroller and Embedded Systems By Muhammad Ali Mazidi [7] Fundamentals Of
Embedded Software By Daniel W Lewis


 Two IR sensors are used, one is at the one end of the gate on track, and the other
one is other side. So the sensor will be operated on both sides.
 Here 89c51 is used, it is advanced version of 8051.
 5 interrupts and two channels external interrupt.
 Dc motor is used to opening and closing the gate.

 The sensors only sense big objects only, not sensing the birds etc.
 The third sensor is used to senses the obstacle in between the railway gate.
 Two trains entering into the same track, the indication is provided.
 Simple rack and pinion mechanism is used to opening/closing the gate.
 This project is fully automated one
 It is used to railway application.
 Industrial applications
 Cane industries