Dr.Y.Narasimha Murthy Ph.D
MICROCONTROLLERS-UNIT-I
1.1 Introduction: Any electronic system built around a single
chip computer or microcontroller to perform a specific dedicated
task /application is known as an embedded system. Software is
programmed in the on-chip ROM of the microcontroller. These
microcontrollers are also popularly known as embedded processors or
computers. Due to this reason some people also call the embedded
system as the combination of hardware, software designed for a
dedicated application. Actually the software program that runs the
application is hidden (Embed) inside the system. Hence the name
Embedded system. For low end embedded systems operating system is
not required. But for those high end real-time embedded systems
operating system is also included .Such operating systems are
popularly known as Real Time operating Systems. In today's world,
embedded systems are everywhere -- homes, offices, cars, factories,
hospitals, and consumer electronics. For Example a typical present
day contains about ten microcontrollers. The modern houses contain
as many as 150 microcontrollers and on average a consumer interacts
with nearly 300 microcontrollers a day. General embedded systems
that are used in our day to day life are , Cell phone , Digital
camera , Microwave Oven, MP3 player, Portable digital assistant,
Bank ATMs , & antilock brake system in vehicles ,computer key
boards ,Mouse ,Printers etc.Due to the unprecedented growth in
embedded system technology and the market demand ,there is also a
great increase in the design and manufacturing of innumerable
number of Microcontrollers in the market. Because without a
suitable microcontroller it is impossible to design any embedded
system. Hence various manufacturers like INTEL ,Motorola,Texas
Instruments, Microchip, Analog Devices, Philips ,Atmel, Samsung
,ARM etc have their share in the market.1.2 Characteristics of an
Embedded System : The important characteristics of an embedded
system are given below.
Speed : Should be of high speed Power : Low power dissipation
Accuracy : The output of the system must be very accurate
Adaptability: Embedded system must be highly adaptable and
accessible to different conditions and situations. Reliability :
The results must be reliable over a long period of time. Size and
weight : As far as possible small in size and low weightAn embedded
system must perform the operations at a high speed so that it can
be readily used for real time applications and its power
consumption must be very low and the size of the system should be
as for as possible small and the readings must be accurate with
minimum error. The system must be easily adaptable for different
situations.
1.3 Types of Embedded Systems: Based on their functionality and
performance Embedded systems are classified into following four
categories.(i).Stand-alone embedded systems(ii).Real-time embedded
systems ( Hard real-time systems & Soft real-time
systems)(iii).Networked embedded systems and (iv).Mobile Embedded
systems.
Based on the performance of the Microcontroller they are also
classified into (i) Small scaled embedded system (ii) Medium scaled
embedded system and (iii) Large scaled embedded system.This
classification can be shown by the figure.1.1
Fig.1.1 Classification of Embedded systemsStand alone Embedded
systems : A stand-alone embedded system works by itself. It is a
self-contained device which do not require any host system like a
computer. It accepts either analog or digital signals through its
input ports and calibrates and processes the signals and finally
outputs the resulting data to the connected output device .
Temperature measurement systems, Video game consoles, MP3 players,
digital cameras, and microwave ovens are the examples for this
category.
Real-time embedded systems : An embedded system which gives the
required output in a specified time or which strictly follows the
time deadlines for completion of a task is known as a Real time
embedded system. i.e a Real Time embedded system not only gives the
correct output but delivers the output in the specified time
also.Real Time Embedded systems are again two types(a) Soft real
time system and (b) Hard real time system.Soft Real-Time system: A
Real time system in which ,the violation of time constraints will
cause only the degraded quality, but the system can continue to
operate normally without any destruction is known as a Soft real
time system. In soft real-time systems, the design focus is to
offer a guaranteed bandwidth to each real-time task and to
distribute the resources to the tasks. Examples are Microwave oven
, Washing machine , TV remote etc.
Hard Real-Time system : A Real time system in which ,the
violation of time constraints will cause critical failure and loss
of life or property, damage or catastrophe is known as a Hard Real
time system.These systems usually interact directly with physical
hardware instead of through human being.For example, Deadline in a
missile control , Delayed alarm during a Gas leakage , car airbag
control system , A delayed response in pacemakers ,Failure in RADAR
functioning etc.Networked embedded systems: The networked embedded
systems are related to a network with network interfaces to access
the resources. The connected network can be a Local Area Network
(LAN) or a Wide Area Network (WAN), or the Internet. The connection
can be either wired or wireless.The networked embedded system is
the fastest growing area in embedded system applications. The
embedded web server is such a system where all embedded devices are
connected to a web server and can be accessed and controlled by any
web browser.Home security system is an example of a LAN networked
embedded system where all sensors (e.g. motion detectors, light
sensors, or smoke sensors) are wired and running on the TCP/IP
protocol.Mobile Embedded systems: The portable embedded devices
like mobile and cellular phones, digital camaras,MP3 players, PDA
(Personal Digital Assistants) are the example for mobile embedded
systems. The basic limitation of these devices is the limitation of
memory and other resources.Small scale embedded system : An
embedded system supported by a single 816 bit Microcontroller with
on-chip RAM and ROM designed to perform simple tasks is a Small
scale embedded system. For example , a simple toy or display system
is an example for Small Scale embedded system.Medium scale embedded
system : An embedded system supported by 1632 bit Microcontroller
/Microprocessor with external RAM and ROM that can perform more
complex operations is a Medium scale embedded system. Large scale
embedded system: An embedded system supported by 32-64 bit multiple
chips which can perform distributed jobs is considered as a Large
scale embedded system.The various Embedded systems and their
applications are given in Table 1.1
S.NoEmbedded SystemApplication
1Home AppliancesDishwasher, washing machine, microwave, Top-set
box, security system , HVAC system, DVD, answering machine, garden
sprinkler systems etc..
2Office Automation Fax, copy machine, smart phone system,
modern, scanner, printers.
3SecurityFace recognition, finger recognition, eye recognition,
building security system , airport security system, alarm
system.
4AcademiaSmart board, smart room, OCR, calculator, smart
cord.
5InstrumentationSignal generator, signal processor, power
supplier,Process instrumentation,
6TelecommunicationRouter, hub, cellular phone, IP phone, web
camera
7Automobile Fuel injection controller, anti-locking brake
system, air-bag system, GPS, cruise control.
8Entertainment MP3, video game, Mind Storm, smart toy.
9Aerospace Navigation system, automatic landing system, flight
attitude controller, space explorer, space robotics.
10Industrial automation Assembly line, data collection system,
monitoring systems on pressure, voltage, current, temperature,
hazard detecting system, industrial robot.
11PersonalPDA, iPhone, palmtop, data organizer.
12Medical CT scanner, ECG , EEG , EMG ,MRI, Glucose monitor,
blood pressure monitor, medical diagnostic device.
13Banking & Finance ATM, smart vendor machine, cash register
,Share market
14Miscellaneous:Elevators, tread mill, smart card, security door
etc.
1.4 Embedded system-Block Diagram approach An Embedded system is
a combination of hardware and software. The hardware system is
built with a microprocessor or microcontroller .A microcontroller
is an integrated chip which comes with built-in memory, I/O ports,
timers, and other components. Most embedded systems are built on
microcontrollers, which run faster than a custom-built system with
a microprocessor, because all components are integrated within that
single chip. Application software and operating systems are the
main components of the software system. But all the embedded
systems do not use the operating system. The systems with high end
applications only use operating system. To use the operating system
the embedded system should have large memory capability. So, This
is not possible in low end applications like remote systems,
digital cameras, MP3 players, robo toys etc. Since an Embedded
system constitutes both hardware and software components, the
architecture can be understood with block diagram approach where
the hardware and software aspects are shown separately. As shown in
Fig 1.2 an embedded system consists of Embedded controller along
with necessary electronic hardware components like sensors ,
drivers ,buffers etc along with communication interfaces and I/O
devices. The application software is loaded into the on chip ROM of
the microcontroller.
Fig.1.2 Block diagram of an embedded systemTo understand the
architecture of an embedded system thoroughly the hardware and
software details are considered in detail in the following
sections.1.4.1 Hardware Architecture:Coming to the hardware details
of the embedded system, it consists of the following important
blocks. CPU(Central Processing Unit) RAM and ROM I/O Devices
Communication Interfaces Sensors etc. (Application specific
circuitry)This hardware architecture is shown in the following
block diagram 1.3
. Fig.1.3 Hardware ArchitectureCentral Processing Unit : A CPU
is composed of an Arithmetic Logic Unit (ALU), a Control Unit (CU),
and many internal registers that are connected by buses. The ALU
performs all the mathematical operations (Add, Sub, Mul, Div),
logical operations (AND, OR), and shifting operations within CPU
.The timing and sequencing of all CPU operations are controlled by
the CU, which is actually built of many selection circuits
including latches and decoders .The CU is responsible for directing
the flow of instruction and data within the CPU and continuously
running program instructions step by step.
The CPU works in a cycle of fetching an instruction, decoding
it, and executing it, known as the fetch-decode-execute cycle. The
cycle begins when an instruction is fetched from a memory location
pointed to by the PC to the IR via the data bus.For embedded system
design, many factors impact the CPU selection, e.g., the maximum
size (number of bits) in a single operand for ALU (8, 16, 32, 64
bits), and CPU clock frequency for timing tick control, i.e. the
number of ticks (clock cycles) per second in measures of MHz.
Memory : Embedded system memory can be either on-chip or off-chip.
On chip memory access is much fast than off-chip memory, but the
size of on-chip memory is much smaller than the size of off-chip
memory. Usually, it takes at least two I/O ports as external
address lines plus a few control lines such as R/W and ALE control
lines to enable the extended memory. Generally the data is stored
in RAM and the program is stored in ROM.I/O Ports : The I/O ports
are used to connect input and output devices. The common input
devices for an embedded system include keypads, switches, buttons,
knobs, and all kinds of sensors (light, temperature, pressure,
etc). The output devices include Light Emitting Diodes (LED),
Liquid Crystal Displays (LCD), printers, alarms, actuators, etc.
Some devices support both input and output, such as communication
interfaces including Network Interface Cards (NIC), modems, and
mobile phones.Communication Interfaces :To transfer the data or to
interact with other devices ,the embedded devices are provided the
various communication interfaces like RS232, RS422, RS485 ,USB,
SPI(Serial Peripheral Interface ) ,SCI (Serial Communication
Interface) ,Ethernet etc.Application Specific Circuitry : The
embedded system some times receives the input from a sensor or
actuator. In such situations certain signal conditioning circuitry
is needed. This hardware circuitry may contain ADC , Op-amps ,DAC
etc. Such circuitry will interact with the embedded system to give
correct output.Power supply: Most of the embedded systems now days
work on battery operated supplies. Because low power dissipation is
always required. Hence the systems are designed to work with
batteries. 1.4.2 Software architecture: To design an efficient
embedded system , both hardware and software aspects are equally
important. The software of an embedded system is mainly aimed at
accessing the hardware resources properly. The software of an
embedded system means both operating system and application
software. But every embedded system need not contain the operating
system.For low end applications, operating system is not needed. In
such cases the designer has to write the necessary software
routines to access the hardware. The architecture of the software
in an embedded system can be shown in the figure1.4.
. Fig.1.4 Software ArchitectureThe central part or nucleus of
the operating system is the Kernel .A kernel connects the
application software to the hardware of an embedded system. The
other important components of the OS are Device manager,
Communication software, Libraries and File system. The kernel will
take care of task scheduling, priorities, memory management etc.It
manages the tasks to achieve the desired performance of the system.
It schedules the tasks and provide inter process communication
between different tasks.The device manager manages the I/O devices
through interrupts and device drivers. The device drivers provide
the necessary interface between the application and the hardware. A
device driver is a specific type of software developed to allow
interaction with hardware devices. This constitutes an interface
for communicating with the device, through the specific system bus
or communications subsystem that the hardware is connected to,
providing commands to receiving data from the device, and on the
other end, the requisite interfaces to the operating system and
software applications.The communication software provides necessary
protocols to make the embedded system network enabled. This
software integrates the upper layer protocols such as TCP/IP stack
with the operating system. Application programming interface is
used by the designer to write the application software.The API
provides the function calls to access the operating system
services.Application Specific software : It sits above the O.S. The
application software is developed according to the features of the
development tools available in the OS.These development tools
provide the function calls to access the services of the OS. These
function calls include, creating a task ,to read the data from the
port and write the data to the memory etc.The various function
calls provided by an operating system arei.To create ,suspend and
delete tasks. ii. To do task scheduling to providing real time
environment.iii.To create inter task communication and achieve the
synchronization between tasks.iv.To access the I/O devices.vi.To
access the communication protocol stack .The designer develops the
application software based on these function calls.Communication
Software: To connect to the external world through internet ,the
embedded system need a communication interface. The communication
software include the Ethernet interface and the TCP/IP protocol
suit .Now a days even small embedded systems like mobile phones
,PDAs are network enabled through these TCP/IP support. The TCP/IP
protocol suite is shown in the diagram below. Communication
Software: To connect to the external world through internet ,the
embedded system need a communication interface. The communication
software include the Ethernet interface and the TCP/IP protocol
suit .Now a days even small embedded systems like mobile phones
,PDAs are network enabled through these TCP/IP support. The TCP/IP
protocol suite is shown in the Fig1.5 below. Application layer
Transport Layer TCP/UDP
IP Layer
Data Link Layer
Physical Layer
Fig 1.5 The TCP/IP protocol suiteThis suite consists of
different layers like, application layer, Transport layer , IP
layer etc.TCP means Transmission Control Protocol. It ensures that
the data is delivered to the application layer without any errors.
The UDP (User Datagram protocol) provides a connectionless service
for error control and flow of data. This TCP/IP protocol suite
helps to understand the working of communication software
packages.1.5 Embedded system Development Tools : Basically the
embedded tools are divided into two types( i).Hardware Development
tools and (ii) Software Development tools.1.5.1 Hardware
development tools : Hardware tools for embedded development include
development or evaluation boards for specific processors, like
Friendly ARMs Mini2440, Pandaboard , Beagleboard and Craneboard
etc..In addition to this various othes devices like Digital
multimeters ,Logic Analyzers , Spectrum Analyzers and Digital CROs
etc.are also required in embedded design.The digital multimeter is
used to measure voltages, currents and to check the continuity in
the circuits in an embedded systems. Because the embedded system
also contains some application specific circuitry which some times
require debugging.The Logic analyzer is used to check the timings
of the signals ,and their correctness.The Spectrum analyzer is
helpful to to analyze the signals in the frequency domain.The
digital CRO helps to display the output waveforms and also to store
a portion of the waveforms etc.1.5.2 Software development tools
/testing tools : The software development tools include the
operating system development suite ,cross platform development
tools, ROM emulator ,EPROM programming and In circuit Emulator
(ICE) etc. The operating system development suite consists of API
calls to access the OS services.This suite can run on either
Windows or UNIX/Linux systems.Under the cross platform tools,the
compiler generates the object code for the source code developed in
high level languages like C and C++ or Java etc.For LINUX systems a
number of GNU tools are available.The EPROM programmer is used to
in circuit programming by burning the code in the memory of the
target system. The instruction set Simulator (ISS) software creates
the virtual version of the processor on the PC.Assembler and
Compiler: The binary code obtained by translating an assembly
language program using an assembler is smaller and runs faster than
the binary code obtained by translating a high level language using
a compiler since the assembly language gives the programmer
complete control over the functioning of a processor. The advantage
of using a high level language is that a program written in a high
level language is easier to understand and maintain than a program
written in assembly language. Hence time critical applications are
written in assembly language while complex applications are written
in a high level language.Cross compilation tools are very important
for successful product development. Selection of these tools should
be made based upon the embedded system itself as well as features
to test and debug software remotely. The cross-platform development
tools should be compatible with the host machine. Depending upon
CPU family used for the target system, the toolset must be capable
of generating code for the target machine. In the case of GNU
development tools, we need to have a number of things to work
together to generate executable code for the target. At least one
of the following tools must be available on the machine.Cross
compilerCross assemblerCross linkerCross debuggerCross-compiled
libraries for the target host.Operating system-dependent libraries
and header files for the target systemSimulator: A simulator is
software tool that runs on the host and simulates the behavior of
the targets processor and memory. The simulator knows the target
processors architecture and instruction set. The program to be
tested is read by the simulator and as instructions are executed
the simulator keeps track of the values of the target processors
registers and the targets memory. Simulators provide single step
and breakpoint facilities to debug the program.
Fig 1.6 Cross compilationEmulator : Another important tool is
the ICE(In-Circuit Emulator),which emulates the CPU. An emulator is
a hardware tool that helps in testing and debugging the program on
the target. The targets processor is removed from the circuit and
the emulator is connected in its place. The emulator drives the
signals in the circuit in the same way as the targets processor and
hence the emulator appears to be the processor to all other
components of the embedded system. Emulators also provide features
such as single step and breakpoints to debug the program.Software
emulators are software tools that can emulate a particular CPU.
Using a software emulator one can debug the code and find out CPU
register values, stack pointers and other information without
having a real CPU. Software emulators are useful when we dont have
the real hardware available for testing and debugging and want to
see how the CPU will behave when a program is run on it.INTEL 8051
MICROCONTROLLERS1.6 IntroductionIn earlier days the process and
control operations were totally implemented by the Microprocessors
only. But since the last decade the situation is drastically
changed and a new device called Microcontroller has emerged in the
market . The advent and development of Microcontrollers is so
drastic that it is impossible to find any electronic gadget without
the use of a microcontroller in the todays electronic markets. This
microcontroller changed the embedded system design so simple that
the embedded market has become one of the most sought after for not
only entrepreneurs but for design engineers also. A Microcontroller
is a single chip computer or A CPU with all the peripherals like
RAM, ROM, I/O Ports, Timers , ADCs etc... on the same chip. Or it
is also defined as aSingle chip computer.For ex: Motorolas 6811,
Intels 8051, Zilogs Z8 and Micro chips PIC 16X etc are few
Microcontrollers available in the market. In addition to these
people there are innumerable number of vendors like Atmel, Philips,
AMD, Infineon (formerly Siemens)Samsung etc in the market.Let us
now see how a microcontroller is different from a microprocessor by
considering the differences between them.A Microprocessor is
basically a CPU built into a single VLSI chip . It is a
general-purpose device and additional external circuitry is added
to work it as a microcomputer. The microprocessor contains
arithmetic and logic unit (ALU), Instruction decoder , control
unit, Instruction register, Program counter (PC), clock circuit
(internal or external), reset circuit (internal or external) and
registers. But the microprocessor has no on chip I/O Ports, Timers
, Memory etc. For example, Intel 8085 is 8-bit microprocessor and
Intel 8086/8088 is 16-bit microprocessor. The block diagram of a
Microprocessor is shown in Fig 1.7.
Fig 1.7.Block diagram of a MicroprocessorA microcontroller is a
highly integrated single chip, which consists of on chip CPU
(Central Processing Unit), RAM (Random Access Memory),
EPROM/PROM/ROM (Erasable Programmable Read Only Memory), I/O
(input/output) serial and parallel, timers, interrupt controller.
For example, Intel 8051 is 8-bit microcontroller and Intel 8096 is
16-bit microcontroller. The block diagram of Microcontroller is
shown in Fig.1.8.
Fig.1.8.Block diagram of Microcontroller Let us now compare the
two devices. The Table 1.2 gives some of the salient differences
between Microprocessors and
Microcontroller.S.NoMicroprocessorMicrocontroller
1A microprocessor is a general purpose device which is called a
CPU A microcontroller is a dedicated chip which is also called
single chip computer
2A microprocessor do not contain onchip I/OPorts, Timers,
Memories etc..A microcontroller includes RAM, ROM, serial and
parallel interface, timers, interruptcircuitry (in addition to CPU)
in a single chip.
3Microprocessors are most commonly used as the CPU in
microcomputer systemsMicrocontrollers are used in small, minimum
component designs performing control-oriented applications.
4
Microprocessor instructions are Microcontroller instructions are
both bit mainly nibble or byte addressableaddressable as well as
byte addressable.
5Microprocessor instruction sets are mainly intended for
catering to large volumes of data.Microcontrollers have instruction
sets catering to the control of inputs and outputs.
6Microprocessor based system design is complex and
expensiveMicrocontroller based system design is rather simple and
cost effective
7The Instruction set of microprocessor is complex with large
number of instructions.The instruction set of a Microcontroller is
very simple with less number of instructions. For, ex: PIC
microcontrollers have only 35 instructions.
8A microprocessor has zero status flag A microcontroller has no
zero flag.
1.7 Computer Architectures : There are two popular computer
architectures in use. One is the Van Neumann architecture and the
other is the Harvard architecture.The first architecture was
developed at Princeton advanced Institute (USA) and after the chief
scientist name Von Neumann it is named as Von Neumann architecture
or Princeton architecture. The second architecture was developed by
a group at Harvard University and it is known as Harvard
architecture.
1.7.1Von Neumann architectureThe Von Neumann architecture refers
a specific type of computer model where the instruction and data
are stored in the same memory. The instruction code is executed
serially and and need more clock cycles. As shown in the Fig 1.14
the heart of the von Neumann computer is the central processing
unit or CPU which consists of the Arithmetic Logic Unit(ALU) and
control unit.The CPU interacts with I/O and memory subsystems and
executes a stream of instructions that process the data stored in
memory
Fig.1.9 Von Neumann architecture
. The basic advantage of von Neumann architecture is that it
simplifies the microcontroller chip design because only one memory
is accessed. For microcontrollers, its biggest asset is that the
contents of RAM (random-access memory) can be used for both
variable (data) storage as well as program instruction storage. An
advantage for some applications is the program counter stack
contents that are available for access by the program. This allows
greater flexibility in developing software, primarily in the areas
of real-time operating systems.1.7.2Harvard architecture In this
computer model separate memories are used for both instruction and
data and separate buses are also used for fetching the instruction
code and data. Due to this ,a kind of parallelism is achieved in
the execution of instructions by the processor
Fig.1.10. Harvard architecture
The Harvard architecture executes instructions in less number of
instruction cycles than the Von Neumann architecture. This is
because a much greater amount of instruction parallelism is
possible in the Harvard architecture. Parallelism means that
fetches for the next instruction can take place during the
execution of the current instruction, without having to either wait
for a "dead" cycle of the instruction's execution or stop the
processor's operation while the next instruction is being
fetched.
Distinguish between von Neumann Architecture and Harvard
ArchitectureVon Neumann ArchitectureHarvard Architecture
This architecture is used in conventional processors found in
PCs and Servers, and embedded systems with only control
functions.This architecture is DSPs and other processors found in
latest embedded systems and Mobile communication systems, audio,
speech, image processing systems etc.
The data and program are stored in the same memoryThe data and
program memories are stored in separate memories.
The code is executed serially and takes more clock cyclesThe
code is executed in parallel and hence less number of clock cycles
are required
There is no exclusive MultiplierIt has MAC (Multiply
Accumulate)
There is no barrel shifter present in this architecture. In this
architecture , the Barrel Shifter helps in shifting and rotating
operations of the data
The programs can be optimized in lesser sizeThe program tend to
grow big in size
1.8 RISC & CISC Processor architectureBased on the
performance and the type of instruction set it handles ,there are
two types of processor architectures. They are RISC processors and
CISC processors.RISC means Reduced Instruction Set Computer. These
RISC processors use only simple instructions that can be executed
within one clock cycle. As each instruction requires only one clock
cycle to execute, the entire program will execute in approximately
the same amount of time . These RISC "reduced instructions" require
less transistors of hardware space than the complex instructions,
leaving more room for general purpose registers. Because all of the
instructions execute in a uniform amount of time (i.e. one clock),
pipelining is possible. The RISC architecture is based on
Load/Store concept. In the RISC processors ,the control unit is
hardwired i.e RISC architectures are not micro programmed. The code
generated by the compiler is directly executed by the hardware. and
it is not interpreted by the micro programming. The number of
instruction formats is also (addressing modes) is also less. Many
times less than four.The RISC architecture has a larger register
file organization.RISC uses the concept of Instruction
pipelining.
CISC stands for Complex Instruction Set Computers. The primary
goal of CISC architecture is to complete a task in as few lines of
assembly as possible. This is achieved by building processor
hardware that is capable of understanding and executing a series of
operations. For example Intel (x86 ) and AMD CPU's are based on
CISC architecturesOne of the primary advantages of CISC
Architecture is that the compiler has to do very little work to
translate a high-level language statement into assembly. Because
the length of the code is relatively short, very little RAM is
required to store instructions. The emphasis is put on building
complex instructions directly into the hardware.Distinguish between
CISC and RISC processors architecturesS.NoCISCRISC
1Stands for Complex Instruction Set ComputerStands for Reduced
Instruction Set Computer
2A typical CISC instruction set contains nearly 120-350
instructionsCISC Instruction set contains around 35
instructions
3The main emphasis is on hardware The main emphasis on
software
4Includes multi-clock complex instructionsSingle-clock, reduced
instruction only
5Most of the instructions are memory basedInstructions are
register based
6Unified cache for Data and InstructionsSeparate instruction
,data caches are used.
7Lower clock rate 33-50M.Hz with CPI between 2-15Higher clock
rate 50-150.Hz with CPI < 1.5
8Lower MIPSHigher MIPS
9CPU control is microcoded control memoryCPU control is
Hardwired
10More number of addressing modes (8-24)Only 3-5 simple
addressing modes.
11INTELX86 , Pentium processors are examples for CISCIBM Power
PC is an example for RISC
12Memory-to-memory:"LOAD" and "STORE"incorporated in
instructionsRegister to register:"LOAD" and "STORE"are independent
instructions
13This architecture do not support Pipelining conceptPipelining
is the main feature of the RISC processors
14Hard to designEasy to design
1.9. EVOLUTION OF MICROCONTROLLERS The first microcontroller
TMS1000 was introduced by Texas Instruments in the year 1974. In
the year 1976, Motorola designed a Microprocessor chip called 6801
which replaced its earlier chip 6800 with certain add-on chips to
design a computer. This paved the way for the new revolution in the
history of chip design and gave birth to a new entity called
Microcontroller. Later the Intel company has produced its first
Microcontroller 8048 with a CPU and 1K bytes of EPROM, 64 Bytes of
RAM an 8-Bit Timer and 27 I/O pins in 1976. Later in the year 1980
the most popular microcontroller 8051 was released with 4K bytes of
ROM,128 Bytes of RAM , a serial port, two 16-bit Timers , and 32
I/O pins. The 8051 family has many additions and improvements over
the years and became the most acclaimed tool for todays circuit
designers. INTEL introduced a 16 bit microcontroller 8096 in the
year 1982 . Later INTEL introduced 80c196 series of 16-bit
Microcontrollers for mainly industrial applications. Microchip,
another chip design company from US is popular for PIC
microcontrollers and has introduced an 8-bit Microcontroller PIC
16C64 in the year 1985.The 32-bit microcontrollers have been
developed by IBM and Motorola. MPC 505 is a 32-bit RISC controller
of Motorola. The 403 GA is a 32 -bit RISC embedded controller of
IBM.In recent times ARM company (Advanced RISC machines) has
developed and introduced 32 bit controllers for high-end
application devices like mobiles , Ipods etc. 1.10 TYPES OF
MICROCONTROLLERS :Microcontrollers are classified on the basis of
internal bus width, architecture, memory and instruction set as
4-bit, 8-bit, 16-bit and 32-bit microcontrollers.4-bit
Microcontrollers: These 4-bit microcontrollers are small size,
minimum pin count and low cost controllers which are widely used
for low end applications like LED & LCD display drivers
,portable battery chargers etc. Their power consumption is very
low. The popular 4-bit controllers are Renasa M34501 which is a 20
pin DIP chip with 4kB of ROM,256 Bytes of RAM,2-Counters and 14 I/O
Pins. Similarly ATAM862 series from ATMEL is also a 4-bit
microcontroller.8-bit Microcontrollers : These are the most popular
and widely used microcontrollers .Nearly 55% of the CPUs sold in
the world market today are 8-bit microcontrollers only. The 8-bit
microcontroller has 8-bit internal bus and the ALU performs all the
arithmetic and logical operations on a byte instruction. The well
known 8-bit microcontroller is 8051 which is designed by Intel in
the year 1980 for the use in embedded systems. Other 8-bit
microcontrollers are Intel 8031/8052 and Motorola MC68HC11 and AVR
Microcontrollers, Microchips PIC microcontrollers 12C5XX ,16C5X and
16C505 etc.16-bit Microcontrollers : When the microcontroller
performs 16-bit arithmetic and logical operations at an
instruction, it is said to be a 16-bit microcontroller. The
internal bus width of these microcontrollers is of 16-bit. These
microcontrollers are having increased memory size and speed of
operation when compared to 8-bit microcontrollers. They are most
suitable for programming in High level languages like C or C++
.They find applications in disk drivers, modems, printers, scanners
and servo motor control. Examples of 16-bit microcontrollers are
Intel 8096 family and Motorola MC68HC12 and MC68332 families, The
performance and computing capability of 16 bit microcontrollers are
enhanced with greater precision as compared to the 8-bit
microcontrollers.32-Bit Microcontrollers :These microcontrollers
used in high-end applications like Automotive control,
Communication networks, Robotics, Cell phones ,GPRS & PDAs
etc..For example PIC32, ARM 7, ARM9 ,SHARP LH79520 , ATMEL 32 (AVR)
,TIs TMS 320F2802x/2803x etc .are some of the popular 32-bit
microcontrollers.1.11 Overview of Intel 8051 MicrocontrollersThe
8051 microcontroller is a very popular 8-bit microcontroller
introduced by Intel in the year 1981 and it has become almost the
academic standard now a days. The 8051 is based on an 8-bit CISC
core with Harvard architecture. Its 8-bit architecture is optimized
for control applications with extensive Boolean processing. It is
available as a 40-pin DIP chip and works at +5 Volts DC. The
salient features of 8051 controller are given below. It has 4 KB on
chip program memory (ROM or EPROM)) and 128 bytes of on chip data
memory(RAM). It has an 8-bit data bus and 16-bit address bus for
internal operations. 32 general purpose registers each of 8 bits
Two -16 bit timers T0 and T1 Five Interrupts (3 internal and 2
external). Four Parallel ports each of 8-bits (PORT0,
PORT1,PORT2,PORT3) with a total of 32 I/O lines. One 16-bit program
counter and One 16-bit DPTR ( data pointer) One 8-bit stack pointer
One Microsecond instruction cycle with 12 MHz Crystal. One full
duplex serial communication port.1.12 Block Diagram and
Architecture of 8051 MicrocontrollerThe architecture of the 8051
microcontroller chip can be understood from its block diagram. It
has Harward architecture with RISC (Reduced Instruction Set
Computer) concept. The block diagram of 8051 microcontroller is
shown in fig1.9 below.It consists of an 8-bit ALU, one 8-bit
PSW(Program Status Register), A and B registers , one 16-bit
Program counter , one 16-bit Data pointer register(DPTR),128 bytes
of RAM and 4kB of ROM and four parallel I/O ports each of 8-bit
width.
Fig 1.9 Block Diagram of 8051 microcontroller8051 has 8-bit ALU
which can perform all the 8-bit arithmetic and logical operations
in one machine cycle. The ALU is associated with two registers A
& BA and B Registers : The A and B registers are special
function registers which hold the results of many arithmetic and
logical operations of 8051.The A register is also called the
Accumulator and as its name suggests, is used as a general register
to accumulate the results of a large number of instructions. By
default it is used for all mathematical operations and also data
transfer operations between CPU and any external memory. The B
register is mainly used for multiplication and division operations
along with A register. MUL AB ; Multiply the contents of A and B,
put the answer in AB DIV AB ; Divide A by B, put quotient in A and
remainder in BIt has no other function other than as a location
where data may be stored.The R registers: The "R" registers are a
set of eight registers that are named R0, R1, etc. up to and
including R7. These registers are used as auxillary registers in
many operations. The "R" registers are also used to temporarily
store values.1.13 Program Counter(PC) : 8051 has a 16-bit program
counter .The program counter always points to the address of the
next instruction to be executed. After execution of one instruction
the program counter is incremented to point to the address of the
next instruction to be executed.It is the contents of the PC that
are placed on the address bus to find and fetch the desired
instruction.Since the PC is 16-bit width ,8051 can access program
addresses from 0000H to FFFFH ,a total of 6kB of code.1.14 Memory
organization : The 8051 has two types of memory and these are
Program Memory and Data Memory. Program Memory (ROM) is used to
permanently save the program being executed, while Data Memory
(RAM) is used for temporarily storing data and intermediate results
created and used during the operation of the microcontroller. The
8051 microcontroller has 128 bytes of Data Memory (Internal RAM )
and 4kB of Program Memory(on chip ROM) .The RAM is also known as
Data memory and the ROM is known as program memory. The program
memory is also known as Code memory .This Code memory holds the
actual 8051 program that is to be executed. In 8051 this memory is
limited to 64K .Code memory may be found on-chip, as ROM or EPROM.
It may also be stored completely off-chip in an external ROM or,
more commonly, an external EPROM. The 8051 has only 128 bytes of
Internal RAM but it supports 64kB of external RAM. As the name
suggests, external RAM is any random access memory which is
off-chip. Since the memory is off-chip it is not as flexible
interms of accessing, and is also slower. For example, to increment
an Internal RAM location by 1,it requires only 1 instruction and 1
instruction cycle but to increment a 1-byte value stored in
External RAM requires 4 instructions and 7 instruction cycles. So,
here the external memory is 7 times slower.1.14.1 Internal RAM OF
8051 :This Internal RAM is found on-chip on the 8051 .So it is the
fastest RAM available, and it is also the most flexible in terms of
reading, writing, and modifying its contents. Internal RAM is
volatile, so when the 8051 is reset this memory is cleared. The 128
bytes of internal RAM is organized as below.(i) Four register banks
(Bank0,Bank1, Bank2 and Bank3) each of 8-bits (total 32 bytes). By
default always the s Bank 0 is selected. The remaining Banks are
selected with the help of RS0 and RS1 bits of PSW Register.(ii) 16
bytes of bit addressable area and(iii) 80 bytes of general purpose
area (Scratch pad memory) as shown in the diagram below. This area
is also utilized by the microcontroller as a storage area for the
operating stack.
Fig.1.10 Internal RAM OrganisationThe 32 bytes of RAM from
address 00 H to 1FH are used as working registers organized as four
banks .Each bank has 8 registers and they are named as R0-R7 .Each
register can be addressed by its name or by its RAM address. For EX
: MOV A, R5 or MOV A, 05H1.14.2 Internal ROM (On chip ROM): The
8051 microcontroller has 4kB of on chip ROM but it can be extended
up to 64kB.This ROM is also called program memory or code memory.
The CODE segment is accessed using the program counter (PC) for
opcode fetches and by DPTR for data. The external ROM is accessed
when the EA(active low) pin is connected to ground or the contents
of program counter exceeds 0FFFH.When the Internal ROM address is
exceeded the 8051 automatically fetches the code bytes from the
external program memory.
1.15 Data Types and DirectivesGenerally while using a processor
/ microcomputer or Microcontroller the programmer should always
know the data types supported by that device.There are different
types of data like integer, character ,float ,signed char ,unsigned
char ,short integer and long integer etc. Also the programmer
should know the bit size supported by the device.For example 8051
microcontroller is an 8-bit controller .So care must be taken while
using the data during the programming. So, a good understanding of
C data types for 8051 help programmers to create smaller hex files.
As 8051 is an 8-bit microcontroller, the character data type is the
most natural choice for many applications. The unsigned char is an
8-bit data type takes a value in the range of 0 255 (00H FFH) and
it is the most widely used data type for 8051. The 8051 supports un
signed character data between 0 to 255 and signed character data
between -128 to +127.This type of data can be used in situations
like setting a counter value and ASCII characters, instead of
signed char. It is important to specify the keyword unsigned in
front of the char else compiler will use the signed char as the
default. The unsigned int is a 16-bit data type that takes a value
in the range of 0 to 65535 (0000 FFFFH). This is used to define
16-bit variables such as memory addresses, set counter values of
more than 256. Since registers and memory accesses are in 8-bit
chunks, the misuse of int variables will result in a larger hex
file. However, for 8051 programming, do not use unsigned int in
place where unsigned char will do the job. Also in situations where
there is no need for signed data we should use unsigned int instead
of signed int. The 8 bit keyword is a widely used 8051 C data types
which is used to access single-bit addressable register. It allows
access to the single bits of the SFR registers. It is a clear fact
that some of the SFRs are bit addressable. Among the SFRs that are
widely used are also bit addressable ports P0-P3.Directives are
special instructions that provide information to the assembler but
do not generate any code.They have no influence on the operation of
the microcontroller. Some of them are obligatory part of every
program while some are used only to facilitate or speed up the
operation. Directives are written in the column reserved for
instructions. There is a rule allowing only one directive per
program line.They are messages to the assembler, nothing else.Under
Data Directives DB is the most widely used directive.
DB (define byte)The DB directive is used to define the 8-bit
data. When DB is used to define data, the numbers can be in
decimal, binary, hex, or ASCII formats. For decimal, the D after
the decimal number is optional, but using B (binary) and H
(hexadecimal) for the others is required. Regardless of which is
used, the assembler will convert the numbers into hex. To indicate
ASCII, simply keep the characters in quotation marks ( ). The
assembler will assign the ASCII code for the numbers or characters
automatically. The DB directive is the only directive that can be
used to define ASCII strings larger than two characters; therefore,
it should be used for all ASCII data definitions.As an illustration
let us consider the following assembly language code.
ORG 000HDATA1 : DB 27 ; Decimal ( 1B in Hex)DATA 2 : DB 01101101
B ; BINARY ( 6D in Hex)DATA 3 : DB 31H ; Hex NumberDATA 4 : DB 1589
; ASCII NumberDATA 5 : DB My name is Murthy : ASCII Characters
Either single or double quotes can be used around ASCII strings.
This can be useful for strings, which contain a single quote. DB is
also used to allocate memory.
In addition to the data directives some directives like ORG ,EQU
and END are also use in 8051 microcontroller assembly language
programming. ORG (origin)The ORG directive is used to indicate the
beginning of the address location. The number that comes after ORG
can be either in hex or in decimal. If the number is not followed
by H, it is decimal and the assembler will convert it to hex. Some
assemblers use . ORG instead of ORG for the origin directive. EQU
(equate)This directive is used to define a constant without
occupying a memory location. The EQU directive does not set aside
storage for a data item but associates a constant value with a data
label so that when the label appears in the program, its constant
value will be substituted for the label. The following example uses
EQU for the counter constant and then the constant is used to load
the R5 register.COUNT EQU 15.. ------------------ --------- MOV
R5,# COUNT
While executing the instruction MOV R5, # COUNT, the register R5
will be loaded with the value 15 . The advantage of using this EQU
directive is ,it becomes easy to change a constant value at
different places in the program .Once the programmer uses this
directive the assembler will automatically apply at all
occurrences. i.e wherever it finds the word COUNT ,it is replaced
with the data value 15END directiveEND directive is an important
pseudo code that indicates to the assembler the end of the source
(asm) file. The END directive is the last line of an 8051 program,
meaning that in the source code anything after the END directive is
ignored by the assembler. Some assemblers use . END ( dot before
END) instead of simply END.
1.16 Flag bits and Program Status Register (PSW) : The 8051 has
a 8-bit PSW register which is also known as Flag register. In the
8-bit register only 6-bits are used by 8051.The two unused bits are
user definable bits.In the 6-bits four of them are conditional
flags .They are Carry CY,Auxiliary Carry-AC, Parity-P,and
Overflow-OV .These flag bits indicate some conditions that resulted
after an instruction was executed.
The bits PSW3 and PSW4 are denoted as RS0 and RS1 and these bits
are used th select the bank registers of the RAM location. The
meaning of various bits of PSW register is shown below.CY PSW.7
Carry Flag.Set to 1 when there is a carry in any arithmetic
operation.Else reset to 0AC PSW.6 Auxiliary Carry FlagFO PSW.5 Flag
0 available for general purpose .RS1 PSW.4 Register Bank select bit
1RS0 PSW.3 Register bank select bit 0OV PSW.2 Overflow flag---
PSW.1 User definable flagP PSW.0 Parity flag .set/cleared by
hardware.The selection of the register Banks and their addresses
are given below table 1.3. Table 1.3 Register banks and their
addressesExample-1To explain the status of the flag bits let us
consider an addition program shown below.And check the status of
CY, AC and P flag after the addition.MOV A, #38HADD A, #2FH On
execution 38 00111000 + 2F 00101111 --------------------------- 67
01100111After addition A=67H, CY=0CY = 0 since there is no carry
beyond the D7 bitAC = 1 since there is a carry from the D3 to the
D4 bitP = 1 since the accumulator has an odd number of 1s (it has
five 1s)Example-2Let us consider the addition of 9CH and 64H in the
following illustration
MOV A, # 9CHADD A, # 64H 9C 10011100 + 64 01100100
------------------------------ After addition, 100 00000000 Here CY
= 1 since there is a carry beyond the D7 bit , AC = 1 since there
is a carry from the D3 to the D4 bit, P = 0 since the accumulator
has an even number of 1s (it has zero 1s)
1.17 Stack Pointer Register (SP) : It is an 8-bit register which
stores the address of the stack top. i.e the Stack Pointer is used
to indicate where the next value to be removed from the stack
should be taken from. When a value is pushed onto the stack, the
8051 first increments the value of SP and then stores the value at
the resulting memory location. Similarly when a value is popped off
the stack, the 8051 returns the value from the memory location
indicated by SP, and then decrements the value of SP. Since the SP
is only 8-bit wide it is incremented or decremented by two . 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.1.18 STACK in 8051 Microcontroller : The
stack is a part of RAM used by the CPU to store information
temporarily. This information may be either data or an address .The
CPU needs this storage area as there are only limited number of
registers. The register used to access the stack is called the
Stack pointer which is an 8-bit register..So,it can take values of
00 to FF H.When the 8051 is powered up ,the SP register contains
the value 07.i.e the RAM location value 08 is the first location
being used for the stack by the 8051 controller There are two
important instructions to handle this stack.One is the PUSH and the
Other is the POP. The loading of data from CPU registers to the
stack is done by PUSH and the loading of the contents of the stack
back into aCPU register is done by POP.EX : MOV R6 ,#35 H MOV R1
,#21 H PUSH 6 PUSH 1 In the above instructions the contents of the
Registers R6 and R1 are moved to stack and they occupy the 08 and
09 locations of the stack.Now the contents of the SP are
incremented by two and it is 0ASimilarly POP 3 instruction pops the
contents of stack into R3 register.Now the contents of the SP is
decremented by 1In 8051 the RAM locations 08 to 1F (24 bytes) can
be used for the Stack.In any program if we need more than 24 bytes
of stack ,we can change the SP point to RAM locations 30-7F H.this
can be done with the instruction MOV SP,# XX.Data Pointer
Register(DPTR) : It is a 16-bit register which is the only
user-accessible. 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. This
DPTR can also be used as two 8-registers DPH and DPL.
During the programming this DPTR can be used similar to any
other register.The following examples illustrates the usage of this
register. MOV DPTR, #1576 H ; move the hex data 1576 into the DPTR
register MOV DPL, #65H ; move the hex data 65 into lower byte of
DPTR MOV A ,DPH ; The contents of higher byte of DPTR are copied to
Accumulator
SPECIAL FUNCTION REGISTERS (SFRs) : In 8051 microcontroller
there certain registers which uses the RAM addresses from 80h to
FFh and they are meant for certain specific operations .These
registers are called Special function registers (SFRs).Some of
these registers are bit addressable also. The list of SFRs and
their functional names are given below. In these SFRs some of them
are related to I/O ports (P0,P1,P2 and P3) and some of them are
meant for control operations (TCON,SCON, PCON..) and remaining are
the auxillary SFRs, in the sense that they don't directly configure
the 8051.S.NoSymbolName of SFRAddress (Hex)
1ACC*Accumulator0E0
2B*B-Register0F0
3PSW*Program Status word register0DO
4SPStack Pointer Register81
5DPTRDPL Data pointer low byte82
DPHData pointer high byte83
6P0*Port 080
P1*Port 190
8P2*Port 20A
9P3*Port 30B
10IP*Interrupt Priority control0B8
11IE*Interrupt Enable control0A8
12TMODTmier mode register89
13TCON*Timer control register88
14TH0Timer 0 Higher byte8C
15TL0Timer 0 Lower byte8A
16TH1Timer 1Higher byte8D
17TL1Timer 1 lower byte8B
18SCON*Serial control register98
19SBUFSerial buffer register99
20PCONPower control register87
The * indicates the bit addressable SFRs Table 1.4 SFRs of 8051
Microcontroller1.19 Pin Diagram of 8051 Microcontroller The 8051
microcontroller is available as a 40 pin DIP chip and it works at
+5 volts DC. Among the 40 pins , a total of 32 pins are allotted
for the four parallel ports P0,P1,P2 and P3 i.e each port occupies
8-pins .The remaining pins are VCC, GND, XTAL1, XTAL2, RST, EA
,PSEN. XTAL1,XTAL2: These two pins are connected to Quartz crystal
oscillator which runs the on-chip oscillator. The quartz crystal
oscillator is connected to the two pins along with a capacitor of
30pF as shown in the circuit. If a source other than the crystal
oscillator is used , it will be connected to XTAL1 and XTAL2 is
left unconnected.
Fig 1.11 Pin Diagram of 8051 MicrocontrollerRST: The RESET pin
is an input pin and it is an active high pin. When a high pulse is
applied to this pin the microcontroller will reset and terminate
all activities. Upon reset all the registers except PC will reset
to 0000 Value and PC register will reset to 0007 value. (External
Access): This pin is an active low pin. This pin is connected to
ground when microcontroller is accessing the program code stored in
the external memory and connected to Vcc when it is accessing the
program code in the on chip memory. This pin should not be left
unconnected.(Program Store Enable) : This is an output pin which is
active low. When the microcontroller is accessing the program code
stored in the external ROM ,this pin is connected to the OE (Output
Enable) pin of the ROM.ALE (Address latch enable): This is an
output pin, which is active high. When connected to external memory
, port 0 provides both address and data i.e address and data are
multiplexed through port 0 .This ALE pin will demultiplex the
address and data bus .When the pin is High , the AD bus will act as
address bus otherwise the AD bus will act as Data bus.P0.0-
P0.7(AD0-AD7) : The port 0 pins multiplexed with Address/data pins
.If the microcontroller is accessing external memory these pins
will act as address/data pins otherwise they are used for Port 0
pins.P2.0- P2.7(A8-A15) : The port2 pins are multiplexed with the
higher order address pins .When the microcontroller is accessing
external memory these pins provide the higher order address byte
otherwise they act as Port 2 pins.P1.0- P1.7 :These 8-pins are
dedicated for Port1 to perform input or output port
operations.P3.0- P3.7 :These 8-pins are meant for Port3 operations
and also for some control operations like Read,Write,Timer0,Timer1
,INT0,INT1 ,RxD and TxD1.20 PORT ORGANISATION :The 8051
microcontroller has four parallel I/O ports , each of 8-bits .So,
it provides the user 32 I/O lines for connecting the
microcontroller to the peripherals. The four ports are P0 (Port 0),
P1(Port1) ,P2(Port 2) and P3 (Port3). Upon reset all the ports are
output ports. In order to make them input, all the ports must be
set i.e a high bit must be sent to all the port pins. This is
normally done by the instruction SETB.Ex: MOV A,#0FFH ; A = FF MOV
P0,A ; make P0 an input portPORT 0:Port 0 is an 8-bit I/O port with
dual purpose. If external memory is used, these port pins are used
for the lower address byte address/data (AD0-AD7), otherwise all
bits of the port are either input or output.. Unlike other ports,
Port 0 is not provided with pull-up resistors internally ,so for
PORT0 pull-up resistors of nearly 10k are to be connected
externally as shown in the fig.1.12.Dual role of port 0: Port 0 can
also be used as address/data bus(AD0-AD7), allowing it to be used
for both address and data. When connecting the 8051 to an external
memory, port 0 provides both address and data. The 8051 multiplexes
address and data through port 0 to save the pins. ALE indicates
whether P0 has address or data. When ALE = 0, it provides data
D0-D7, and when ALE =1 it provides address and data with the help
of a 74LS373 latch.
Fig 1.12 Port 1 with external pull-up resistors.Port 1: Port 1
occupies a total of 8 pins (pins 1 through 8). It has no dual
application and acts only as input or output port. In contrast to
port 0, this port does not need any pull-up resistors since pull-up
resistors connected internally. Upon reset, Port 1 is configured as
an output port. To configure it as an input port , port bits must
be set i.e a high bit must be sent to all the port pins. This is
normally done by the instruction SETB. For Ex : MOV A, #0FFH ; A=FF
HEX MOV P1,A ; make P1 an input port by writing 1s to all of its
pinsPort 2 : Port 2 is also an eight bit parallel port. (pins 21-
28). It can be used as input or output port. As this port is
provided with internal pull-up resistors it does not need any
external pull-up resistors. Upon reset, Port 2 is configured as an
output port. If the port is to be used as input port, all the port
bits must be made high by sending FF to the port. For ex, MOV A,
#0FFH ; A=FF hex MOV P2, A ; make P2 an input port by writing all
1s to itDual role of port 2 : Port2 lines are also associated with
the higher order address lines A8-A15. In systems based on the
8751, 8951, and DS5000, Port2 is used as simple I/O port.. But, in
8031-based systems, port 2 is used along with P0 to provide the
16-bit address for the external memory. Since an 8031 is capable of
accessing 64K bytes of external memory, it needs a path for the 16
bits of the address. While P0 provides the lower 8 bits via A0-A7,
it is the job of P2 to provide bits A8-A15 of the address. In other
words, when 8031 is connected to external memory, Port 2 is used
for the upper 8 bits of the 16 bit address, and it cannot be used
for I/O operations.PORT 3 : Port3 is also an 8-bit parallel port
with dual function.( pins 10 to 17). The port pins can be used for
I/O operations as well as for control operations. The details of
these additional operations are given below in the table. Port 3
also do not need any external pull-up resistors as they are
provided internally similar to the case of Port2 & Port 1. Upon
reset port 3 is configured as an output port . If the port is to be
used as input port, all the port bits must be made high by sending
FF to the port. For ex, MOV A, #0FFH ; A= FF hex MOV P3, A ; make
P3 an input port by writing all 1s to itAlternate Functions of Port
3 : P3.0 and P3.1 are used for the RxD (Receive Data) and TxD
(Transmit Data) serial communications signals. Bits P3.2 and P3.3
are meant for external interrupts. Bits P3.4 and P3.5 are used for
Timers 0 and 1 and P3.6 and P3.7 are used to provide the write and
read signals of external memories connected in 8031 based
systems
S.No Port 3 bit Pin No Function
1P3.010RxD
2P3.111TxD
3P3.212
4P3.313
5P3.414T0
6P3.515T1
7P3.616
8P3.717
Table 1.5 Alternate functions of PORT 3 I/O Programming:I/O
Programming mainly deals with the transfer of data between external
device to microcontroller or vice versa through the ports. This
concept of I/O programming also helps to understand the interfacing
of peripheral devices to the microcontroller and parallel data
transfer.8051 microcontroller has four I/O ports each of
8-bit.These ports can be accessed similar to any other register of
the 8051 microcontroller. MOV P0 ,# 25H ; Move or copy the Hex data
25 into the Port0 ADD A,P1 ; Adds the contents of Port1 to the
contents of Accumulator AAvery important feature of these ports is
upon reset all the ports acts as output ports and to make them as
input ports ,all the port pins must be made high. i.e a I or HIGH
must be sent to each pin.This can be done by the following
instruction. MOV P0 ,#OFFH ; All the port pins are made high.Or
using the instruction SETB (ser bit) also a particular port pin can
be made as out port.Ex: SETB P2.1 ; Makes the pin1 of port2 as out
port.The following program continuously send out alternate values
55H and AAH to Port0BACK: MOV A,#55H ; copy the Hex value 55 to the
Accumulator MOV P0, A ; send the hex value 55 to Port0 ACALL DELAY
; Call a delay program MOV A,#0AAH ; copy the Hex value AA to the
Accumulator MOV P0,A ; send the hex value AA to Port0 ACALL DELAY ;
Call a delay program SJMP BACK ; Go back to starting of the
program.On execution ,the code sends alternately the values 55 and
AA to Port 0.The following code will continuously send out to port
0 the alternating value 55H and AAH
MOV A,#55H ; copy the Hex value 55 to the Accumulator BACK: MOV
P1,A ; end the hex value 55 to Port0
ACALL DELAY ; Call a delay program CPL A ; Complement A i.e
A=AAH SJMP BACK ; repeat the program continuously.This program also
sends the values 55H and AA H to the Port1 continuously .Bit
Manipulation: Microcontroller always allows bit manipulations.i.e a
single bit of a byte data can be either set (1) or reset (0) by
using suitable instructions. This feature has many advantages in
various real time applications. The instructions used to manipulate
single bit data are given below. SETB ; bit Set the bit (bit = 1)
CLR bit ; Clear the bit (bit = 0) CPL bit ; Complement the bit (bit
= NOT bit) JB bit, target ; Jump to target if bit = 1 (jump if bit)
JNB bit, target ; Jump to target if bit = 0 (jump if no bit) JBC
bit, target ; Jump to target if bit = 1, clear bit(jump if bit,
then clear)
Example: Generate a 50% duty cycle square wave on bit 1 of Port1
by using the bit manipulation instructions.The 50% duty cycle means
that the on and off state (or the high and low portion of the
pulse) have the same length. Therefore, we toggle P1.0 with a time
delay in between each state.
HERE: SETB P1.1 ; set to high bit 1 of port 1 LCALL DELAY ; call
the delay subroutine CLR P1.0 ; P1.1 = 0 LCALL DELAY ; call the
delay subroutine SJMP HERE ; keep doing itThe same program can also
implemented by usnin another bit manipulation instruction namely
CPL HERE : CPL P11 ; set to high bit 1 of port 1 LCALL DELAY ; call
the delay subroutine SJMP HERE ; keep doing
continuously.8051P1.01.21Interrupts : An interrupt is an external
or internal event that disturbs the microcontroller to inform it
that a device needs its service. The program which is associated
with the interrupt is called the interrupt service routine (ISR) or
interrupt handler. Upon receiving the interrupt signal the
Microcontroller , finish current instruction and saves the PC on
stack. Jumps to a fixed location in memory depending on type of
interrupt Starts to execute the interrupt service routine until
RETI (return from interrupt)Upon executing the RETI the
microcontroller returns to the place where it was interrupted. Get
pop PC from stackThe 8051 microcontroller has FIVE interrupts in
addition to Reset. They are Timer 0 overflow Interrupt Timer 1
overflow Interrupt External Interrupt 0(INT0) External Interrupt
1(INT1) Serial Port events (buffer full, buffer empty, etc)
InterruptEach interrupt has a specific place in code memory where
program execution (interrupt service routine) begins. External
Interrupt 0: 0003 H Timer 0 overflow: 000B H External Interrupt
1:0013 H Timer 1 overflow: 001B H Serial Interrupt : 0023 HUpon
reset all Interrupts are disabled & do not respond to the
Microcontroller. These interrupts must be enabled by software in
order for the Microcontroller to respond to them. This is done by
an 8-bit register called Interrupt Enable Register (IE).Interrupt
Enable Register :
EA : Global enable/disable. To enable the interrupts this bit
must be set High. --- : Undefined-reserved for future use. ET2 :
Enable /disable Timer 2 overflow interrupt. ES : Enable/disable
Serial port interrupt. ET1 : Enable /disable Timer 1 overflow
interrupt. EX1 : Enable/disable External interrupt1. ET0 : Enable
/disable Timer 0 overflow interrupt. EX0 : Enable/disable External
interrupt0Upon reset the interrupts have the following
priority.(Top to down). The interrupt with the highest PRIORITY
gets serviced first.1. External interrupt 0 (INT0)2. Timer
interrupt0 (TF0)3. External interrupt 1 (INT1)4. Timer interrupt1
(TF1)5. Serial communication (RI+TI)Priority can also be set to
high or low by 8-bit IP register.- Interrupt priority register
IP.7: reservedIP.6: reservedIP.5: Timer 2 interrupt priority bit
(8052 only)IP.4: Serial port interrupt priority bitIP.3: Timer 1
interrupt priority bitIP.2: External interrupt 1 priority bitIP.1:
Timser 0 interrupt priority bitIP.0: External interrupt 0 priority
bit 1.20 Timer/Counter Modules in 8051 Microcontrollers : The 8051
microcontroller has two 16-bit timers Timer 0 (T0) and Timer 1(T1)
which can be used either to generate accurate time delays or as
event counters. These timers are accessed as two 8-bit registers
TLO, THO & TL1 ,TH1 because the 8051 microcontroller has 8-bit
architecture.TIMER 0 : The Timer 0 is a 16-bit register and can be
treated as two 8-bit registers (TL0 & TH0) and these registers
can be accessed similar to any other registers like A,B or R1,R2,R3
etcEx : The instruction Mov TL0,#07 moves the value 07 into lower
byte of Timer0.Similarly Mov R5,TH0 saves the contents of TH0 in
the R5 register.
TIMER 1 : The Timer 1 is also a 16-bit register and can be
treated as two 8-bit registers (TL1 & TH1) and these registers
can be accessed similar to any other registers like A,B or R1,R2,R3
etcEx : The instruction MOV TL1,#05 moves the value 05 into lower
byte of Timer1.Similarly MOV R0,TH1 saves the contents of TH1 in
the R0 register
TMOD Register : The various operating modes of both the timers
T0 and T1 are set by an 8-bit register called TMOD register. In
this TMOD register the lower 4-bits are meant for Timer 0 and the
higher 4-bits are meant for Timer1.
GATE: This bit is used to start or stop the timers by hardware
.When GATE= 1 ,the timers can be started / stopped by the external
sources. When GATE= 0, the timers can be started or stopped by
software instructions like SETB TR0 or SETB TR1C/T (clock/Timer) :
This bit decides whether the timer is used as delay generator or
event counter. When C/T = 0 ,the Timer is used as delay generator
and if C/T=1 the timer is used as an event counter. The clock
source for the time delay is the crystal frequency of 8051.M1,M0
(Mode) : These two bits are the timer mode bits. The timers of the
8051 can be configured in three modes.Mode0, Mode1 and Mode2.The
selection and operation of the modes is shown below.S.No M0 M1
ModeOperation
100013-bit Timer mode8-bit Timer/counter. THx with TLx as 5-bit
prescalar
201116-bit Timer mode.16-bit timer /counter without
pre-scalar
31028-bit auto reload. THx contains a value that is to be loaded
into TLx each time it overflows
4113Split timer mode