0% found this document useful (0 votes)
7 views

1.0 Introduction to Microcontroller Technology

The document provides an overview of microcontroller technology, detailing the definition, components, and architecture of microcontrollers compared to microprocessors. It explains the internal architecture of microcontrollers, including the CPU, RAM, ROM, I/O ports, timers, counters, and interrupts, as well as the classification of microcontrollers based on various criteria. Additionally, it discusses popular microcontroller families and their architectures, such as Von-Neumann and Harvard, along with the differences between CISC and RISC architectures.

Uploaded by

bosiremuriithi8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

1.0 Introduction to Microcontroller Technology

The document provides an overview of microcontroller technology, detailing the definition, components, and architecture of microcontrollers compared to microprocessors. It explains the internal architecture of microcontrollers, including the CPU, RAM, ROM, I/O ports, timers, counters, and interrupts, as well as the classification of microcontrollers based on various criteria. Additionally, it discusses popular microcontroller families and their architectures, such as Von-Neumann and Harvard, along with the differences between CISC and RISC architectures.

Uploaded by

bosiremuriithi8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1.

0 Introduction to Microcontroller Technology

1.1 A Controller and Microcontroller

A controller is a device used to control some process or an external device or aspect


of the environment.

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).

In summary, the microcontroller:


 contains all components which allow it to operate stand-alone;
 has been designed in particular for monitoring and/or control tasks;
 sees the integration of a number of useful functions into a single
IC package.
These functions are:
 The ability to execute a stored set of instructions to carry out user defined
tasks.
 The ability to be able to access external memory chips to both read
and write data from and to the memory.

In comparison with microprocessor:


 The microprocessor is a normal single chip CPU.
 Communication with external devices is achieved via a data bus, hence the chip
mainly features data and address pins as well as a couple of control pins.
 All peripheral devices (memory, floppy controller, USB controller, timer, etc) are
connected to the bus.
 A microprocessor cannot be operated stand-alone, at the very least it requires
some memory and an output device to be useful.

Fig. 1(a) and (b) Structure of microprocessor and microcontroller

1
V.W For Internal Use Only
1.2 Elements of Microcontroller Internal Architecture

The microcontroller typically includes:


i) CPU (Central Processing Unit),
ii) RAM (Random Access Memory),
iii) ROM / PROM / EPROM (Erasable Programmable Read Only Memory),
iv) I/O (Input/Output) ports – serial and parallel,
v) Timers and Counters,
vi) Interrupt controller.

Fig 2(a) 8051 Microcontroller Internal Architecture (General block diagram)

2
V.W For Internal Use Only
Fig 2(b) 8051 Microcontroller Internal Architecture (General block diagram)

1.2.1 MC 8051 CPU (Central Processing Unit)

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).

 Instruction decoder recognizes program instructions and prompts an action. Its


abilities are expressed in the "instruction set" which is different for each
microcontroller family.

 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.

Memory Mapping (Address range of RAM)

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.

MC 8051 RAM Memory Space Allocation

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

1.2.3 ROM (Read Only Memory)

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.

On-chip ROM and external chip ROM

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.

The internal ROM;


 is smaller.
 is more expensive.
 leaves more pins available for connecting to peripheral environment.

Memory Mapping (Address range of ROM)

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.

Difference between RAM and 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.

1.2.3.1 EPROM (Erasable Programmable Read Only Memory)

This is a ROM that can be programmed and then the program made permanent.

1.2.3.2 EEPROM (Electrically Erasable Programmable ROM)

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).

A disadvantage of EEPROM memory is that the process of programming is relatively


slow. It is measured in milliseconds.

The EEPROM is not contained in all microcontrollers.

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 Timers and Counters

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.

Watchdog Timer provides ability to recover from software malfunction or hardware


upset by resetting the system and program execution starting again from the first
instruction.

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.

Interrupt Interrupt Address


IE0 0003 H
TF0 000B H
TE1 0013 H
TF1 001B H
R1+T1 0023 H

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

Microcontrollers can be classified on the basis of;


i) Internal Bus Width,
ii) Architecture,
iii) Memory,
iv) Instruction Set.

Fig. 3: Various Types of Microcontrollers

8
V.W For Internal Use Only
1.3.1 Popular Microcontrollers

8051 PIC AVR ARM


