1.0 Introduction to Microcontroller Technology
1.0 Introduction to Microcontroller Technology
Microcontroller;
Microcontroller is a highly integrated single chip microcomputer containing all or
most of the parts needed for a controller.
Microcontroller is a microprocessor (CPU) with all the peripherals like RAM,
ROM / EPROM, I/O ports, timers, counters, and ADC on the same chip (on a
single IC package).
1
V.W For Internal Use Only
1.2 Elements of Microcontroller Internal Architecture
2
V.W For Internal Use Only
Fig 2(b) 8051 Microcontroller Internal Architecture (General block diagram)
This is a processor unit which monitors and controls all processes within the
microcontroller.
This is the unit containing the microprocessor which interprets the input signals and
carries out the control actions, according to the program stored in its memory,
communicating the decisions as action signals to the outputs.
The MC 8051 CPU consists of Instruction Decoder, and 8-bit ALU (Arithmetic
Logic Unit) with associated registers like A (Accumulator), B (Register B), PSW
(Program Status Word), SP (Stack Pointer), 16-bit PC (Program Counter) and DPTR
(Data Pointer).
Arithmetical Logical Unit (ALU) performs all arithmetic and logical operations
on 8-bit data. The arithmetic unit can perform addition, subtraction, multiplication
and division while the logic unit performs logical operations such as AND, OR,
NOT, EXOR, rotate, clear, parity, complement, computation, testing for zero etc.
3
V.W For Internal Use Only
1.2.2 RAM (Random Access Memory) / Data Memory
It is a type of memory used for temporary storing data and intermediate results created
and used during the operation of the microcontrollers.
RAM consists of the register banks and stack for temporary data storage. It also
consists of some special function registers (SFR) which are used to program and
control different hardware peripherals like timers/counters, serial port, and I/O ports
among others. SFR perform specific purpose as per the program.
RAM memory is non-volatile memory; the content of RAM memory is cleared once
the power supply is off.
MC 8051 has 128 byte RAM for data storage. What are address range which is
provided for data storage. 128 byte = 27 byte
7654 3210
if initial address is 0000 0000 (00H)
then final or last address is 0 1 1 1 1 1 1 1 (7FH)
The memory locations from 00H to 7FH is provided for data storage in 128 byte
RAM. The program counter can be moved between these locations.
The MC 8051 has 128 bytes of RAM assigned address 00H to 7FH. These 128 bytes
are divided into three different groups as follows:
32 bytes from location 00H to 1FH are set aside for register banks and the
stack (4 register banks each of 8 bytes)
16 bytes from location 20H to 2FH are set aside for bit addressable RAM
80 bytes from 30H to 7FH are set aside for general purpose registers e.g.
read/write (RD/WR) storage which is normally called as Scratch Pad RAM
It is a type of memory used to permanently store the program being executed, and the
operating system and fixed data used by the CPU.
ROM memory is volatile memory; the data saved in this memory does not disappear
when power supply is off.
The size of the program that can be written depends on the size of ROM built in the
microcontroller. However, we can interface up to 64KB ROM memory as an external
chip if the application is large. These sizes vary as specified by manufacturers of the
various type of the microcontrollers.
4
V.W For Internal Use Only
If ROM is added as an external chip;
the microcontroller is cheaper.
the program can be considerably longer.
a number of available pins is reduced as the microcontroller uses its own
input/output ports for connection to the chip.
MC 8051 has 4KB ROM for program storage. What are address range which is
provided for program storage. 4KB = 22 . 210 B (since 1Kilobyte = 210 Byte)
= 212 B
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Starting Address 0 0 0 0 0 0 00 0000 0000 (0000H)
Last Address 0 1 1 1 1 1 11 1111 1111 (7FFFH)
The memory locations from 0000H to 7FFFH is available for program storage in 4KB
ROM.
i) RAM is used for data storage while ROM is used for program storage
ii) Data of RAM can be changed during processing while data of ROM can’t be
changed during processing i.e. RAM is for temporary storage of date while
ROM is for permanent storage of data.
This is a ROM that can be programmed and then the program made permanent.
It is a special type of memory often used to store values, created and used during
operation (such as calibration values, codes, values to count up to etc.), which must be
saved after turning the power supply off.
Its contents may be changed during program execution (similar to RAM), but remains
permanently saved even after the loss of power (similar to ROM).
5
V.W For Internal Use Only
1.2.4 I/O (Input/Output) Ports – Serial and Parallel
These are registers (called ports) connected to the microcontroller pins and used to
interface between the microcontroller and the peripheral devices.
I/O ports allow for connections to be made through I/O channels to input devices such
as sensors and output devices such as motors and solenoids, and also to enter program
from a program panel
They are called input/output ports because it is possible to change a pin function
according to the user's needs.
These registers are the only registers in the microcontroller the state of which can be
checked by voltmeter!
1.2.5.1 Timer
Timer is a register which uses an internal quartz oscillator as a clock source that
makes it possible to measure the time between two events.
It resets the system if the software is malfunctioning and does not progress properly.
It also resets the system if the hardware does not work properly.
1.2.5.2 Counter
Counter is a register which uses pulses coming from external source to increment its
contents
Note: Timers and counters are commonly 8-bit or 16-bit SFRs, the contents of which
is automatically incremented by each coming pulse and an interrupt generated once
the register is completely loaded.
6
V.W For Internal Use Only
1.2.6 Interrupt (Interrupt routine)
Interrupt is a signal which informs the central processor that an external event worth
immediate response has happened and interrupts regular program execution. Such an
event might be the arrival of a measurement value that is to be processed as fast as
possible. Interrupts from various sources can occur at any time during the program
execution.
When an interrupt occurs, the current state of the program counter (memory
address) is pushed onto the stack and the appropriate program is executed. This is
achieved by the processor executing a kind of LCALL instruction, which results in a
jump to address associated with the interrupt routine.
Interrupt sources and associated addresses are listed in the table below.
By storing the current state of the PC, after a subroutine or an interrupt execution, the
microcontroller knows from where to continue regular program execution. This
address is cleared after returning to the program because there is no need to save it
any longer, and one location of the stack is automatically available for further use.
Why interrupt: Electronics is usually faster than physical processes it should keep
under control. This is why the microcontroller spends most of its time waiting for
something to happen or execute. In other words, when some event takes place, the
microcontroller does something. In order to prevent the microcontroller from
spending most of its time endlessly checking for logic state on input pins and
registers, an interrupt is generated.
7
V.W For Internal Use Only
1.3 Classification of Microcontrollers
8
V.W For Internal Use Only
1.3.1 Popular Microcontrollers
This memory architecture has a single data bus that is used to fetch both instructions
and data
Both program instructions and data are stored in a common main memory
When the controller addresses main memory, it first fetches an instruction, and then it
fetches the data to support the instruction. The two separate fetches slows up the
controller’s operation.
9
V.W For Internal Use Only
Instruction Code
Program
ROM
Data
Addr Memory
Processor
Variable Interface
Ctrl and
RAM Unit
Built-in
Registers
Stack
RAM
This memory architecture has separate data bus and an instruction bus. This allows
execution to occur in parallel. As an instruction is being “pre-fetched”, the current
instruction is executing on the data bus. Once the current instruction is complete, the
next instruction is ready to go. This allows for much faster execution than Von-
Neumann architecture.
Control Space
Data
Addr Instruction
Decoder
Ctrl
Program Registers
RAM Space
PC Stack
Data
Processor and
register Addr
interface
Ctrl
10
V.W For Internal Use Only
1.3.1.3 CISC (Complex Instruction Set Computer) Architecture
This instruction set architecture (ISA) has over 80 instructions, many of them very
powerful and very specialized for specific control tasks.
The instructions behave quite differently. Some instructions might only operate on
certain address spaces or registers and others might only recognize certain addressing
modes
Its advantage;
Many of the instructions are micro-link, allowing the programmer to use one
instruction in place of many simpler instructions.
This instruction set architecture (ISA) has fewer instructions implemented hence the
chip is able to implement some performance enhancing features
Intel 8048, Intel 8051 (is 8-bit microcontroller), Intel 8096 (is 16-bit microcontroller),
NEC 7800, and Intel 8044
Other microcontroller series include Motorola 6811, Zilog’s Z8 and PIC 16x etc
11
V.W For Internal Use Only
1.4 Criteria in choosing a Microcontroller
1. Meeting the computing needs of the task at hand efficiently and cost effectively
i) Speed of operation,
ii) the amount of RAM and ROM on chip,
iii) the number of I/O pins and timers on chip,
iv) size,
v) packaging,
vi) power consumption,
vii) easy to upgrade,
viii) cost per unit,
ix) noise of environment
2. Availability of software development tools such as
i) assemblers,
ii) debuggers,
iii) C compilers,
iv) emulator,
v) simulator,
vi) technical support
3. Wide availability and reliable sources of the microcontroller
12
V.W For Internal Use Only
9) A five source interrupt structure with two priority levels
10) Four registers bank
11) Full duplex Serial Port
12) 210 bit addressable
13) Boolean processor
14) Single +5V operating voltage
13
V.W For Internal Use Only
1.6.2 Pin out of 8051 Microcontroller
RST (Reset) – to reset the microcontroller and terminate all activities (Also referred
to as power-on reset, it causes all values in the registers to be lost and set PC to 0s)
XTAL1 and XTAL2 (Crystal Oscillator) – are for external oscillator input used to
keep the microcontroller clocking/running. The microcontroller speed is determined
by the maximum oscillator frequency connected to XTAL.
PSEN (Program Store Enable) – store to read (enable) the external program memory
ALE (Address Latch Enable) – to select valid address i.e. 1 (address on AD0-AD7) 0
(data on AD0-AD7)
EA/Vpp (External Access Enable)
EA-0 – execute program in external memory
EA-1 – execute program in internal memory
Vpp – it receives 21V for on-chip EPROM
14
V.W For Internal Use Only
1.6.3 I/O Ports of 8051 Microcontroller
The 8051 microcontroller contains four 8-bit I/O ports (P0, P1, P2, and P3). Each port
can be used as input or output (bi-direction). Direction is set in Special Function
Registers. Each port consist of a latch, an output driver and an input buffer.
Port 0
It is an 8-bit R/W general purpose I/O port
It functions as multiplexed data and low order address bus (AD0 – AD7) during
external memory access (It emits the low-order address byte during accesses to
external memory).
Pins 32-39 (P0.7 – P0.0) AD7 – AD0
Part 1
It functions as simply an 8-bit R/W general purpose I/O port (and it does not have
any alternative function)
Pins 1-8 (P1.0 – P1.7)
Port 2
It is an 8-bit R/W general purpose I/O port
It functions as high order address bus (A8 – A15) during external memory access
(It emits the higher order address byte during accesses to external memory).
Pins 21-28 (P2.0 – P2.7) A8 – A15
Port 3
It is an 8-bit R/W general purpose I/O port
In an alternating function, each pins can be used as a special function I/O pin.
That is, the pins can be used by internal peripherals (timers) or external interrupts
Pins 10-17 (P3.0 – P3.7)
15
V.W For Internal Use Only
1.7 MC 8051 Registers
A register or a memory cell is an electronic circuit which can memorize the state of
one byte. They are classified as general-purpose registers or special function registers.
The 8051 registers include A (Accumulator), B (Register B), PSW (Program Status
Word), DPTR (Data Pointer), SP (Stack Pointer), and 16-bit PC (Program Counter).
These are special function registers.
They are part of RAM memory. All SFR are assigned names (which are different for
different types of the microcontrollers) and each of them has a special function (as
their name suggests). SFR uses memory addresses between 80H and FFH.
SFR are used to program, and monitor and control different hardware peripherals like
timers/counters, serial port, and I/O ports among others.
SFR is also used to access the CPU registers, interrupt control registers, UART
(Universal Asynchronous Receiver and Transmitter) and power control.
It’s an 8-bit register used to hold/store the data for the ALU operations.
It holds a source operand and receives the results of the arithmetic instructions, logical
operation and a number of special data movement instructions.
Note: The name “Accumulator” came from the fact this register is used to accumulate
(or store) the result of all Arithmetic and most of the Logical Operations.
16
V.W For Internal Use Only
1.7.3 B (Register B)
The B Register is used along with the ACC in multiplication and division operations
(i.e. it is used in only two instructions MUL AB and DIV AB).
These two operations are performed on data that are stored only in Registers A and B.
It can also be used as a General Purpose Register for normal operations and is often
used as an Auxiliary Register by Programmers to store temporary results
The PSW register contains program status information which is an 8-bit word.
The PSW Register consists of Flag Bits, which help the programmer in checking the
condition of the result and also make decisions
17
V.W For Internal Use Only
Register bank selection
The DPTR Register is used to hold address of external memory (Program ROM or
Data RAM).
It serves as a base register in indirect jumps, lookup table instructions and external
data transfer.
It consists of a low byte DPL (Data Pointer Low) and a high byte DPH (Data Pointer
High). It may be manipulated as a 16-bit data register or as two independent 8-bit
registers.
It’s an 8-bit register used to access the stack. It points out to the top of the Stack and
indicates the next data to be accessed.
It’s incremented by 1 before data is stored during PUSH and CALL execution. When
reading data from stack, the data is retrieved from the Address in SP and after that the
SP is decremented by 1. The SP is also accessed using POP and RET instructions.
While the stack may reside anywhere in on-chip RAM, the stack pointer is initialized
to 07H after reset. This causes the stack to begin at location 08H.
Stack
Stack is the part of RAM used to store the current state of the program counter
(memory address) when an interrupt occurs.
After each instruction execution, the value of the counter is incremented by 1. For this
reason, the program executes only one instruction at a time.
However, the value of the program counter can be changed at any moment, which
causes a “jump” to a new memory location. This is how sub-routines/interrupts and
branch instructions are executed. After jumping, the counter resumes 1 incremented.
18
V.W For Internal Use Only
1.7.7 Special Peripheral Functions
The microprocessor system requires peripheral chips such as timers, counters, UARTs
etc to meet these requirements. However 8051 family integrates such capabilities in
Timers/counter and Serial interface
Timer / Counter
The 8051 has two 16 bit timer/counter registers – Timer O and Timer 1, which can be
independently configured to operate as a timer or counter.
When used as a timer the register is incremented after every machine cycle. Since a
machine cycle consists of 12 oscillator periods the count rate is 1/12 of the oscillator
frequency.
Serial Interface
19
V.W For Internal Use Only
1.6.6 8051 Addressing Modes
The MC 8051 has eight addressing modes (aspects instruction set architecture which
define how machine language instructions in that architecture identify the operands of
each instruction).
1. Register Addressing
In this mode, the instruction operates on data in the Rn registers (R0 - R7) of the
selected register bank.
The upper five bits of the op-code indicate the instruction while least significant
three bits of the op-code indicate which register is to be used. A, B, DPTR and
CY, the Boolean processor accumulator can be addressed as registers.
Format of instruction: Op-code Rn
Example 1;
Logically OR the contents of accumulator A with that of register R3
ORL A, R3 ; OR content of A with that of R3
Example 2;
Add the content of register R3 and R4 from bank 2
MOV PSW #00001000B ; select register bank 2
MOV A, R3 ; move content of R3 to A
ADD A, R4 ; add content of R4 to A
2. Direct Addressing
In this mode, the instruction operate on data of the SFR. Direct addressing is the
only method of accessing the special function registers. The lower 128 bytes of
internal RAM are also directly addressable.
Format of instruction: Op-code Direct address
Example 1;
Transfer the content of the accumulator to Port 1
MOV P1, A ; move content of A to port 1
Example 2;
Add the content of memory locations 50H and 51H.
MOV A, 50H ; load byte from address 50H to A
ADD A, 51H ; add content of address 51H to A
20
V.W For Internal Use Only
Example 2;
Add the content of register R1 to the content of register R0.
MOV A, @R0 ; load the content R0 in to A
ADD A, @R1 ; add the content of R1 to that of A
4. Immediate addressing
In this mode, the operand of the instruction may be a numeric constant, a symbolic
variable or an arithmetic expression using constants, symbols and operators.
The symbol # before an operand denote immediate addressing.
Format of instruction: Op-code #constant
Example 1;
Logically AND the constant 77 decimal with that of accumulator
ANL A, #77 ; AND immediate data to A
Example 2;
Add the constant 52 decimal in accumulator
MOV A, #52 ; move immediate data to A
5. Relative Addressing
Also called program counter relative addressing, this mode is used only with
certain jump instructions. A relative address (offset) is an 8-bit signed value which
is added to the PC to form the address of the next instruction executed.
Example;
JZ rel ;
6. Absolute Addressing
In this mode, there are only two instructions, ACALL (absolute call) and AJMP
(absolute jump) which perform branching.
Example;
ACALL addr11 ; absolute subroutine call
7. Long Addressing
In this mode, there are only two instructions, LCALL (long call) and LJMP (long
jump) which enable the program to branch.
Example;
LCALL addr16 ; long subroutine call
8. Indexed Addressing
In this mode, the address in a base register is added to a positive offset to form an
effective address. This mode facilitates lookup table accesses.
Example 1;
Jump instruction
JMP @A+DPTR ; jump indirect relative to DPTR to A
The base register is the DPTR and the positive offset is
held in the accumulator
21
V.W For Internal Use Only
Example 2;
Move code byte instructions
MOVC A, @A+DPTR ; move content relative to DPTR to A
The base register is the DPTR and the positive offset is
held in the accumulator
2. Write an ALP in 8051 to subtract two 8-bit numbers and exchange digits.
Mnemonic Comment
MOV A, #9F ; get first number to A
MOV R0, #40 ; get the second number in Ro
SUBB A, R0 ; A ← [A – {Ro}]
SWAP A ; exchange digits
5. Write an ALP in 8051 to generate BCD up counter and send each count to port 1.
Label Mnemonic Comments
MOV A, #00 ; Initialize counter
BACK: MOV P1, A ; Send count to port 1
ADD A, #01 ; Increment counter
DA A ; Decimal adjust the counter
AJMP BACK ; Jump back.
22
V.W For Internal Use Only