You are on page 1of 52

Kick Start

Board

Powered by:
The 8051 is the name of a big family of microcontrollers. The device which we
are going to use along this tutorial is the 'AT89V51RD2' which is a typical 8051
microcontroller manufactured by Atmel™. Note that this part doesn't aim to
explain the functioning of the different components of a 89V51RD2
microcontroller, but rather to give you a general idea of the organization of the
chip and the available features, which shall be explained in detail along this
tutorial.

This figure shows the main features and components that the designer can
interact with. You can notice that the 89V51RD2 has 4 different ports, each one
having 8 Input/output lines providing a total of 32 I/O lines. Those ports can be
used to output DATA and orders do other devices, or to read the state of a
sensor, or a switch.
Figure: 1

Some of the features that have made the 8051 popular are:

4 KB on chip program memory.


128 bytes on chip data memory (RAM).
4 register banks.
128 user defined software flags.
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.
1 Microsecond instruction cycle with 12 MHz Crystal.

8051 models may also have a number of special, model-specific features, such as
UARTs, ADC, OpAmps, etc...

8051 chips are used in a wide variety of control systems, telecom applications, robotics
as well as in the automotive industry. By some estimation, 8051 family chips make up
over 50% of the embedded chip market.
Figure: 2

Basic Pins

PIN 9: PIN 9 is the reset pin which is used to reset the microcontroller‟s internal
registers and ports upon starting up. (Pin should be held high for 2 machine cycles.)

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 store 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.

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.

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.

All four ports in the 80C51 are bidirectional. Each consists of a latch (Special Function
Registers P0 through P3), an output driver, and an input buffer. The output drivers of
Ports 0 and 2, and the input buffers of Port 0, are used in accesses to external memory.
In this application, Port 0 outputs the low byte of the external memory address, time-
multiplexed with the byte being written or read. Port 2 outputs the high byte of the
external memory address when the address is 16 bits wide. Otherwise, the Port 2 pins
continue to emit the P2 SFR content.

All the Port 3 pins are multifunctional. They are not only port pins, but also serve the
functions of various special features as listed below:

Port Pin Alternate Function


P3.0 RxD (serial input port)
P3.1 TxD (serial output port)
P3.2 INT0 (external interrupt)
P3.3 INT1 (external interrupt)
P3.4 T0 (Timer/Counter 0 external input)
P3.5 T1 (Timer/Counter 1 external input)
P3.6 WR (external Data Memory write strobe)
P3.7 RD (external Data Memory read strobe)
The alternate functions can only be activated if the corresponding bit latch in the port
SFR contains a 1. Otherwise the port pin remains at 0.
Reset Button for POWE
reset R
microcontroller ON/OF
F

POWER
ON/OFF

Microcontroller

To
RS232
supply
connect the
DC(5volt
kickstartboar
s)
d To
computer

Figure: 3
About flash magic

Flash magic is software used to burn the instruction in microcontrollers and also
perform a lot of task.

Figure: 4
NOTE – Flash magic support only those microcontroller which have the ISP(In
System Programming) feature.

One thing is also note that before working in flash magic we have to enable
“Assert DTR and RTS while COM Port open”. For enable this function follow this
steps-

1. Choose “Advance
Option” from “Option”
Menu

2. Enable “Assert DTR and


RTS while COM Port open”
from Hardware configutation
Step 1. Place the Microcontroller in kickstart board

Figure: 5
Step 2. Follow the procedure shown in figure 3.

3. Put ON
the
Switch

1. To
POWER
Supply

2. To
Comput
er

Figure: 6
Step 3. Open the flash magic and choose the proper communication port from
device manager and
microcontroller device as shown in
figure: 7.

NXP89V51RD2

1. Select proper
device of
Microcontroller
family & COM Port

Figure: 7
Step 4. Open the flash magic and choose the reset option from ISP menu of
menu bar as shown in figure: 8.

Figure: 8
Step 5 . After choosing reset option a dialogue box appears, follow this
procedure as shown in figure 9.

1. After clicking
start this dialogue
box appear

2. Press”RESET”

“After reset the reset


window shown in “1”
will disappear”

3.After pressing reset


button of “kickstart
board” finished status
shows that the
Microcontroller has reset

Figure: 9

After press reset the microcontroller will be reset.


Step 6. Now for burn instruction in microcontroller, follow the procedure as
shown in figure : 10.

2. Choose the
programme
file
3. Click on

“Start”

Figure: 10
Step 7 . Follow the procedure as shown in figure:11.

1. After clicking
start this dialogue
box appear

2.Press “RESET”
button

Note -“After reset


the reset window
shown in “1” will
disappear”

3.finished status
shows that the
instructions have
burn in
Microcontroller
Figure: 11

Now the microcontroller has programmed.


If we look around, we will find ourselves to be surrounded by computing systems. Every
year millions of computing systems are built destined for desktop computers (Personal
Computers, workstations, mainframes and servers) but surprisingly, billions of
computing systems are built every year embedded within larger electronic devices and
still goes unnoticed. Any device running on electric power either already has computing
system or will soon have computing system embedded in it.

