embedded systems training report
TRANSCRIPT
9/24/2012
8051 uC Using C | AKHIL GARG | 2809154
DKOP
LABS
PVT.
LTD.
“EMBEDDED SYSTEMS”
1
Summer Training Report
On
EMBEDDED SYSTEMS
Submitted in the partial fulfillment of the requirement for the award of
degree of
BACHELOR OF TECHNOLOGY
IN
ELECTRONICS & COMMNICATION ENGINEERING
Submitted by:
AKHIL GARG
University Roll No. 2809154
Under the guidance of:
Mr. JATIN GARG
DKOP Labs Pvt. Ltd., Noida
DEPARTMENT OF ELECTRONICS & COMMUNICATION ENGINEERING
PANIPAT INSTITUTE OF ENGINEERING & TECHNOLOGY
PATTIKALYANA, SAMALKHA, PANIPAT (132102) SESSION 2009 TO 2013
2
CERTIFICATE
This is certify that Mr. AKHIL GARG, student of P.I.E.T., Samalkha bearing Roll no.
2809154 of the department of Electronics & Communication, has successfully undergone
training in EMBEDDED SYSTEMS from 16th July 2012 to 08th Sept 2012.
During his training duration, the regularity and code of conduct was found
satisfactory.
Mr. JASPAL JINDAL,
H.O.D. (ECE),
P.I.E.T, Samalkha
3
ACKNOWLEDGEMENT
I would like to thank all the people without those help this training would not have
been completed. Firstly, I would like to express my gratitude and appreciation to
Mr. Jatin Garg and Mr. Abhishek Goel (course trainers) who explained me
everything about the training process , my guide was extremely patient and open to
whatever question I had to make and in the process gently guide me whenever I
went wrong. I would like to thank Mr. JASPAL JINDAL (H.O.D ECE
Department) P.I.E.T, Samalkha, who gave me chance to work with this institute.
Then I would like to thank my parents without whom I can never learn from such a
great institution.
I would be failing in my duty if I do not express my gratitude to the almighty
GOD.
4
PREFACE
Practical exposure in the field of embedded system is extremely important as it gives a close
view of the real electronic world issues. It helps to cover all parts that remained uncovered in the
classroom. It helps to gain experience. Just theoretical knowledge is not sufficient for the success
of any engineer student. So one should have practical knowledge about each theory of life.
I learnt lot of new things from this training, which could never have been learnt from theory
classes.
If any findings & recommendations go in any way to prove some new ground in helping the
commodity future sector, I shall deem my efforts have dully served the purpose. In the
forthcoming pages an attempt has been made to present report covering different aspects of my
training.
5
Contents
1.INTRODUCTION TO EMBEDDED SYSTEM ......................................................................... 7
1.1 EMBEDDED SYSTEM ........................................................................................................ 7
1.2EXAMPLES OF EMBEDDED SYSTEM:- .......................................................................... 8
2. MICROPROCESSOR v/s MICROCONTROLLER .................................................................. 8
2.1MICROPROCESSOR (MPU) ............................................................................................... 8
2.2 MICROCONTROLLERS (MCU) ........................................................................................ 9
2.3 COMPARISON BETWEEN MICROPROCESSORS & MICROCONTROLLERS:- . 9
2.4. ARCHITECTURES ........................................................................................................... 10
2.4.1.VON NEUMANN:- ...................................................................................................... 10
2.4.2.HARVARD:- ................................................................................................................ 10
2.5Comparison of the RISC and CISC architecture:- ............................................................... 12
3. THE “8051” MICROCONTROLLER FAMILY:- ................................................................... 12
3.2. FEATURES OF THE P89V51RD2:- ................................................................................. 13
3.3. PIN DESCRIPTIONOF THE P89C51RD2:- ..................................................................... 14
3.4. PROGRAMMING MODEL OF 8051: .............................................................................. 16
3.4.1. ACCUMULATOR (REGISTER A): ........................................................................... 16
3.4.2. B REGISTER: ............................................................................................................. 17
3.5. SPECIALFUNCTIONREGISTER(SFR)MEMORY:- ................................................. 17
3.6. BASIC REGISTERS .......................................................................................................... 17
3.6.1. THE ACCUMULATOR.............................................................................................. 17
3.6.2. THE DATA POINTER (DPTR).................................................................................. 17
3.6.3. THE PROGRAM COUNTER (PC) ............................................................................ 17
3.6.4. THE STACK POINTER (SP) ..................................................................................... 18
4. Summary of 8051 ...................................................................................................................... 19
5. Programming in C ..................................................................................................................... 20
5.1. W.A.P. to blink LEDs connected on port ........................................................................... 21
5.2. W.A.P. in C for 8051 to show 3 leds running .................................................................... 22
5.3. W.A.P. in c for 8051 to show 0-9 on a 7segment .............................................................. 23
5.4. W.A.P. in C for 8051 to show 1234 continuously on a 4digit 7segment display. ............. 24
6
5.5. WAP to interface keypad with 8051 showing the pressed key on LEDs. .......................... 25
5.6. WAP to interface keypad with 8051 showing the pressed key on 4digit 7seg. ................ 27
5.7. WAP to interface LCD with 8051 displaying your name on it. ......................................... 31
5.8. WAP for 8051 to configure the timer operation ................................................................ 33
5.9. WAP for 8051 to serially transfer your name .................................................................... 34
5.10. WAP for 8051 to generate a GSM call ............................................................................ 35
5.11. WAP for 8051 to demonstrate Interrupts ......................................................................... 36
5.12 WAP for 8051 to use relay at pin P1.0 .............................................................................. 36
5.13. WAP for 8051 to start the buzzer ..................................................................................... 37
6. DIFFERENT TOOLS REQUIRED .......................................................................................... 38
6.1 STEPS TO WRITE A PROGRAM IN KEIL COMPILER ................................................ 38
6.2 Steps to use FLASH-MAGIC ............................................................................................. 44
6.3 PROTEUS7:ISIS PROFESSIONAL.............................................................................. 47
7. CONCLUSION ..................................................................................................................... 48
8. BIBLIOGRAPHY ................................................................................................................. 49
7
1.INTRODUCTION TO EMBEDDED SYSTEM
Microcontroller 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 perform 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 task 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 are 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, Motorolla, AMD & 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, Motorolla 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. In higher performance Embedded system
the trend is to integrate more & more function on the CPU chip & let the designer decide which
feature he/she wants to use.
1.1 EMBEDDED SYSTEM
An Embedded System employs a combination of hardware & software (a “computational
engine”) to perform a specific function; is part of a larger system that may not be a
“computer works in a reactive and time-constrained environment. Software is used for
providing features and flexibility Hardware = {Processors, ASICs, Memory...} is used for
performance (& sometimes security).
An embedded system is a special purpose system in which the computer is completely
encapsulated by the device it controls. Unlike a general purpose computer, such as a PC,
an embedded system performs predefined task’s usually with very specific tasks design
engineers can optimize it reducing the size and cost of the product. Embedded systems
are often mass produced, so the cost savings may be multiplied by million of items.
8
The core of any embedded system is formed by one or several microprocessor or micro
controller programmed to perform a small number of tasks. In contrast to a general
purpose computer, which can run any software application, the user chooses, the software
on an embedded system is semi-permanent, so it is often called firmware.
1.2EXAMPLES OF EMBEDDED SYSTEM:-
o Automated tiller machines (ATMS).
o Avionic, such as inertial guidance systems, flight control hardware / software
and letter integrated system in aircraft and missile.
o Cellular telephones and telephonic switches.
o Computer network equipment, including routers timeservers and firewalls
o Computer printers, Copiers.
o Disk drives (floppy disk drive and hard disk drive)
o Engine controllers and antilock brake controllers for automobiles.
o Home automation products like thermostat, air conditioners sprinkles and
security monitoring system.
o House hold appliances including microwave ovens, washing machines, TV
sets DVD players/recorders.
o Medical equipment.
o Measurement equipment such as digital storage oscilloscopes, logic analyzers
and spectrum analyzers.
o Multimedia appliances: internet radio receivers, TV set top boxes.
o Personal digital assistants (PDA’s), i.e., small hand held computer with P1M5
and other applications.
o Programmable logic controllers (PLC’s) for industrial automation and
monitoring.
o Stationary video game controllers.
o Wearable computers.
2. MICROPROCESSOR v/s MICROCONTROLLER
2.1MICROPROCESSOR (MPU)
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 an
arithmetic and logical unit (ALU), a program counter (PC), a stack pointer (SP),some
working registers, a clock timing circuit, and interrupt circuits.
9
FIG2.1:BLOCK DIAGRAM OF A MICROPROCESSOR
2.2 MICROCONTROLLERS (MCU)
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.
FIG2.2:BLOCK DIAGRAM OF A MICROCONTROLLER
2.3 COMPARISON BETWEEN MICROPROCESSORS & MICROCONTROLLERS:-
The microprocessor must have many additional parts to be operational as a computer whereas
microcontroller requires no additional external digital parts.
The prime use of microprocessor is to read data, perform extensive calculations on that
data and store them in the mass storage device or display it. The prime functions of
microcontroller is to read data, perform limited calculations on it, control its environment
CPU
General-
Purpose
Micro-
processor
RAM ROM I/O
Port Timer
Serial
COM
Port
Data Bus
RAM ROM
I/O
Port Timer
Serial
COM
Port
CPU
A single chip
10
based on these data. Thus the microprocessor is said to be general-purpose digital
computers whereas the microcontroller are intend to be special purpose digital controller.
Microprocessor need many opcodes for moving data from the external memory to the
CPU, microcontroller may require just one or two, also microprocessor may
have one or two types of bit handling instructions whereas microcontrollers have many.
Thus microprocessor is concerned with the rapid movement of the code and data from the
external addresses to the chip, microcontroller is concerned with the rapid movement of
the bits within the chip.
Lastly, the microprocessor design accomplishes the goal of flexibility in the hardware
configuration by enabling large amounts of memory and I/O that could be connected to the
address and data pins on the IC package. The microcontroller design uses much more limited set
of single and double byte instructions to move code and data from internal memory to ALU.
2.4. ARCHITECTURES
There are two basic types of architecture: Harvard and Von Neumann. Microcontrollers most
often use a Harvard or a modified Harvard-based architecture.
2.4.1.VON NEUMANN:-
Von Neumann architecture has a single, common memory space where both program
instructions and data are stored. There is a single data bus which fetches both instructions
and data. Each time the CPU fetches a program instruction it may have to perform one or
more read/write operations to data memory space. It must wait until these subsequent
operations are complete before it can fetch and decode the next program instruction. The
advantage to this architecture lies in its simplicity and economy.
FIG2.4: VON NEUMANN ARCHITECTURE
2.4.2.HARVARD:-
Harvard architecture computers have separate memory areas for program instructions and
data. There are two or more internal data buses which allow simultaneous access to both
instructions and data. The CPU fetches instructions on the program memory bus. If the
fetched instruction requires an operation on data memory, the CPU can fetch the next
program instruction while it uses the data bus for its data operation. This speeds up
execution time at the cost of more hardware complexity. Since Harvard machines assume
11
that only instructions are stored in program memory space, how do you write and access
data stored in program memory space? For example, a data value declared as a C constant
must be stored in ROM as a constant value. Different microcontrollers have different
solutions to this problem. A good C compiler automatically generates the code to suit the
target hardware’s requirements. Some chips have special instructions allowing the
retrieval of information from program memory space. These instructions are always more
complex or expensive than the equivalent instructions for fetching data from data
memory. Typically these chips have a register analogous to the program counter (PC)
which refers to addresses in program space. Also, some chips support the use of any 16
bit value contained in data space as a pointer into the program address space. These chips
have special instructions to use these data pointers.
FIG2.5: HARVARD ARCHITECTURE
Pronounced risk, acronym for reduced instruction set computer, a type of microprocessor that
recognizes a relatively limited number of instructions. Until the mid-1980s, the tendency among
computer manufacturers was to build increasingly complex CPUs that had ever-larger sets of
instructions. At that time, however, a number of computer manufacturers decided to reverse this
trend by building CPUs capable of executing only a very limited set of instructions. One
advantage of reduced instruction set computers is that they can execute their instructions very fast
because the instructions are so simple. Another, perhaps more important advantage, is that RISC
chips require fewer transistors, which makes them cheaper to design and produce. Since the
emergence of RISC computers, conventional computers have been referred to as Cisks (complex
instruction set computers).
There is still considerable controversy among experts about the ultimate value of RISC
architectures. Its proponents argue that RISC machines are both cheaper and faster, and
are therefore the machines of the future. Skeptics note that by making the hardware
simpler, RISC architectures put a greater burden on the software. They argue that this is
not worth the trouble because conventional microprocessors are becoming increasingly
fast and cheap anyway.
To some extent, the argument is becoming moot because CISC and RISC
implementations are becoming more and more alike. Many of today's RISC chips support
as many instructions as yesterday's CISC chips. And today's CISC chips use many
techniques formerly associated with RISC chips.
12
The simplest way to examine the advantages and disadvantages of RISC architecture is
by contrasting it with its predecessor: CISC (Complex Instruction Set Computers)
architecture.
2.5Comparison of the RISC and CISC architecture:-
CISC RISC
Emphasis on hardware Emphasis on software
Includes multi-clock
complex instructions
Single-clock,
reduced instruction only
Memory-to-memory:
"LOAD" and "STORE"
incorporated in instructions
Register to register:
"LOAD" and "STORE"
are independent instructions
Small code sizes,
high cycles per second
Low cycles per second,
large code sizes
Transistors used for storing
complex instructions
Spends more transistors
on memory registers
TABLE2.1:CISC Vs RISC
3. THE “8051” MICROCONTROLLER FAMILY:-
(P89V51RD2):-
3.1. DESCRIPTION:-
The P89V51RD2 is an 80C51 microcontroller with 64 kB Flash and 1024 bytes of data
RAM.
A key feature of the P89V51RD2 is its X2 mode option. The design engineer can choose
to run the application with the conventional 80C51 clock rate (12 clocks per machine
cycle) or select the X2 mode (6 clocks per machine cycle) to achieve twice the
throughput at the same clock frequency. Another way to benefit from this feature is to
keep the same performance by reducing the clock frequency by half, thus dramatically
reducing the EMI.
The Flash program memory supports both parallel programming and in serial In-System
Programming (ISP). Parallel programming mode offers gang-programming at high speed,
reducing programming costs and time to market. ISP allows a device to be reprogrammed
in the end product under software control. The capability to field/update the application
firmware makes a wide range of applications possible.
13
The P89V51RD2 is also In-Application Programmable (IAP), allowing the Flash
program memory to be reconfigured even while the application is running.
3.2. FEATURES OF THE P89V51RD2:-
80C51 Central Processing Unit
5 V Operating voltage from 0 to 40 MHz
64 kB of on-chip Flash program memory with ISP (In-System Programming) and
IAP (In-Application Programming)
Supports 12-clock (default) or 6-clock mode selection via software or ISP
SPI (Serial Peripheral Interface) and enhanced UART
PCA (Programmable Counter Array) with PWM and Capture/Compare functions
Four 8-bit I/O ports with three high-current Port 1 pins (16 mA each)
Three 16-bit timers/counters
Programmable Watchdog timer (WDT)
Eight interrupt sources with four priority levels
Second DPTR register
Low EMI mode (ALE inhibit)
TTL- and CMOS-compatible logic levels
Brown-out detection
Low power modes
o Power-down mode with external interrupt wake-up
o Idle mode
PDIP40, PLCC44 and TQFP44 packages
14
3.3. PIN DESCRIPTIONOF THE P89C51RD2:-
Although 8051 family members come in different packages such DIP(dual in line
package),QFP(Quad flat package), and LLC(leadless chi0p carrier),they all have 40 pins that are
dedicated to various functions such as I/O,RD,WR,address,data and interrupts.
Wednesday, November
11, 2009
Mahdi Hassanpour
Pin Description of the 8051
PDIP/Cerdip
1234567891011121314151617181920
4039383736353433323130292827262524232221
P1.0P1.1P1.2P1.3P1.4P1.5P1.6P1.7RST
(RXD)P3.0(TXD)P3.1
(T0)P3.4(T1)P3.5
XTAL2XTAL1
GND
(INT0)P3.2
(INT1)P3.3
(RD)P3.7(WR)P3.6
VccP0.0(AD0)P0.1(AD1)P0.2(AD2)P0.3(AD3)P0.4(AD4)P0.5(AD5)P0.6(AD6)P0.7(AD7)
EA/VPPALE/PROG
PSENP2.7(A15)P2.6(A14)P2.5(A13)P2.4(A12)P2.3(A11)P2.2(A10)P2.1(A9)P2.0(A8)
8051
(8031)
FIG3.1: PIN DIAGRAM OF THE P89C51RD2
VCC:
Pin 40 provides supply voltage to the chip. The voltage source is +5 Volts.
GND:
Pin 20 is the ground.
XTAL1 and XTAL2:
The 8051 has an on chip oscillator but requires an external clock to run it. Most often a quartz
crystal oscillator is connected to inputs XTAL1 (pin 19) and XTAL2 (pin 18). The quartz crystal
oscillator connected to XTAL1 and XTAL2 also needs two capacitors of 30 pf value. One side of
each capacitor is connected to the ground. Speed refers to the maximum oscillator frequency
connected to XTAL .When the 8051 is connected to a crystal oscillator is powered up we can
observe the frequency on the XTAL2 pin using the oscilloscope.
15
RST:
Pin 9 is the RESET pin. It is an input and is active high. Upon applying a high pulse to this pin
the microcontroller well reset and terminate all activities. This is often referred to as a power on
reset .Activating a power on reset will cause all values the registers to be lost. It will set program
counter to all 0s.
In order for the RESET input to be effective it must have a minimum duration of two machine
cycles. In other words the high pulse must be high for a minimum of two machine cycles before
it is allowed to go low.
EA:
The 8051 family members such as the 8751/52, 89C51/52 or DS89C4*0 all come with on chip
ROM to store programs. In such cases the EA pin is connected to Vcc. For family members such
as the 8031 and 8032 in which there is no on chip ROM, code is stored on an external ROM and
is fetched by 8031/32. Therefore for the 8031 the EA pin must be connected to GND to indicate
that the code is stored externally. EA which stands for “external access” is pin number 31 in the
DIP packages. It is an input pin and must be connected to either Vcc or GND. In other words it
can not be unconnected.
PSEN:
This is an output pin. PSEN stands for “program store enable”. In an 8031 based system in which
an external ROM holds the program code, this pin is connected to the OE pin of the ROM.
ALE:
ALE stands for “address latch enable. It is an output pin and is active high. When connecting an
8031 to external memory, port 0 provides both address and data. In other words the 8031
multiplexes address and data through port 0 to save pins. The ALE pin is used for de-
multiplexing the address and data by connecting to G pin of the 74LS373 chip.
PORTS 0,1,2,3:
All the ports upon RESET are configured as input, since P0-P3 have value FFH on them. The
following is a summary of features of P0-P3.
PORT 0:
Port 0 is also designated as AD0-AD7 allowing it to be used for both address and data. When
connecting an 8051/31 to an external memory, port 0 provides both address and data. The 8051
multiplexes address and data through port 0 to save pins. ALE indicates if p0 has address A0-
A7.in the 8051 based systems where there is no external memory connection the pins of P0 must
be connected externally to 10k-ohm pull-up resistor. This is due to the fact that P0 is an open
drain, unlike P1, P2 and P3. Open drain is a term used for MOS chips in the same way that open
collector is used for TTL chips. In many systems using the 8751, 89c51 or DS89c4*0 chips we
normally connect P0 to pull up resistors.
PORT 1, PORT 2:
In 8051 based systems with no external memory connection both P1 and P2 are used as simple
I/O. however in 8031/51 based systems with external memory connections P2 must be used
16
along with P0 to provide the 16-bit address for the external memory. P2 is also designated as
A8-A15 indicating its dual function. Since an 8031/51 is capable of accessing 64k bytes of
external memory it needs a path for the 16 bits of address. While P0 provides the lower 8 bits via
A0-a7 it is the job P2 to provide bits A8-A15 of the address. In other words when the 8031/51 is
connected to external memory P2 is used for the upper 8 bits of the 16 bit address and it cannot
be used for I/O.
PORT 3:
Port 3 occupies a total of 8 pins 10 through 17. It can be used as input or output. P3 does not
need any pull-up resistors the same as P1 and P2 did not. Although port 3 is configured as input
port upon reset this is not the way it is most commonly used. Port 3 has the additional function of
providing some extremely important signals such as interrupts.
Port 3 Alternate functions:
P3 Bit
Function Pin
P3.0 RxD 10
P3.1 TxD 11
P3.2 INT0 12
P3.3 INT1 13
P3.4 T0 14
P3.5 T1 15
P3.6 WR 16
P3.7 RD 17
TABLE3.1: Pin description of 8051 Port 3
3.4. PROGRAMMING MODEL OF 8051:
In programming model of 8051 we have different types of registers are available and these
registers are used to store temporarily data is then the information could be a byte of data to be
processed or an address pointing to the data to be fetched the majority of registers is 8051 are 8-
bit registers.
3.4.1. ACCUMULATOR (REGISTER A):
Accumulator is a mathematical register where all the arithmetic and logical operations are
done is this register and after execution of instructions the outpour data is stored in the
register is bit addressable near. We can access any of the single bit of this register.
17
3.4.2. B REGISTER:
B register is same as that of accumulator of. It is also an 8 bit register and every bit of this
is accessible. This is also a mathematical register B which is used mostly for
multiplication and division.
3.5. SPECIALFUNCTIONREGISTER(SFR)MEMORY:-
8051 has 21 special function registers (SFR’s) at the top of the internal RAM from
address 80H to FFH.
Most of the addresses from 80H to FFH are not defined, except for 21 of them.
Some SFR’s are both bit addressable and byte addressable depending on the instruction
accessing the register.
3.6. BASIC REGISTERS
3.6.1. THE ACCUMULATOR
The Accumulator, as its name suggests, is used as a general register to accumulate the
results of a large number of instructions. It can hold an 8-bit (1-byte) value and is the
most versatile register the 8051 has due to the shear number of instructions that make use
of the accumulator. More than half of the 8051s 255 instructions manipulate or use the
accumulator in some way.
For example, if you want to add the number 10 and 20, the resulting 30 will be stored in
the Accumulator. Once you have a value in the Accumulator you may continue
processing the value or you may store it in another register or in memory.
3.6.2. THE DATA POINTER (DPTR)
The Data Pointer (DPTR) is the 8051s only user-accessible 16-bit (2-byte) register. The
Accumulator, "R" registers, and "B" register are all 1-byte values.
DPTR, as the name suggests, is used to point to data. It is used by a number of
commands, which allow the 8051 to access external memory. When the 8051 accesses
external memory, it will access external memory at the address indicated by DPTR.
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, which have nothing to do with memory locations.
3.6.3. THE PROGRAM COUNTER (PC)
The Program Counter (PC) is a 2-byte address, which tells the 8051 where the next
instruction to execute is found in memory. When the 8051 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.
18
The Program Counter is special in that there is no way to directly modify its value. That
is to say, you can’t do something like PC=2430h. On the other hand, if you execute
LJMP 2430h 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 8051 "What address are you about to execute?" As it turns out, this is not
completely true: There is one trick that may be used to determine the current value of PC.
This trick will be covered in a later chapter.
3.6.4. THE STACK POINTER (SP)
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 8051 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 8051 returns the value from the memory location
indicated by SP and then decrements the value of SP.
This order of operation is important. When the 8051 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 was mentioned two
paragraphs above: First the 8051 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 8051 by six instructions: PUSH, POP, ACALL, LCALL,
RET, and RETI. It is also used intrinsically whenever an interrupt is triggered.
19
4. Summary of 8051
The 8051 microcontroller family remains one of the most popular processors in the world. Its
ease of use and relatively high performance make it ideal for many applications, including
portable and handheld products. The introduction of Dallas Semiconductor high-speed
microcontrollers allows a way for existing 8051 designs to improve their power efficiency
without a costly redesign.
The benefits of the high-speed microcontrollers that reduce power consumption can be
summarized as follows:
A high performance CPU allows the processor clock to be slowed, resulting in the same
level of performance at less power. Alternatively, the performance of an existing system
can be increased without increasing power consumption.
The high-speed microcontroller incorporates features such as watchdog timers, additional
UARTs, and precision reset circuits. External components consume more power.
The introduction of two new low-power modes provides a low-power alternative to the
idle mode. In addition to reducing current consumption, power management modes such
as those used in the DS87C520 allow the processor to perform tasks such as polling while
in a low state. Conventional 8051 architectures require the processor to operate at the
maximum clock rate, even if only minimal processing power is required.
The benefits of a programmable clock rate and high-performance core can be combined with the
Stop mode to greatly reduce power consumption. Examples have been presented that show how
energy consumption can be reduced by matching the clock rate of the device to the desired
performance level.
20
5. Programming in C
21
5.1. W.A.P. in C for 8051 to blink LEDs connected on port #include<reg51.h>
void delay(int time);
void main()
{P1=0x00;
while(1)
{ P1=0xff;
delay(100);
P1=0x00;
delay(100);
}
}
void delay(int time)
{int i,j;
for(i=0;i<time;i++)
{for(j=0;j<1275;j++)
}
}
22
5.2. W.A.P. in C for 8051 to show 3 leds running
#include<reg51.H>
void main()
{
int i;
P1=0x00;
while(1)
{ P1=0xe0;
for(i=0;i<5;i++)
{
P1=P1>>1;
}
}
}
23
5.3. W.A.P. in c for 8051 to show 0-9 on a 7segment #include<reg51.h>
void delay(time);
void main()
{unsigned int arr[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xad,0xfd,0x87,0xff,0xef};
unsigned int i,j=0;
P1=0x00;
P2=0x00;
while(1)
{ P2=0x00;
for(i=0;i<10;i++)
{ P1=arr[i];
delay(100);
}
}
}
void delay(int time)
{int i,j;
for(i=0;i<time;i++)
{for(j=0;j<1275;j++)
}
}
24
5.4. W.A.P. in C for 8051 to show 1234 continuously on a 4digit 7segment
display. #include<reg51.h>
void delay(int time);
void main()
{
unsigned int arr[5]={0x40,0x79,0x24,0x30,0x19};// calculations for common anode display
unsigned int i,j;
P1=0x00;
P2=0x00;
while(1)
{
j=0x01;
for(i=1;i<5;i++)
{
P2=j;
j=j<<1;
P1=arr[i];
delay(1);
}
}
}
void delay(int time)
{
int i,j;
for(i=0;i<time;i++)
{for(j=0;j<1275;j++)
}
}
25
5.5. WAP to interface keypad with 8051 showing the pressed key on LEDs.
#include<reg51.h>
sbit row0=P1^0;
sbit row1=P1^1;
sbit row2=P1^2;
sbit row3=P1^3;
sbit col0=P1^4;
sbit col1=P1^5;
sbit col2=P1^6;
sbit col3=P1^7;
void keychek();
void main()
{
P1=0xff;
P2=0x00;
keychek();
}
void keychek()
{
row0=0;
if (col0==0)
{
P2=0x01;
}
if(col1==0)
{
P2=0x02;
}
if(col2==0)
{
P2=0x03;
}
row0=1;
row1=0;
if (col0==0)
{
P2=0x04;
}
26
if(col1==0)
{
P2=0x05;
}
if(col2==0)
{
P2=0x06;
}
row1=1;
row2=0;
if (col0==0)
{
P2=0x07;
}
if(col1==0)
{
P2=0x08;
}
if(col2==0)
{
P2=0x09;
}
row2=1;
}
27
5.6. WAP to interface keypad with 8051 showing the pressed key on 4digit
7seg.
#include<reg51.h>
sbit row0=P1^0;
sbit row1=P1^1;
sbit row2=P1^2;
sbit row3=P1^3;
sbit col0=P1^4;
sbit col1=P1^5;
sbit col2=P1^6;
sbit col3=P1^7;
sbit seg1=P3^7;
sbit seg2=P3^6;
sbit seg3=P3^5;
sbit seg4=P3^4;
unsigned int key,stop=0,flag=0;
unsigned int thou=0,hund=0,tens=0,ones=0;
void keychek();
void display();
void shift();
void delay(int tym);
void main()
{
P1=0xff;
P2=0x00;
P3=0x00;
while(1)
{
key=10;
keychek();
shift();
display();
}
}
void keychek()
{row0=0;
if (col0==0)
{
key=0x01;
}
28
if(col1==0)
{
key=0x02;
}
if(col2==0)
{
key=0x03;
}
row0=1;
row1=0;
if (col0==0)
{
key=0x04;
}
if(col1==0)
{
key=0x05;
}
if(col2==0)
{
key=0x06;
}
row1=1;
row2=0;
if (col0==0)
{
key=0x07;
}
if(col1==0)
{
key=0x08;
}
if(col2==0)
{
key=0x09;
}
row2=1;
row3=0;
if(col1==0)
{
key=0x00;
}
row3=1;
29
if(key==10)
{
flag=0;
}
}
void shift()
{
if(key!=10&&flag==0)
{
flag=1;
thou=hund;
hund=tens;
tens=ones;
ones=key;
}
}
void display()
{
unsigned int arr[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
seg4=0;
P2=arr[ones];
delay(10);
seg4=1;
seg3=0;
P2=arr[tens];
delay(10);
seg3=1;
seg2=0;
P2=arr[hund];
delay(10);
seg2=1;
seg1=0;
P2=arr[thou];
delay(10);
seg1=1;
}
30
void delay(int tym)
{
unsigned int i,j;
for(i=0;i<tym;i++)
{
for(j=0;j<100;j++)
{ }
}
}
31
5.7. WAP to interface LCD with 8051 displaying your name on it. #include<reg51.h>
sbit rs=P1^0;
sbit en=P1^1;
void init();
void lcd_com();
void lcd_data();
void delay(int tym);
void string();
void main()
{
P1=0x00;
P2=0x00;
init();
string();
while(1)
{}
}
void lcd_com()
{rs=0;
en=1;
delay(20);
en=0;
}
void init()
{P2=0x01;
lcd_com();
P2=0x02;
lcd_com();
P2=0x06;
lcd_com();
P2=0x0f;
lcd_com();
P2=0x3c;
lcd_com();
}
void string()
{unsigned char arr[20]={"AKHIL GARG"};
unsigned int i;
for(i=0;i<10;i++)
{P2=arr[i];
32
lcd_data();
}
}
void lcd_data()
{
rs=1;
en=1;
delay(20);
en=0;
}
void delay(int tym)
{
unsigned int i,j;
for(i=0;i<tym;i++)
{
for(j=0;j<200;j++)
{}
}
}
33
5.8. WAP for 8051 to configure the timer operation
#include<reg51.h>
void main()
{
P1=0x00;
TR1=1;
TR0=1;
TMOD=0X33;
TH1=0X44;
while(1)
{
P1=~P1;
}
}
34
5.9. WAP for 8051 to serially transfer your name
#include<reg51.h>
void uart_init(int baud);
void uart_w(char *arr,int siz);
char arr[10]={"akhil garg"};
int siz=12;
void main()
{
P1=0x00;
uart_init(9600);
uart_w(arr,siz);
}
void uart_init(int baud)
{
int b;
TMOD=0X20;
b=28800/baud;
TH1=-(b);
TR1=1;
SCON=0x50;
}
void uart_w(char *arr,int siz)
{ unsigned int i;
for(i=0;i<siz;i++)
{ SBUF=arr[i];
while(TI==0)
{}
TI=0;
}
}
35
5.10. WAP for 8051 to generate a GSM call #include<reg51.h>
void uart_init(int baud);
void uart_w(char *arr,int siz);
void msg();
char arr[20]={"ATD08802009711;"};
int siz=15;
void main()
{ P1=0x00;
uart_init(9600);
uart_w(arr,siz);
SBUF=13;
while(1)
}
void uart_init(int baud)
{ int b;
TMOD=0X20;
b=28800/baud;
TH1=-(b);
TR1=1;
SCON=0x50;
}
void uart_w(char *arr,int siz)
{ unsigned int i;
for(i=0;i<siz;i++)
{ SBUF=arr[i];
while(TI==0)
{}
TI=0;
}
}
36
5.11. WAP for 8051 to demonstrate Interrupts
#include<reg51.h>
void spec();
void main()
{
P1=0x00;
IE=0x88;
TMOD=0x20;
TH1=0xfa;
TR1=1;
while(1)
{}
}
void spec() interrupt 3
{
P1=~P1;
}
5.12 WAP for 8051 to use relay at pin P1.0 #include<reg51.h>
sbit relay1 =P1^1;
sbit relay2= P1^2;
void delay(int);
void main()
{
P1=0X00;
relay1=1;
relay2=1;
delay(1);
relay1=0;
relay2=0;
delay(1);
}
void delay(int value)
{
int y,z;
for(y=0;y<value;y++)
for(z=0;z<1275;z++);
37
5.13. WAP for 8051 to start the buzzer
#include<reg51.h>
sbit buzzer=P0^3;
void delay(unsigned int);
void main()
{
while(1)
{
P1=0X00;
buzzer=1;
delay(50);
buzzer=0;
delay(20);
buzzer=1;
delay(50);
buzzer=0;
delay(50);
buzzer=1;
delay(50);
}
}
void delay(unsigned int value)
{
int x,y;
for(y=0;y<value;y++)
for(x=0;x<1250;x++); }
38
6. DIFFERENT TOOLS REQUIRED
KEIL MICROVISION 4.0(COMPILER)
FLASH MAGIC: Programming Tool:
WINDOWS XP
8051 PROGRAMMING KIT
12V DC SUPPLY
8051 Microcontroller
Proteus 7 professional (Virtual simulator)
6.1 STEPS TO WRITE A PROGRAM IN KEIL COMPILER
1. Double Click on the icon present on the desktop.
2. The following window will be popped-up
39
3. Go to the project & click on new project
4. Make a folder on desktop & give file name.
40
5. when you click on the save button ,following window opens
6. Select NXP(founded by philips) & p89v51RD2
41
7. Then select NO on the pop-up given below.
8. Then make a New File by Ctrl+N and save it with .c or .asm extension depending on
your coding.
42
9. Go to target & then source group, right click on there & click on the option add files
to the project. Select your asm or c file which you want to add.
10. Go to the option for Flash & then Configure flash tools, click on output & tick on
create hex file.
43
11. Now build target.(Click on the pointed option)..
12. It will show you 0 errors & 0 warning on Output Window.
↑↑
As we can see here a hex file is created in the project directory.
After performing all these steps the chip will be configured through Flash Magic i.e the
program code in hex format is burnt on the 8051 uC through the Flash Magic.Let us hand
on the steps of chip configuration through Flash Magic………
44
Special Notes: -
Make all the DIP switches in off position before burning the program in the controller.
Connect the Programming Cable on your Kit (prog. Conn.)And other side of cable with
the COM Port of the Computer.
Burn the Program in the microcontroller with help of Flash Magic as explained in the
next section.
6.2 Steps to use FLASH-MAGIC
1. Double Click on the icon present on the desktop.
The following window will be popped-up
2. Press “cancel” to continue.
45
3. Configuration:Click options and then click advanced options…
4. Now set the parameters as shown below
46
5. After selection of the chip (P89C51RDHxx) , Port (Com1), Osc.Mhz(11.0592) we can see
the window as below:
6. Select the blocks to erase, browse for the hex file to be loaded. Press Start
Within 5-6 seconds the message will be displayed
*************“FINISHED”.************
47
6.3 PROTEUS7:ISIS PROFESSIONAL
48
7. CONCLUSION
From years 8051 has been the most used microcontroller and a perfect one to make a strong base
to understand the embedded programming concepts. It is helping the budding geeks to explore
the power of microcontrollers to do anything with a tiny black box packed in an inch sized chip.
It is a must for every budding engineer to study it so that various other advanced and complex
microcontrollers can be studied and explored.
Various programming techniques used help one to implement any logic so that any application
can be designed and realized.
All the basic learnt in this course will stay with me all my life.
49
8. BIBLIOGRAPHY
www.alldatasheets.com
www.datasheets.com
www.electronicsforyou.com
www.projectguidance.com
linear integrated circuits by Gayakwad
8051 microcontroller and embedded systems by Ali Mazidi
8051 microcontroller with cd by Ayala(paperback-2007)
8051 microcontroller with Mackenzie(2008)
8051 microcontroller & embedded systems by Rajeev Kapadia (paperback-2007)
www.engineersgarage.com
www.jeremyblum.com
www.8051projects.net ---- Ricky’s World