You are on page 1of 64

Introduction to Embedded

Technology
Embed is defined as inserting or implanting subspace (small
structure) into whole space (larger structure). And embedded
technology means embedding smaller circuits to larger one
to

make

one

complete

circuit.

In

todays

world

the

embedded technology is used to make the circuit smaller


and these are called as embedded circuits. Some of them
are known to us as microcontrollers and microprocessors.
In my six weeks training I studied one of these
called microcontrollers. Microcontrollers are widely used in
Embedded System products. An Embedded product uses the
microprocessor (or microcontroller) to do one task & one
task only. A printer is an example of embedded system since
the processor inside it performs one task only namely
getting the data and printing it. Contrast this with Pentium
based PC. A PC can be used for any no. of applications such
as word processor, print server, bank teller terminal, video
game player, network server or internet terminal. Software
for variety of applications can be loaded and run. Of course
the reason a PC can perform multiple tasks is that it has RAM
memory and an operating system that loads the application
software into RAM & lets the CPU run it.
In and Embedded system there is only one
application software that is typically burn into ROM. An

x86PC Contain or its connected to various Embedded


Products such as keyboard, printer, modem, Disc controller,
Sound card, CD-ROM Driver, Mouse & so on. Each one of
these peripherals as a microcontroller inside it that performs
only one task. For example inside every mouse there is
microcontroller to perform the task of finding the mouse
position and sending it to PC. Although microcontroller is
preferred choice for many embedded systems, there are
times that a microcontroller is inadequate for the task. For
this reason in recent years many manufactures of general
purpose microprocessors such as INTEL, Motorola, AMD and
Cyrix have targeted their microprocessors for the high end of
embedded market. While INTEL, AMD, Cyrix push their x86
processors for both the embedded and desktop pc market,
Motorola is determined to keep the 68000 families alive by
targeting it mainly for high end of embedded system.
One of the most critical needs of the embedded
system is to decrease power consumptions and space. This
can be achieved by integrating more functions into the CPU
chips. All the embedded processors based on the x86 and
680x0 have low power consumptions in additions to some
forms of I/O, Com port & ROM all on a single chip.

Microprocessors
A microprocessor is a general-purpose digital computer
central processing unit (CPU). Although popularly known as a
computer on a chip is in no sense a complete digital
computer. The block diagram of a microprocessor CPU is
shown, which contains arithmetic and logical unit (ALU), a
program counter (PC), a stack pointer (SP), some working
registers, a clock timing circuit and interrupts circuits. Out of
the CPU it is connected to different peripherals to complete
its operations.

Fig. shows
microprocessor

the

block

diagram

of

typical

Microcontrollers
Figure shows the block diagram of a typical microcontroller,
which is a true computer on a chip. The design incorporates
all of the features found in Micro-processor CPU: ALU, PC, SP,
and registers. It also added the other features needed to

make a complete computer: ROM, RAM, parallel I/O, serial


I/O, counters, and clock circuit.

Fig. shows
microcontroller

the

block

diagram

of

typical

8051 MICROCONTROLLER
The 8051 Microcontrollers Architecture consists of these
specific features
8-bit data bus
16-bit address bus
32 general purpose registers each of 8 bits
16 bit timers (usually 2, but may have more, or less).
3 internal and 2 external interrupts.
Bit as well as byte addressable RAM area of 16 bytes.
Four 8-bit ports, (short models have two 8-bit ports).
16-bit program counter and data pointer
3

8051 models may also have a number of special, modelspecific features, such as UARTs, ADC, Op-Amps, etc...
PIN DESCRIPTION

PIN 9: PIN 9 is the reset pin which is used reset the


microcontrollers internal registers and ports upon starting
up.2 machine cycle should be high at this pin.

PINS 18 & 19: The 8051 has a built-in oscillator amplifier


hence we need to only connect a crystal at these pins to
provide clock pulses to the circuit.

PIN 40 and 20: Pins 40 and 20 are VCC and ground


respectively. The 8051 chip needs +5V 500mA to function
properly, although there are lower powered versions like the

Atmel 2051 which is a scaled down version of the 8051


which runs on +3V.

PINS 29, 30 & 31: As described in the features of the


8051, this chip contains a built-in flash memory. In order to
program this we need to supply a voltage of +12V at pin 31.
If external memory is connected then PIN 31, also called
EA/VPP, should be connected to ground to indicate the
presence of external memory. PIN 30 is called ALE (address
latch enable), which is used when multiple memory chips are
connected to the controller and only one of them needs to
be selected. We will deal with this in depth in the later
chapters. PIN 29 is called PSEN. This is "program select
enable". In order to use the external memory it is required to
provide the low voltage (0) on both PSEN and EA pins.

Ports
There are 4 8-bit ports: P0, P1, P2 and P3.

PORT P1 (Pins 1 to 8): The port P1 is a general purpose