Today, embedded systems are found in cell phones, digital cameras, camcorders,
portable video games, calculators, and personal digital assistants, microwave ovens,
answering machines, home security systems, washing machines, lighting systems, fax
machines, copiers, printers, and scanners, cash registers, alarm systems, automated
teller machines, transmission control, cruise control, fuel injection, anti-lock brakes,
active suspension and many other devices/ gadgets.

Definition

A precise definition of embedded systems is not easy. Simply stated, all computing
systems other than general purpose computer (with monitor, keyboard, etc.) are
embedded systems.

System is a way of working, organizing or performing one or many tasks according to a


fixed set of rules, program or plan. In other words, an arrangement in which all units
assemble and work together according to a program or plan. An embedded system is a
system that has software embedded into hardware, which makes a system dedicated
for an application (s) or specific part of an application or product or part of a larger
system. It processes a fixed set of pre-programmed instructions to control
electromechanical equipment which may be part of an even larger system (not a
computer with keyboard, display, etc).

A general-purpose definition of embedded systems is that they are devices used to


control, monitor or assist the operation of equipment, machinery or plant. “Embedded”
reflects the fact that they are an integral part of the system. In many cases, their
“embeddedness” may be such that their presence is far from obvious to the casual
observer.
Block diagram of a typical embedded system is shown in fig.

An embedded system is an engineering artifact involving computation that is subject to


physical constraints (reaction constraints and execution constraints) arising through
interactions of computational processes with the physical world. Reaction constraints
originate from the behavioral requirements & specify deadlines, throughput, and jitter
whereas execution constraints originate from the implementation requirements & put
bounds on available processor speeds, power, memory and hardware failure rates. The
key to embedded systems design is to obtain desired functionality under both kinds of
constraints.

Characteristics
a) Embedded systems are application specific & single functioned; application is
known apriori, the programs are executed repeatedly.

b) Efficiency is of paramount importance for embedded systems. They are optimized


for energy, code size, execution time, weight & dimensions, and cost.

c) Embedded systems are typically designed to meet real time constraints; a real
time system reacts to stimuli from the controlled object/ operator within the time
interval dictated by the environment. For real time systems, right answers arriving
too late (or even too early) are wrong.

d) Embedded systems often interact (sense, manipulate & communicate) with


external world through sensors and actuators and hence are typically reactive
systems; a reactive system is in continual interaction with the environment and
executes at a pace determined by that environment.

e) They generally have minimal or no user interface.


Design Metrics for Embedded Systems

In addition to meeting the desired functionality of an embedded system, embedded


system designer must optimize on the following design metrics.

Non Recurring Engineering (NRE) Cost: Money invested in R&D and developing
first, functional and tested prototype.
Unit Cost: Cost of producing one unit
Electrical Power
Performance - System Throughput, Computational Power, response time
Functional Correctness
Dependability; Fault Tolerance, Reliability, Maintainability, Availability
Physical Size and Weight
Time to prototype
Time to market
Safety: It should not cause harm to others.
Maintenance, Ease of Use

These metrics compete amongst themselves; increasing one may affect others. Hence
optimization of these metrics is a challenge for an embedded system designer.

Elements of Embedded Systems

Hardware

Core element of an embedded system is the processor or a computational unit.


Processors can act as brain of the system. They can be programmed to do perform a
task. This can be designed using variety of options.

General Purpose Microprocessors

General purpose microprocessors are single chip semi conductor device which is a
computer on chip, but not a complete computer. Its CPU contains an Arithmetic & Logic
Unit(ALU), a Program Counter(PC), a Stack Pointer(SP), registers, a clock and
interrupts circuit on a single chip. To make complete micro computer, one must add
memory usually ROM and RAM, memory decoder, an oscillator, a number of serial and
parallel ports
A general-purpose processor is designed to cater for large amount of applications and
hence is produced in bulk. Using it in an embedded system offers various benefits.
Design time is low as only software is to be developed, no digital design is involved.
Typical characteristics of a general purpose processors are relatively high cost, high
speeds, higher Power consumption, large architecture, large memory size, onboard
flash and cache, an external bus interface for greater memory usage.
Examples: Motorola‟s 680x0, Intel„s x86

Microcontrollers/ Embedded Processors

A microcontroller is a functional computer system-on-a-chip. It contains an integrated


processor, memory (a small amount of RAM, program memory, or both), several
peripheral devices, such as timers, analog to digital converters, and serial
communication devices all on one chip resulting in compact and low-power
implementations. It is not expandable as it has no external bus interface. Examples
are PIC‟s DSPIC33 / PIC24, Motorola‟s 6811, Intel‟s 8051. Typical characteristics of a
microcontroller are: Low cost, Low speed, Low Power, small architecture, Small
memory size, Onboard Flash, Limited I/O.
Microcontrollers provide pin access which allows programs to easily monitor sensors,
set actuators, and transfer data with other devices. Providing specialized instructions
improves performance for embedded systems applications; thus, microcontrollers can
be considered ASIPs to some degree.

Special microcontrollers are often called embedded processors. The difference


between a microcontroller and an embedded processor is not clear, but processors
with large architectures with fast processing, fast context-switching & atomic ALU
operations are marketed by many vendors as embedded processors. Examples of
embedded processors are ARM 7, INTEL i960, AMD 29050.
A microcontroller is an economical computer-on-a-chip built for dealing with specific
tasks, such as displaying or receiving information through LEDs or remote controlled
devices. The most commonly used set of microcontrollers belong to 8051 Family. 8051
Microcontrollers continue to remain a preferred choice for a vast community of
hobbyists and professionals. Through 8051, the world became witness to the most
revolutionary set of microcontrollers.

