Professional Documents
Culture Documents
Ilovepdf Merged PDF
Ilovepdf Merged PDF
Mohammad Ali Mazidi, Janice G. Mazidi, Rolin D. McKinlay, The 8051 Microcontroller and
Embedded Systems, 2014, 2nd Edition, Pearson, India.
3.1 8051 : Architecture, Memory organization and Machine cycle
AT89C2051 2K 128 32 3 8 3V 20
AT89LV51 4K 128 32 2 6 3V 40
AT89LV52 8K 128 32 3 8 3V 40
3.1 8051 : Architecture, Memory organization and Machine cycle
3. 8051 Architecture
detailed:
3.1 8051 : Architecture, Memory organization and Machine cycle
4. 8051 Memory
Organization:
2.4 DATA memory
(RAM): SFRs
SFR Register layout
3.1 8051 : Architecture, Memory organization and Machine cycle
Mohammad Ali Mazidi, Janice G. Mazidi, Rolin D. McKinlay, The 8051 Microcontroller and
Embedded Systems, 2014, 2nd Edition, Pearson, India.
3.2 8051 Addressing modes
1. Addressing Modes
• Addressing mode is a way to address an operand. Operand means
the data we are operating upon.
• There are five major addressing modes available in the 8051:
– Immediate
– Direct
– Register
- Register indirect
– Indexed
3.2 8051 Addressing modes
1. Addressing Modes
a. IMMEDIATE ADDRESSING MODE
• This addressing mode is named as “immediate” because it transfers
an 8-bit data immediately to the accumulator (destination operand).
• In general we can write MOV A, #data.
MOV A, #6AH
• The ‘#’ symbol before 6AH indicates that operand is a data (8 bit).
1. Addressing Modes
a. IMMEDIATE ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
a. IMMEDIATE ADDRESSING MODE
• The opcode for MOV A, # data is 74H.
The opcode is saved in program memory at 0202 address.
The data 6AH is saved in program memory 0203.
• When the opcode 74H is read, the next step taken would be to
transfer whatever data at the next program memory address
(here at 0203) to accumulator A (E0H is the address of
accumulator).
• This instruction is of two bytes. So after the execution of this
instruction, program counter will add 2 and move to 0204 of program
memory.
• This instruction is executed in one cycle
3.2 8051 Addressing modes
1. Addressing Modes
a. IMMEDIATE ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
a. IMMEDIATE ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
a. IMMEDIATE ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
b. DIRECT ADDRESSING MODE
• Here the address of the data (source data ) is given as operand.
Lets take an example.
MOV A, 04H
• Here 04H is the address of register 4 of register bank#0.
When this instruction is executed, what ever data is stored in
register 04H is moved to accumulator.
• In the figure register 04H holds the data 1FH. So the data 1FH is
moved to accumulator.
3.2 8051 Addressing modes
1. Addressing Modes
b. DIRECT ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
b. DIRECT ADDRESSING MODE
• The opcode for instruction MOV A, address is E5H.
When the instruction at 0202 is executed (E5H),
accumulator is made active and ready to receive data.
• Then program control goes to next address that is 0203 and
look up the address of the location (04H) where the source data
(to be transferred to accumulator) is located.
• At 04H the control finds the data 1F and transfers it to accumulator
and hence the execution is completed.
3.2 8051 Addressing modes
1. Addressing Modes
b. DIRECT ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
b. DIRECT ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
c. REGISTER ADDRESSING MODE
• In this addressing mode we use the register name directly (as
source operand). At a time registers can take value from
R0,R1…to R7.An example is shown below.
MOV A, R4
• In register direct addressing mode, data is transferred
from the register (based on which register bank is selected)
to accumulator.
• PSW.3 and PSW.4bits are known as register bank select bits as
they are used to select register banks.
3.2 8051 Addressing modes
1. Addressing Modes
c. REGISTER ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
c. REGISTER ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
c. REGISTER ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
d. REGISTER INDIRECT ADDRESSING MODE
• In this addressing mode, address of the data (source data to
transfer) is given in the register operand.
MOV A, @R0
• Here the value inside R0 is considered as an address,
which holds the data to be transferred to accumulator
• If R0 holds the value 20H, and
we have a data 2F H stored at the address 20H,
then the value 2FH will get transferred to accumulator after
executing this instruction.
3.2 8051 Addressing modes
1. Addressing Modes
d. REGISTER INDIRECT ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
d. REGISTER INDIRECT ADDRESSING MODE
• The opcode for MOV A, @R0 is E6H.
Assuming that register bank #0 is selected.
So the R0 of register bank #0 holds the data 20H.
• Program control moves to 20H
where it locates the data 2FH and
it transfers 2FH to accumulator.
• This is a single byte instruction and
the program counter increments 1 and moves to 0203 of
program memory.
• Only R0 and R1 (of All register banks) are allowed to form a
register indirect addressing instruction..
3.2 8051 Addressing modes
3.2 8051 Addressing modes
3.2 8051 Addressing modes
3.2 8051 Addressing modes
3.2 8051 Addressing modes
1. Addressing Modes
e. INDEXED ADDRESSING MODE
MOVC A, @A+DPTR and MOVC A, @A+PC
• where DPTR is data pointer and PC is program counter
(both are 16 bit registers).
• The source operand is @A+DPTR and
we will get the location of the source data (to transfer).
1. Addressing Modes
e. INDEXED ADDRESSING MODE
3.2 8051 Addressing modes
1. Addressing Modes
e. INDEXED ADDRESSING MODE
• The opcode for the instruction is 93H.
DPTR holds the value 01FE, where 01 is located in DPH (higher 8 bits)
and FE is located in DPL (lower 8 bits).
• Accumulator now has the value 02H.
A 16 bit addition is performed and
now 01FE H+02 H results in 0200 H.
• What ever data is in address 0200 H will get transferred to
accumulator.
• The previous value inside accumulator (02H) will get replaced with new
data from 0200H. New data in the accumulator is shown in dotted line
box.
3.2 8051 Addressing modes
1. Addressing Modes
e. INDEXED ADDRESSING MODE
• This is a 1 byte instruction with 2 cycles needed for execution.
So, the execution time required for this instruction is high compared to
other addressing modes (which all were 1 cycle).
• The other example MOVC A, @A+PC works the same way as above
example.
The only difference is, instead of adding DPTR with accumulator, here
data inside program counter (PC) is added with accumulator to
obtain the target address
3.2 8051 Addressing modes
3.2 8051 Addressing modes
3.3 8051 Instruction set
Mohammad Ali Mazidi, Janice G. Mazidi, Rolin D. McKinlay, The 8051 Microcontroller and
Embedded Systems, 2014, 2nd Edition, Pearson, India.
3.3 8051 Instruction set
1. Instruction Set
• The process of writing program for the microcontroller mainly
consists of
giving instructions (commands) in the specific order in which
they should be executed
in order to carry out a specific task.
• As electronics cannot “understand” what for example an instruction
“if the push button is pressed- turn the light on” means,
then a certain number of simpler and precisely defined orders
that decoder can recognize must be used.
• All commands are known as INSTRUCTION SET.
3.3 8051 Instruction set
1. Instruction Set
• All microcontrollers compatible with the 8051 have in total of 255
instructions, i.e. 255 different words available for program writing.
• Many instructions are considered to be “different”, even though they
perform the same operation,
so there are only 111 truly different commands.
• For example:
ADD A,R0,
ADD A,R1, ... ADD A,R7
are instructions that perform the same operation (addition of the
accumulator and register). Taking into account that all instructions
perform only 53 operations (addition, subtraction, copy etc.)
3.3 8051 Instruction set
1. Instruction Set
8051 instructions have 8-bit opcode
• There are 256 possible instructions of which 255 are implemented
• Every 8-bit opcode from 00 to FF is used except for A5.
• Some instructions have one or two additional bytes for data or
address
• There are 139 1-byte instructions,
92 2-byte instructions, and
24 3-byte instruction
3.3 8051 Instruction set
1. Instruction Set
Depending on operation they perform, all instructions are divided in 5
groups:
• Arithmetic Instructions
• Logic Instructions
• Program Branching Instructions
• Boolean or Bit-oriented Instructions
• Data Transfer Instructions
3.3 8051 Instruction set
1. Instruction Set
DATA PROGRAM
ARITHMETIC LOGICAL BOOLEAN
TRANSFER BRANCHING
JMP / SJMP / AJMP/
MOV ADD / ADDC ANL CLR
LJMP
MOVC SUBB ORL SETB LCALL / ACALL
MOVX INC XRL MOV RET / RETI
PUSH DEC ANL JZ / JNZ
POP MUL ORL JC /JNC
XCH DIV CPL JB / JNB
XCHD DA A JBC
CLR CJNE
CPL DJNZ
RL / RLC NOP
RR / RRC
SWAP
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
• Using Arithmetic Instructions, you can perform addition,
subtraction, multiplication and division.
• The arithmetic instructions also include increment by one,
decrement by one and Decimal Adjust Accumulator.
• The arithmetic instructions has no knowledge about the data
format i.e. signed, unsigned, ASCII, BCD, etc.
• The operations performed by the arithmetic instructions affect flags
like carry, overflow, zero, etc. in the PSW
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
• The Mnemonics associated with the Arithmetic Instructions of the
8051 Microcontroller Instruction Set are:
ADD CLR
ADDC CPL
SUBB RL
MUL RLC
DIV RR
INC RRC
DEC SWAP
DA A
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
• ADD:
8 bit addition with accumulator (A) and second operand
Result is always in accumulator.
CY flag is set/reset appropriately
• ADDC
8 bit addition between accumulator, a second operand and the
previous value of CY flag
Useful for 16 bit addition in two steps.
CY flag is set/reset appropriately
3.3 8051 Instruction set
3.3 8051 Instruction set
Decrement R2 and
Jump when Not zero to
AGAIN
3.3 8051 Instruction set
Analysis of 6.2
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
• SUBB:
Subtract with borrow
Subtract an operand and previous value of borrow (carry) flag from
accumulator (A)
A = A - <operand> - CY
Result is always saved in accumulator.
CY flag is set/reset appropriately
3.3 8051 Instruction set
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
• MUL AB / DIV AB
Multiply A by B and place the result in A:B
Divide A by B and place the result in A:B
• Multiplication – single byte product
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
• MUL AB / DIV AB
Multiply A by B and place the result in A:B
Divide A by B and place the result in A:B
• Divide – single byte division (If B is zero, A/B will set Over flow flag)
• Result in A and B with
Quotient byte in A and
Remainder byte in B
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
a. ARITHMETIC INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
b. LOGICAL INSTRUCTIONS
• The Logical Instructions perform logical operations like
AND, OR, XOR.
• Logical Instruction are performed on Bytes of data on a bit-by-bit
basis.
• ANL
• ORL
• XRL
3.3 8051 Instruction set
1. Instruction Set
b. LOGICAL INSTRUCTIONS
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
1. Instruction Set
c. BOOLEAN or BIT ORIENTED INSTRUCTIONS
• The Boolean or Bit Manipulation Instructions will deal with bit
variables.
• We know that there is a special bit-addressable area in the RAM
and some of the Special Function Registers (SFRs) are also bit
addressable.
• CLR
• SETB
• CPL
• MOV
• ANL
• ORL
3.3 8051 Instruction set
1. Instruction Set
c. BOOLEAN or BIT ORIENTED INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
c. BOOLEAN or BIT ORIENTED INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
c. BOOLEAN or BIT ORIENTED INSTRUCTIONS
3.3 8051 Instruction set
Swap A
3.3 8051 Instruction set
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
• These instructions control the flow of program logic.
SJMP JZ / JNZ
LJMP JC / JNC
AJMP JB / JNB
JMP JBC
LCALL CJNE
ACALL DJNZ
RET NOP
RETI
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
• These instructions control the flow of program logic.
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
d. PROGRAM BRANCHING INSTRUCTIONS
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
1. Instruction Set
e. DATA TRANSFER INSTRUCTIONS
• The Data Transfer Instructions are associated with transfer with data
between registers or external program memory or external data
memory. MOV
MOVC
MOVX
PUSH
POP
XCH
XCHD
3.3 8051 Instruction set
3.3 8051 Instruction set
1. Instruction Set
e. DATA TRANSFER INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
e. DATA TRANSFER INSTRUCTIONS
3.3 8051 Instruction set
1. Instruction Set
e. DATA TRANSFER INSTRUCTIONS
3.3 8051 Instruction set
3.3 8051 Instruction set
3.3 8051 Instruction set
2. Arithmetic Flags
• Flag: It is a 1-bit register that indicates the status of the result
from an operation
• Flags are either at a flag-state of value 0 or 1
• Arithmetic flags indicate the status of the results from mathematical
operations (+, -, *, /)
• There are 4 arithmetic flags in the 8051:
Carry (C), Auxiliary Carry (AC),
Overflow (OV), Parity (P)
• All the above flags are stored in the Program Status Word(PSW)
3.3 8051 Instruction set
2. Arithmetic Flags
• Instructions that affect Flag
3.3 8051 Instruction set
2. Arithmetic Flags
• Instructions that affect Flag
3.3 8051 Instruction set
Overflow problem
3.3 8051 Instruction set
Rotate left
3.3 8051 Instruction set
Rotate Right
3.3 8051 Instruction set
Mohammad Ali Mazidi, Janice G. Mazidi, Rolin D. McKinlay, The 8051 Microcontroller and
Embedded Systems, 2014, 2nd Edition, Pearson, India.
4.1 8051 IO Ports
1. 8051: IO Ports
4.1 8051 IO Ports
1. 8051: IO Ports
• All 8051 microcontrollers have 4 I/O ports each comprising 8 bits
which can be configured as inputs or outputs.
• In total of 32 input/output pins enabling the microcontroller to be
connected to peripheral devices are available for use.
• Pin configuration, i.e. whether it is to be configured as an input (1) or an
output (0), depends on its logic state.
• When the first 0 is written to a port, it becomes output port.
For reconfiguring it as input, 1 must be sent to the port (programmed)
• All the ports upon RESET are configured as input, ready to be used as
input ports
4.1 8051 IO Ports
1.1 PORT - 0
• Port 0 occupies a total of 8 pins (pins 32-39). It can be used for input
or output.
• To use the pins of port 0 as both input and output ports, each pin
must be connected externally to a 10K ohm pull-up resistor. This is
due to the fact that P0 is an open drain, unlike P1, P2, and P3.
• Dual role of port-0: Port 0 is also designated as AD0-AD7, allowing it
to be used for both address and data. When ALE = 0, it provides
data D0-D7, but when ALE =1 it has address and data with the help
of a 74LS373 latch.
4.1 8051 IO Ports
1.1 PORT - 0
4.1 8051 IO Ports
1.2 PORT - 1
• Port 1 occupies a total of 8 pins (pins 1 through 8). ‰
Port 1 can be
used as input or output
• In contrast to port 0, this port does not need any pull-up resistors
since it already has pull-up resistors internally
• Upon reset, port 1 is configured as an input port
• To make port 1 an input port, it must be programmed as such by
writing 1 to all its bits
4.1 8051 IO Ports
1.3 PORT - 2
• Port 2 occupies a total of 8 pins (pins 21- 28). It can be used as
input or output. Upon reset, Port 2 is configured as an input port.
• Just like P1, P2 does not need any pull-up resistors since it
already has pull-up resistors internally.
• In many 8051-based system, P2 is used as simple I/O
but in 8031-based systems, port 2 must be used along with P0 to
provide the 16-bit address for the external memory
• Port 2 is also designated as A8 –A15, indicating its dual function
and Port 0 provides the lower 8 bits via A0 –A7.
4.1 8051 IO Ports
1.4 PORT - 3
• Port 3 occupies a total of 8 pins, pins 10 through 17.
• It can be used as input or output.
Upon reset, Port 3 is configured as an input port.
• P3 does not need any pull-up resistors, the same as P1 and P2
did not.
• Port 3 has the additional function of providing some extremely
important signals such as interrupts.
4.1 8051 IO Ports
1.4 PORT - 3
4.1 8051 IO Ports
PORT 1 BITS P1.7 P1.6 P1.5 P1.4 P1.3 P1.2 P1.1 P1.0
BIT ADDRESS 97H 96H 95H 94H 93H 92H 91H 90H
PORT 2 BITS P2.7 P2.6 P2.5 P2.4 P2.3 P2.2 P2.1 P2.0
BIT ADDRESS A7H A6H A5H A4H A3H A2H A1H A0H
PORT 3 BITS P3.7 P3.6 P3.5 P3.4 P3.3 P3.2 P3.1 P3.0
BIT ADDRESS B7H B6H B5H B4H B3H B2H B1H B0H
4.1 8051 IO Ports
IO Program: 01
4.1 8051 IO Ports
IO Program: 02
• The following code will continuously send out to port 1 the
alternating value 55H and AAH.
4.1 8051 IO Ports
IO Program: 03
4.1 8051 IO Ports
IO Program: 04
4.1 8051 IO Ports
IO Program: 05
4.1 8051 IO Ports
IO Program: 06
4.1 8051 IO Ports
IO Program: 07
4.2 8051 Timers and Counters
Mohammad Ali Mazidi, Janice G. Mazidi, Rolin D. McKinlay, The 8051 Microcontroller and
Embedded Systems, 2014, 2nd Edition, Pearson, India.
4.2 8051 Timers and Counters
1. Timers
• The 8051 has two timers/counters, they can be used either as
• Timers to generate a time delay
• To generate a waveform with specific frequency
• To generate baud rate signal for serial communication
• Event counters to count events happening outside the
microcontroller
• Both Timer 0 and Timer 1 are 16 bits wide
• Since 8051 has an 8-bit architecture,
each 16-bits timer is accessed as two separate registers of low
byte and high byte
4.2 8051 Timers and Counters
1. Timers
• Register related to work with 8051 timers are:
• TH & TL Timer/counter register– To hold the value for generating
time delay
• TMOD Register - to define mode of timer operation
• TCON Register – To control the timer operation
4.2 8051 Timers and Counters
Problems: EXAMPLE-1:
In the following program, we create a square wave of 50% duty
cycle (with equal portions high and low) on the P1.5bit. Timer 0
is used to generate the time delay. Analyze the program.
Assume XTAL = 11.0592 MHz
Problems: EXAMPLE-1:
DELAY: SETB TR0 ;start the timer 0
AGAIN: JNB TF0,AGAIN ;monitor timer flag 0
CLR TR0 ;stop timer 0
CLR TF0 ;clear timer 0 flag
RET
• The number of counts for the roll over is FFFFH – FFF2H = 0DH (13
decimal).
• However, we add one to 13 because of the extra clock needed when it
rolls over from FFFF to 0 and raise the TF flag. This gives 14 ×1.085us =
15.19us for half the pulse.
• For the entire period it is T = 2 ×15.19us = 30.38us as the time delay
generated by the timer.
4.2 8051 Timers and Counters
Problems: EXAMPLE-2:
Assume that XTAL = 11.0592 MHz. What value do we need to load the
timer’s register if we want to have a time delay of 5 ms
(milliseconds)? Show the program for timer 0 to create a pulse width
of 5 ms on P2.7
Since XTAL = 11.0592 MHz, the counter counts up every 1.085 us.
This means that out of many 1.085 us intervals we must make a 5 ms
pulse.
To get that, we divide one by the other. We need 5 ms / 1.085 us = 4608
clocks.
To Achieve that we need to load into TL and TH the value 65536 – 4608 =
EE00H.
Therefore, we have TH = EE and TL = 00.
4.2 8051 Timers and Counters
Problems: EXAMPLE-3
Calculate TL and TH to get the largest time delay possible. Find
the delay in ms. In your calculation, exclude the overhead due to
the instructions in the loop
Solution:
• To get the largest delay we make TL and TH both 0. This will count
up from 0000 to FFFFH and then roll over to zero
• Making TH and TL both zero means that the timer will count from
0000 to FFFF, and then roll over to raise the TF flag.
• As a result, it goes through a total Of 65536 states. Therefore, we
have delay =(65536 - 0) ×1.085 us = 71.1065ms.
4.2 8051 Timers and Counters
Problems: EXAMPLE-4
Assume that XTAL = 11.0592 MHz, write a program to generate a
square wave of 2 kHz frequency on pin P1.7
Solution:
T = 1 / f = 1 / 2 kHz = 500 us the period of square wave.
(b) 1 / 2 of it for the high and low portion of the pulse is 250 us.
(c) 250 us / 1.085 us = 230 and 65536 – 230 = 65306 which in hex
FF1AH.
(d) TL = 1A and TH = FF, all in hex. The program is as follows:
4.2 8051 Timers and Counters
Problems: EXAMPLE-5
Generate a square wave with TON of 3ms and T0FF of 10ms on all
pins of port 0. Assume an XTAL of 22MHz.
Problems: EXAMPLE-5
MOV TMOD, #01H
BACK: MOV TL0, #75H ORG 300H
MOV TH0, #0B8H DELAY: SETB TR0
MOV P0, #00H AGAIN: JNB TF0, AGAIN
ACALL DELAY CLR TR0
MOV TL0, #8AH CLR TF0
MOV TH0, #0EAH RET
MOV P0, #0FFH END
ACALL DELAY
SJMP BACK
4.2 8051 Timers and Counters
Problems: EXAMPLE-7
Assuming XTAL = 22MHz write a program to generate a square
pulse of frequency 100kHz on port pin P1.2.
3.1 Counters
• Timers can also be used as counters counting events happening
outside the 8051
• When it is used as a counter, it is a pulse outside of the 8051 that
increments the TH, TL registers
• TMOD and TH, TL registers are the same as for the timer
discussed previously
• Programming the timer in the last section also applies to
programming it as a counter
• Except the source of the frequency
4.2 8051 Timers and Counters
3.1 Counters
• The C/T bit in the TMOD registers decides the source of the clock
for the timer
• When C/T = 1, the timer is used as a counter and gets its pulses
from outside the 8051
• The counter counts up as pulses are fed from pins 14 and 15, these
pins are called T0 (timer 0 input) and T1 (timer 1 input)
4.2 8051 Timers and Counters
3.1 Counters
• Mode 1:
EXAPMLE-1: Assuming that clock pulses are fed into pin T1, write a
program for counter 1 in mode 2 to count the pulses and display the
state of the TL1 count on P2, which connects to 8 LEDs.
EXAPMLE-2: Write an 8051 assembly language program to implement
4.2 8051 Timers and Counters
a counter for counting pulses of an input signals. Assume the crystal
frequency as 22 MHz. Configure TIMER 1 to generate a clock pulse for
every one seconds at P3.5 and TIMER 0 as a counter which receives input
pulses at P3.4 from P3.5 Display final count values in port P1 (TL0) &
P2(TH0).
EXAPMLE-2:4.2Write an 8051 assembly language program to implement a counter
8051 Timers and Counters
for counting pulses of an input signals. Assume the crystal frequency as 22 MHz.
Configure TIMER 1 to generate a clock pulse for every one seconds at P3.5 and TIMER
0 as a counter which receives input pulses at P3.4 from P3.5 Display final count values
in port P1 (TL0) & P2(TH0).
ORG 000H BACK: JNB TF1, BACK
REPEAT: MOV TMOD, #15H CLR TF1
SETB P3.4 CLR TR1
MOV TL0, #00 DJNZ R0, AGAIN
MOV TH0, #00
SETB TR0 MOV A, TL0
MOV P1,A
MOV R0,#28 MOV A, TH0
AGAIN: MOV TL1,#00 MOV P2,A
MOV TH1, #00 SJMP REPEAT
SETB TR1 END
4.3 8051 Serial Communication and Interrupts
Mohammad Ali Mazidi, Janice G. Mazidi, Rolin D. McKinlay, The 8051 Microcontroller and
Embedded Systems, 2014, 2nd Edition, Pearson, India.
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
• Computers transfer data in two ways:
• Serial
To transfer to a device located many
meters away, the serial method is used,
The data is sent one bit at a time
• Parallel
Often 8 or more lines (wire conductors)
are used to transfer data to a device
that is only a few feet away
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
Types of Data transmission
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
Serial data communication uses two methods
• Synchronous method transfers a block of data at a time
• Asynchronous method transfers a single byte at a time
1. Serial Communication
• Protocol is a set of rules agreed by both the sender and receiver on
• How the data is packed
• How many bits constitute a character
• When the data begins and end
• Asynchronous serial data communication is widely used for
character-oriented transmissions - Each character is placed in b/w
start and stop bits, is called framing
• Block-oriented data transfers use the synchronous method
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
• The start bit is always one bit,
but the stop bit can be one or two bits
• The start bit is always a 0 (low) &
the stop bit(s) is 1 (high)
• Due to the extended ASCII characters, 8-bit ASCII data is common
• In older systems, ASCII characters were 7-bit
• In modern PCs the use of one stop bit is standard
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
• The rate of data transfer in serial data communication is stated in
bps(bits per second)
• Another widely used terminology for bps is baud rate
• It is modem terminology and
is defined as the number of signal changes per second
• The baud rate and bps are the same, and we use the terms inter
changeably
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
• The data transfer rate of given computer system depends on
communication ports incorporated into that system
• An interfacing standard RS232 was set by the Electronics Industries
Association (EIA) in 1960
• In RS232, a 1 is represented by -3 ~ -25 V, while a 0 bit is +3 ~ +25
V, making -3 to +3 undefined
• The standard was set long before the advent of the TTL logic family,
its input and output voltage levels are not TTL compatible
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
• Current terminology classifies data communication equipment as
1. Serial Communication
• The simplest connection between a PC and microcontroller requires
a minimum of three pins, TxD, RxD, and ground
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
In data transmission, serial communication is the process of
sending data one bit at a time, sequentially, over
a communication channel or computer bus.
To reduce the number of pins in a package, many ICs use a
serial bus to transfer data when speed is not important.
Some examples of such low-cost serial buses
include SPI, I²C, DC-BUS, UNI/O, and 1-Wire.
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
8051 has two pins that are used specifically for transferring and
receiving data serially
These two pins are called TxD and RxD and are part of the
port 3 group (P3.0 and P3.1)
These pins are TTL compatible; therefore, they require a line
driver to make them RS232 compatible
We need a line driver (voltage converter) to convert the R232’s
signals to TTL voltage levels that will be acceptable to 8051’s
TxD and RxD pins
4.3 8051 Serial Communication and Interrupts
1. Serial Communication
• A line driver such as the MAX232 chip is required to convert RS232
voltage levels to TTL levels, and vice versa
4.3 8051 Serial Communication and Interrupts
This register contain not only the mode selection bits but also the
9th data bit for transmit and receive (TB8 and RB8) and the serial
port interrupt bits (TI and RI).
4.3 8051 Serial Communication and Interrupts
4. Interrupt
A single microcontroller can serve several devices by two ways: (i)
Interrupt (ii). Polling
Interrupts: Whenever any device needs its service, the device
notifies the microcontroller by sending it an interrupt signal
Upon receiving an interrupt signal, the microcontroller
interrupts whatever it is doing and serves the device
The program which is associated with the interrupt is called
the interrupt service routine (ISR) or interrupt handler
4.3 8051 Serial Communication and Interrupts
4. Interrupt
Polling can monitor the status of several devices and serve each
of them as certain conditions are met
The polling method is not efficient, since it wastes much of
the microcontroller’s time by polling devices that do not need
service
ex. JNB TF,target
• The advantage of interrupts is that the microcontroller can serve
many devices (not all at the same time)
4.3 8051 Serial Communication and Interrupts
4.3 External
Harware
Interrupts
4.3 8051 Serial Communication and Interrupts
//Delay of 500ms
DELAY: MOV R2,#04H ;LOAD R2 WITH 07 HEX
HERE3: MOV R1,#0FFH ;LOAD R1 WITH 0FF HEX
HERE2: MOV R0,#0FFH ;LOAD R2 WITH 0FF HEX
HERE1: DJNZ R0,HERE1 ;DECREMENT R0
DJNZ R1,HERE2 ;DECREMENT R1
DJNZ R2,HERE3 ;DECREMENT R2
RET ;RETURN
END
4.3 8051 Serial Communication and Interrupts
Recap of TCON
4.3 8051 Serial Communication and Interrupts
Recap of TCON
4.3 8051 Serial Communication and Interrupts
4.3 External
Harware
Interrupts:
b. Edge
triggered
mode:
4.3 8051 Serial Communication and Interrupts
4.3 8051 Serial Communication and Interrupts
4.3 8051 Serial Communication and Interrupts
4.3 8051 Serial Communication and Interrupts
Problem
Discuss what happens if interrupts INT0, TF0, and INT1 are
activated at the same time. Assume priority levels were set by
the power-up reset and the external hardware interrupts are
edge-triggered.
• Solution:
If these three interrupts are activated at the same time, they
are latched and kept internally.
Then the 8051 checks all five interrupts according to the
sequence listed above Table.
If any is activated, it services it in sequence.
Therefore, when the above three interrupts are activated, IE0
(external interrupt 0) is serviced first, then timer 0 (TF0), and
finally IE1 (external interrupt 1)
4.3 8051 Serial Communication and Interrupts
When INT0, INT1, and TF0 interrupts are activated at the same
time, the 8051 services INT1 first, then it services INT0, then
TF0.
5.1 8051 Input/Output interfacing – LCD and LED
Mohammad Ali Mazidi, Janice G. Mazidi, Rolin D. McKinlay, The 8051 Microcontroller and
Embedded Systems, 2014, 2nd Edition, Pearson, India.
5.1 8051 Input/Output interfacing – LCD and LED
2. Send 0FH for making LCD ON, cursor ON and cursor blinking ON.
4. Send 01H for clearing the display and return the cursor.
5.1 8051 Input/Output interfacing – LCD and LED
ORG 300H
MYCOM: DB 38H, 0EH, 01, 06, 84H, 0 ; commands and null
MYDATA: DB “HELLO”, 0 ; Data is stored
END