input/output port which can be used for a variety of
interfacing tasks. The other ports P0, P2 and P3 have dual
roles or additional functions associated with them based
upon the context of their usage.
5

PORT P3 (Pins 10 to 17): PORT P3 acts as a normal IO


port, but Port P3 has additional functions such as, serial
transmit and receive pins, 2 external interrupt pins, 2
external counter inputs, read and write pins for memory
access.

PORT P2 (pins 21 to 28): PORT P2 can also be used as


a general purpose 8 bit port when no external memory is
present, but if external memory access is required then PORT
P2 will act as an address bus in conjunction with PORT P0 to
access external memory. PORT P2 acts as A8-A15, as can be
seen from fig.

PORT P0 (pins 32 to 39): PORT P0 can be used as a


general purpose 8 bit port when no external memory is
present, but if external memory access is required then PORT
P0 acts as a multiplexed address and data bus that can be
used to access external memory in conjunction with PORT
P2. P0 acts as AD0-AD7, as can be seen from fig.

Port Pin Alternate Functions


P3.0 RXD (serial input port)
P3.1 TXD (serial output port)
P3.2 INT0 (external interrupt 0)
P3.3 INT1 (external interrupt 1)
6

P3.4 T0 (timer 0 external input)


P3.5 T1 (timer 1 external input)
P3.6 WR (external data memory write strobe)
P3.7 RD (external data memory read strobe)
RST Reset input. A high on this pin for two machine cycles
while the oscillator is running resets the device.
ALE/PROG -- Address Latch Enable 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 ROM 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.
Note, however, that one ALE pulse is skipped during
each access to external Data Memory. 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 is the read strobe to external
program memory. When the 8051 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, how ever, 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, for parts that
require12-volt VPP.
XTAL1Input to the inverting oscillator amplifier and input
to the internal clock operating circuit.
XTAL2-- Output from the inverting oscillator amplifier.

Programming
The programming language used by us was C and I.D.E.
(Integrated development environment) which we were using
8

was KEIL Vision in which we did our programming.


Common to all Keil software development tools, KEIL'S
Vision

IDE

and

Debugger

combines

project

management, make facilities, revision control, source code


editing, target program debugging, device simulation and
Flash programming in a single fully-integrated environment.
With a simple and logical Getting-Started process,
the Vision development platform helps you to quickly
create embedded programs that work. The Editor and
Debugger are integrated in a single application that provides
a seamless embedded project development environment.
KEIL software is used for the software implementation of the
developed

system.

Vision2

Integrated

Development

Environment is an IDE that encapsulates a project manager,


make facility, tool configuration, editor and a powerful
debugger. Vision2 is used to write and compile the
programs using the tools. It can transfer the assembly
language as well as, C code into the hex file.
Keil software consists of a Linker Control File, Map
File, Project Target, Source File Group, Toolset.
i.) Linker control file: It is a text file that Vision passes
to the linker when linking. The control file includes all
directives and names of object files and library files to
include in the output file.
ii.)

Map File: The Map File is a listing file generated

by the linker.

iii.)

Project Target: In a project, a target is an

executable program that is generated. A project may


generate a target that runs on an 8051. Targets may be
created for builds with no optimization and for builds
with full optimization.
iv.)

Source File Group: In a project, a group is a

number of source files that compose the project target.


Although you may individually specify the toolset
options for a file, a group lets you apply the same
options to a group of source files. The options for a
group may be different from the options for the target.
v.)

Toolset: A toolset include an assembler, compiler,

linker,

HEX

converter,

debugger,

and

the

other

associated tools for a particular device family like the


8051. All of the tools or programs in a toolset are
dedicated to generating target code for a specific family
of chips [27]. To evaluate the software for correct
operation

the

file

was

programmed

into

the

microcontroller on the relevant development board.


Programming of the microcontroller was achieved using
the VPL-SPROG programmer. It is a handy serial
programmer. This permits hexadecimal files to be
loaded

into

the

microcontroller.

Initially

the

microcontroller was programmed by removing it from


the socket on the board and inserting it into the multipin socket on the programmer.

10

Power Supply
The power supply supplies the required energy for both the
microcontroller and the associated circuits. It is the most
essential part of the circuit because to run its constituent
ICs circuit has to be provided with power. These ICs can run
on DC power. Hence the required D.C supply has to be
generated. The main parts of a power supply unit and their
function are as follows:

Power supply schematic


DESCRIPTION:
Power supply is the main requirement of every project. The
various parts of a power supply are explained below:

11

Transformer: The function of the transformer is to step


down the voltage level from the available A.C.220V to
the desired voltage. The 9-0-9 rating of the transformer
upon the requirements of the ICs in the circuit is used.
The secondary has a center tapping which forms the
neutral terminal.
Bridge rectifier: The function of the rectifier is to
convert

the

unidirectional
semiconductor

alternating
one.

This

diodes

voltage
function

signal
is

connected