8051 Family

Intel fabricated the original 8051 which is known as MCS-51. The other two members of
the 8051 family are:

8052 – This microcontroller has 256 bytes of RAM and 3 timers. In addition to the
standard features of 8051, this microcontroller has an added 128 bytes of RAM and
timer. It has 8K bytes of on chip program ROM. The programs written for projects
using 8051 microcontroller can be used to run on the projects using 8052
microcontroller as 8051 is a subset of 8052.

8031 – This microcontroller has all the features of 8051 except for it to be ROM-less.
An external ROM that can be as large as 64 K bytes should be programmed and
added to this chip for execution. The disadvantage of adding external ROM is that 2
ports (out of the 4 ports) are used. Hence, only 2 ports are left for I/O operations
which can also be added externally if required for execution.
Comparison of 8051 family members:

Features 8051 8052 8031

RAM 128 256 128

ROM 4K 8K 0K

Timers 2 3 2

Serial port 1 1 1

I/O pins 32 32 32

Interrupt sources 6 8 6

Various 8051 microcontrollers

8051 microcontrollers use two different kinds of memory such as UV- EPROM, Flash
and NV-RAM. Hence 8051 will not be seen in the part number even though it is the most
popular member of the 8051 family.

i. 8751 – This microcontroller is the UV-EPROM version of 8051. This chip has
only 4K bytes of UV-EPROM. It is required to have access to the PROM
burner and the UV-EPROM eraser to erase the contents inside the chip
before it is programmed again. The disadvantage of using this memory is
the waiting time of around 20 minutes to erase the contents in order to
program it again. Due to this limitation, manufacturers fabricated flash and
NV-RAM versions of 8051.

ii. AT89C51 from Atmel Corporation – Atmel fabricated the flash ROM version
of 8051 which is popularly known as AT89C51 („C‟ in the part number
indicates CMOS). The flash memory can erase the contents within seconds
which is best for fast growth. Therefore, 8751 is replaced by AT89C51 to
eradicate the waiting time required to erase the contents and hence
expedite the development time. To build up a microcontroller based system
using AT89C51, it is essential to have ROM burner that supports flash
memory. Note that in Flash memory, entire contents must be erased to
program it again. The contents are erased by the ROM burner. Atmel is
working on a newer version of AT89C51 that can be programmed using the
serial COM port of IBM PC in order to get rid of the ROM burner.
iii. DS5000 from Dallas Semiconductor – Dallas Semiconductor fabricated the
NV-RAM version of the 8051 which is known as DS5000. The PC serial port
is utilized to load the program onto the in-built ROM. The advantage of NV-
RAM memory is the facility to erase the contents one byte at a time.

iv. One - Time - Programmable (OTP) versions of the 8051 – This version of
microcontroller is cheaper and available from various manufacturers. The
manufacturers use OTP microcontroller for mass production because the
price per unit is very cheap.

Microcontroller structure

The basic structure and block diagram of a microcontroller.

CPU

CPU is the brain of a microcontroller .CPU is responsible for fetching the


instruction, decodes it, and then finally executed. CPU connects every part of a
microcontroller into a single system. The primary function of CPU is fetching and
decoding instructions. Instruction fetched from program memory must be
decoded by the CPU.
Memory

The function of memory in a microcontroller is same as microprocessor. It is


used to store data and program. A microcontroller usually has a certain amount
of RAM and ROM (EEPROM, EPROM, etc) or flash memories for storing program
source codes.

Parallel input/output ports

Parallel input/output ports are mainly used to drive/interface various devices


such as LCD‟S, LED‟S, printers, memories, etc to a microcontroller.

Serial ports

Serial ports provide various serial interfaces between microcontroller and other
peripherals like parallel ports.

Timers/counters

This is the one of the useful function of a microcontroller. A microcontroller may


have more than one timer and counters. The timers and counters provide all
timing and counting functions inside the microcontroller. The major operations of
this section are perform clock functions, modulations, pulse generations,
frequency measuring, making oscillations, etc. This also can be used for
counting external pulses.

Analog to Digital Converter (ADC)

ADC converters are used for converting the analog signal to digital form. The
input signal in this converter should be in analog form (e.g. sensor output) and
the output from this unit is in digital form. The digital output can be use for
various digital applications (e.g. measurement devices).

Digital to Analog Converter (DAC)

DAC perform reversal operation of ADC conversion. DAC convert the digital
signal into analog format. It usually used for controlling analog devices like DC
motors, various drives, etc.

Interrupt control

The interrupt control used for providing interrupt (delay) for a working program
.The interrupt may be external (activated by using interrupt pin) or internal (by
using interrupt instruction during programming).
Special functioning block

Some microcontrollers used only for some special applications (e.g. space
systems and robotics) these controllers containing additional ports to perform
such special operations.
This considered as special functioning block.

Features of 8051

This microcontroller is also called as “System on a chip” because it has all the features
on a single chip. The Block Diagram of 8051 Microcontroller is as shown in Figure