Microcontroller: Microcontroller: Microcontroller: Microcontroller:
(Peripheral (Advanced (Advanced RISC
Interface Virtual RISC) Machine)
Controller)
Manufacturer Intel Microchip Atmel ARM Holdings
Technology
Families: 8051 variants PIC16,PIC17, Tiny, Atmega, ARM v4,5,6,7
PIC18, PIC24, Xmega, special and series
PIC32 purpose AVR
Popular AT89C51, PIC18fXX8, Atmega8, 16, LPC2148, ARM
Microcontrollers P89v51, etc PIC16f88X, 32, Arduino Cortex-M0 to
PIC32MXX Community ARM Cortex-
M7, etc.
Bus width 8-bit for 8/16/32-bit 8/32-bit 32-bit mostly
standard core also available in
64-bit
ISA CISC Some feature of RISC RISC
RISC
Memory Von Neumann Harvard Modified Modified
Architecture architecture architecture Harvard
architecture
Speed 12 4 1 clock/ 1 clock/
Clock/instruction Clock/instruction instruction cycle instruction cycle
cycle cycle
Memory ROM, SRAM, SRAM, FLASH Flash, SRAM, Flash, SDRAM,
FLASH EEPROM EEPROM
Communication UART, PIC, UART, UART, USART, UART, USART,
Protocols USART,SPI,I2C USART, LIN, SPI, I2C, (special LIN, I2C, SPI,
CAN, Ethernet, purpose AVR CAN, USB,
SPI, I2S support CAN, Ethernet, I2S,
USB, Ethernet) DSP, SAI (serial
audio interface),

1.3.1.1 Von-Neumann Architecture

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

Fig 4: Von-Neumann Architecture

1.3.1.2 Harvard Architecture

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.

The Harvard architecture also has some added fabrication complexity.

Control Space
Data

Addr Instruction
Decoder
Ctrl

Program Registers
RAM Space

PC Stack

Data
Processor and
register Addr
interface
Ctrl

Fig 5: Harvard architecture

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.

1.3.1.4 RISC (Reduced Instruction Set Computer) Architecture

This instruction set architecture (ISA) has fewer instructions implemented hence the
chip is able to implement some performance enhancing features

The RISC design is simple. Benefits of the simple design include;

i) The chip is smaller in size


ii) It has smaller pin count
iii) Its power consumption is very low

1.3.2 Intel Microcontroller Series

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

1.3.3 The Intel 8051 Microcontroller Family Members

8032 AH, 8751 AH, 80C32, 8052, and 8032

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

1.5 Applications of Microcontrollers

a) Used in control applications such as home monitoring system


b) Microcontrollers are found in embedded applications e.g. automotive applications,
appliances (microwave oven, refrigerators, vending machines, television e.t.c),
automobiles (engine control, diagnostics e.t.c), instrumentations among others
c) It’s used in robotics
d) Used in data logging applications for data monitoring and recording.

1.6 The 8051 Family Microcontroller


A single chip microcontroller contains all essential elements of a microcomputer on a
single chip.

The Intel 8051 Basic Components/Features:

1) 8-bit CPU (optimized for control applications)


2) 128 bytes internal RAM (data memory on chip)
3) 4KB internal ROM (program memory on chip)
4) Four 8-bit I/O ports, P0 – P3 (32 bidirectional I/O lines)
5) Two 16-bit timers/counters
6) 64k address space for external data memory
7) 64k address space for external program memory
8) On-chip oscillator and clock frequency

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

1.6.1 8051 Microcontroller Chip Architecture

Fig. 6: 8051 Microcontroller Chip Architecture (8051 Internal Block Diagram)

13
V.W For Internal Use Only
1.6.2 Pin out of 8051 Microcontroller

The microcontroller 8051 is available in a standard 40 pin dual in line package.


However, its on-chip program memory may differ with other series.

Fig. Pin diagram 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.

The input/output ports have different identifications which are as described.

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)

Port 3 Pin Alternate function


P 3.0 RxD (serial input port) – receives serial data of serial COM circuit
P 3.1 TxD (serial output port) – transmit serial data
P 3.2 INT0 (external hardware interrupt 0) – receives peripheral interrupts
P 3.3 INT1 (external hardware interrupt 1) – receives peripheral interrupts
P 3.4 T0 (timer/counter 0 external input) – receives external clock pulses
P 3.5 T1 (timer/counter 1 external input) – receives external clock pulses
P 3.6 WR (external data memory write strobe) – performs write operation to
external RAM memory
P 3.7 RD (external data memory read strobe) – performs read operation
from external RAM (external data memory)

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.

1.7.1 Special Function Registers (SFR)

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.