into

provided
in

a
by

bridge

configuration. Diodes 1N4007 are used as rectifier.


Ripple Rejection: The output voltage of the rectifier is
unidirectional but pulsating capacitor of 1000Pf is used
for ripple rejection.
Regulation: To obtain a constant voltage specific ICs
are used as voltage regulator. Voltage regulator LM7805
is used. These ICs have three terminals input, output
and a ground terminal.

12

LCD Display
Liquid crystal displays (LCD) are widely used in recent years
as compares to LEDs. This is due to the declining prices of
LCD, the ability to display numbers, characters and graphics,
incorporation of a refreshing controller into the LCD, their by
relieving the CPU of the task of refreshing the LCD and also
the ease of programming for characters and graphics. HD
44780 based LCDs are most commonly used. LCD pin
description. The LCD discuss in this section has the most
common connector used for the Hitachi 44780 based LC1D is
14 pins in a row and modes of operation and how to program
and interface with microcontroller is describes in this section.
LCD pin description
The LCD discuss in this section has the most common
connector used for the Hitachi 44780 based LCD is 14 pins in
a row and modes of operation and how to program and
interface with microcontroller is describes in this section.

13

Pin diagram of LCD


VCC, VSS, VEE
The voltage VCC and VSS provided by +5V and ground
respectively while VEE is used for controlling LCD contrast.
Variable voltage between Ground and Vcc is used to specify
the contrast (or "darkness") of the characters on the LCD
screen.
RS (register select)
There are two 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, then allowing to 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)

14

The R/W (read/write) input allowing the user to write