The main features of 8051 microcontroller are:

RAM – 128 Bytes (Data memory)


ROM – 4Kbytes (ROM signify the on – chip program space)
Serial Port – Using UART makes it simpler to interface for serial
communication.
Two 16 bit Timer/ Counter
Input/output Pins – 4 Ports of 8 bits each on a single chip.
6 Interrupt Sources
8 – bit ALU (Arithmetic Logic Unit)
Harvard Memory Architecture – It has 16 bit Address bus (each of RAM and
ROM) and 8 bit Data Bus.
8051 can execute 1 million one-cycle instructions per second with a clock
frequency of 12MHz.

Memory Architecture

The 4 discrete types of memory in 8051 are:

i. Internal RAM – This memory is located from address 0 to 0xff. The memory
locations from 0x00 to 0x7F are accessed directly. The bytes from 0x20 to
0x2F are bit-addressable. Loading R0 and R1 the memory location from 0x80
to 0xFF can easily accessed.

ii. Special Function Registers (SFR) – Located from address 0x80 to 0xFF of the
memory location. The same instructions used for lower half of Internal RAM
can be used to access SFR‟s. The SFR‟s are bit addressable too.

iii. Program Memory – This is read only memory which is located at address 0.
With the help of 16 bit Special Function Register DPTR, this memory can also
save the tables of constants.

iv. External Data Memory – Located at address 0. The Instruction MOVX (Move
External) should be used to access the external data memory.

History of 8051 and Key Developments

Intel Corporation fabricated the 8 – bit microcontroller which was referred as MCS-51 in
1981. This microcontroller was also referred as “system on a chip” because it has 128
bytes of RAM, 4Kbytes of ROM, 2 Timers, 1 Serial port, and four ports on a single chip.
The CPU can work for only 8bits of data at a time because 8051 is an 8-bit processor. In
case the data is larger than 8 bits then it has to be broken into parts so that the CPU can
process conveniently. Most manufacturers have put 4Kbytes of ROM even though the
quantity of ROM can be exceeded up to 64 K bytes.

Intel permitted other manufacturers to fabricate different versions of 8051 but with the
limitation that code compatibility should be maintained. This has added advantage that
if the program is written then it can be used for any version of 8051 despite of
manufacturer.

As years passed by, the quality of technology surpassed the expectation of the greatest
minds, with gadgets becoming smaller, sleeker and more efficient. Microcontrollers
were seen as the answer to the requirements raised in advanced electronics. This is the
reason why manufacturers have now focused their production around the following
main developmental aspects:

i. Ease-of-use
ii. Market availability
iii. Less power usage
iv. Smaller processing power
v. More integrated features like RF and USB
vi. Smaller form factors

Applications

The 8051 has been in use in a wide number of devices, mainly because it is easy to
integrate into a project or build a device around. The following are the main areas of
focus:

i. Energy Management: Efficient metering systems help in controlling energy


usage in homes and industrial applications. These metering systems are made
capable by incorporating microcontrollers.

ii. Touch screens: A high number of microcontroller providers incorporate touch-


sensing capabilities in their designs. Portable electronics such as cell phones,
media players and gaming devices are examples of microcontroller-based touch
screens.

iii. Automobiles: The 8051 finds wide acceptance in providing automobile


solutions. They are widely used in hybrid vehicles to manage engine variants.
Additionally, functions such as cruise control and anti-brake system have been
made more efficient with the use of microcontrollers.

iv. Medical Devices: Portable medical devices such as blood pressure and glucose
monitors use microcontrollers will to display data, thus providing higher
reliability in providing medical results.
1. Program to turn ON the LED.

#include<reg52.h>
sbit d1=P1^6;
sbit d2=P3^0;
sbit d3=P3^1;

/*||||||||||||||||||||||||||||||||||||||

SW1 (PORT3, BIT2)


SW2 (PORT3, BIT3)

SW3 (PORT3, BIT4)

RST To reset the microcontroller


NOTE: SW1 can be used as external interrupt (INT0)
SW2 can be used as external interrupt (INT1)

SENSORS:
+ VCC

- GND

S3 AD4
S2 AD5
S1 AD6
S0 AD7

NOTE: This port is also used as 8 bit/10 bit ADC input channel.

LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)

LCD CONTROL PIN(RW) GND


LCD CONTROL PIN(EN) (PORT1, BIT5)

LCD DATA PINS (PORT1, BIT0-3)


LED‟S:
LED1(D1) (PORT1, BIT6)

LED2(D2) (PORT3, BIT0)


LED3(D3) (PORT3, BIT1)

LED4(D6) Programming LED


LED5(PWR-ON) Power supply indication

MOTOR DRIVER IC:


MOTOR DRIVER 1 (PORT 2, BIT(0-3))

MOTOR DRIVER 2 (PORT 2, BIT(4-7)) */

void delay()
{
unsigned int i,j;
for(i=0;i<500;i++)
{
for(j=0;j<400;j++);
}
}

