Microcontrollers - UNIT I
Microcontrollers - UNIT I
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
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
modern houses
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
An 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
Stand alone Embedded systems : A stand-alone embedded system works by itself. It is a selfcontained 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
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
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
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.No
Embedded System
Home Appliances
Office Automation
Security
Academia
Instrumentation
Telecommunication
Automobile
Entertainment
Application
Dishwasher, washing machine, microwave, Top-set box,
security system , HVAC system, DVD, answering
machine, garden sprinkler systems etc..
Fax, copy machine, smart phone system, modern,
scanner, printers.
Face recognition, finger recognition, eye recognition,
building security system , airport security system,
alarm system.
Smart board, smart room, OCR, calculator, smart cord.
Signal generator, signal processor, power
supplier,Process instrumentation,
Router, hub, cellular phone, IP phone, web camera
Fuel injection controller, anti-locking brake system, airbag system, GPS, cruise control.
MP3, video game, Mind Storm, smart toy.
Aerospace
10
Industrial automation
11
Personal
12
Medical
13
14
Miscellaneous:
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.
I/O Devices
Communication Interfaces
Central 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.
The 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.
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
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
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 compiler
Cross assembler
Cross linker
Cross debugger
Cross-compiled libraries for the target host.
Operating system-dependent libraries and header files for the target system
Simulator: 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.
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
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
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
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.
For example, Intel 8051 is 8-bit microcontroller and Intel 8096 is 16-bit
Microprocessor
Microcontroller
A microprocessor is a general
CPU
2
Microprocessor
Microcontroller
both bit
instructions
instructions
are
are addressable as well as byte addressable.
The
Instruction
A microprocessor has
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
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
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
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 Architecture
Harvard Architecture
The code is executed serially and takes more The code is executed in parallel and hence less
clock cycles
number of clock cycles are required
There is no exclusive Multiplier
The programs can be optimized in lesser size The program tend to grow big in size
1.8 RISC & CISC Processor architecture
Based 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 architectures
One 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 architectures
S.N
o
1
2
CISC
RISC
Single-clock,
only
reduced
instruction
10
11
12
13
14
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 Microcontrollers
The 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.
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.
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
DIV AB
It 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
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.
For
EX : MOV A, R5
or
MOV A, 05H
1.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.
help
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.
DATA1 :
DATA 2 :
DATA 3 :
DATA 4 :
DATA 5 :
ORG
000H
DB 27
; Decimal ( 1B in Hex)
DB 01101101 B
; BINARY ( 6D in Hex)
DB 31H
; Hex Number
DB 1589
; ASCII Number
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 15
END directive
END 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
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
AC
PSW.6
FO
PSW.5
RS1
PSW.4
RS0
PSW.3
OV
PSW.2
Overflow flag
---
PSW.1
PSW.0
The selection of the register Banks and their addresses are given below table 1.3.
38 00111000
+ 2F
00101111
--------------------------67 01100111
After addition
A=67H, CY=0
MOV A, # 9CH
ADD A, # 64H
9C
10011100
+ 64
01100100
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 0A
Similarly POP 3 instruction pops the contents of stack into R3 register.Now the contents of the
SP is decremented by 1
In 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
MOV A ,DPH
Symbol
Name of SFR
Address (Hex)
ACC*
Accumulator
0E0
B*
B-Register
0F0
PSW*
0DO
SP
81
82
DPL
DPTR
83
P0*
Port 0
80
P1*
Port 1
90
P2*
Port 2
0A
P3*
Port 3
0B
10
IP*
0B8
11
IE*
0A8
12
TMOD
89
13
TCON*
88
14
TH0
8C
15
TL0
8A
16
TH1
8D
17
TL1
8B
18
SCON*
98
19
SBUF
99
20
PCON
87
DPH
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.
(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 TxD
1.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
PORT 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.
Port 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
MOV P2, A
; A=FF hex
; make P2 an input port by writing all 1s to it
Dual 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 8031based 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
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
MOV P3, A
A= FF hex
Alternate 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
P3.0
10
RxD
P3.1
11
TxD
P3.2
12
P3.3
13
P3.4
14
T0
P3.5
15
T1
P3.6
16
P3.7
17
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 ;
ADD A,P1
Avery 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 Port0
BACK:
MOV A,#55H ;
MOV P0, A ;
ACALL DELAY ;
MOV A,#0AAH ;
MOV P0,A
ACALL DELAY ;
SJMP BACK
;
Complement A i.e A=AAH
; 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
CLR bit
CPL bit
JB bit, target
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
LCALL DELAY ;
CLR P1.0
P1.1 = 0
LCALL DELAY ;
SJMP HERE
keep doing it
The same program can also implemented by usnin another bit manipulation instruction namely
CPL
HERE
CPL P11
P1.0
1.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 stack
The 8051 microcontroller has FIVE interrupts in addition to Reset. They are
Each interrupt has a specific place in code memory where program execution (interrupt service
routine) begins.
Timer 0 overflow:
Timer 1 overflow:
001B H
Serial Interrupt :
0023 H
000B H
Upon 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 :
n EA : Global enable/disable. To enable the interrupts this bit must be set High.
n ---
IP.7: reserved
IP.6: reserved
IP.5: Timer 2 interrupt priority bit (8052 only)
IP.4: Serial port interrupt priority bit
IP.3: Timer 1 interrupt priority bit
IP.2: External interrupt 1 priority bit
IP.1: Timser 0 interrupt priority bit
IP.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
etc
Ex : 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
etc
Ex : 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 TR1
C/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
1
M0
M1
Mode
Operation
13-bit Timer mode
8-bit Timer/counter. THx with TLx as 5-bit
prescalar