information from it. R/W=1, when it read and R/W=0, when it
writing.
EN (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 power, a high-to-low pulse must be
applied to this pin in order to for the LCD to latch in the data
presented at the data pins.
D0-D7 (data lines):
The 8-bit data pins, D0-D7, are used to send information to
the LCD or read the contents of the LCDs internal registers.
To displays the letters and numbers, we send ASCII codes for
the letters A-Z, a-z, and numbers 0-9 to these pins while
making RS =1. There are also command codes that can be
sent to clear the display or force the cursor to the home
position or blink the cursor. We also use RS =0 to check the
busy flag bit to see if the LCD is ready to receive the
information. The busy flag is D7 and can be read when R/W
=1 and RS =0, as follows: if R/W =1 and RS =0, when D7
=1(busy flag =1), the LCD is busy taking care of internal
operations and will not accept any information. When D7 =0,
the LCD is ready to receive new information.

15

Interfacing of micro controller with LCD display


In most applications, the "R/W" line is grounded. This
simplifies the application because when data is read back,
the microcontroller I/O pins have to be alternated between
input and output modes. In this case, "R/W" to ground and
just wait the maximum amount of time for each instruction
(4.1

msecs

for

clearing

the

display

or

moving

the

cursor/display to the "home position", 160 secs for all other

16

commands) and also the application software is simpler, it


also frees up a microcontroller pin for other uses.
Different LCD execute instructions at different
rates and to avoid problems later on (such as if the LCD is
changed to a slower unit). Before sending commands or data
to the LCD module, the Module must be initialized. Once the
initialization is complete, the LCD can be written to with data
or instructions as required. Each character to display is
written like the control bytes, except that the "RS" line is set.
During initialization, by setting the "S/C" bit during the
"Move Cursor/Shift Display" command, after each character
is sent to the LCD, the cursor built into the LCD will
increment to the next position (either right or left). Normally,
the "S/C" bit is set (equal to "1").

Interfacing of Microcontroller with LCD.

17

Interfacing of MCU with LED:A light-emitting diode (LED) is a semiconductor diode that emits incoherent
narrow spectrum light when electrically biased in the forward direction of
the p-n junction. This effect is a form of electroluminescence. An LED is
usually a small area source, often with extra optics added to the chip that
shapes its radiation pattern. The color of the emitted light depends on the
composition and condition of the semiconducting material used, and can be
infrared, visible, or near-ultraviolet. An LED can be used as a regular
household light source

18

VCC

D 1
LED
D 2
U ?

LED
39
38
37
36
35
34
33
32

D 3
LED
D 4

1
2
3
4
5
6
7
8

LED
D 5
LED
D 6

19
18
9

Y ?

LED

31

C R Y STAL
C ?
C AP N P

D 7
LED

40

P
P
P
P
P
P
P
P

0 .0 /A
0 .1 /A
0 .2 /A
0 .3 /A
0 .4 /A
0 .5 /A
0 .6 /A
0 .7 /A

D
D
D
D
D
D
D
D

P
P
P
P
P
P
P
P

1 .0 /T2
1 .1 /T 2 -E X
1 .2
1 .3
1 .4
1 .5
1 .6
1 .7

XTA L1
XTA L2
R ST

0
1
2
3
4
5
6
7

P 2 .0 /A 8
P 2 .1 /A 9
P 2 .2 /A 1 0
P 2 .3 /A 1 1
P 2 .4 /A 1 2
P 2 .5 /A 1 3
P 2 .6 /A 1 4
P 2 .7 /A 1 5
P 3 .0 /R XD
P 3 .1 /T XD
P 3 .2 /IN T O
P 3 .3 /IN T 1
P 3 .4 /TO
P 3 .5 /T1
P 3 .6 /W R
P 3 .7 /R D
PSEN
A L E /P R O G

21
22
23
24
25
26
27
28
10
11
12
13
14
15
16
17
29
30

E A /V P P

AT89C 52

20

R 1
R

D 8
LED

Device interfacing with MCU

RELAY

Figure Electromagnetic Solenoid Valve

19

The electromagnetic relay consists of a multi-turn coil,


wound on an iron core, to form an electromagnet. When the
coil is energized, by passing current through it, the core
becomes temporarily magnetized. The magnetized core
attracts the iron armature. The armature is pivoted which
causes it to operate one or more sets of contacts. When the
coil is de-energized the armature and contacts are released.
The coil can be energized from a low power source such as a
transistor while the contacts can switch high powers such as
the mains supply. The relay can also be situated remotely
from the control source. Relays can generate a very high
voltage across the coil when switched off. This can damage
other components in the circuit to prevent this coil.

As there are always some chances of high voltage


spikes back from the switching circuit i.e. heater so an
Optocoupler/isolator MCT2e is used. It provides and electrical
isolation between the microcontroller and the heater. MCT2e
is a 6-pin IC with a combination of optical transmitter LED
and an optical receiver as phototransistor. Microcontroller is
connected to pin no 2 of MCT2e through a 470-ohm resistor.
Pin no.1 is given +5V supply and pin no.4 is grounded.
20

To handle the current drawn by the heater a power


transistor BC-369 is used as a current driver. Pin no.5 of opto
coupler is connected to the base of transistor. It takes all its
output to Vcc and activates the heater through relay circuit.
The electromagnetic relay consists of a multi-turn coil,
wound on an iron core, to form an electromagnet. When the
coil is energized, by passing current through it, the core
becomes temporarily magnetized. The magnetized core
attracts the iron armature. The armature is pivoted which
causes it to operate one or more sets of contacts. When the
coil is de-energized the armature and contacts are released.
Relays can generate a very high voltage across the coil when
switched off. This can damage other components in the
circuit. To prevent this diode is connected across the coil.
Relay has five points. Out of the 2 operating points one is
permanently connected to the ground and the other point is
connected to the collector side of the power transistor.
When Vcc reaches the collector side i.e. signal is
given to the operating points the coil gets magnetized and
attracts the iron armature. The iron plate moves from
normally connected (NC) position to normally open (NO)
position. Thus the heater gets the phase signal and is ON. To
remove the base leakage voltage when no signal is present a
470-ohm resistance is used.

21

phase
3

D11
1

6
5

VCC
R2

MCT2E

1
2
R4
BC-369

VCC

J4
5 NC
4

NO

1
2
HEATER

RELAY

From P3.0 of
microcontroller

SEVEN SEGMENT DISPLAY

22

Controlling the Seven Segment Display


The diagram below illustrates an arrangement showing how a
seven segment display can be interfaced along with switch
inputs.

Note the RA0 and RA1 port bits are connected to both the
A1, A0 Switches, the transistor drivers to select the display
digits.

To drive the display, the RA0 and RA1 bits are configured as
outputs. To input from the two switches, the RA0 and RA1
bits are configured as inputs. The two 10K resistors prevent
damage to the port when configured as outputs and a switch
is closed.

USER INTERFACE
23

what they want to do The junction between a user and a


computer program. An interface is a set of commands or
menus through which a user communicates with a program.
A command-driven interface is one in which you enter
commands. A menu-driven interface is one in which you
select command choices from various menus displayed on
the screen. The user interface is one of the most important
parts of any program because it determines how easily you
can make the program do what we want to do. It is widely
accepted that the user interface can make a critical
difference in the perceived utility of a system regardless of
the system's performance.

D.C. MOTOR
Working Principle:
The principle upon which the DC motor works is very simple.
If a current carrying conductor is placed in a magnetic field,
mechanical force is experienced on the conductor, the
direction of which is given by the Fleming's left hand rule
and hence the conductor moves in the direction of force.
When the motor is connected to the DC supply
mains a direct current passes through the brushes and the
commutator to the armature winding; while it passes
through the commutator it is converted into AC so that the
group of conductors under successive field poles carries

24

current in the opposite direction. Also the directions of the


currrent in the individual conductors reverse as they pass
away from the influence of one pole to that of the next. The
split phase arrangement of the motor creates two fluxes
B1and B2 which induces voltage around them in the rotor
and under the influence of these induced voltages current
flows in the rotor. The current i1 produced by flux B1 reacts
with flux B2 and develops force F1.
Types of D.C. motor:
I.

Permanent

magnet

motors:

It

consists

of

an

armature and one or several permanent magnets


encircling the armature. Field coils are usually not
required. However some of these motors do have coils
wound on the poles. If they exist, these coils are
intended only for recharging the magnets in the event
that they loose their strength.
II.

Separately excited D.C. motors: These motors have


field coils similar to those of a shunt wound machine,
but the armature and field coils are fed from different
supply sources and may have different voltage ratings.

III.

Series wound D.C. motor: As the name indicates, the


field coils, consisting of few turns of a thick wire are
connected in series with the armature. The crosssectional area of the wire used for the field has to be
fairly large to carry the armature current, but owing to

25

the higher current, the number of turns of wire in them


need not be large.
IV.

Shunt wound D.C. motor: These motors are so


named because they basically operate with field coils
connected in parallel with the armature. The field
winding consists of a large number of turns of
comparatively

fine

wire

so

as

to

provide

large

resistance. The field current is much less than the


armature current, sometimes as low as 5%.
V.

Compound wound D.C. motor: A compound wound


D.C. motor has both shunt and series field coils. The
shunt field is normally stronger of the two. Compound
wound motors are of two types:
a) Cumulative compound wound motor.
b) Differential compound wound motor.