void main()
{
d1=0;
d2=0;
d3=0; //LED1,LED2,LED3 TURN OFF
delay();
while(1)
{

d1=1; //LED1 TURN ON

d2=1; //LED2 TURN ON

d3=1; //LED3 TURN ON

}
2. Program to toggle the LED.

#include<reg52.h>
sbit d1=P1^6;
sbit d2=P3^0;
sbit d3=P3^1;

/*||||||||||||||||||||||||||||||||||||||

SW1 (PORT3, BIT2)


SW2 (PORT3, BIT3)
SW3 (PORT3, BIT4)

RST To reset the microcontroller


NOTE: SW1 can be used as external interrupt (INT0)
SW2 can be used as external interrupt (INT1)

SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7

NOTE: This port is also used as 8 bit/10 bit ADC input channel.

LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)

LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication

MOTOR DRIVER IC:


MOTOR DRIVER 1 (PORT 2,BIT(0-3))
MOTOR DRIVER 2 (PORT 2,BIT(4-7)) */
void delay()
{
unsigned int i,j;
for(i=0;i<500;i++)
{
for(j=0;j<400;j++);
}
}

void main()
{
d1=0;
d2=0;
d3=0; /*TURN OFF LED1, LED2, LED3 */
delay();
while(1)
{

d1=1;

d2=1;

d3=1; /*TURN ON LED1,LED2,LED3*/

delay();

d1=0;

d2=0;

d3=0; /*TURN OFF LED1, LED2, LED3*/

delay();

}
3. PROGRAM TO DISPLAY SLOGAN1 ON FIRST LINE AND DISPLAY SLOGAN2 ON
SECOND LINE OF THE LCD

#include<reg52.h>
#include"lcd.h"
sbit rs=P1^4;
sbit lcde=P1^5;
sbit d4=P1^0;
sbit d5=P1^1;
sbit d6=P1^2;
sbit d7=P1^3;
sbit motor1=P2^0;
code const unsigned char slogan1[]="THINNKWARE ";
code const unsigned char slogan2[]=" WELCOME ";

/*||||||||||||||||||||||||||||||||||||||

SW1 (PORT3, BIT2)


SW2 (PORT3, BIT3)
SW3 (PORT3, BIT4)

RST: To reset the microcontroller


NOTE: SW1 can be used as external interrupt (INT0)
SW2 can be used as external interrupt (INT1)

SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7

NOTE: This port is also used as 8 bit/10 bit ADC input channel.

LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)

LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication

MOTOR DRIVER IC:


MOTOR DRIVER 1 (PORT 2,BIT(0-3))
MOTOR DRIVER 2 (PORT 2,BIT(4-7)) */

/************FUNCTION FOR SWAPPING LSBYTE AND MSBYTE OF THE DATA***********/

unsigned char xch(unsigned char data1)


{
unsigned char temp,temp1;
temp=data1;
data1=data1>>4;
temp1=data1;
data1=temp;
data1=data1<<4;
data1=data1|temp1;
return(data1);
}

/**********FUNCTION FOR 4BIT DATA TRANSFER ON LCD PINS IN 4-BIT MODE*********/


void datatr(unsigned char data1)
{
d4=data1&0x01;
data1=data1>>1;
d5=data1&0x01;
data1=data1>>1;
d6=data1&0x01;
data1=data1>>1;
d7=data1&0x01;
}

/********************** INITIALIZATION OF LCD ***********************************/


void lcdinit()
{
clr_lcd(); /*FUNCTION SET */
send_command(0x28);
delay();
send_command(0x28);
delay();
send_command(0x28);
delay();
send_command(0x06); //ENTRY MODE
delay();

send_command(0x0e); //DISPLAY ON/OFF


delay();

clr_lcd();

/******************FUNCTION FOR DISPLAYING DATA ON THE LCD ******************/

void dispslogan(unsigned char *p)


{
unsigned char data1;
while(*p)
{
data1=*p;
senddata(data1);
p++;
}
}

/***************** FUNCTION FOR SENDING LCD COMMANDS*****************/

void send_command(unsigned char data1)


{
unsigned char data2;

rs=0;
delay();
lcde=1;
delay();
data2=xch(data1);
datatr(data2);
lcde=0;
delay();
lcde=1;
datatr(data1);
delay();
lcde=0;
delay();
rs=1;

/****************** FUNCTION FOR WRITING DATA ON THE LCD ******************/

void senddata(unsigned char data1)


{
unsigned char data2;

rs=1;
delay();
lcde=1;
delay();
data2=xch(data1);
datatr(data2);
lcde=0;
delay();
lcde=1;
datatr(data1);
delay();
lcde=0;
delay();
rs=0;
}

/*************************** delay for 20 micro second ***************************/

void delay()
{
unsigned char i,j;
for(i=0;i<20;i++)
{
for(j=0;j<100;j++)
{}
}}

/**************COMMAND FOR BRINGING LCD CURSOR ON SECOND LINE **************/

void next_line()
{
send_command(0xc0);
delay();
}

/****COMMAND FOR CLEARING LCD AND BRINGING LCD CURSOR ON FIRST LINE****/

void clr_lcd()
{
send_command(0x01);
delay();
send_command(0x02);
delay();
}

void main()
{

delay();
delay();

lcdinit(); //INITIALIZE THE LCD


clr_lcd();
while(1)
{
motor1=0;
dispslogan(slogan1); //DISPLAY SLOGAN1 ON FIRST LINE
next_line();
dispslogan(slogan2); //DISPLAY SLOGAN2 ON SECOND LINE
}
}

4. PROGRAM TO DEMONSTRATE SWITCH PRESS DETECTION

#include<reg52.h>
sbit d1=P1^6;
sbit d2=P3^0;
sbit d3=P3^1;
sbit sw1=P3^2;
sbit sw2=P3^3;
sbit sw3=P3^4;

/*||||||||||||||||||||||||||||||||||||||
SW1 (PORT3, BIT2)
SW2 (PORT3, BIT3)

SW3 (PORT3, BIT4)

RST To reset the microcontroller


NOTE: SW1 can be used as external interrupt (INT0)
SW2 can be used as external interrupt (INT1)

SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7

NOTE: This port is also used as 8 bit/10 bit ADC input channel.

LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)

LED‟S:
LED1(D1) (PORT1, BIT6)

LED2(D2) (PORT3, BIT0)


LED3(D3) (PORT3, BIT1)

LED4(D6) Programming LED


LED5(PWR-ON) Power supply indication

MOTOR DRIVER IC:


MOTOR DRIVER 1 (PORT 2,BIT(0-3))
MOTOR DRIVER 2 (PORT 2,BIT(4-7)) */

void main()
{
d1=0;
d2=0;
d3=0;
while(1) //MAIN BODY OF THE LOOP
{
if(sw1==0)
d1=1;
else
d1=0; //IF SWITCH1 PRESSED,LED1 GLOWS ELSE LED1 TURNED OFF
if(sw2==0)
d2=1;
else
d2=0; //IF SWITCH2 PRESSED,LED2 GLOWS ELSE LED2 TURNED OFF
if(sw3==0)
d3=1;
else
d3=0; //IF SWITCH3 PRESSED,LED3 GLOWS ELSE LED3 TURNED OFF

5. Program to on/off DC motor1, DC motor2 based on switch1 inputs

#include<reg52.h>
sbit sw1=P3^2;
sbit motor1_plus=P2^0;
sbit motor1_minus=P2^1;
sbit motor2_plus=P2^2;
sbit motor2_minus=P2^3;

/*||||||||||||||||||||||||||||||||||||||

SW1 (PORT3, BIT2)


SW2 (PORT3, BIT3)
SW3 (PORT3, BIT4)

RST To reset the microcontroller


NOTE: SW1 can be used as external interrupt (INT0)
SW2 can be used as external interrupt (INT1)

SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7

NOTE: This port is also used as 8 bit/10 bit ADC input channel.

LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)

LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication

MOTOR DRIVER IC:


MOTOR DRIVER 1 (PORT 2,BIT(0-3))
MOTOR DRIVER 2 (PORT 2,BIT(4-7)) */

/* MAIN LOOP*/

void main()
{
while(1)
{
if(sw1==0) /*MOTOR1,MOTOR2 TURNS ON BASED ON SWITCH1 PRESS*/
{
motor1_plus=1;
motor1_minus=0;
motor2_plus=1;
motor2_minus=0;
}
else /*MOTOR1,MOTOR2 TURNS OFF BASED ON SWITCH1 NOT PRESSED */
{
motor1_plus=1;
motor1_minus=1;
motor2_plus=1;
motor2_minus=1;
}

}}
6. PROGRAM TO DEMONSTRATE STEPPER MOTOR'S MOVEMENT IN FORWARD
AND REVERSE DIRECTIONS

#include<reg52.h>
#include"stepper.h"
sbit coil_a=P2^0;
sbit coil_b=P2^1;
sbit coil_c=P2^2;
sbit coil_d=P2^3;
sbit sw1=P3^2;

/*||||||||||||||||||||||||||||||||||||||

SW1 (PORT3, BIT2)


SW2 (PORT3, BIT3)
SW3 (PORT3, BIT4)

RST To reset the microcontroller


NOTE: SW1 can be used as external interrupt (INT0)
SW2 can be used as external interrupt (INT1)

SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7

NOTE: This port is also used as 8 bit/10 bit ADC input channel.

LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)

LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication
MOTOR DRIVER IC:
MOTOR DRIVER 1 (PORT 2,BIT(0-3))
MOTOR DRIVER 2 (PORT 2,BIT(4-7)) */

/****************function for setting stepper motor delay**********************/


void stepperdelay()
{
unsigned int i,j;
for(i=0;i<50;i++)
{
for(j=0;j<20;j++);
}
}

/********************starting of MOTOR CONTROL PROJECT****/

void main()
{

/************starting of the main loop***************************************/


while(1)
{
coil_a=1;
coil_b=1;
coil_c=1;
coil_d=1;
if(sw1==0)
fwdstepper(); //stepper motor running in fwd dirn. if sw1 pressed
else
revstepper(); //stepper motor running in reverse dirn. if sw1 not pressed
}}

/***********function for running the stepper motor in forwrd


direction**************************/

void fwdstepper()
{

coil_a=0;
coil_b=1;
coil_c=1;
coil_d=1;
stepperdelay();
coil_a=1;
coil_b=0;
coil_c=1;
coil_d=1;
stepperdelay();
coil_a=1;
coil_b=1;
coil_c=0;
coil_d=1;
stepperdelay();
coil_a=1;
coil_b=1;
coil_c=1;
coil_d=0;
stepperdelay();
}

/***********function for running the stepper motor in reverse direction*******************/

void revstepper()
{
coil_a=1;
coil_b=1;
coil_c=1;
coil_d=0;
stepperdelay();
coil_a=1;
coil_b=1;
coil_c=0;
coil_d=1;
stepperdelay();
coil_a=1;
coil_b=0;
coil_c=1;
coil_d=1;
stepperdelay();
coil_a=0;
coil_b=1;
coil_c=1;
coil_d=1;
stepperdelay();

}
7. PROGRAM TO RUN A COUNTER COUNTING FROM 0-99 ON THE SEVEN SEGMENT

#include<reg51.h>
sbit com0=P3^0;
sbit com1=P3^7;
#define segport P2
unsigned char degdata[]={0x81,0xE7,0x92,0xC2,0xE4,0xC8,0x8C,0xE3,0x80,0xE0,0Xff};
unsigned char unit,tens,increment_count;

/*||||||||||||||||||||||||||||||||||||||
SW1 (PORT3, BIT2)
SW2 (PORT3, BIT3)
SW3 (PORT3, BIT4)

RST To reset the microcontroller


NOTE: SW1 can be used as external interrupt (INT0)
SW2 can be used as external interrupt (INT1)

SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7

NOTE: This port is also used as 8 bit/10 bit ADC input channel.

LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)

LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication

MOTOR DRIVER IC:


MOTOR DRIVER 1 (PORT 2,BIT(0-3))
MOTOR DRIVER 2 (PORT 2,BIT(4-7)) */
void segdelay()
{
unsigned int i,j;
for(i=0;i<50;i++)
{
for(j=0;j<40;j++)
{}
}}

/*function to implement 0-99 counter*/

void increment()
{
increment_count++;
if(unit<9)
{
if(increment_count==20)
{
increment_count=0;
unit++; //increment unit counter
}}
else if(increment_count==20)
{
increment_count=0;
unit=0;
if(tens<9)
tens++; //increment tens counter
else
tens=0;
}
}

void display()
{
com0=1; //turn on segment1 display
com1=0;
segport=0xff;
segdelay();
segport=degdata[unit]; //display unit count on seven segment display
segdelay();
com1=1;
com0=0; //turn on segment2 display
segport=0xff;
segdelay();
segport=degdata[tens]; //display tens count on seven segment display
segdelay();
}

void main()
{
unit=0;
tens=0;
while(1)
{
display(); //seven segment display function
increment(); //0-99 counter function
}
}

8. PROGRAM TO DEMONSTRATE MATRIX KEYPAD FUNCTIONING

#include<reg52.h>
#include"matrix_keypad.h"
#include"lcd.h"
sbit rs=P1^4;
sbit lcde=P1^5;
sbit d4=P1^0;
sbit d5=P1^1;
sbit d6=P1^2;
sbit d7=P1^3;
sbit row0=P2^0;
sbit row1=P2^1;
sbit row2=P2^2;
sbit row3=P2^3;
sbit col0=P2^4;
sbit col1=P2^5;
sbit col2=P2^6;
sbit col3=P2^7;
bit flag;
unsigned keyno;

/*||||||||||||||||||||||||||||||||||||||
SW1 (PORT3, BIT2)
SW2 (PORT3, BIT3)
SW3 (PORT3, BIT4)

RST To reset the microcontroller


NOTE: SW1 can be used as external interrupt (INT0)
SW2 can be used as external interrupt (INT1)

SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7

NOTE: This port is also used as 8 bit/10 bit ADC input channel.

LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)

LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication

MOTOR DRIVER IC:


MOTOR DRIVER 1 (PORT 2,BIT(0-3))
MOTOR DRIVER 2 (PORT 2,BIT(4-7)) */

/************FUNCTION FOR SWAPPING LSBYTE AND MSBYTE OF THE DATA***********/

unsigned char xch(unsigned char data1)


{
unsigned char temp,temp1;
temp=data1;
data1=data1>>4;
temp1=data1;
data1=temp;
data1=data1<<4;
data1=data1|temp1;
return(data1);
}
/*********FUNCTION FOR 4BIT DATA TRANSFER ON LCD PINS IN 4-BIT MODE*********/

void datatr(unsigned char data1)


{
d4=data1&0x01;
data1=data1>>1;
d5=data1&0x01;
data1=data1>>1;
d6=data1&0x01;
data1=data1>>1;
d7=data1&0x01;
}

/********************** INITIALIZATION OF LCD ***********************************/


void lcdinit()
{
clr_lcd(); /*FUNCTION SET */
send_command(0x28);
delay();
send_command(0x28);
delay();
send_command(0x28);
delay();
send_command(0x06); //ENTRY MODE
delay();

send_command(0x0c); //DISPLAY ON/OFF


delay();

clr_lcd();

/***************** FUNCTION FOR DISPLAYING DATA ON THE LCD *****************/

void dispslogan(unsigned char *p)


{
unsigned char data1;
while(*p)
{
data1=*p;
senddata(data1);

p++;
}
}

/***************** FUNCTION FOR SENDING LCD COMMANDS *****************/

void send_command(unsigned char data1)


{
unsigned char data2;

rs=0;
delay();
lcde=1;
delay();
data2=xch(data1);
datatr(data2);
lcde=0;
delay();
lcde=1;
datatr(data1);
delay();
lcde=0;
delay();
rs=1;

/********************* FUNCTION FOR WRITING DATA ON THE LCD *********************/

void senddata(unsigned char data1)


{
unsigned char data2;

rs=1;
delay();
lcde=1;
delay();
data2=xch(data1);
datatr(data2);
lcde=0;
delay();
lcde=1;
datatr(data1);
delay();
lcde=0;
delay();
rs=0;
}

/************ delay for 20 micro second **********************************************/

void delay()
{
unsigned char i,j;
for(i=0;i<100;i++)
{
for(j=0;j<20;j++)
{}
}}

/*********** COMMAND FOR BRINGING LCD CURSOR ON SECOND LINE *************/

void next_line()
{
send_command(0xc0);
delay();
}

/**************COMMAND FOR CLEARING LCD AND BRINGING LCD CURSOR ON FIRST


LINE********/

void clr_lcd()
{
send_command(0x01);
delay();
send_command(0x02);
delay();
}

/*FUNCTION TO SENSE ANY OF THE 4*4 KEYS PRESS ACTION*/

void keychk()
{
row0=0;
row1=1;
row2=1;
row3=1; //CLEAR ROWO PORT PIN AND SET ROW1,ROW2,ROW3 PORT PINS
if(col0==0)
{
keyno=0x00; //MAKE KEYNO=00H IF ROW0=0,COL0=0
return;
}
if(col1==0)
{
keyno=0x01; //MAKE KEYNO=01H IF ROW0=0,COL1=0
return;
}
if(col2==0)
{
keyno=0x02; //MAKE KEYNO=02H IF ROW0=0,COL2=0
return;
}
if(col3==0)
{
keyno=0x03; //MAKE KEYNO=03H IF ROW0=0,COL3=0
return;
}
row0=1;
row1=0;
row2=1;
row3=1; //CLEAR ROW1 PORT PIN AND SET ROW0,ROW2,ROW3 PORT PINS
if(col0==0)
{
keyno=0x04; //MAKE KEYNO=04H IF ROW1=0,COL0=0
return;
}
if(col1==0)
{
keyno=0x05; //MAKE KEYNO=05H IF ROW1=0,COL1=0
return;
}
if(col2==0)
{
keyno=0x06; //MAKE KEYNO=06H IF ROW1=0,COL2=0
return;
}
if(col3==0)
{
keyno=0x07; //MAKE KEYNO=07H IF ROW1=0,COL3=0
return;
}
row0=1;
row1=1;
row2=0;
row3=1; //CLEAR ROW2 PORT PIN AND SET ROW0,ROW1,ROW3 PORT PINS
if(col0==0)
{
keyno=0x08; //MAKE KEYNO=08H IF ROW2=0,COL0=0
return;
}
if(col1==0)
{
keyno=0x09; //MAKE KEYNO=09H IF ROW2=0,COL1=0
return;
}
if(col2==0)
{
keyno=0x10; //MAKE KEYNO=10H IF ROW2=0,COL2=0
return;
}
if(col3==0)
{
keyno=0x11; //MAKE KEYNO=11H IF ROW2=0,COL3=0
return;
}
row0=1;
row1=1;
row2=1;
row3=0; //CLEAR ROW3 PORT PIN AND SET ROW0,ROW1,ROW2 PORT PINS
if(col0==0)
{
keyno=0x12; //MAKE KEYNO=12H IF ROW3=0,COL0=0
return;
}
if(col1==0)
{
keyno=0x13; //MAKE KEYNO=13H IF ROW3=0,COL1=0
return;
}
if(col2==0)
{
keyno=0x14; //MAKE KEYNO=14H IF ROW3=0,COL2=0
return;
}
if(col3==0)
{
keyno=0x15; //MAKE KEYNO=15H IF ROW3=0,COL3=0
return;
}
flag=0;
keyno=0x50; //MAKE KEYNO=50H AND flag=0 for no key press
}

//function to display 0-f on lcd based on key press detection

void keyaction()
{
if(keyno==0x50)
return;
if(flag==1)
return;
else
{
flag=1; //key-bouncing protection code
if(keyno==0x00)
senddata(48);
if(keyno==0x01)
senddata(49);
if(keyno==0x02)
senddata(50);
if(keyno==0x03)
senddata(51);
if(keyno==0x04)
senddata(52);
if(keyno==0x05)
senddata(53);
if(keyno==0x06)
senddata(54);
if(keyno==0x07)
senddata(55);
if(keyno==0x08)
senddata(56);
if(keyno==0x09)
senddata(57);
if(keyno==0x10)
senddata('A');
if(keyno==0x11)
senddata('B');
if(keyno==0x12)
senddata('C');
if(keyno==0x13)
senddata('D');
if(keyno==0x14)
senddata('E');
if(keyno==0x15)
senddata('F'); //display 0-f if keyno=00h-15h
}}

void main()
{
lcdinit();
delay();
next_line();
dispslogan("keypad_display");
while(1)
{
keychk(); //key sense function
send_command(0x80);
keyaction(); //function to display 0-f on lcd based on key press detection
}}

You might also like