Microprocessor System Architecture Overview
Microprocessor System Architecture Overview
It acts as the interface between hardware and It deals with the components of a connection in a
software. system.
Computer Architecture helps us to understand the Computer Organization tells us how exactly all
functionalities of a system. the units in the system are arranged and
interconnected.
A programmer can view architecture in terms of Whereas Organization expresses the realization of
instructions, addressing modes and registers. architecture.
While designing a computer system architecture is An organization is done on the basis of
considered first. architecture.
Computer Architecture deals with high-level Computer Organization deals with low-level
design issues. design issues.
Architecture involves Logic (Instruction sets, Organization involves Physical Components
Addressing modes, Data types, Cache (Circuit design, Adders, Signals, Peripherals)
optimization)
MICROCONTROLLER OVERVIEW OF MICROPROCESSOR BASED SYSTEM
• Memory is used to store code (programs) and data. • It can be various kinds of like
I
Unit (ALU)
VON NEUMANN ARCHITECTURE HARVARD ARCHITECTURE
It is ancient computer architecture based on It is modern computer architecture based on
stored program computer concept. Harvard Mark I relay based model.
Same physical memory address is used for Separate physical memory address is used for
instructions and data. instructions and data.
There is common bus for data and instruction Separate buses are used for transferring data and
transfer. instruction.
Two clock cycles are required to execute single
An instruction is executed in a single cycle.
instruction.
It is cheaper in cost. It is costly than Von Neumann Architecture.
CPU can not access instructions and read/write at CPU can access instructions and read/write at the
the same time. same time.
It is used in personal computers and small It is used in micro controllers and signal
computers. processing.
2.
MICROCONTROLLER OVERVIEW OF MICROPROCESSOR BASED SYSTEM
RISC vs CISC
RISC CISC
It is a Reduced Instruction Set Computer. It is a Complex Instruction Set Computer.
It emphasizes on software to optimize the It emphasizes on hardware to optimize the
instruction set. instruction set.
It is a hard wired unit of programming in the Microprogramming unit in CISC Processor.
RISC Processor.
It requires multiple register sets to store the It requires a single register set to store the
instruction. instruction.
RISC has simple decoding of instruction. CISC has complex decoding of instruction
Uses of the pipeline are simple in RISC. Uses of the pipeline are difficult in CISC.
It uses a limited number of instruction that It uses a large number of instruction that requires
requires less time to execute the instructions. more time to execute the instructions.
It used LOAD and STORE that are independent It used LOAD and STORE instruction in the
instructions in the register-to-register a memory interaction of a program.
program’s interaction.
RISC has more transistors on memory registers. CISC has transistors to store complex
instructions.
The execution time of RISC is very short. The execution time of CISC is longer.
RISC architecture can be used with high-end CISC architecture can be used with low- end
applications like telecommunication, image applications like home automation, security
processing, video processing, etc. system, etc.
It has fixed format instruction. It has variable format instruction.
The program written for RISC architecture needs Program written for CISC architecture tends to
to take more space in memory. take less space in memory.
Example of RISC: ARM, PA-RISC, Power Examples of CISC: VAX, Motorola 68000
Architecture, Alpha, AVR, ARC and the SPARC family, System/360, AMD and the Intel x86
CPUs.
MICROCONTROLLER OVERVIEW OF MICROPROCESSOR BASED SYSTEM
• Stack Pointer:
• It is used as a memory pointer.
• It points to a memory location in read/write memory, called the stack.
• It is always incremented / decremented by 2 during push and pop operation.
• A stack pointer is a small register that stores the address of the last program request in a stack.
• A stack is a specialized buffer which stores data from the top down. As new requests come in, they
"push down" the older ones.
MICROCONTROLLER THE MEMORY SYSTEMS
Memory Hierarchy
• The Computer memory hierarchy looks like a
pyramid structure which is used to describe the
differences among memory types.
• It separates the computer storage based on hierarchy.
• Level 0: CPU registers
• Level 1: Cache memory
• Level 2: Main memory or primary memory
• Level 3: Magnetic disks or secondary memory
• Level 4: Optical disks or magnetic types or tertiary
Memory
Level-0 – Registers
• The registers are present inside the CPU. As they
are present inside the CPU, they have least access
time.
• Registers are most expensive and smallest in
size generally in kilobytes.
• They are implemented by using Flip-Flops.
Level-1 – Cache
• Cache memory is used to store the segments of
a program that are frequently accessed by the
processor. It is expensive and smaller in size
generally in Megabytes and is implemented by
using static RAM.
Level-2 − Primary or Main Memory
• It directly communicates with the CPU and with
auxiliary memory devices through an I/O processor.
• Main memory is less expensive than cache
memory and larger in size generally in Gigabytes.
• This memory is implemented by using dynamic
RAM.
Level-3 − Secondary storage
• Secondary storage devices like Magnetic Disk are
present at level 3.
• They are used as backup storage.
• They are cheaper than main memory and larger in
size generally in a few TB.
Level-4 − Tertiary storage
• Tertiary storage devices like magnetic tape are present at level 4.
• They are used to store removable files and are the cheapest and largest in size (1-20 TB).
MICROCONTROLLER THE MEMORY SYSTEMS
4. Access Methods
• It is a fundamental characteristic of memory devices. It is the sequence or order in which memory can
be accessed. There are three types of access methods:
• Random Access: If storage locations in a particular memory device can be accessed in any order and
access time is independent of the memory location being accessed. Such memory devices are said to
have a random access mechanism. RAM (Random Access Memory) IC’s use this access method.
• Serial Access: If memory locations can be accessed only in a certain predetermined sequence, this access
method is called serial access. Magnetic Tapes, CD-ROMs employ serial access methods.
• Semi random Access: Memory devices such as Magnetic Hard disks use this access method. Here each
track has a read/write head thus each track can be accessed randomly but access within each track is
restricted to a serial access.
MICROCONTROLLER THE MEMORY SYSTEMS
5. Performance:
• The performance of the memory system is determined using three parameters
Access Time: In random access memories, it is the time taken by memory to complete the read/write
operation from the instant that an address is sent to the memory. For non-random access memories, it is
the time taken to position the read write head at the desired location. Access time is widely used to
measure performance of memory devices.
Memory cycle time: It is defined only for Random Access Memories and is the sum of the access time
and the additional time required before the second access can commence.
Transfer rate: It is defined as the rate at which data can be transferred into or out of a memory unit.
6. Physical type:
Memory devices can be either semiconductor memory (like RAM) or magnetic surface memory (like Hard disks).
[Link] Characteristics:
Volatile/Non- Volatile: If a memory devices continues hold data even if power is turned off. The memory device
is non-volatile else it is volatile.
8. Organization:
Erasable/Non-erasable: The memories in which data once programmed cannot be erased are called Non-erasable
memories. Memory devices in which data in the memory can be erased is called erasable memory.
E.g. RAM(erasable), ROM(non-erasable).
RAM
• It is also called read-write memory or the main memory or the primary memory.
• The programs and data that the CPU requires during the execution of a program are stored in this
memory.
• It is a volatile memory as the data is lost when the power is turned off.
• RAM is further classified into two types
1. SRAM (Static Random Access Memory)
2. DRAM (Dynamic Random Access Memory).
ROM
• Stores crucial information essential to operate the system, like the program essential to boot the
computer.
• It is not volatile.
• Always retains its data.
• Used in embedded systems or where the programming needs no change.
• Used in calculators and peripheral devices.
• ROM is further classified into 4 types- ROM, PROM, EPROM, and EEPROM.
• Types of Read Only Memory (ROM) –
1. PROM (Programmable read-only memory) – It can be programmed by user. Once programmed, the
data and instructions in it cannot be changed.
2. EPROM (Erasable Programmable read only memory) – It can be reprogrammed. To erase data from
it, expose it to ultra violet light. To reprogram it, erase all the previous data.
3. EEPROM (Electrically erasable programmable read only memory) – The data can be erased by
applying electric field, no need of ultra violet light. We can erase only portions of the chip.
RAM ROM
It is a Random-Access Memory. It is a Read Only Memory.
Read and write operations can be performed. Only Read operation can be performed.
Data can be lost in volatile memory when the Data cannot be lost in non-volatile memory when
power supply is turned off. the power supply is turned off.
Storage data requires to be refreshed in RAM. Storage data does not need to be refreshed in ROM.
The size of the chip is bigger than the ROM chip to The size of the chip is smaller than the RAM chip
store the data. to store the same amount of data.
Types of RAM: DRAM and SRAM Types of ROM: MROM, PROM, EPROM,
EEPROM
MICROCONTROLLER THE MEMORY SYSTEMS
SRAM DRAM
It is a Static Random-Access Memory. It is a Dynamic Random Access Memory.
The access time of SRAM is slow. The access time of DRAM is high.
It uses flip-flops to store each bit of information. It uses a capacitor to store each bit of information.
It does not require periodic refreshing to preserve It requires periodically refreshing to preserve the
the information. information.
Virtual Memory
• In the most computer system, the
physical main memory is not as large as
address space of the processor.
• When we try to run a program, if it do
not completely fit into the main memory
the parts of its currently being executed
are stored in main memory and remaining
portion is stored in secondary storage
device such as HDD.
• When a new part of program is to be
brought into main memory for execution
and if the memory is full, it must replace
another part which is already is in main
memory.
• As this secondary memory is not actually
part of system memory, so for CPU,
secondary memory is Virtual Memory.
• Techniques that automatically more
program and data blocks into physical
memory when they are required for
execution are called virtual memory
• Virtual Memory is used to logically
extend the size of main memory.
• When Virtual Memory is used, the
address field is virtual address.
• A special hardware unit knows as MMU translates Virtual Address into Physical Address.
MICROCONTROLLER THE MEMORY SYSTEMS
Cache Memory
• Cache Memory is a special very highspeed memory.
• It is used to speed up and synchronize with high-speed CPU.
• Cache memory is costlier than main memory or disk memory but more economical than CPU registers.
• Cache memory is an extremely fast memory type that acts as a buffer between RAM and the CPU.
• It holds frequently requested data and instructions so that they are immediately available to the CPU
when needed.
Level 1:
• It is the first level of cache memory, which
is called Level 1 cache or L1 cache.
• In this type of cache memory, a small
amount of memory is present inside the
CPU itself. If a CPU has four cores (quad
core cpu), then each core will have its own
level 1 cache.
• As this memory is present in the CPU, it
can work at the same speed as of the CPU.
• The size of this memory ranges from 2KB
to 64 KB.
• The L1 cache further has two types of
caches: Instruction cache, which stores
instructions required by the CPU, and the
data cache that stores the data required by
the CPU.
Level 2:
• This cache is known as Level 2 cache or
L2 cache.
• This level 2 cache may be inside the CPU
or outside the CPU.
• All the cores of a CPU can have their own
separate level 2 cache, or they can share one L2 cache among themselves.
• In case it is outside the CPU, it is connected with the CPU with a very high-speed bus.
• The memory size of this cache is in the range of 256 KB to the 512 KB.
• In terms of speed, they are slower than the L1 cache.
Level 3:
• It is known as Level 3 cache or L3 cache.
• This cache is not present in all the processors; some high-end processors may have this type of cache.
• This cache is used to enhance the performance of Level 1 and Level 2 cache.
• It is located outside the CPU and is shared by all the cores of a CPU.
• Its memory size ranges from 1 MB to 8 MB.
• Although it is slower than L1 and L2 cache, it is faster than Random Access Memory (RAM).
MICROCONTROLLER THE MEMORY SYSTEMS
There are a few basic design elements that serve to classify and differentiate cache architectures.
They are listed down:
1. Cache Addresses
2. Cache Size
3. Mapping Function
4. Replacement Algorithm
5. Write Policy
6. Line Size
7. Number of caches
1. Cache Addresses
• When virtual addresses are used, the cache can be
placed between the processor and the MMU or
between the MMU and main memory.
• A logical cache, also known as a virtual cache,
stores data using virtual addresses.
• The processor accesses the cache directly, without
going through the MMU.
2. Cache Size:
• The size of the cache should be small enough so that
the overall average cost per bit is close to that of main
memory alone and large enough so that the overall
average access time is close to that of the cache alone.
3. Mapping Function
• As there are fewer cache lines than main memory
blocks, an algorithm is needed for mapping main
memory blocks into cache lines.
• Further, a means is needed for determining which
main memory block currently occupies a cache line. •
The choice of the mapping function dictates how the
cache is organized. Three techniques can be used:
direct, associative, and set associative.
• DIRECT MAPPING: The simplest technique,
known as direct mapping, maps each block of main
memory into only one possible cache line.
• The direct mapping technique is simple and
inexpensive to implement.
• ASSOCIATIVE MAPPING: Associative mapping
overcomes the disadvantage of direct mapping by
permitting each main memory block to be loaded into
any line of the cach
• SET-ASSOCIATIVE MAPPING: Set-associative
mapping is a compromise that exhibits the strengths of
both the direct and associative approaches. With set-
associative mapping, block can be mapped into any of the lines of set j.
MICROCONTROLLER THE MEMORY SYSTEMS
4. Replacement Algorithms
• Once the cache has been filled, when a new block is brought into the cache, one of the existing blocks
must be replaced.
• For direct mapping, there is only one possible line for any particular block, and no choice is possible.
• For the associative and set associative techniques, a replacement algorithm is needed.
• To achieve high speed, such an algorithm must be implemented in hardware.
• Least Recently Used (LRU), Least Frequently Used(LFU), First In First Out (FIFO) are some
replacement algorithms.
5. Write Policy
• When a block that is resident in the cache is to be replaced, there are two cases to consider.
• If the old block in the cache has not been altered, then it may be overwritten with a new block without
first writing out the old block.
• If at least one write operation has been performed on a word in that line of the cache, then main memory
must be updated by writing the line of cache out to the block of memory before bringing in the new block.
6. Line Size
• When a block of data is retrieved and placed in the cache, not only the desired word but also some
number of adjacent words is retrieved.
• Basically, as the block size increases, more useful data are brought into the cache.
• The hit ratio will begin to decrease, however, as the block becomes even bigger and the probability of
using the newly fetched information becomes less than the probability of reusing the information that has
to be replaced.
[Link] of Caches
• When caches were originally introduced, the typical system had a single cache.
• More recently, the use of multiple caches has become an important aspect. There are two design issues
surrounding number of caches.
• MULTILEVEL CACHES: Most contemporary designs include both on-chip and external caches.
• The simplest such organization is known as a two-level cache, with the internal cache designated as
level 1 (L1) and the external cache designated as level 2 (L2).
• There can also be 3 or more levels of cache. This helps in reducing main memory accesses.
• UNIFIED VERSUS SPLIT CACHES: Earlier on-chip cache designs consisted of a single cache used
to store references to both data and instructions.
• This is the unified approach. More recently, it has become common to split the cache into two: one
dedicated to instructions and one dedicated to data. These two caches both exist at the same level.
• This is the split cache. Using a unified cache or a split cache is another design issue.
MICROCONTROLLER THE MEMORY SYSTEMS
Interleaved Memory
• The memory system in which successive addresses are evenly spread across memory bank to compensate
for the relatively slow speed of DRAM.
• The contagious memory reads and writes are using each bank in term, resulting in higher memory
throughputs due to reduced waiting for memory banks to become ready for desired operations.
Types of Interleaved Memory
1. High order interleaving:
• In high order memory interleaving,
the most significant bits of the
memory address decides memory
banks where a particular location
resides.
• But, in low order interleaving the
least significant bits of the memory
address decides the memory banks.
• The least significant bits are sent
as addresses to each chip.
• One problem is that consecutive
addresses tend to be in the same chip.
• The maximum rate of data transfer is limited by the memory cycle time. It is also known as Memory
Banking.
• The least significant bits are sent as addresses to each chip.
• One problem is that consecutive addresses tend to be in the same chip.
• The maximum rate of data transfer is limited by the memory cycle time.
• It is also known as Memory Banking.
Associative Memory
• This memory is also known as content
addressable memory.
• Its hardware organization consist of
argument register to hold CPU's request &
a key register to hold predefined "mask"
which is logically 'AND'ed with the CPU's
request in argument register.
• The result of CPU's request will be
masked and remaining unmasked bits are
then parallel y searched against each word
in the associative memory array as shown
below.
• During this parallel search as soon as the
match is found the corresponding match
register bit will be set and the data word
from this matched location is placed on the
OUTPUT for CPU.
8051 MICROCONTROLLER
parameter microprocessor microcontroller
Definition Microprocessors can be Microcontrollers can be
understood as the heart of a understood as the heart of an
computer system. embedded system.
What is it? A microprocessor is a processor A microcontroller is a
where the memory and I/O controlling device wherein the
component are connected memory and I/O output
externally. component are present
internally.
Circuit complexity The circuit is complex due to Microcontrollers are present on
external connection. chip memory. The circuit is less
complex.
Memory and I/O components The memory and I/O The memory and I/O
components are to be connected components are available.
externally.
Compact system compatibility Microprocessors can't be used in Microcontrollers can be used
compact system. with a compact system.
Efficiency Microprocessors are not Microcontrollers are efficient.
efficient.
Zero status flag Microprocessors have a zero Microcontroller doesn't have a
status flag. zero status flag.
Number of registers Microprocessors have less Microcontrollers have more
number of registers. number of registers.
Applications Microprocessors are generally Microcontrollers are generally
used in personal computers. used in washing machines, and
air conditioners.
MICROCONTROLLER 8051 MICROCONTROLLER
2. Register B
• The "B" register is very similar to the Accumulator in the sense that it may hold an 8-bit (1-byte) value.
• The "B" register is used only by two 8051 instructions: MUL AB and DIV AB.
• To quickly and easily multiply or divide A by another number, it may store the other number in "B" and
make use of these two instructions.
a. CY : Carry flag
• This flag is set whenever there is a carry out from the D7 bit.
• This flag bit is affected after an 8-bit addition or subtraction.
• It can also be set to 1 or 0 directly by an instruction such as “SETB C” and “CLR C” where “SETB C”
stands for “set bit carry” and “CLR C” for “clear carry”.
c. P : Parity flag
• The parity flag reflects the number of 1 s in the A (accumulator) register only.
• If the A register contains an odd number of Is, then P = 1.
• Therefore, P = 0 if A has an even number of Is.
d. OV : Overflow flag
• This flag is set whenever the result of a signed number operation is too large, causing the high-order bit
to overflow into the sign bit.
• In general, the carry flag is used to detect errors in unsigned arithmetic operations.
• The overflow flag is only used to detect errors in signed arithmetic operations.
PC : Program Counter
• The Program Counter is one of the most important registers in the CPU.
• A program is a series of instructions stored in the memory.
• These instructions tell the CPU exactly how to get the desired result.
• It is important that these instructions must be executed in a proper order to get the correct result.
• This sequence of instruction execution is monitored by the program counter.
• It keeps track of which instruction is being executed and what the next instruction will be.
MICROCONTROLLER 8051 MICROCONTROLLER
Register Banks:
• It has four register banks i.e Bank 0 to Bank 3
• Each bank has 8 registers.
• Therefore there are 32 registers in the register bank area and each register is of 8 bit. Each bank has
register RO to R7.
• RSO and RS1 bits of Program status word register are used to select register bank.
• The address of the register bank area starts at 00h and ends at 1Fh.
P3.0 (RXD) :
• 10th pin is RXD (serial data receive pin) which is for serial input.
• Through this input signal microcontroller receives data for serial communication.
P3.1 (TXD) :
• 11th pin is TXD (serial data transmit pin) which is serial output pin.
• Through this output signal microcontroller transmits data for serial communication.
MICROCONTROLLER 8051 MICROCONTROLLER
P3.6 (WR’) :
• 16th pin is for external memory write i.e. writing data to the external memory.
P3.7 (RD’) :
• 17th pin is for external memory read i.e. reading data from external memory.
Pin 20 (GND) –
• This pin is connected to the ground. It has to be provided with 0V power supply.
• Hence it is connected to the negative terminal of the power supply.
Pin 40 (VCC) –
• This pin provides power supply voltage i.e. +5 Volts to the circuit.
8051 MICROCONTROLLER
What is a Microcontroller?
A Microcontroller is a programmable digital processor with necessary peripherals. Both microcontrollers
and microprocessors are complex sequential digital circuits meant to carry out job according to the program
/ instructions. Sometimes analog input/output interface makes a part of microcontroller circuit of mixed
mode (both analog and digital nature).
A microcontroller can be compared to a Swiss knife with multiple functions incorporated in the same IC.
Microcontrollers Vs Microprocessors
Microcontrollers have gone through a silent evolution (invisible). The evolution can be rightly termed as silent
as the impact or application of a microcontroller is not well known to a common user, although
microcontroller technology has undergone significant change since early 1970's. Development of some
popular microcontrollers is given as follows.
Microprocessors have undergone significant evolution over the past four decades. This development is
clearly perceptible to a common user, especially, in terms of phenomenal growth in capabilities of personal
computers. Development of some of the microprocessors can be given as follows.
Intel 80586 I
1993
MMX
1997
Celeron II 64 bit
1999
III
2000
IV
It is 8-bit microcontroller, means MC 8051 can Read, Write and Process 8 bit data. This is
mostly used microcontroller in the robotics, home appliances like mp3 player, washing machines,
electronic iron and industries. Mostly used blocks in the architecture of 8051 are as follows:
1. Oscillator and clock generator:
All operations in a microcontroller are synchronized by the help of an oscillator clock. The
oscillator clock generates the clock pulses by which all internal operations are synchronized. A
resonant network connected through pins XTAL1 and XTAL2 forms up an oscillator. For this
purpose a quartz crystal and capacitors are employed. The crystal run at specified maximum and
minimum frequencies typically at 1 MHz to 16 MHz.
2. ALU:
It is 8 bit unit. It performs arithmetic operation as addition, subtraction, multiplication,
division, increment and decrement. It performs logical operations like AND, OR and EX-OR. It
manipulates 8 bit and 16 bit data. It calculates address of jump locations in relative branch
instruction. It performs compare, rotate and compliment operations. It consists of Boolean
processor which performs bit, set, test, clear and compliment. 8051 micro controller contains 34
general purpose registers or working registers.2 of them are called math registers A & B and 32
are bank of registers.
a. Accumulator(A-reg):
It is 8 bit register. Its address is E0H and it is bit and byte accessible. Result of arithmetic
& logic operations performed by ALU is accumulated by this register. Therefore it is called
accumulator register. It is used to store 8 bit data and to hold one of operand of ALU units during
arithmetical and logical operations. Most of the instructions are carried out on accumulator data.
It is most versatile of 2 CPU registers.
b. B-register:
It is special 8 bit math register. It is bit and byte accessible. It is used in conjunction with
A register as I/P operand for ALU. It is used as general purpose register to store 8 bit data.
c. PSW:
It is 8 bit register. Its address is D0H and It is bit and byte accessible. It has 4 conditional
flags or math flags which sets or resets according to condition of result. It has 3 control flags, by
setting or resetting bit required operation or function can be achieved. The format of flag register
is as shown below:
d. FLAG:
1. Carry Flag(CY): During addition and subtraction any carry or borrow is generated then
carry flag is set otherwise carry flag resets. It is used in arithmetic, logical, jump, rotate
and Boolean operations.
2. Auxiliary carry flag(AC): If during addition and subtraction any carry or borrow is
generated from lower 4 bit to higher 4 bit then AC sets else it resets. It is used in BCD
arithmetic operations.
3. Overflow flag(OV): If in signed arithmetic operations result exceeds more than 7 bit
than OV flag sets else [Link] is used in signed arithmetic operations only.
4. Parity flag(P): If in result, even no. Of ones "1" are present than it is called even parity
and parity flag sets. In result odd no. Of ones "1"are present than it is called odd parity
and parity flag resets.
iii. Special function Registers(SFR): The 8051 microcontroller has 11 SFR divided in 4 groups:
A. Timer/Counter register: 8051 microcontroller has 2-16 bit Timer/counter registers called
Timer-reg-T0 And Timer/counter [Link] register is 16 bit register divide into lower and
higher byte register as shown below: These register are used to hold initial no. of count. All of
the 4 register are byte addressable.
1. Timer control register: 8051 microcontroller has two 8-bit timer control register i.e.
TMOD and TCON register. TMOD Register: it is 8-bit register. Its address is 89H. It is byte
addressable. It used to select mode and control operation of time by writing control word.
2. TCON register: It is 8-bit register. Its address is 88H. It is byte addressable. Its MSB 4-
bit are used to control operation of timer/ counter and LSB 4-bit are used for external
interrupt control.
B. Serial data register: 8051 micro controller has 2 serial data register viz. SBUF and SCON.
1. Serial buffer register (SBUF): it is 8-bit register. It is byte addressable .Its address is
99H. It is used to hold data which is to be transferred serially.
2. Serial control register (SCON): it is 8-bit register. It is bit/byte addressable. Its address
is 98H. The 8-bit loaded into this register controls the operation of serial communication.
C. Interrupt register: 8051 µC has 2 8-bit interrupt register.
1. Interrupt enable register (IE): it is 8-bit register. It is bit/byte addressable. Its address
is [Link] is used to enable and disable function of interrupt.
2. Interrupt priority register (IP): It is 8-bit register. It is bit/byte addressable. Its address
is B8H. it is used to select low or high level priority of each individual interrupts.
D. Power control register (PCON): it is 8-bit register. It is byte addressable .Its address is 87H. Its
bits are used to control mode of power saving circuit, either idle or power down mode and also
one bit is used to modify baud rate of serial communication.
Internal RAM
Internal RAM has memory 128-byte. See 8051 hardware for further internal RAM design.
Internal RAM is organized into three distinct areas: 32 bytes working registers from address 00h
to 1Fh 16 bytes bit addressable occupies RAM byte address 20h to 2Fh, altogether 128
addressable bits General purpose RAM from 30h to 7Fh.
Internal ROM
Data memory and program code memory both are in different physical memory but both
have the same addresses. An internal ROM occupied addresses from 0000h to 0FFFh. PC
addresses program codes from 0000h to 0FFFh. Program addresses higher than 0FFFh that
exceed the internal ROM capacity will cause 8051 architecture to fetch codes bytes from external
program memory.
The lower 32 bytes are divided into 4 separate banks. Each register bank has 8 registers of one byte each. A
register bank is selected depending upon two bank select bits in the PSW register. Next 16bytes are bit
addressable. In total, 128bits (16X8) are available in bitaddressable area. Each bit can be accessed and
modified by suitable instructions. The bit addresses are from 00H (LSB of the first byte in 20H) to 7FH (MSB
of the last byte in 2FH). Remaining 80bytes of RAM are available for general purpose.
The special function registers (SFRs) are mapped in the upper 128 bytes of internal data memory address.
Hence there is an address overlap between the upper 128 bytes of data RAM and SFRs. Please note that the
upper 128 bytes of data RAM are present only in the 8052 family. The lower128 bytes of RAM (00H - 7FH)
can be accessed both by direct or indirect addressing while the upper 128 bytes of RAM (80H - FFH) are
accessed by indirect [Link] SFRs (80H - FFH) are accessed by direct addressing only. This feature
distinguishes the upper 128 bytes of memory from the SFRs, as shown in fig 5.4.
SFR Map
The set of Special Function Registers (SFRs) contains important registers such as Accumulator, Register B, I/O
Port latch registers, Stack pointer, Data Pointer, Processor Status Word (PSW) and various control registers.
Some of these registers are bit addressable (they are marked with a * in the diagram below). The detailed
map of various registers is shown in the following figure.
Address
F8H
F0H B*
E8H
E0H ACC*
D8H
D0H PSW*
C8H (T2CON)* (RCAP2L) (RCAP2H) (TL2) (TH2)
C0H
B8H IP*
B0H P3*
A8H IE*
A0H P2*
98H SCON* SBUF
90H P1*
88H TCON* TMOD TL0 TL1 TH0 TH1
80H P0* SP DPL DPH PCON
PSW register stores the important status conditions of the microcontroller. It also stores the bank select bits
(RS1 & RS0) for register bank selection.
1. Idle Mode
2. Power Down mode.
The two power saving modes are entered by setting two bits IDL and PD in the special function register
(PCON) respectively.
The schematic diagram for 'Power down' mode and 'Idle' mode is given as follows:
Fig 12.2 Schematic diagram for Power Down and Idle mode implementation
Idle Mode
Idle mode is entered by setting IDL bit to 1 (i.e., =0). The clock signal is gated off to CPU, but not to
the interrupt, timer and serial port functions. The CPU status is preserved entirely. SP, PC, PSW,
Accumulator and other registers maintain their data during IDLE mode. The port pins hold their logical
states they had at the time Idle was initiated. ALE and are held at logic high levels.
Ways to exit Idle Mode:
1. Activation of any enabled interrupt will clear PCON.0 bit and hence the Idle Mode is exited. The
program goes to the Interrupt Service Routine (ISR). After RETI is executed at the end of the ISR,
the next instruction will start from the one following the instruction that enabled Idle Mode.
2. A hardware reset exits the idle mode. The CPU starts from the instruction following the
instruction that invoked the 'Idle' mode.
Power Down Mode:
The Power down Mode is entered by setting the PD bit to 1. The internal clock to the entire
microcontroller is stopped (frozen). However, the program is not dead. The Power down Mode is exited
(PCON.1 is cleared to 0) by Hardware Reset only. The CPU starts from the next instruction where the
Power down Mode was invoked. Port values are not changed/ overwritten in power down mode. Vcc
can be reduced to as low as 2V in Power Down mode. However, Vcc has to be restored to normal value
before Power Down mode is exited.
MEMORY ORGANISATION:
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. Depending on the model in use (we are still talking about the 8051 microcontroller
family in general) at most a few Kb of ROM and 128 or 256 bytes of RAM is used
All 8051 microcontrollers have a 16-bit addressing bus and are capable of addressing 64 kb memory. It
is neither a mistake nor a big ambition of engineers who were working on basic core development. It is
a matter of smart memory organization which makes these microcontrollers a real “programmers’
goody“.
Program Memory
The first models of the 8051 microcontroller family did not have internal program memory. It was added
as an external separate chip. These models are recognizable by their label beginning with 803 (for
example 8031 or 8032). All later models have a few Kbyte ROM embedded. Even though such an amount
of memory is sufficient for writing most of the programs, there are situations when it is necessary to use
additional memory as well. A typical example are so called lookup tables. They are used in cases when
equations describing some processes are too complicated or when there is no time for solving them. In
such cases all necessary estimates and approximates are executed in advance and the final results are
put in the tables (similar to logarithmic tables).
How does the microcontroller handle external memory depends on the EA pin logic state:
EA=0 In this case, the microcontroller completely ignores internal program memory and executes only
the program stored in external memory.
EA=1 In this case, the microcontroller executes first the program from built-in ROM, then the program
stored in external memory.
In both cases, P0 and P2 are not available for use since being used for data and address transmission.
Besides, the ALE and PSEN pins are also used.
Data Memory
As already mentioned, Data Memory is used for temporarily storing data and intermediate results
created and used during the operation of the microcontroller. Besides, RAM memory built in the 8051
family includes many registers such as hardware counters and timers, input/output ports, serial data
buffers etc. The previous models had 256 RAM locations, while for the later models this number was
incremented by additional 128 registers. However, the first 256 memory locations (addresses 0-FFh) are
the heart of memory common to all the models belonging to the 8051 family. Locations available to the
user occupy memory space with addresses 0-7Fh, i.e. first 128 registers. This part of RAM is divided in
several blocks.
The first block consists of 4 banks each including 8 registers denoted by R0-R7. Prior to accessing any of
these registers, it is necessary to select the bank containing it. The next memory block (address 20h-
2Fh) is bit- addressable, which means that each bit has its own address (0-7Fh). Since there are 16 such
registers, this block contains in total of 128 bits with separate addresses (address of bit 0 of the 20h
byte is 0, while address of bit 7 of the 2Fh byte is 7Fh). The third group of registers occupy addresses
2Fh-7Fh, i.e. 80 locations, and does not have any special functions or features.
Additional RAM
In order to satisfy the programmers’ constant hunger for Data Memory, the manufacturers decided to
embed an additional memory block of 128 locations into the latest versions of the 8051 microcontrollers.
However, it’s not as simple as it seems to be… The problem is that electronics performing addressing
has 1 byte (8 bits) on disposal and is capable of reaching only the first 256 locations, therefore. In order
to keep already existing 8-bit architecture and compatibility with other existing models a small trick was
done.
What does it mean? It means that additional memory block shares the same addresses with locations
intended for the SFRs (80h- FFh). In order to differentiate between these two physically separated
memory spaces, different ways of addressing are used. The SFRs memory locations are accessed by
direct addressing, while additional RAM memory locations are accessed by indirect addressing.
Memory expansion
In case memory (RAM or ROM) built in the microcontroller is not sufficient, it is possible to add two
external memory chips with capacity of 64Kb each. P2 and P3 I/O ports are used for their addressing
and data transmission.
From the user’s point of view, everything works quite simply when properly connected because most
operations are performed by the microcontroller itself. The 8051 microcontroller has two pins for data
read RD#(P3.7) and PSEN#. The first one is used for reading data from external data memory (RAM),
while the other is used for reading data from external program memory (ROM). Both pins are active
low. A typical example of memory expansion by adding RAM and ROM chips (Hardward architecture), is
shown in figure above.
Even though additional memory is rarely used with the latest versions of the microcontrollers, we will
describe in short what happens when memory chips are connected according to the previous
schematic. The whole process described below is performed automatically.
When the program during execution encounters an instruction which resides in external memory
(ROM), the microcontroller will activate its control output ALE and set the first 8 bits of address (A0-A7)
on P0. IC circuit 74HCT573 passes the first 8 bits to memory address pins.
A signal on the ALE pin latches the IC circuit 74HCT573 and immediately afterwards 8 higher bits of
address (A8-A15) appear on the port. In this way, a desired location of additional program memory is
addressed. It is left over to read its content.
Port P0 pins are configured as inputs, the PSEN pin is activated and the microcontroller reads from
memory chip.
Similar occurs when it is necessary to read location from external RAM. Addressing is performed in the
same way, while read and write are performed via signals appearing on the control outputs RD (is short
for read) or WR (is short for write).
If external program/data memory are to be interfaced, they are interfaced in the following way.
External program memory is fetched if either of the following two conditions are satisfied.
1. (Enable Address) is low. The microcontroller by default starts searching for program from
external program memory.
2. PC is higher than FFFH for 8051 or 1FFFH for 8052.
tells the outside world whether the external memory fetched is program memory or data memory.
is user configurable. is processor controlled.
8051 INSTRUCTION SET AND PROGRAMMING:
8051 Addressing Modes
2. Register Addressing: This way of addressing accesses the bytes in the current register bank. Data is
available in the register specified in the instruction. The register bank is decided by 2 bits of Processor
Status Word (PSW).
For example-
ADD A, R0; Adds content of R0 to A and stores in A
4. Register Indirect Addressing: The address of data is available in the R0 or R1 registers as specified in the
instruction.
For example -
MOV A, @R0 moves content of address pointed by R0 to A
Arithmetic Operations:
Port-0 can be configured as a normal bidirectional I/O port or it can be used for address/data interfacing for
accessing external memory. When control is '1', the port is used for address/data interfacing. When the
control is '0', the port can be used as a normal bidirectional I/O port.
Let us assume that control is '0'. When the port is used as an input port, '1' is written to the latch. In this
situation both the output MOSFETs are 'off'. Hence the output pin floats. This high impedance pin can be
pulled up or low by an external source. When the port is used as an output port, a '1' written to the latch
again turns 'off' both the output MOSFETs and causes the output pin to float. An external pull-up is required
to output a '1'. But when '0' is written to the latch, the pin is pulled down by the lower MOSFET. Hence the
output becomes zero.
When the control is '1', address/data bus controls the output driver MOSFETs. If the address/data bus
(internal) is '0', the upper MOSFET is 'off' and the lower MOSFET is 'on'. The output becomes '0'. If the
address/data bus is '1', the upper transistor is 'on' and the lower transistor is 'off'. Hence the output is '1'.
Hence for normal address/data interfacing (for external memory access) no pull-up resistors are required.
Port-0 latch is written to with 1's when used for external memory access.
Port-1 does not have any alternate function i.e. it is dedicated solely for I/O interfacing. When used as
output port, the pin is pulled up or down through internal pull-up. To use port-1 as input port, '1' has to be
written to the latch. In this input mode when '1' is written to the pin by the external device then it read fine.
But when '0' is written to the pin by the external device then the external source must sink current due to
internal pull-up. If the external device is not able to sink the current the pin voltage may rise, leading to a
possible wrong reading.
Port-2 has 8-pins (P2.0-P2.7) . The structure of a port-2 pin is shown in fig 6.4.
Port-3 has 8 pin (P3.0-P3.7) . Port-3 pins have alternate functions. The structure of a port-3 pin is shown in
fig 6.5.
Each pin of Port-3 can be individually programmed for I/O operation or for alternate function. The alternate
function can be activated only if the corresponding latch has been written to '1'. To use the port as input
port, '1' should be written to the latch. This port also has internal pull-up and limited current driving
capability.
P3.0 RxD
P3.1 TxD
P3.2
P3.3
P3.4 T0
P3.5 T1
P3.6
P3.7
Note:
There is a subtle difference between reading a latch and reading the output port pin.
The status of the output port pin is sometimes dependant on the connected load. For instance if a port is
configured as an output port and a '1' is written to the latch, the output pin should also show '1'. If the
output is used to drive the base of a transistor, the transistor turns 'on'.
If the port pin is read, the value will be '0' which is corresponding to the base-emitter voltage of the
transistor.
Reading a latch: Usually the instructions that read the latch, read a value, possibly change it, and then
rewrite it to the latch. These are called "read-modify-write" instructions. Examples of a few instructions are-
In this the latch value of P2 is read, is modified such that P2.1 is the same as Carry and is then written back
to P2 latch.
Reading a Pin: Examples of a few instructions that read port pin, are-
MOV A, P0 ; Move port-0 pin values to A
MOV A, P1; Move port-1 pin values to A
INTERRUPTS OF 8051:
There are five interrupt sources for the 8051, which means that they can recognize 5 different
events that can interrupt regular program execution. Each interrupt can be enabled or disabled
by setting bits of the IE register. Likewise, the whole interrupt system can be disabled by clearing
the EA bit of the same register. Refer to figure below.
Now, it is necessary to explain a few details referring to external interrupts- INT0 and INT1. If
the IT0 and IT1 bits of the TCON register are set, an interrupt will be generated on high to low
transition, i.e. on the falling pulse edge (only in that moment). If these bits are cleared, an
interrupt will be continuously executed as far as the pins are held low.
Interrupt Priorities
It is not possible to forseen when an interrupt request will arrive. If several interrupts are
enabled, it may happen that while one of them is in progress, another one is requested. In order
that the microcontroller knows whether to continue operation or meet a new interrupt request,
there is a priority list instructing it what to do.
The priority list offers 3 levels of interrupt priority:
1. Reset! The absolute master. When a reset request arrives, everything is stopped and the
microcontroller restarts.
2. Interrupt priority 1 can be disabled by Reset only.
3. Interrupt priority 0 can be disabled by both Reset and interrupt priority 1.
The IP Register (Interrupt Priority Register) specifies which one of existing interrupt sources
have higher and which one has lower priority. Interrupt priority is usually specified at the
beginning of the program. According to that, there are several possibilities:
• If an interrupt of higher priority arrives while an interrupt is in progress, it will be immediately
stopped and the higher priority interrupt will be executed first.
• If two interrupt requests, at different priority levels, arrive at the same time then the higher
priority interrupt is serviced first.
• If the both interrupt requests, at the same priority level, occur one after another, the one which
came later has to wait until routine being in progress ends.
• If two interrupt requests of equal priority arrive at the same time then the interrupt to be
serviced is selected according to the following priority list:
1. External interrupt INT0
2. Timer 0 interrupt
3. External Interrupt INT1
4. Timer 1 interrupt
5. Serial Communication Interrupt
Handling Interrupt
When an interrupt request arrives the following occurs:
1. Instruction in progress is ended.
2. The address of the next instruction to execute is pushed on the stack.
3. Depending on which interrupt is requested, one of 5 vectors (addresses) is written to the
program counter in accordance to the table below:
4.
INTERRUPT SOURCE VECTOR (ADDRESS)
IE0 3h
TF0 Bh
TF1 1B h
RI, TI 23 h
Reset
Reset occurs when the RS pin is supplied with a positive pulse in duration of at least 2 machine
cycles (24 clock cycles of crystal oscillator). After that, the microcontroller generates an internal
reset signal which clears all SFRs, except SBUF registers, Stack Pointer and ports (the state of the
first two ports is not defined, while FF value is written to the ports configuring all their pins as
inputs). Depending on surrounding and purpose of device, the RS pin is usually connected to a
power-on reset push button or circuit or to both of them. Figure below illustrates one of the
simplest circuit providing safe power-on reset.
Basically, everything is very simple: after turning the power on, electrical capacitor is being charged for
several milliseconds throgh a resistor connected to the ground. The pin is driven high during this process.
When the capacitor is charged, power supply voltage is already stable and the pin remains connected to
the ground, thus providing normal operation of the microcontroller. Pressing the reset button causes the
capacitor to be temporarily discharged and the microcontroller is reset. When released, the whole
process is repeated…
Microcontrollers normally operate at very high speed. The use of 12 Mhz quartz crystal enables
1.000.000 instructions to be executed per second. Basically, there is no need for higher
operating rate. In case it is needed, it is easy to built in a crystal for high frequency. The problem
arises when it is necessary to slow down the operation of the microcontroller. For example
during testing in real environment when it is necessary to execute several instructions step by
step in order to check I/O pins’ logic state.
Interrupt system of the 8051 microcontroller practically stops operation of the microcontroller
and enables instructions to be executed one after another by pressing the button. Two interrupt
features enable that:
• Interrupt request is ignored if an interrupt of the same priority level is in progress.
• Upon interrupt routine execution, a new interrupt is not executed until at least one instruction
from the main program is executed.
In order to use this in practice, the following steps should be done:
1. External interrupt sensitive to the signal level should be enabled (for example INT0).
2. Three following instructions should be inserted into the program (at the 03hex. address):
What is going on? As soon as the P3.2 pin is cleared (for example, by pressing the button), the
microcontroller will stop program execution and jump to the 03hex address will be executed.
This address stores a short interrupt routine consisting of 3 instructions.
The first instruction is executed until the push button is realised (logic one (1) on the P3.2 pin).
The second instruction is executed until the push button is pressed again. Immediately after
that, the RETI instruction is executed and the processor resumes operation of the main
program. Upon execution of any program instruction, the interrupt INT0 is generated and the
whole procedure is repeated (push button is still pressed). In other words, one button press –
one instruction.
Serial ports on 8051:
Serial Interface
The serial port of 8051 is full duplex, i.e., it can transmit and receive simultaneously.
The register SBUF is used to hold the data. The special function register SBUF is physically two registers. One
is, write-only and is used to hold data to be transmitted out of the 8051 via TXD. The other is, read-only and
holds the received data from external sources via RXD. Both mutually exclusive registers have the same
address 099H.
Serial Port Control Register (SCON)
Register SCON controls serial data communication.
Address: 098H (Bit addressable)
1. RI must be zero. (i.e., the previously received byte has been cleared from SBUF)
2. Mode bit SM2 = 0 or stop bit = 1.
After the data is received and the data byte has been loaded into SBUF, RI becomes one.
Timer-1 is used to generate baud rate for mode-1 serial communication by using overflow flag of the timer
to determine the baud frequency. Timer-1 is used in timer mode-2 as an auto-reload 8-bit timer. The data
rate is generated by timer-1 using the following formula.
Where,
SMOD is the 7th bit of PCON register
fosc is the crystal oscillator frequency of the microcontroller
It can be noted that fosc/ (12 X [256- (TH1)]) is the timer overflow frequency in timer mode-2, which is the
auto-reload mode.
Timer-1 can be run using the internal clock, fosc/12 (timer mode) or from any external source via pin T1
(P3.5) (Counter mode).
Example: If standard baud rate is desired, then 11.0592 MHz crystal could be selected. To get a standard
9600 baud rate, the setting of TH1 is calculated as follows.
Or,
Or,
In mode-1, if SM2 is set to 1, no receive interrupt (RI) is generated unless a valid stop bit is received.
In this mode 11 bits are transmitted through TXD or received through RXD. The various bits are as follows: a
start bit (usually '0'), 8 data bits (LSB first), a programmable 9 th (TB8 or RB8)bit and a stop bit (usually '1').
While transmitting, the 9 th data bit (TB8 in SCON) can be assigned the value '0' or '1'. For example, if the
information of parity is to be transmitted, the parity bit (P) in PSW could be moved into TB8. On reception
of the data, the 9 th bit goes into RB8 in 'SCON', while the stop bit is ignored. The baud rate is programmable
to either 1/32 or 1/64 of the oscillator frequency.
In this mode 11 bits are transmitted through TXD or received through RXD. The various bits are: a start bit
(usually '0'), 8 data bits (LSB first), a programmable 9 th bit and a stop bit (usually '1').
Mode-3 is same as mode-2, except the fact that the baud rate in mode-3 is variable (i.e., just as in mode-1).
Accordingly, if the content of the timer T0 is equal to 0 (T0=0) then both registers it consists of
will contain 0. If the timer contains for example number 1000 (decimal), then the TH0 register
(high byte) will contain the number 3, while the TL0 register (low byte) will contain decimal
number 232.
0 0 0 13-bit timer
0 1 1 16-bit timer
1 0 2 8-bit auto-reload
1 1 3 Split mode
• GATE0 enables and disables Timer 1 using a signal brought to the INT0 pin (P3.2):
o 1 – Timer 0 operates only if the INT0 bit is set.
o 0 – Timer 0 operates regardless of the logic state of the INT0 bit.
• C/T0 selects pulses to be counted up by the timer/counter 0:
o 1 – Timer counts pulses brought to the T0 pin (P3.4).
o 0 – Timer counts pulses from internal oscillator.
• T0M1, T0M0 These two bits select the oprtaional mode of the Timer 0.
T0M1 T0M0 MODE DESCRIPTION
0 0 0 13-bit timer
0 1 1 16-bit timer
1 0 2 8-bit auto-reload
1 1 3 Split mode
The only application of this mode is when two timers are used and the 16-bit Timer 1 the
operation of which is out of control is used as a baud rate generator.
Timer Control (TCON) Register
TCON register is also one of the registers whose bits are directly in control of timer operation.
Only 4 bits of this register are used for this purpose, while rest of them is used for interrupt
control to be discussed later.
Referring to figure above, the timer 0 operates in mode 1 and counts pulses generated by
internal clock the frequency of which is equal to 1/12 the quartz frequency.
Turn on the timer:
The TR0 bit is set and the timer starts operation. If the quartz crystal with frequency of 12MHz is
embedded then its contents will be incremented every microsecond. After 65.536 microseconds,
the both registers the timer consists of will be loaded. The microcontroller automatically clears
them and the timer keeps on repeating procedure from the beginning until the TR0 bit value is
logic zero (0).
How to ‘read’ a timer?
Depending on application, it is necessary either to read a number stored in the timer registers
or to register the moment they have been cleared.
– It is extremely simple to read a timer by using only one register configured in mode 2 or 3. It is
sufficient to read its state at any moment. That’s all!
– It is somehow complicated to read a timer configured to operate in mode 2. Suppose the
lower byte is read first (TL0), then the higher byte (TH0). The result is:
TH0 = 15 TL0 = 255
Everything seems to be ok, but the current state of the register at the moment of reading was:
TH0 = 14 TL0 = 255
In case of negligence, such an error in counting (255 pulses) may occur for not so obvious but
quite logical reason. The lower byte is correctly read (255), but at the moment the program
counter was about to read the higher byte TH0, an overflow occurred and the contents of both
registers have been changed (TH0: 14→15, TL0: 255→0). This problem has a simple solution.
The higher byte should be read first, then the lower byte and once again the higher byte. If the
number stored in the higher byte is different then this sequence should be repeated. It’s about
a short loop consisting of only 3 instructions in the program.
There is another solution as well. It is sufficient to simply turn the timer off while reading is
going on (the TR0 bit of the TCON register should be cleared), and turn it on again after reading
is finished.
Timer 0 Overflow Detection
Usually, there is no need to constantly read timer registers. It is sufficient to register the
moment they are cleared, i.e. when counting starts from 0. This condition is called an overflow.
When it occurrs, the TF0 bit of the TCON register will be automatically set. The state of this bit
can be constantly checked from within the program or by enabling an interrupt which will stop
the main program execution when this bit is set. Suppose it is necessary to provide a program
delay of 0.05 seconds (50 000 machine cycles), i.e. time when the program seems to be
stopped:
First a number to be written to the timer registers should be calculated:
When enabled, the timer will resume counting from this number. The state of the TF0 bit, i.e.
whether it is set, is checked from within the program. It happens at the moment of overflow, i.e.
after exactly 50.000 machine cycles or 0.05 seconds.
How to measure pulse duration?
Suppose it is necessary to measure the duration of an operation, for example how long a device has
been turned on? Look again at the figure illustrating the timer and pay attention to the function of the
GATE0 bit of the TMOD register. If it is cleared then the state of the P3.2 pin doesn’t affect timer
operation. If GATE0 = 1 the timer will operate until the pin P3.2 is cleared. Accordingly, if this pin is
supplied with 5V through some external switch at the moment the device is being turned on, the timer
will measure duration of its operation, which actually was the objective.
How to count up pulses?
Similarly to the previous example, the answer to this question again lies in the TCON register.
This time it’s about the C/T0 bit. If the bit is cleared the timer counts pulses generated by the
internal oscillator, i.e. measures the time passed. If the bit is set, the timer input is provided
with pulses from the P3.4 pin (T0). Since these pulses are not always of the same width, the
timer cannot be used for time measurement and is turned into a counter, therefore. The highest
frequency that could be measured by such a counter is 1/24 frequency of used quartz-crystal.
Timer 1
Timer 1 is identical to timer 0, except for mode 3 which is a hold-count mode. It means that
they have the same function, their operation is controlled by the same registers TMOD and
TCON and both of them can operate in one out of 4 different modes.
TH1 TU
bitl5 bit14 b it13 b it \Z bitl1 bit \0 b it9 hit8 tsit7 bit6 bits bit4 bits bitz bi Al bit0
Timer 1
0 0 D 0 0 0 0 0 **•• 'R •
Bitname
b'âT 5it6 bi‹s bit4 bits bit2 bit1 b'J0
0 0 0 0 0 0 0 0 *” ”^’R=‹*
TU Bit name
MICROCONTROLLER ARM7
Chapter-5. ARM7
Introduction to ARM7
ARM 7
• The ARM processor core originates within a British computer company called Acorn.
• In the mid-1980s they were looking for replacement for the 6502 processor used in their BBC computer
range, which were widely used in UK schools.
• None of the 16-bit architectures becoming available at that time met their requirements, so they designed
their own 32-bit processor.
• The RISC philosophy was used by ARM to create embedded processor.
• The first ARM processor was developed at Acorn Computers Limited.
• Initially ARM stood for Acorn RISC Machine. Later, it is renamed as Advanced RISC Machine.
• ARM Ltd now designs the ARM family of RISC processor cores with a range of other supporting
technologies.
• ARM does not fabricate silicon itself, but instead just produces the design.
• It is an Intellectual Property (or IP) company.
Features of ARM7
• 32/16-bit RISC architecture.
• 32-bit ARM instruction set for maximum performance and flexibility.
• 16-bit Thumb instruction set for increased code density.
• Unified bus interface, 32-bit data bus carries both instructions and data.
• Three-stage pipeline.
• 32-bit ALU.
• Very small die size and low power consumption.
• Fully static operation.
• Coprocessor interface.
Architecture of ARM7
MICROCONTROLLER ARM7
1. Data bus
• The data enters the ARM core through the data bus.
• The same data bus is used by all the instructions as the ARM core uses VonNeumann architecture.
• There are two buses A and B The data can be an instruction opcode
3. Address register
• It is responsible for storing the address generated by the load and store Instructions and placing it on the
address bus.
4. Incrementer
• The Incrementer is responsible for upgrading the address register contents before the core reads the next
register value from memory location or writes the next register value to memory location.
• In case of exception/interrupt the execution of instructions is changed.
5. Barrel shifter
• The R register contents can be preprocessed in the barrel shifter before applying them to the ALU.
• A wide range of shift operations are possible using the barrel shifter and the ALU.
6. Register file
• The register file is a bank of sixteen 32-bit registers. The registers are used for storing data.
7. Sign extend
• Some of the instructions require signed values.
• Whenever the processor reads an 8 or 16 bit signed number from the memory, the sign extend hardware
converts the numbers to 32 bit values and places the number in the register file.
8. Instruction decoder
The instruction decoder decodes the instruction opcode read from the memory and then the instruction is
executed.
The ARM cores are of three types:
(1) Application Core
(2) Embedded Core
(3) Secure Core
• Since the architecture of ARM based processor takes very less space and performs a lot of operations it
is fit to be used in embedded systems
MICROCONTROLLER ARM7
5. Registers RO-R7
• These registers are called as unbanked registers.
• They are general purpose registers and can be used wherever general purpose registers can be used.
• Each register is a 32 bit physical register in all the processor modes.
• The registers RO-R7 do not have any specific use.
6. Registers R8-R14
• These registers are called as banked registers.
• This is because the physical size of each of these registers depends on the current processor mode.
Barrel Shifter
• The ARM processor does not support actual shift instructions.
• But instead it supports a Barrel shifter.
• The Barrel shifter is responsible for carrying the shifts as a part of other instructions.
• The barrel shifter can be used to perform left shift operation by required amount. i.e. it multiplies by the
powers of 2.
• The barrel shifter can also to used to
perform logical and arithmetic right.
• In logical shift right the barrel shifter
shifts the bits to the right by specified
amount. i.e. it divides by power of 2.
• In arithmetic shift right the barrel shifter
shifts the bits to the right by specified
amount and preserves the sign bit, for 2's
complement operations.
• The barrel shifter can also be used for
performing rotations.
• In Rotate Right (ROR) the barrel shifter
performs operation similar to arithmetic
shift right but wraps the bits around as
they leave the LSB and appear as MSB.
• The last bit rotated is also used as carry
out along with LSB.
• For Rotate Right through carry or
Rotate Right Extended (RRX) the barrel
shifter uses the CPSR C flag as 33rd bit.
• In this operation the bits are rotated
through the carry.
• The carry propagates to LSB and MSB
to carry.
• Using the barrel shifter alongwith
ALU a wide range of addresses can
be calculated.
• The contents of register Rm are
preprocessed in the barrel shifter
before applying them as input to the
ALU.
• The register Rm can be a register, shifted register or immediate value.
MICROCONTROLLER ARM7
Pipelining
• The Process of fetching the
next instruction while the
current instruction is being
executed is called as
“pipelining”.
• Pipelining is supported by
the processor to increase the
speed of program execution.
• Increases throughput.
Several operations take place
simultaneously, rather than
serially in pipelining.
• The Pipeline has three stages
fetch, decode and execute
• The three stages used in the
pipeline are:
(i) Fetch : In this stage the
ARM processor fetches the
instruction from the memory.
(ii) Decode : In this stage
recognizes the instruction that
is to be executed.
(iii) Execute : In this stage the
processor processes the
instruction and writes the result
back to desired register.
• If these three stages of
execution are overlapped, it will achieve higher speed of execution. Such pipeline exists in version 7 of
ARM processor.
• Once the pipeline is filled, each instructions require s one cycle to complete execution.
MICROCONTROLLER ARM7
ARM Assembler
• The source to the assembler is the
assembly file i.e. the near machine
language instructions.
• It includes the ARM and the THUMB instructions.
• The assembler as discussed earlier gives a of file.
Compiler
• The C compiler is mostly used for writing
programs
• The advantage of writing programs in C is
that it is a very simple programming language
and the complicated task be easily
implemented.
• The only disadvantage is that there are
certain operations that are not possible in C
and that we need to use the assembly
programming language
• C generates an object code that is extremely
fast an compact, but it is not as fast as the
object code generated by a good programmer
using assembly language.
Method 1
Some compilers have built-in inline
assembler that are used to include assembly
language routines in the C language program.
These assembly language routines are compiled along with the 'C' language routines rather and then
linked together using the linker in the C Compiler
MICROCONTROLLER ARM7
Method 2
Some compilers do not have an in-line assembler. In such cases we need to write separate C routines and
assembly routines.
• They are to be separately compiled or assembled.
• The C subroutines are to be compiled to give the object file; while the assembly files are to be assembled
to give the object file.
• The multiple object files generated in this manner are to be linked together to give a single executable
file.
• It shows Compile, Assemble, and link processes using C compiler, Assembler and Linker.
Linker
• The linker can link one or more .a of files or the object files.
• Hence the input to the Linker is one or multiple .aof files and its output is a .aif file.
• It uses the object libraries to perform its task.
• If the object files are compiled in such a manner that the full debug information is to be generated then
the full symbolic debug tables will be generated when linking the source code.
ARMsd
• It is a symbolic debugger.
• The ARMSd as discussed earlier can be used to emulate the ARM based programs written in C or in
assembly languages.
• It can be either interfaced with the ARM development tool kit or with the ARMulator.
• The ARMulator will give a proper timing related idea and works as an emulator for ARM processor.
• The ARMSd can also be used to interface the code with the development toolkit and verify the working
of the program written.
Unmodified value :
• In this addressing mode, the register or a value is given unmodified i.e. without any shift or rotation e.
g,
• MOV R0, # 1234 H : This instruction will move the immediate constant value 1234 into register R0.
MICROCONTROLLER ARM7
Modified value :
• In this addressing mode, the given value or register is shifted or rotated.
• These are Different shift and rotate operations possible as listed below with examples.
1. Arithmetic Operations
• Arithmetic instructions implements addition and subtraction of 32 bit signed and unsigned values.
Syntax : <instruction>{<cond>} {S} Rd, Rn, N
MICROCONTROLLER ARM7
3. Logical Operations
• Logical Instructions perform
bitwise logical operations on
the two register.
Syntax:
<instruction>{<cond>} {S}
Rd, Rn, N
MICROCONTROLLER ARM7
D) Multiplication Instruction
• Multiply instruction will multiply contents of pair of registers and depending upon the instruction store
the result in another register.
Syntax: MLA{<cond>} {S} Rd, Rm, Rs, Rn
MUL{<cond>} {S} Rd, Rm, Rs
H) Coprocessor Instructions
1. CDP : Coprocessor Data Operation
CDP {<cond>} <coproc>, <opcode_1>
<𝑪𝑹𝒅 >, <𝑪𝑹𝒏 >, <𝑪𝑹𝒎 >, <opcode_2>
Unit (ALU)
VON NEUMANN ARCHITECTURE HARVARD ARCHITECTURE
Same physical memory address is used for Separate physical memory address is used for
instructions and data. instructions and data.
There is common bus for data and instruction Separate buses are used for transferring data and
transfer. instruction.
CPU can not access instructions and read/write at CPU can access instructions and read/write at the
the same time. same time.
It is used in personal computers and small It is used in micro controllers and signal
computers. processing.
2.
MICROCONTROLLER ARM7
1. Programmed I/O
• Data transfer between CPU and the I/O devices may be done in any of the three possible ways:
1. Programmed I/O
In the programmed I/O method of interfacing CPU has direct
control over I/O
• The processor checks the status of the and issues read or write
commands and then transfers data.
• During the data transfer, CPU waits for 1/0 module to
complete operation and hence this system wastes the CPU time.
RISC vs CISC
RISC CISC
It is a Reduced Instruction Set Computer. It is a Complex Instruction Set Computer.
It emphasizes on software to optimize the It emphasizes on hardware to optimize the
instruction set. instruction set.
It is a hard wired unit of programming in the Microprogramming unit in CISC Processor.
RISC Processor.
It requires multiple register sets to store the It requires a single register set to store the
instruction. instruction.
RISC has simple decoding of instruction. CISC has complex decoding of instruction.
Uses of the pipeline are simple in RISC. Uses of the pipeline are difficult in CISC.
It uses a limited number of instruction that It uses a large number of instruction that
requires less time to execute the instructions. requires more time to execute the instructions.
It uses LOAD and STORE that are independent It uses LOAD and STORE instruction in the
instructions in the register-to-register a program memory to memory interaction of program. a
Interaction.
RISC has more transistors on memory registers. CISC has transistors to store complex
instructions.
The execution time of RISC is very short. The execution time of CISC is longer.
RISC architecture can be used with high-end CISC architecture can be used with low- end
application like telecommunication image applications like home automation, security
processing, video processing, etc. system, etc.
It has fixed format instruction. It has variable format instruction.
The program written for RISC architecture needs Program written for CISC architecture tends to
to take more space in memory. take less space in memory.
Example of RISC: ARM, PA-RISC, Power Examples of CISC: VAX, Motorola 68000
Architecture, Alpha, AVR, ARC and the family, System/360, AMD and the Intel x86
SPARC. CPUs.
MICROCONTROLLER ARM7
• Stack Pointer:
• It is used as a memory pointer.
• It points to a memory location in read/write memory, called the stack.
• It is always incremented / decremented by 2 during push and pop operation.
• A stack pointer is a small register that stores the address of the last program request in a stack.
• A stack is a specialized buffer which stores data from the top down. As new requests come in, they "push
the older ones.
MICROCONTROLLER Study 8 bit microcontroller Applications
B. Status Register
• To indicate arithmetic
conditions
• It is a 8-bit register
• Five bits are used
D0: C Carry Flag
D1: DC Digital Carry Flag
D2: Z Zero Flag
D3: OV Overflow Flag
D4: N Negative Flag
D5, D6 & D7 are not implemented & reserved for future used
C. Stack
• PIC18CXX8 has a 21-bit stack with 31 levels, or in other words, a group of 31 memory locations, 21
bits wide, with special purpose.
• Its basic role is to keep the value of program counter after a jump from the main program to an address
of a subprogram.
• In order for a program to know how to go back to the point where it started from, it has to return the
value of a program counter from a stack.
• When moving from a program to a subprogram, program counter is being pushed onto a stack
• When executing instructions such as RETURN, RETLW or RETFIE which were executed at the end of
a subprogram, program counter will be taken from a stack so that program will continue where was
MICROCONTROLLER Study 8 bit microcontroller Applications
3. Data Memory
• The data memory address bus is 12 bits with the capability to address up to 4Mbytes.
• The memory in general consists of sixteen banks, each of 256 bytes, where only 6 banks are used.
• The PIC18F452 has 1536 bytes of data memory (6 banks 256 bytes each) occupying the lower end of
the data memory.
• Bank switching happens automatically when a high-level language compiler is used, and thus the user
need not worry about selecting memory banks during programming.
4. Program Memory
• Program memory addresses consist of 21 bits, capable of accessing 2Mbytes of program memory
locations.
• The PIC18F452 has only 32Kbytes of program memory, which requires only 15 bits.
• The remaining 6 address bits are redundant and not used.
• A table pointer provides access to tables and to the data stored in program memory.
• The program memory contains a 31-level stack which is normally used to store the interrupt and
subroutine return addresses.
5. PWM Mode
• The pulse width modulation (PWM) mode
produces a PWM output at 10-bit resolution.
• A PWM output is basically a square waveform
with a specified period and duty cycle.
• The module is controlled by Timer 2. The PWM
period is given by:
Where
PR2 is the value loaded into Timer
2 register TMR2PS is the Timer 2
prescaler value TOSC is the clock
oscillator period (seconds) The
PWM frequency is defined as
1/(PWM period).
• The resolution of the PWM duty
cycle is 10 bits.
• The PWM duty cycle is selected by
writing the eight most significant bits
into the CCPR1L register and the two
least
MICROCONTROLLER Study 8 bit microcontroller Applications
4. Peripheral features
• Two 8-bit Timer/Counters with separate prescaler and compare mode
• One 16-bit Timer/Counter with separate prescaler, compare mode, and capture mode
• Real time counter with separate oscillator
• Six PWM channels
• 8-channel 10-bit ADC in TQFP and QFN/MLF package
6. Operating voltage:
• 2.7V to 5.5V for ATmega328
7. Temperature range:
• Automotive temperature range: -40°C to +125°C
8. Speed grade:
• 0 to 8MHz at 2.7 to 5.5V (automotive temperature range: -40°C to +125°C)
• 0 to 16MHz at 4.5 to 5.5V (automotive temperature range: -40°C to +125°C)
• N : Negative Flag: The Negative Flag, N, indicates a negative result in an arithmetic or logic operation.
• V : Two's Compliment Overflow Flag: The Two’s Complement Overflow Flag, V, supports two’s
complement arithmetic.
• S : Sign Flag : The S-bit is always an exclusive or between the Negative Flag and the Two’s
Complement Overflow Flag.
• H : Half Carry Flag: It indicates a half carry in some arithmetic operations. It is useful in Binary Code
Decimal (BCD) arithmetic.
• T : Copy Storage: The Bit Copy instructions Bit Load (BLD) and Bit Store (BST) use the T-bit as a
source or destination for the operated bit
• I : Global Interrupt Enable: This bit must be set for the interrupts to be enabled. The individual
interrupt enable control is then performed in separate control registers.
MICROCONTROLLER Study 8 bit microcontroller Applications
3. Indirect with Displacement – The Indirect with Displacement mode reaches 63 address locations from
the base address given by the Y or Z register.
4. Indirect with Pre-decrement – The address registers X, Y, & Z are decremented
5. Indirect with Post-increment – The address registers X, Y, & Z are incremented