26

Stepper Motor
Motion Control, in electronic terms, means to accurately
control the movement of an object based on speed, distance,
load, inertia or a combination of all these factors. There are
numerous types of motion control systems, including;
Stepper Motor, Linear Step Motor, DC Brush, Brushless,
Servo, Brushless Servo and more.
A stepper motor is an electromechanical device
which converts electrical pulses into discrete mechanical
movements. Stepper motor is a form of ac. motor .The shaft
or spindle of a stepper motor rotates in discrete step
increments when electrical command pulses are applied to it
in the proper sequence. The motors rotation has several
direct relationships to these applied input pulses. The
sequence of the applied pulses is directly related to the
direction of motor shafts rotation. The speed of the motor
shafts rotation is directly related to the frequency of the
input pulses and the length of rotation is directly related to
the number of input pulses applied [39].
27

For every input pulse, the motor shaft turns


through a specified number of degrees, called a step. Its
working principle is one step rotation for one input pulse.
The range of step size may vary from 0.72 degree to 90
degree. In position control application, if the number of input
pulses sent to the motor is known, the actual position of the
driven job can be obtained.
A stepper motor differs from a conventional motor (CM) as
under:
a) Input to SM is in the form of electric pulses whereas
input to a CM is invariably from a constant voltage
source.
b) A CM has a free running shaft whereas shaft of SM
moves through angular steps.
c) In control system applications, no feedback loop is
required when SM is used but a feedback loop is
required when CM is used.
d) A SM is a digital electromechanical device whereas a
CM is an analog electromechanical device [40].
Open Loop Operation
One of the most significant advantages of a stepper motor is
its ability to be accurately controlled in an open loop system.
Open loop control means no feedback information about
position is needed. This type of control eliminates the need
for expensive sensing and feedback devices such as optical

28

encoders. Control position is known simply by keeping track


of the input step pulses [39].
Every stepper motor has a permanent magnet
rotor (shaft) surrounded by a stator. The most common
stepper motor has four stator windings that are paired with a
center-tapped common. This type of stepper motor is
commonly referred to as a four- phase stepper motor. The
center tap allows a change of current direction in each of two
coils when a winding is grounded, thereby resulting in a
polarity

change

of

the

stator.

Notice

that

while

conventional motor shaft runs freely, the stepper motor shaft


moves in a fixed repeatable increment which allows one to
move it to a precise position.
This repeatable fixed movement is possible as a
result of basic magnetic theory where poles of the same
polarity repel and opposite poles attract. The direction of the
rotation is dictated by the stator poles. The stator poles are
determined by the current sent through the wire coils. As the
direction of the current is changed, the polarity is also
changed causing the reverse motion of the rotor. The
stepper motor used here has a total of 5 leads: 4 leads
representing the four stator windings and 1 common for the
center tapped leads. As the sequence of power is applied to
each stator winding, the rotor will rotate. There are several
widely used sequences where each has a different degree of
precision. Table shows the normal 4-step sequence. For

29

clockwise go for step 1 to 4 & for counter clockwise go for


step 4 to 1.

Rotor Alignment
Step
1
2
3
4

Winding A
Winding B
Winding C
0
1
1
1
0
1
1
1
0
1
1
1
Input Sequence to the Windings

Winding D
1
1
1
0

30

Step Angle & Steps per Revolution