Special Function Registers of MC 8051 are categorized in to:


 Mathematics or CPU Registers: A and B
 Status Register: PSW (Program Status Word)
 Pointer Registers: DPTR (Data Pointer – DPL, DPH) and SP (Stack Pointer)
 I/O Port Latches: P0 (Port 0), P1 (Port 1), P2 (Port 2) and P3 (Port 3)
 Peripheral Control Registers: PCON (power control), SCON (serial port
control), TCON (timer/counter control), TMOD (timer/counter mode control),
IE (interrupt enable control) and IP (interrupt priority)
 Peripheral Data Registers: TL0, TH0, TL1, TH1 (Timers) and SBUF (serial
port data buffer)

1.7.2 Accumulator (Register A or ACC or A)

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.

Some of the operations where the Accumulator is used are:


 Arithmetic Operations like Addition, Subtraction, Multiplication and Division.
 Logical Operations like AND, OR, NOT etc.
 Data Transfer Operations (between 8051 and External Memory)

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.

 During Multiplication Operation, one of the operand (multiplier or multiplicand)


is stored in B Register and also the higher byte of the result.
 During Division Operation, the B Register holds the divisor and also the
remainder of the result.

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

1.7.4 Program Status Word (PSW) / Flag Register

The PSW register contains program status information which is an 8-bit word.

The contents of the PSW Register:


(MSB (LSB)
PSW.7 PSW.0
CY AC F0 RS1 RS0 OV - P

The PSW Register consists of Flag Bits, which help the programmer in checking the
condition of the result and also make decisions

Symbol Bit Flag Name Significance


CY PSW.7 Carry flag Used in arithmetic or logic or Boolean operations
AC PSW.6 Auxiliary Used in BCD arithmetic operations during addition
Carry flag or subtraction instructions to indicate carry or
borrow out of bit 3
F0 PSW.5 Flag 0 This is a general purpose user flag
RS1 PSW.4 Register bank Select control bits 1 and 0 for banks 0 to 3
RS0 PSW.3 Register bank Select control bits 1 and 0 for banks 0 to 3
OV PSW.2 Over flow Used in arithmetic operations to indicate overflow
- PSW.1 Reserved May be used as a general purpose flag
P PSW.0 Parity flag Used to indicate an odd/even number of 1-bit in the
accumulator i.e. even parity

17
V.W For Internal Use Only
Register bank selection

RS1 RS0 Register Bank selected Address memory


0 0 Bank 0 00H – 07H
0 1 Bank 1 08H – 0FH
1 0 Bank 2 10H – 17H
1 1 Bank 3 18H – 1FH

1.7.5 Data Pointer (DPTR)

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.

1.7.6 Stack Pointer (SP)

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.

Program Counter (PC)

Program Counter points to the memory address of next instruction to be executed.

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

A microcontroller or microcomputer system has to fulfill certain common needs so as


to make the system more versatile such as;

i) Keeping track of elapsed real time


ii) Maintaining a count of signal transitions
iii) Measuring the precise width of input pulse
iv) Communicating with other system or people
v) Closely monitoring asynchronous external events

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.

When used as a counter the register is incremented in response to a high or low


transition of the corresponding external input pin T0 and T1.

Serial Interface

Full duplex asynchronous serial interface is possible to implement on chip, using


serial interface control circuitry. The special feature of this circuit is a double be
buffered receiver, due to which it can receive a second character while the first one is
being held in an intermediate register until the software fetches it. This double
fetching reduces the time complexity of servicing a serial interface and reduces the
chances of an overrun error.

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

3. Indirect Addressing (Register–Indirect Addressing)


In this mode, the instruction performs an operation on the data whose address is
contained in register R0 or R1 in the selected register bank. It may be used also
for execution of PUSH and POP instructions.
The symbol @ before R0 or R1 denotes indirect addressing.
Format of instruction: Op-code @R0 or @R1
Example 1;
SUBB A, @R0 ; subtract the content of R0 from 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

MOVC A, @A+PC ; move content relative to PC to A


The base register is the PC and the positive offset is
held in the accumulator

MC 8051 Sample Programs


1. Write an ALP (Assembly language program) in 8051 to add two 8-bit numbers
Mnemonic Comment
MOV A, #30H ; (A) ← 30H
ADD A, #50H ; (A) ← (A) + 50H

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

3. Write an ALP in 8051 to multiply two 8 bit numbers


Mnemonic Comment
MOV A, #8F ; get the first number in A
MOV B, #77 ; get the second number in B
MUL AB ; (A x B) higher byte of B & lower byte of A.

4. Write an ALP in 8051 for division of two 8 bit numbers


Mnemonic Comments
MOV A, #80 ; get the first number in A
MOV B, #40 ; get the second number in B
DIV AB ; (A ÷ B) remainder in B and quotient in A

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

You might also like