Microcomputer and Microcontroller
Microcomputer and Microcontroller
MICROCONTROLLER
Accumulator
I/O Ports
Registers Internal
ROM Interrupt circuits
Internal RAM
Clock circuits
ROM
PC DPTR IE latch Port2
IP
16 bit address bus PCON
SBUF
System Timing Byte/Bit address
SCON
Register Bank 3
TCON
System Interrupts Register Bank 2
TMOD latch
Timers Register Bank 1
TL0
Register Bank 0
TH0
Data buffers TL1 Port3
Memory control TH1
8051 Block Diagram
• ALU:-Performs addition, subtraction,
multiplication, division, logical AND, OR,
EXOR, rotate, clear & compliment operations.
Manipulates 8bit and 16 bit data. Individual bits
may be set, tested, cleared, complimented, moved
and used in logical computation. Provides data
paths and temporary registers for internal
transfers. Makes conditional branching
decisions.Increments registers and automatically
computes program jump addresses. Decrements
registers and compare two bytes through
subtraction. Provides dedicated logic to build
complex instructions such as incrementing a 16
bit register pair.
8051 Block Diagram
• To execute one form of the compare instructions
e.g. the 8051 increments the PC.three times,
reads three bytes of program memory, computes
a register address with logical operations, reads
internal data memory twice, makes an arithmetic
comparison of two variables, computes a sixteen
bit destination address and decides whether or
not to make a branch within two microseconds.
Additional Boolean processor that can be used
for control applications.
8051 architecture
7 6 5 4 3 2 1 0
TCON TF1 TR1 TF0 TR0 IE1 IT1 IE0 IT0
TF -> Timer overflow flag; set to 1 when the count is
changed from FFFF to 0000. Cleared automatically
when interrupt vectored to location 001B(TF1) or 000B
(TF0).
TR -> Timer run bit; 1= run the timer 0 = stop the timer
IE -> External interrupt flag:set to 1 when the external
interrupt is activated; cleared automatically when
vectored to location 0013(IE1)and 0003(IE0).
IT -> external Interrupt type; 1= Edge triggered mode
0= level triggered mode
TMOD 7 6 5 4 3 2 1 0
GATE1 C/T1 M1 M0 GATE0 C/T0 M1 M0
SMOD -- -- -- GF 1 GF 0 PD IDL
FFFF
EXTERNAL
EXTERNAL INTERNAL
INTERNAL
EA = 0 EA
EA==11
0000
PSEN
CASE 1= EA = 1 PC accesses 4k internal and 60k external program
memories.
CASE 2 = EA = 0 PC accesses 64k external program memory.
DATA MEMORY ORGANIZATION
External accesses are EXTERNAL FFFF
distinguished by the mnemonic
x in the instruction e.g. movx INTERNAL
* Can access internal and FF
external RAMS
independently 7F
* Rp(R0 and R1) is used to 00
point internal and 0000
external(port0) and port2 for RD WR
page RAMS.
* DPTR is used to point
external(port 0 & port 2) flat
memory RAM
XTAL1
P0.7
XTAL2 P0.0
EA P1.7
P1.0
PSEN
ALE P2.7
P2.0
RXD/P 3.0
RST TXD/P3.1
INT0/P3.2
INT1/P3.3
VSS
T0/P3.4
T1/P3.5
VCC
WR/P3.6
8051 pin functions
• XTAL1:- I/P line of the internal clock generator. It is
connected to the inverting amplifier.
• XTAL2:-O/P line of the internal inverting amplifier.
XTAL1and XTAL2 are used to connect the crystal.
XTAL2 is used to drive 8051 from a external clock
source, but XTAL1 should EA be grounded.
• EA:- When EA is low, the 8051 accesses only external
program memory otherwise it accesses internal and
external program memories.
• PSEN:- It is used to read contents of external program
memory. It is activated twice every external program
memory cycle. It is not activated for internal fetches.
8051 pin functions
• ALE:- It is used to latch lower byte of external
memory address(program and data memories).
To provide properly timed signals to latch the
lower byte of address, the ALE is activated twice
every machine cycle. It is activated once in
external data memory access cycle.It is activated
in all internal operations. If the external data
memory is not accessed, then the ALE is
activated at a constant rate of 1/6 the oscillator
frequency. It can be used for external clocking or
timing purposes
8051 pin functions
• RST:- It is a Schmitt triggered reset I/p line. It
should be maintained high for at least two
machine cycles while oscillator is running. It
initializes ALE and PSEN signals in input modes.
The internal reset is executed during second cycle
and is repeated every cycle until RST goes low.It
clears the following registers. PC, ACC, B,PSW,
DPTR,TMOD, TCON, TH0, TL0,TH1, TL1 and
SCON. SP-> 07, P0-P3 -> FF SBUF -> , IP->
00000, IE->0 00000, PCON ->0
. Internal RAM is not affected by reset.
8051 pin functions
• PORT0 :- It is an 8 bit bi-directional I/O port.
Logic 1 should be written to float these lines. In
these case, it will function as a high
impedance(open drain) I/p port. During external
accesses, it functions as a multiplexed data and
low order address bus.But internal pull-up is
available during external access.
• PORT1:- It is an 8 bit bi-directional I/O port. To
operate in input mode, logic 1 should be written
into all port lines.
• PORT2:- It is an 8 bit bi-directional I/O port.
During external access, it functions as a high
order address bus.
8051 pin functions
• PORT3:-It is an 8 bit bi-directional I/O port.
Each line can be used as a special function line
specified by contents of special function registers.
P3.0 -> RXD(SBUF), P3.1 -> TXD(SBUF), P3.2
-> INT0(TCON1), P3.3 -> INT1(TCON3), P3.4 ->
T0(TMOD2), P3.5 ->T1(TMOD6) P3.6 -> WR,
P3.7 -> RD. Each pin of port 3 may be
individually programmed to be used either as I/O
or one of the alternative functions. The alternate
function can only be activated if the
corresponding bit of latch in the port SFR
contains a 1. Otherwise the port pin is stuck at 0.
Delay in real time control
• Delay has to be provided between two events.
• Two types of events - Software and hardware.
• Software event is initiated by instructions.
• Hardware event is initiated by external device
through interrupt or other signal.
• Event may be periodic or non periodic.
• Two delay methods have been adopted- software
and hardware.
• In software delay the delay program is used to
provide delay between two events.
Software delay in software events
Execute instruction 1 Event 1
Delay program
Delay
time
Actual
t2
t1
Desired delay
time
Delay Delay
due due
to instruction1 to instruction 2
Software delay with hardware event
Provides delay between two external events
INT1 is activated to begin event.
INT2 is activated to terminate event.
P IS INTERRUPTED Event 1
DELAY ISR
P IS INTERRUPTED Event 2
STOP EVENT
Software delay with hardware event
Desired delay
t1 t2
Delay time
Actual delay
t1 t2
Port
C line 555
Mono
INT O/P
2) Digital:- Count in
C Programmable
clk Timer
INT
Event detection
• Event is detected through either hardware or
software.
• Event detection through software:-
• Checks overflow of the timer.
• Drawbacks:- Microprocessor can not perform
other operations.
• Performance of the system is degraded.
• Advantage over software delay- 1) Accurate
• 2)Can handle two or more events simultaneously.
• 8051 Software delay:-
Event detection
• R7- 1millisecond delay, Register A + Register B--
1 to 65,535 milliseconds XTAL freq.= 12 MHz
and CPU freq.=1MHz
%Error = Actual delay - desired delay
desired delay 100
0.4 + 0.3B(0.59) +1.5/ desired delay
• For 1ms - Error = 2.04%
• For 256 ms - Error = 41%
• For 65,535 ms - Error = 40%
Event detection
• Hardware delay with software event detection-
• Error = Actual delay - Desired delay
Desired delay
Error = Desired delay15 + 3B + 10 s
Desired delay( ms)
% Error = 1.5 + 0.3B +1/desired delay
• For 1ms delay error - 2.5%
• For 256ms delay error - 1.5%
• For 65,535 ms delay error - 1.5%
Hardware delay with hardware event
detection
• In hardware event detection, the microprocessor
can perform all other operations.
• Error is very small and independent of count
value.
• The microprocessor can handle real time events
efficiently.
TIMER/COUNTER
The 8051provides two 16 bit timers viz.. timer0 and
timer1.Each timer contains 16 bit up counter. Each timer
operates in two modes viz. timer and counter. In timer
mode, it counts internal clock pulses or m/ccycles to
provide delay between two events.Min delay =1s Max.
delay =65535m/c cycle time.The timer mode can be used
to measure time intervals, determine pulse width or
initiate events with one microsecond resolution, upto
maximum interval of 64k m/c cycles.Longer delays may be
accumulated through software. In the timer function, the
register is incremented every machine cycle. The machine
cycle consists of 12 crystal clock periods, hence the count
rate is 1/12 of the oscillator frequency.
TIMER/COUNTER
In counter mode, the same hardware measures external events or
clock cycles at frequency DC to500khz. In counter mode,
the count register is incremented at the falling edge of external
clock applied at T0 or T1. In this mode, T0 or T1pin is sampled
during S5P2 of every machine cycle. When the samples show
high in one cycle and low in next cycle the count is incremented.
The new count will appear in the register during S3P1of next
machine cycle. Since it takes 2 machine cycles(24 clock periods)
to recognize falling edge of the clock signal,the maximum count
rate is 1/24 of the oscillator frequency. Each high and low state
of the clock cycle must be held constant for at least one machine
cycle to ensure reliable counting. The counter/timer operation is
selected by C/T bit of the TMOD register.There is no restriction
on duty cycle.
TIMER MODES
Timer operates in 4 modes viz.. mode 0, mode1,
mode2 and mode3. These modes are selected by M1
and M0 of TCON register.
Mode 0: In mode 0, the timer functions like 8048
timer which is an 8 bit counter with a divide by 32
prescalar.In this mode, timer register is configured
as a 13 bit register.
The higher register TH1/0 functions as an 8 bit up
counter TL1/0 functions as a 5 bit counter. As the 8
bit count of TH1/0changes from FF to 00, it sets
TF1/0 flag and activates timer interrupt.
MODE 0
OSC. 12
INT1/0
TIMER MODE 0
To enable counter TR1/0 should be 1 and Gate1/0
should be 0 or INT1/0 should be 1. When Gate1/0 is 1
then the counter is enabled by INT1/0 pin.The pin
INT1/0 functions as an active high GATE1/0 input of
the timer0/timer1. The Pulse width of the signal can
be measured by applying pulse to the INT0/1 pin.
W
Timer mode 0
The 13 bit register consist of 8 bits of TH 0/1 and
lower 5 bits of TL0/1. Upper 3 bits of TL0/1 are
irrelevant.
It counts from N to 3FFF.
Setting TR0/1 flag doesn't clear the registers.
Timer mode:-
Delay = (256 - N )32 12 Crystal clock period.
Counter mode:-
Delay = (256- N) 32 External clock period.
The maximum clock frequency of external clock
signal is crystal frequency/24.
Mode 0 initialization
Initialize
ResetTR0/1
INT1/0
Mode 2 8 bit auto reload
• In this mode TL0/1 is automatically loaded with
contents of TH0/1 after each overflow.
• It is used to generate periodic events like pulse
train.
• TH0/1 functions as preset register.
• TL0/1 functions as an 8 bit up counter.
• Initialization sequence is same as mode0 except
that the counter is used for multiple
transitions( N to 255).
MODE 2
OSC. 12
Port8051
line
RXD
SIPO REG.
Portline
Control points
Operation of serial in mode0
It is not intended for serial communication. It can
be used to connect external PISO and SIPO
registers. The RXD line is common for transmission
and reception, hence external hardware is required
to isolate external transmitting device in transmit
mode. Operation of data transmission:-1) The
microcontroller enables SIPO register of external
device.
2) It writes appropriate control word
(SM2=0,SM1=0 SM0=0,REN=0)
3)It writes data into SBUF. The logic 1 is also
written into 9 bit of shift register at S6P2.
Mode 0
• It activates internal SEND signal to enable shift register.
The time gap between write to SBUF and activation of
SEND is one m/c cycle.
• The SEND signal also activates shift clock on TXD line.
• The shift clock is low during S3,S4 and S5 of the every
m/c cycle and high during S6,S1 and S2.
• The contents of transmit shift register are shifted to the
right one position.
• During transmission MSB of the transmitter loads next
bit with 9th bit(logic 1). This 9th bit indicates the end of
transmission to the internal TX control block and
disables SEND signal and sets TI. Both of these actions
occur at S1P1of the 10th m/c cycle after write to SBUF.
Mode 0 p.b.borole
• Reception is enabled by setting REN bit and RI=0.
• At S6P2 of the next m/c cycle, the RX control unit writes
the bits 11111110 to the receive shift register and in the
next phase activates internal RECEIVE. Signal.
• RECEIVE enables shift clock on TXD line. Shift clock
makes transitions at S3P1 and S6P1 of every m/c cycle.
• At S6P2 of every m/c cycle in which RECEIVE is active,
the contents of the receive shift register are shifted to the
left one bit position. The value that comes in the right
position is the value sampled at the RXD at S5P2 of the
same m/c cycle.
• When the 0 that was initially loaded in rightmost
position arrives at the leftmost position in the shift
register, it indicates the end reception and loads SBUF.
Mode 0
RXD TXD
TF0 2
IE1 3
TF1 4
RI+TI 5
Interrupt operations
• The interrupt flags are sampled at S5P2 of every m/c
cycle.
• But the samples are checked in next machine cycle.This
cycle is also called polling cycle.
• If the flags are set, it resolves priority of unmasked
interrupts and accepts high priority interrupt.
• It executes LCALL instruction after completion of
polling cycle provided that the interrupt is not blocked
by any of the following conditions:
• 1) An interrupt of high or same level interrupt is already
in progress. 2)The polling cycle is not the final cycle of
the instruction cycle. 3)The CPU is executing IRET or
instruction accessing IE and IP.
Interrupt operations
• If the interrupt is activated in last m/c of the current
instruction cycle, the microprocessor completes next
instruction execution.
• The microprocessor does not terminate current
instruction execution.
• The microprocessor always completes next instruction
after RETI and access to IP and IE register.
• All flags are polled in each machine cycle.If the
interrupt is not accepted by CPU due to any of blocking
conditions in the current polling cycle, the CPU will get
new values of flags in the next m/c cycle. Hence previous
sampled values are cancelled. The interrupt source may
lose interrupt activity due to blocking conditions.
Interrupt operations
• The LCALL takes two m/c cycles. If high priority
interrupt is activated before S5P2 of first m/c
cycle of LCALL and after accepting lower
priority interrupt, the LCALL of the new higher
priority interrupt will be executed after
completion of current LCALL execution.
• During execution of LCALL, it clears IE0(edge
triggered) IE1 (edge triggered), TF0 and TF1. It
never clears TI and RI.
• The LCALL saves contents of PC on the stack. It
does not save contents of PSW.
• The microprocessor executes ISR.
Interrupt operations
• The addresses of ISRs are given below:
• IE0 -> 0003H, TF0-> 000BH, IE1-> 0013H
TF1 -> 001BH, TI+RI -> 0023H
• At the end of ISR it executes RETI instruction to
transfer microprocessor control to main
program.
• This instruction tells the completion of ISR.But
RET will not be able to tell the completion of the
ISR.
• The RETI pops two bytes from the stack.
Interrupts of 8051
• Types of interrupts:-
• External interrupts - 8051 provides two external
interrupts. They can be programmed in either edge or
level triggered modes depending of IT bit of TCON
register.
• In edge triggered mode, the interrupt is requested at the
falling edge of the signal. This signal sets IE bit of the
TCON register. Then this bit activates the
corresponding external interrupt. In this mode the IE
bit is cleared automatically after invoking ISR.
• In level triggered mode, the interrupt is requested at the
low level of the signal.
Interrupts of 8051
• In level triggered mode, the corresponding IE bit
TCON register is set at low level of this signal
then this bit activates the corresponding external
interrupt. But this bit is not cleared
automatically after invoking ISR, hence the IE
bit must be cleared by the ISR.
• The external interrupts are sampled once each
m/c cycle, Hence two m/c cycles are needed to
recognize falling edge of the interrupt signal in
edge triggered mode, hence the signal should be
maintained high for at least one m/c cycle. Then
it should be maintained low for at least one m/c
cycle.
Interrupts of 8051
• In level triggered mode, the interrupt signal should be
maintained low until it is acknowledged. This signal
should be removed before completion of the ISR.
• The levels of INT0 and INT1 are inverted and latched
into IE0 and IE1at S5P2 of every m/c cycle.
• But the values are not actually polled by the CPU until
the next m/c cycle. If the request is active and interrupt
is valid then it executes LCALL instruction internally to
transfer microprocessor’s control to the ISR after
completion of the current m/c cycle. Hence the minimum
time gap between activation of interrupt and beginning
of execution of ISR is three m/c cycles.
Interrupts of 8051
• Timer interrupts :- Provides two timer interrupts.
• Timer1 interrupt is requested by TF1 and timer0
interrupt is requested by TF0.
• TF0 and TF1 are set by timers.
• These flags are automatically cleared after invoking
timer ISR.
• Serial interrupt:- This interrupt is common for serial
transmission and serial reception.
• It is activated by TI or RI.Where TI is set by transmitter
( SBUF is empty) and RI is set by receiver( SBUF is full).
• These bits are not cleared after invoking ISR.
• TI and RI bits are used by ISR to distinguish
between transmitter and receiver interrupts.
Single step operation
• In response to any interrupt the microprocessor
completes current instruction cycle. This feature
can be used to complete current instruction of
the main program.
• It doesn't accept same priority level interrupt
during execution of the ISR, hence we can use
same interrupt for other purpose during
execution of same interrupt ISR.
• If the interrupt is activated in IRET instruction,
the microprocessor completes IRET and next
instruction. This feature can be used to execute
only one instruction of the main program.
Single stepping +5v
8051
Single stepping
Single step ISR
L1:JNB P3.2 L1
L2:JB P3.2 L2
RETI
EXPANSION OF
MICROCONTROLLERS
• Needs of expansion:- • Expandable
• Insufficient internal resources:-
resources. • Program memory.
• Software and • Data memory.
hardware • I/O ports.
development.
• System debugging
and testing.
8051 MICROCONTROLLER
EXPANSION
• On chip resources
• ROM 4k 8 bits [0000 to 0FFF] POINTER
-> PC(16 bit).
• RAM 128 8bits {1- Four register banks
of each 8 bytes[00 to 1F] 2- Bit mapped
memory 16 bytes [20 to 2F]3- General
purpose data memory 8bytes [30 to 7F]4-
Special function registers [80 to FF]}.
• 8751 4k 8bits EPROM, 8031-> NIL.
MEMORY EXPANSION
FEATURES
• The internal partitioning of program and data is
extended to external memory.
• Each uses same address but different control
signals i.e. program memory is accessed by PSEN
signal and data memory is accessed by RD AND
WR signals.
• Both can be expanded upto 64k bytes.
• External data memory can be divided into pages
of 256 each.
MEMORY EXPANSION
FEATURES
• Provides strappable pin EA
• EA = 1 ->4K+60K ROM(Internal and external)
and 128+128+64K RAM(Internal and external).
• EA = 0 -> 64K (External) program memory and
128 +128 +64K RAM (Internal and external).
• External data and program memories can be
combined to follow Von Neumann architecture.
MEMORY EXPANSION
FEATURES
WR MEMW
RD
PSEN MEMR