Movement associated with a single step, depends on the
internal construction of the motor, in particular the number
of teeth on the stator and the rotor. The step angle is the
minimum degree of rotation associated with a single step.
Step per revolution is the total number of steps
needed to rotate one complete rotation or 360 degrees (e.g.,
180 steps * 2 degree = 360) [31].
Since the stepper motor is not ordinary motor and has four
separate coils, which have to be energized one by one in a
stepwise fashion. We term them as coil A, B, C and D. At a
particular instant the coil A should get supply and then after
some delay the coil B should get a supply and then coil C
and then coil D and so on the cycle continues. The more the
delay is introduced between the energizing of the coils the
lesser is the speed of the stepper motor and vice versa.

31

Project Description
Project features:
Interfaced with 8 different devices.
Access protected with password.
Taking different work from different devices with the
help of MCU.

INTERFACED DEVICES
Seven devices are interfaced with one MCU
LCD
5 switches
LEDs
Relay(interfaced with 220v device)
Stepper motor
DC motor
7-segment

32

Schematic Diagram of
Project

33

In the above shown Fig. the 8 different devices with one


switching ckt , One crystal oscillator and one reset ckt is
interfaced with MCU to complete the whole ckt.

Source code
Header Files
1. Delay.h
void ms_delay(unsigned char n)
{
unsigned char i,j;
for(j=0;j<n;j++)
{
for(i=0;i<200;i++)
{
_nop_();
}
}
}
void sec_delay(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
{
ms_delay(250);

34

ms_delay(250);
ms_delay(250);
ms_delay(250);
}
}

2. LCD.h
#define lcd P1
sbit s1=P3^0;
sbit s2=P3^1;
sbit s3=P3^2;
sbit s4=P3^3;
sbit s5=P3^4;
sbit rs=P3^5;
sbit rw=P3^6;
sbit e=P3^7;
void lcd_cmd(unsigned char a)
{
ms_delay(2);
lcd=a;
rs=0;
rw=0;
e=1;
ms_delay(2);
e=0;
}
void lcd_data(unsigned char a)

35

{
ms_delay(2);
lcd=a;
rs=1;
rw=0;
e=1;
ms_delay(2);
e=0;
}
void lcd_initialize()
{
lcd_cmd(0x38);
ms_delay(2);
lcd_cmd(0x0C);
ms_delay(2);
lcd_cmd(0x06);
ms_delay(2);
lcd_cmd(0x01);
ms_delay(2);
lcd_cmd(0x80);
ms_delay(2);
}
void lcd_puts(unsigned char *s)
{
unsigned char i;

36

for(i=0;s[i]!='\0';i++)
{
lcd_data(s[i]);
}
}
unsigned char lcd_pwd(unsigned char loc)
{
unsigned char i=0;
while(s5==1)
{
if(s1==0)
i=1;
if(s2==0)
i=2;
if(s3==0)
i=3;
if(s4==0)
i=4;
lcd_cmd(loc);
lcd_data(i+48);
}
lcd_cmd(loc);
lcd_data('*');
ms_delay(250);
return(i);
}

37

3. Scroll.h
unsigned char index=0,turn=0;
code unsigned char line[118]={"

WELCOME

TO THE WORLD OF MICROCONTROLLER


"};
code unsigned char line1[146]={"
MADE BY K.P.SINGH ROLL NO. 67/08 E.C.E. Deptt.
D.A.V.I.E.T., JALANDHAR

"};

code unsigned char line2[91]={"

PROJECT

ASSISTANCE BY EUREKA ELECTROSOFT SOLUTIONS Pvt.


Ltd.

"};

void scroll()
{
unsigned char i;
for(i=0;i<=75;i++)
{
lcd_cmd(0xc0);
while(index<16)
{
lcd_data(line2[index+turn]);
index++;
}
turn++;
index=0;
ms_delay(150);

38

if(turn==75)
turn=0;
}
for(i=0;i<=131;i++)
{
lcd_cmd(0x80);
while(index<16)
{
lcd_data(line[index+turn]);
index++;
}
lcd_cmd(0xc0);
index=0;
while(index<16)
{
lcd_data(line1[index+turn]);
index++;
}
turn++;
index=0;
ms_delay(125);
if(turn==131)
turn=0;
}

39

4. Stepper.h
sbit st1=P0^0;
sbit st2=P0^1;
sbit st3=P0^2;
sbit st4=P0^3;
void stepfwd(unsigned char i)
{
unsigned char a;
for(a=0;a<i;a++)
{
st1=1;st2=0;st3=0;st4=0;
ms_delay(150);
st1=0;st2=1;st3=0;st4=0;
ms_delay(150);
st1=0;st2=0;st3=1;st4=0;
ms_delay(150);
st1=0;st2=0;st3=0;st4=1;
ms_delay(150);
}
}
void stepback(unsigned char i)
{
unsigned char a;
for(a=0;a<i;a++)
{

40

st1=0;st2=0;st3=0;st4=1;
ms_delay(150);
st1=0;st2=0;st3=1;st4=0;
ms_delay(150);
st1=0;st2=1;st3=0;st4=0;
ms_delay(150);
st1=1;st2=0;st3=0;st4=0;
ms_delay(150);
}
}
void stepper()
{
step:lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("s1 s2 s3 s4 ");
lcd_cmd(0xc0);
lcd_puts("fwd rev f-b b-f");
ms_delay(250);
while(s5==1)
{
if(s1==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("FORWD RUN");
stepfwd(12);

41

}
if(s2==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("REVERSE RUN");
stepback(12);
}
if(s3==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("FWD-REVERSE RUN");
stepfwd(12);
stepback(12);
}
if(s4==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("REVERSE-FWD RUN");
stepback(12);
stepfwd(12);
}
goto step;
}

42

5. Sevensegment.h
#define seg P2
void sevenseg()
{
unsigned char i=0;
unsigned

char

a[10]={0xc0,0xF9,0xa4,0xb0,0x99,0x92,0x82,0xF8,0x
80,0x90};
seg: lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("s1 s2 s3 s4 ");
lcd_cmd(0xc0);
lcd_puts("inc dec fwd dwn");
ms_delay(250);
while(s5==1)
{
if(s1==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("INCREMENT");
i++;
ms_delay(200);
if(i>9)
i=0;

43

seg=a[i];
ms_delay(250);
}
if(s2==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("DECREMENT");
i--;
ms_delay(200);
if(i==255)
i=9;
seg=a[i];
ms_delay(250);
}
if(s3==0)
{
unsigned char j;
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("UP COUNT");
for(j=0;j<10;j++)
{
i++;
if(i>9)
i=0;

44

seg=a[i];
sec_delay(1);
}
}
if(s4==0)
{
char j;
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("DOWN COUNT");
for(j=9;j>=0;j--)
{
i--;
if(i==255)
i=9;
seg=a[i];
sec_delay(1);
}
}
goto seg;
}
P2=0xff;
}

6. Dcmotor.h
sbit dc=P0^5;
void dcspeed(unsigned char ton)

45

{
dc=1;
ms_delay(ton);
dc=0;
ms_delay(100-ton);
}
void dcspeedctrl()
{
dc:

lcd_cmd(0x01);
lcd_puts("SPD s1 s2 s3 s4 ");
lcd_cmd(0xc0);
lcd_puts(" % 10 25 50 100");
ms_delay(250);
while(s5==1)
{
if(s1==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("10 % speed");
dcspeed(10);
}
if(s2==0)
{
lcd_cmd(0x01);
46

lcd_cmd(0x80);
lcd_puts("25 % speed");
dcspeed(25);
}
if(s3==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("50 % speed");
dcspeed(50);
}
if(s4==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("100 % speed");
dcspeed(100);
}
if(s1==1 && s2==1 && s3==1 && s4==1)
{
goto dc;
}
}
}

7. Relay.h

47

sbit relay=P0^6;
unsigned char i;
void relayinterface()
{
rel: lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("s1 s2 s3 s4 SEC ");
lcd_cmd(0xc0);
lcd_puts("5 10 15 20 ON ");
ms_delay(250);
while(s5==1)
{
if(s1==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("5 SECONDS ON");
relay=1;
sec_delay(5);
relay=0;
}
if(s2==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("10 SECONDS ON");

48

relay=1;
sec_delay(10);
relay=0;
}
if(s3==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("15 SECONDS ON");
relay=1;
sec_delay(15);
relay=0;
}
if(s4==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("20 SECONDS ON");
relay=1;
sec_delay(20);
relay=0;
}
goto rel;
}
}

8. Led.h

49

void led()
{
unsigned char a,b,d,k;
le:

lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("s1 s2 s3 s4 ");
lcd_cmd(0xc0);
lcd_puts("run X sdgls e-o");
ms_delay(250);
while(s5==1)
{
if(s1==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("RUNNING LIGHTS");
d=0x7f;
for(k=0;k<8;k++)
{
seg=d;
ms_delay(250);
ms_delay(250);
d=d>>1;
d=d|0x80;
}
d=0xfe;

50

for(k=0;k<8;k++)
{
seg=d;
ms_delay(250);
ms_delay(250);
d=d<<1;
d=d|0x01;
}
}
if(s2==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("X - PATTERN");
a=0x7f;
b=0xfe;
for(k=0;k<8;k++)
{
seg=a&b;
a=a>>1;
b=b<<1;
a=a|0x80;
b=b|0x01;
ms_delay(250);
ms_delay(250);

51

}
}
if(s3==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("SAND GLASS");
b=0xff;
for(k=0;k<8;k++)
{
a=0x7f;
for(d=0;d<(8-k);d++)
{
seg=a&b;
ms_delay(250);
ms_delay(250);
a=a>>1;
a=a|0x80;
}
b=b<<1;
}
}
if(s4==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);

52

lcd_puts("EVEN ODD");
for(k=0;k<5;k++)
{
seg=0xaa;
ms_delay(250);
ms_delay(250);
seg=0x55;
ms_delay(250);
ms_delay(250);
}
}
seg=0xff;
goto le;
}
}

53

Main Program
#include<intrins.h>
#include<reg51.h>
#include<delay.h>
#include<lcd.h>
#include<scroll.h>
#include<stepper.h>
#include<sevensegment.h>
#include<dcmotor.h>
#include<relay.h>
#include<led.h>
sbit led1=P0^4;
sbit seg1=P0^7;
void main()
{
54

unsigned char pwd[4];


unsigned char z;
P0=0x00;
P2=0xff;
led1=1;
seg1=1;
lcd_initialize();
lcd_cmd(0x38);
sec_delay(2);
if(s1==0)
goto pwd;
scroll();
while(1)
{
z=0;
pwd:lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("ENTER PASSWORD");
lcd_cmd(0xc0);
lcd_puts("TO ACCESS SYSTEM");
sec_delay(1);
lcd_cmd(0xc0);
lcd_puts("

");

lcd_cmd(0xc0);
pwd[0]=lcd_pwd(0xc0);
sec_delay(1);

55

pwd[1]=lcd_pwd(0xc1);
sec_delay(1);
pwd[2]=lcd_pwd(0xc2);
sec_delay(1);
pwd[3]=lcd_pwd(0xc3);
sec_delay(1);

if(pwd[0]==1&&pwd[1]==2&&pwd[2]==4&&pwd[3]=
=3)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts(" WELCOME TO THE ");
lcd_cmd(0xc0);
lcd_puts("

SYSTEM

");

sec_delay(1);
while(s5==1)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("s1 s2 s3 s4 ");
lcd_cmd(0xc0);
lcd_puts("stp mtr 7sg 220v");
sec_delay(1);
lcd_cmd(0x01);
lcd_cmd(0x80);
56

lcd_puts("

s1&s2

");

lcd_cmd(0xc0);
lcd_puts("

LED

");

if(s1==0 && s2==0)


{
led1=0;
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("LED Interfaced");
sec_delay(1);
led();
led1=1;
ms_delay(150);
}
else if(s1==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("Stepper Motor");
sec_delay(1);
stepper();
ms_delay(150);
}
else if(s2==0)

57

{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("Motor speed ctrl");
sec_delay(1);
dcspeedctrl();
ms_delay(150);
}
else if(s3==0)
{
seg1=0;
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("Seven segment");
sec_delay(1);
sevenseg();
ms_delay(150);
seg1=1;
}
else if(s4==0)
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("Relay Interfaced");
sec_delay(1);
relayinterface();

58

ms_delay(150);
}
led1=0;
seg1=0;
//

seg=0x7f;
sec_delay(1);
led1=1;
seg1=1;

//

seg=0xff;

}
sec_delay(1);
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("THANKS

FOR ");

lcd_cmd(0xc0);
lcd_puts("YOUR ATTENTION");
sec_delay(6);
}
else
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("Invalid Password");
sec_delay(1);
lcd_cmd(0x01);
lcd_cmd(0x80);

59

lcd_puts("ATTEMPTS LEFT

");

lcd_cmd(0xc8);
lcd_data(2-z+48);
sec_delay(1);
z++;
if(z==3)
{
z=0;
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("System Locked");
sec_delay(1);
mxc:

lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("ENTER MAIN CODE");
lcd_cmd(0xc0);
pwd[0]=lcd_pwd(0xc0);
sec_delay(1);
pwd[1]=lcd_pwd(0xc1);
sec_delay(1);
pwd[2]=lcd_pwd(0xc2);
sec_delay(1);
pwd[3]=lcd_pwd(0xc3);
sec_delay(1);

60

if(pwd[0]==1&&pwd[1]==2&&pwd[2]==3&&pwd[3]=
=4)
goto pwd;
else
{
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("Invalid Password");
sec_delay(1);
lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("ATTEMPTS LEFT

");

lcd_cmd(0xc8);
lcd_data(2-z+48);
sec_delay(1);
z++;
if(z==3)
{
while(1)
{
if(s1==0

&&

s2==0 && s3==0 && s4==0 && s5==0)


goto pwd;
else
{

61

lcd_cmd(0x01);
lcd_cmd(0x80);
lcd_puts("
ENTRY BLOCKED ");
sec_delay(2);
}
}
}
goto mxc;
}
}
goto pwd;
}
}
}

62

Bibliography
1. 8051

microcontroller

and

its

applications,

Muhammad Ali Mazidi


2. http://en.wikipedia.org/wiki/Embedded_system
3. http://www.keil.com/c51/Atmel.com
4. http://en.wikipedia.org/wiki/8051
5. http://en.wikipedia.org/wiki/C_
%28programming_language%29
6. http://en.wikipedia.org/wiki/Microcontroller
7. http://www.esskayinstitute.com/newproject.htm

63

You might also like