SlideShare a Scribd company logo
1
8051 MCU
AN INTRODUCTION
Arun Umrao
https://sites.google.com/view/arunumrao
DRAFT COPY - GPL LICENSING
2
Contents
1 Introduction 5
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1.1 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.2 DPTR . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.1.3 Special Function Register (SFR) . . . . . . . . . . . . . 9
1.1.4 Input/Output Ports . . . . . . . . . . . . . . . . . . . 11
1.1.5 bit addressing . . . . . . . . . . . . . . . . . . . . . . . 13
1.1.6 Status Flags . . . . . . . . . . . . . . . . . . . . . . . . 14
1.1.7 Addressing . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.2 RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.2.1 Register Bank . . . . . . . . . . . . . . . . . . . . . . . 29
1.2.2 Bit Addressable RAM . . . . . . . . . . . . . . . . . . 31
1.2.3 Scratch Pad . . . . . . . . . . . . . . . . . . . . . . . . 34
1.2.4 Stack Pointer . . . . . . . . . . . . . . . . . . . . . . . 35
1.2.5 Program Counter . . . . . . . . . . . . . . . . . . . . . 35
1.3 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1.3.1 Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2 Instructions 39
2.1 Instruction Sets . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.1.1 ACALL . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.1.2 ADD . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
2.1.3 ADDC . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.1.4 AJMP . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
2.1.5 ANL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
2.1.6 CJNE . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
2.1.7 CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
2.1.8 CPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
2.1.9 DA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
2.1.10 DEC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.1.11 DIV . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
2.1.12 DJNZ . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
2.1.13 INC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
2.1.14 JB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
2.1.15 JBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3
2.1.16 JC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
2.1.17 JMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.1.18 JNB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
2.1.19 JNC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
2.1.20 JNZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.1.21 JZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
2.1.22 LCALL . . . . . . . . . . . . . . . . . . . . . . . . . . 56
2.1.23 LJMP . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.1.24 MOV . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
2.1.25 MOVC . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.1.26 MOVX . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
2.1.27 MUL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
2.1.28 NOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
2.1.29 ORG . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.1.30 ORL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
2.1.31 POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
2.1.32 PUSH . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
2.1.33 RET . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
2.1.34 RETI . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
2.1.35 RL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
2.1.36 RLC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
2.1.37 RR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.1.38 RRC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
2.1.39 SETB . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
2.1.40 SJMP . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.1.41 SUBB . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
2.1.42 SWAP . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
2.1.43 XCH . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
2.1.44 XCHD . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
2.1.45 XRL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
3 µC Structure 85
3.1 Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
3.1.1 TMOD . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
3.1.2 TCON . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
3.2 Interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
3.2.1 External Interrupt . . . . . . . . . . . . . . . . . . . . 96
3.2.2 Interrupt Priority Register . . . . . . . . . . . . . . . . 97
4 Introduction
3.2.3 Interrupt Vector Table . . . . . . . . . . . . . . . . . . 98
3.3 Communication . . . . . . . . . . . . . . . . . . . . . . . . . . 100
3.3.1 Baud Rate . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.3.2 SBUF . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
3.3.3 SCON . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
3.3.4 PCON . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
3.4 Assembly Directives . . . . . . . . . . . . . . . . . . . . . . . . 108
3.4.1 Symbol Definition . . . . . . . . . . . . . . . . . . . . . 108
3.4.2 Data Definition . . . . . . . . . . . . . . . . . . . . . . 108
3.5 Assembly in C . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
3.5.1 Byte Register . . . . . . . . . . . . . . . . . . . . . . . 117
3.5.2 Bit Register . . . . . . . . . . . . . . . . . . . . . . . . 117
3.6 Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
3.6.1 Hex File . . . . . . . . . . . . . . . . . . . . . . . . . . 122
3.7 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
4 Numerical Problems 123
1.1. INTRODUCTION 5
1Introduction
1.1 Introduction
During the compilation and execution of codes, we use MCU 8051 IDE for the
purpose. Codes given below are never ported into real devices. As we know
that in 8051 MCU, data can be provided in decimal form or in hex form. If
a number is not suffixed with ‘H’ then it is taken as decimal value. In MCU
processor, it is first converts into the hex value and then it is executed. For
example, port P0 in MCU uses memory address 80H. If an instruction has
to access this port, it must identify it by its memory address, that is 0×80
(80H). If instruction is followed by 80 only, then it shall be treated as decimal
80 or 0×50 (50H) and instruction will access memory location other than
port P0.
✞
1 MOV P0 ,#0 FFH ; Set port P0 to input port
CPL 80 ;80th register of RAM
3 ;in hex it is 50H which is not
;address of port P0
5 END
✌
✆
Above example does not complement to the value of port P0 and it comple-
ments to 80th
register, i.e. 80 is in decimal value and it is not the address of
port P0. Again if above program is modified as given below
✞
1 MOV P0 ,#0 FFH ; Set port P0 to input port
CPL 80H ;80H is 128 in decimal. i.e.
3 ;128th register of RAM. It is
;address of port P0
5 END
✌
✆
then it complements to the value at port P0. As 80H represents to 128th
register of RAM that is address of port P0. So be careful while you are using
address as count form (decimal) or in hex form. User’s input data should
6 Introduction
be started with a numeric digit and prefixed with # symbol. Numeric input
data should be started with a numeric digit. See example below:
✞
1 MOV P0 ,#0 FFH ; Valid
MOV P0 ,# FFH ; Invalid
3 MOV P0 ,#80H ; Valid
END
✌
✆
Second line in above codes has invalid input. The input is consider as group
of characters rather than numerical input value. If an input is not prefixed
with # symbol then it is considered as RAM memory address or port address.
20 21
19 22
18 23
17 24
16 25
15 26
14 27
13 28
12 29
11 30
10 31
9 32
8 33
7 34
6 35
5 36
4 37
3 38
2 39
1 40
P1.0
P1.1
P1.2
P1.3
P1.4
P1.5
P1.6
P1.7
RST
P0.0
P0.1
P0.2
P0.3
P0.4
P0.5
P0.6
P0.7
Vcc
P3.0
P3.1
P3.2
P3.3
P3.4
P3.5
P3.6
P3.7
XTAL2
XTAL1
GND P2.0
P2.1
P2.2
P2.3
P2.4
P2.5
P2.6
P2.7
EA
ALE
PSEN
Figure 1.1: 8051 pin diagram.
EA is acronym of External Access Enable. This pin is active when it
is low and it is used to access external memory locations 0 to 4K. This pin
must be connected to VCC if MCU wants to access internal ROM and should
connect to GND to access external ROM. 8051 micro controller has two 16
bit registers (PC & DPTR). PSEN is acronym of Program Store Enable
which controls the read signal for external program memory (active when
it is low). ALE is acronym of Address Latch Enable, to latch address
outputs at Ports P0 and P2. XTAL1 and XTAL2 pins are used for crystal
inputs for internal oscillator.
1.1. INTRODUCTION 7
1.1.1 Registers
Registers are used to store information temporarily. 8051 micro controller
unit (MCU) has 8 numbers of 8 bit registers. These eight registers are named
from R0 to R7 to process data. These are default registers. The registers are
used to access the stack pointer registers also. A register can be used as a
pointer to the data. For this purpose only register R0 and R1 are used. Other
registers can not be used to hold the address of operand located in RAM. To
hold the address of RAM location, the registers R0 and R1 must be prefixed
with symbol ‘@’. Special Function Register (SFR) can be accessed by their
names or by their address. Address of register A is 0E0H and of register B is
0F0H. The Carry flag is denoted by ‘C’ in 8051. 8051 MCU as one primary
accumulator and one secondary accumulator. They are represented by A and
B respectively. In 8051 MCU there are two other register banks named as
Bank 0 and Bank 1. Each bank has eight resisters named from R0 to R7.
Bank 0 or Bank 1 can be selected by using instructions like given below:
✞
SET B RS0
2 ; OR
SET B RS1
✌
✆
Application of register 1 and register 2 as RAM address is shown in the
following codes.
✞
1 MOV R1 ,#20H ;Value at register R1 is 0x20
MOV @R1 ,#30H;Use register 1 (R1) value as
3 ;RAM Address , i.e. 0x20
✌
✆
x0 x1 x2 x3 x4 x5 x6 x7
00 00 20 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
20 30 00 01 00 00 00 00 00
28 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 1.1: Memory result.
8 Introduction
1.1.2 DPTR
Data Pointers i.e. DPTR is used either to store or to access 16bit data
from the internal RAM/ROM memory. While using the DPTR, source and
destination registers must have equal size. For example:
✞
1 ;Copy data in DPTR by direct addressing.
MOV DPTR ,#1012H
3 ;Copy low byte into R0 register
MOV R0,DPL
5 ;Copy high byte into R1 register
MOV R1,DPH
7 ;End the program execution
END
✌
✆
The data stored in DPTR is two bytes long. Its Low byte is stored in DPL
register and High byte is stored in DPH register. DPTR is a Special Function
Register (SFR). In instruction
✞
MOV DPTR ,#1012H
✌
✆
Low byte is 12H and high byte is 10H. Therefore DPL register has value 12H
and DPH register has 10H. DPTR acts an intermediate buffer where data
is stored temporarily before it moves from memory to register or register to
memory. See another example:
✞
1 ORG 0
MOV DPTR ,# MYDATA
3 MOV R1 ,#80H ;Start the point of data copying
B1:
5 CLR A ;Clear accumulator
MOVC A,@A+DPTR ;Get ROM data to Accumulator
7 MOV @R1 ,A ;Copy accumulator data to R1
MOV P0,A ;Copy accumulator data to P0
9 JZ EXIT
INC DPTR ;Increase data pointer
11 INC R1 ;Increase ROM location
SJMP B1
13 EXIT:
SJMP $ ;Stay here
15 ORG 30H ;New location of RAM
MYDATA: ;ROM data
1.1. INTRODUCTION 9
17 DB ’This is my word’ ,0
END
✌
✆
1.1.3 Special Function Register (SFR)
Special Function Register (SFR) are used by MCU internally to perform op-
erations. The SFR registers are P0, SP, DPL, DPH, PCON, TCON, TMOD,
TL0, TL1, P1, SCON, SBUG, P2, IE, P3, IP, PSW, ACC and B. In which
only ACC, B, PSW, IP, IE, SCON, TCON and input/output ports are bit
addressable. The bit indices, in hexa-decimal numbers, for the location of
each bit of SFR is given in the following table. These indices are used with
bit addressing instructions only.
D7 D6 D5 D4 D3 D2 D1 D0
P0 87 86 85 84 83 82 81 80
TCON 8F 8E 8D 8C 8B 8A 89 88
P1 97 96 95 94 93 92 91 90
SCON 9F 9E 9D 9C 9B 9A 99 98
P2 A7 A6 A5 A4 A3 A2 A1 A0
IE AF AE AD AC AB AA A9 A8
P3 B7 B6 B5 B4 B3 B2 B1 B0
IP BC BB BA B9 B8
PSW D7 D6 D5 D4 D3 D2 D1 D0
ACC E7 E6 E5 E4 E3 E2 E1 E0
B F7 F6 F5 F4 F3 F2 F1 F0
Table 1.2: Special function registers and indices of their corresponding bit in
hexa-decimal form.
Solved Problem 1.1 Load 24H in the accumulator of 8051MCU and access
its D3 bit as bit addressable mode.
10 Introduction
Solution Accumulator is a bit addressable register and its bit can be
accessed independently. According to this problem, we first load a value 24H
into the accumulator. Then we complement the fourth bit D3 bit of the
accumulator value by using CPL instruction. See the program given below:
✞
MOV A ,#24H ; A=24H = 0010 0100
2 CPL A.3 ; A=2CH = 0010 1100
END ; End the program
✌
✆
Solved Problem 1.2 Write a 8051MCU assembly program for continuous
toggling of D5 port pin of port P0 by accessing address location of port P0.
Solution The address location of port P0 ranges from RAM address 0×80
to 0×87 RAM location and they represent to the corresponding bit from D0
to D7 of the port P0. To toggle the D5 pin we have to complement the value
at 0×85 RAM Location.
✞
1 ORG 0000H
HERE:
3 CPL 85H ; Complement D_5 pin of port P0
CPL 85H ; Complement D_5 pin of port P0
5 JMP HERE; Continuous loop
END ; End the program
✌
✆
Solved Problem 1.3 Write a 8051MCU assembly program for continuous
toggling of D6 bit of accumulator by accessing address location method.
Solution The address location of accumulator ranges from RAM address
0×E0 to RAM address 0×E7 and they represent to the corresponding bits,
from D0 to D7 of the accumulator. To toggle the D6 bit of accumulator we
have to complement the value at 0×E6 RAM Location.
✞
ORG 00H
2 HERE:
CPL 0E6H ; Complement D_6 of accumulator
4 CPL 0E6H ; Complement D_6 of accumulator
JMP HERE ; Continuous loop
6 END ; End the program
✌
✆
1.1. INTRODUCTION 11
1.1.4 Input/Output Ports
8051 has four 8-bit long I/O ports named from P0 to P3 and uses 8 pins.
When all or one of the 8 bit are in 0 state, then port or corresponding port
bit acts as output port. Similarly, when all or one of the 8 bit are in state 1,
then the port or port bit acts as input port. By default all ports are input
ports. Before we may start using port, it must be reconfigured. Each bit of
an Port is numbered from ‘Pn.0’ to ‘Pn.7’. Each bit pin of a port (say P0 for
example) can be accessed by its name or addresses, as pins have designated
addresses from 0×80 to 0×87. Port pin bit of port P1 can be accessed by
using either their name or addresses, as pins have designated addresses from
0×90 to 0×97. Similarly port pin bit of port P2 and P3 can be accessed by
using either their name or address as pins have designated addresses from
0×A0 to 0×A7 and from 0×B0 to 0×B7 respectively. Port P0 must be
connected with 10K ohm pull-up register as it is like open drain. Only port
P0 required pull up registers. Port P2 is used with port P0 as upper byte
and provides sixteen bit port address.
✞
MOV A ,#0FFH ;Set A=FF hex
2 MOV P0 ,A ;Reset all bit of port P0 to 1
;Hence it is made to input port.
4 END ;End of program
✌
✆
A port pins can be addressed either by its name or by its address. In the
following example, port pins are accessed by using their addresses.
✞
MOV P0 ,#0 FFH ; set port to FFH
2 CPL 80H ; Compliment the first LSB to 0
MOV P1 ,#0 FFH ; set port to FFH
4 CPL 90H ; Compliment the first LSB to 0
MOV P2 ,#0 FFH ; set port to FFH
6 CPL 0A0H ; Compliment the first LSB to 0
MOV P3 ,#0 FFH ; set port to FFH
8 CPL 0B0H ; Compliment the first LSB to 0
END
✌
✆
12 Introduction
Port Hex Value D7 D6 D5 D4 D3 D2 D1 D0
P0 FE 1 1 1 1 1 1 1 0
P1 FE 1 1 1 1 1 1 1 0
P2 FE 1 1 1 1 1 1 1 0
P3 FE 1 1 1 1 1 1 1 0
87H
P0.7
86H
P0.6
85H
P0.5
84H
P0.4
83H
P0.3
82H
P0.2
81H
P0.1
80H
P0.0
97H
P1.7
96H
P1.6
95H
P1.5
94H
P1.4
93H
P1.3
92H
P1.2
91H
P1.1
90H
P1.0
0A7H
P2.7
0A6H
P2.6
0A5H
P2.5
0A4H
P2.4
0A3H
P2.3
0A2H
P2.2
0A1H
P2.1
0A0H
P2.0
0B7H
P3.7
0B6H
P3.6
0B5H
P3.5
0B4H
P3.4
0B3H
P3.3
0B2H
P3.2
0B1H
P3.1
0B0H
P3.0
Figure 1.2: Port pin addresses of ports P0, P1, P2 and P3.
Similarly, in the following example, port pins are accessed by using their port
name. For example, ‘P1.0’ for first pin of port P1 from LSB side, ‘P3.3’ for
D3 pin of port P3 from LSB side etc.
✞
1 MOV P0 ,#0 FFH ; set port to FFH
CPL P0.0 ; Compliment the first LSB to 0
3 MOV P1 ,#0 FFH ; set port to FFH
CPL P1.0 ; Compliment the first LSB to 0
5 MOV P2 ,#0 FFH ; set port to FFH
CPL P2.0 ; Compliment the first LSB to 0
7 MOV P3 ,#0 FFH ; set port to FFH
CPL P3.0 ; Compliment the first LSB to 0
9 END
✌
✆
1.1. INTRODUCTION 13
Port Hex Value D7 D6 D5 D4 D3 D2 D1 D0
P0 FE 1 1 1 1 1 1 1 0
P1 FE 1 1 1 1 1 1 1 0
P2 FE 1 1 1 1 1 1 1 0
P3 FE 1 1 1 1 1 1 1 0
1.1.5 bit addressing
We can access the entire 8-bits of a port or a specific bit of any port without
any alteration in other bits. To access a particular bit of a port, code syntax
is used like :
✞
1 SETB X.Y
✌
✆
Here ‘X’ is port name and ‘Y’ is specific bit number ranging from 0 to 7. For
example, first and second bit of port P1 are accessed by
✞
1 BACK:
CPL P1.0
3 CPL P1.1
SJMP BACk
5 END
✌
✆
Addresses of bit of ports P0 to P3 are given below:
1. Addresses of port bit of port P0 ranges from 80H to 87H
2. Addresses of port bit of port P1 ranges 90H to 97H
3. Addresses of port bit of port P2 ranges A0H to A7H
4. Addresses of port bit of port P3 ranges B0H to B7H
Registers A, B, Process State Word (PSW), IP, IE, ACC, SCON and TCON
are bit addressable.
D7 D6 D5 D4 D3 D2 D1 D0
CY AC RS1 RS0 OV P
14 Introduction
For example to check the Overflow bit, we use instruction as
✞
1 JB PSW.2 <target >
✌
✆
1.1.6 Status Flags
The ALU of 8051 includes five flip-flops which are set or reset after an oper-
ation according to data conditions of the result in the accumulator and other
registers. They are called Carry (CY), Auxiliary Carry (AC), RS1, RS0,
Over Flow (OV) and Parity (P) flags. All these flags are arranged as shown
below and they form a 8-bits long word (i.e. one byte long) that is called as
Process State Word (PSW).
D7 D6 D5 D4 D3 D2 D1 D0
CY AC RS1 RS0 OV P
Carry Flag The carry flag is commonly used to indicate whether an
addition causes a ‘carry’ into the next higher order digit. The carry flag is
also used as a ‘borrow’ flag in subtractions. To set the carry flag, syntax is
used as
✞
1 SETB C
; OR
3 SETB CY
✌
✆
Or via bit addressing of PSW as
✞
1 SETB PSW.7
✌
✆
Auxiliary Carry Flag The auxiliary carry flag indicates a carry out of
third bit of the accumulator. It is present to enable the Decimal Adjust
Accumulator (DAA) to perform its functions. The AC flag can be accessed
by bit addressing of PSW like
✞
1 SETB PSW.6
✌
✆
Register Bank 1 Select Control Bit (RS1) When RS1 is set to high then
register bank 1 is selected. In this bank there are eight registers those can
1.1. INTRODUCTION 15
be accessed by using register’s name, R0 to R7. Both resister banks of the
8051 MCU can not be used simultaneously, hence before selecting of resister
bank 1, resister bank 0 must be deselected. The RS1 bit can be addressed
via bit addressing of PSW like
✞
1 SETB PSW.4
✌
✆
Register Bank 0 Select Control Bit (RS0) When RS0 is set to high then
register bank 0 is selected. In each bank there are eight registers can be
accessed by register name, R0 to R7. Both resister banks of the 8051 MCU
can not be used simultaneously, hence before selecting of resister bank 0,
resister bank 1 must be deselected. The RS0 bit can be addressed via bit
addressing of PSW like
✞
1 SETB PSW.3
✌
✆
Overflow Flag In additions, the Overflow (OV) bit is set if there is a
carry out-of D6 or out-of D7, but not both. The OV flag can be accessed by
bit addressing of PSW like
✞
1 SETB PSW.2
✌
✆
✞
1 MOV A ,#+96 ;96D = 0110 0000
MOV R0 ,#+70 ;70D = 0100 0110
3 ADD A,R0 ;166D= 1010 0110
END ; End of program
✌
✆
In above sum, sum of D6 bit of both numbers sends a carry to D7 bits. But
on the sum of D7 bit of the numbers there is no further carry. Hence overflow
flag is set to high.
Parity Flag Parity is determined by counting the number of one bit set
in the result in the accumulator. Instructions that affect the parity flag set
the flag to one for even parity and reset the flag to zero to indicate odd parity.
The parity flag can be accessed by bit addressing of PSW like
✞
SETB PSW.0
✌
✆
In the following table, some mathematical mnenomics are listed which uses
the PSW flags.
16 Introduction
Operator C AC OV P
ADD Y Y
ADDC Y Y
DA Y Y
DIV Y Y
MUL Y Y
RLC
RRC
SETB
SUBB
1.1.7 Addressing
In 8051 MCU data is copied or moved from register to accumulator or vice-
versa. Data transfer between registers is not acceptable and cause errors.
During data transfter, size of the source register and the destination register
should be same. Before going through the addressing, remember that the
values which are not ends with ‘H’ are taken as decimal number by truncating
trailing characters other than numeric digits. For example
✞
1 MOV A ,#10 ;Copy decimal 10 (hex A) to accumulator
MOV A,#1A ;"Invalid numeric base ‘A ’" if used
3 ; directly. If used via EQU directive
;Copy decimal 1 (hex 1) to accumulator
5 ;and truncate trailing character ’A’
MOV A,#A1 ;A1 is considered as label rather than value
7 ;There shall be error of "symbol not defined"
✌
✆
See the example below:
✞
1 AGE EQU 1A ;EQU directive for AGE
MOV P0 ,# AGE ;AGE is used by immediate addressing
3 MOV P1 ,#15 ;Copy decimal 15 (hex F) to port P1
MOV P2 ,#15H ;Copy hexa -decimal 15 to port P2
1.1. INTRODUCTION 17
5 END
✌
✆
After execution of the above code, the port values will be as given below:
Port Hex Value D7 D6 D5 D4 D3 D2 D1 D0
P0 01 0 0 0 0 0 0 0 1
P1 0F 0 0 0 0 1 1 1 1
P2 15 0 0 0 1 0 1 0 1
P3 00 0 0 0 0 0 0 0 0
With above examples it is clear that a data value is either copied from
the port or resister, or it moved to port or register.
Direct Addressing
In direct addressing mode data is transferred from register to accumulator or
vice-versa by identifying the source and destination as indicated in operands.
In direct addressing, source or destination is identified by its name.
✞
1 MOV A ,04H
✌
✆
Here 04H represents to the register R4. Pushing and popping the stack is
allowed only in direct addressing mode. PUSH A is not valid while PUSH
0E0H is valid. Here 0E0H is address of the accumulator. Looping is not
allowed in direct addressing mode. The RAM locations 30H to 7FH are only
accessible by direct addressing mode only. The direct addressing can access
to on-chip memory location.
Indirect Addressing Mode
Registers R0 and R1 are operated as pointer registers. Indirect addressing
is represented by an ‘@’ symbol before R0 and R1. In indirect addressing,
source or destination is identified by its address rather than its name. For
example
✞
1 MOV A,@R0
✌
✆
18 Introduction
Register Direct Addressing
In register direct addressing mode, data is addressed by register name. In
register direct addressing mode, a register is either target for data pasting or
source for data copying. For example
✞
1 MOV A,R4
✌
✆
Here, register R4 of a register bank is addressed directly and its contents are
copied into the accumulator.
Register Indirect Addressing
This mode is similar to the direct address. In this type of addressing, contents
of the memory location registers are copied into the internal registers, i.e.
accumulator or secondary accumulator. The registers in register indirect
addressing mode, register is identified by its address rather than register
name. In This addressing mode, source or destination is the sum of the
base address and the accumulator (acts as index). The base address may be
DPTR or PC. Example with DPTR base:
✞
1 MOV DPTR , #20H
MOV A, #15H
3 MOVC A, @A+DPTR ;A <- M[35H]
✌
✆
Example with PC base:
✞
1 ORG 0100H
100: MOV A, #05H
3 102: MOVC A, @A+PC ; A <- M[108]
103: NOP ; PC Location
✌
✆
Index Addressing
Index addressing mode is widely used in accessing data elements of the look-
up table entries located in the program ROM. The syntax use is
✞
MOVC A,@A+DPTR
✌
✆
In the following example, index addressing is used to return the index values
starting from numeric 1 to 9. Last digit 0 is used to halt the process.
1.1. INTRODUCTION 19
✞
1 ORG 0
MOV DPTR ,#30H ;Fetch the data pointer from address
03H
3 ;It may be used as MOV DPTR ,# MYDATA
also
BACK:
5 CLR A ;Clear the accumulator
MOVC A,@A+DPTR;Get the data pointer value and copy
7 ;in accumulator
MOV P0 ,A ;Copy accumulator value to port P0
9 JZ EXIT ;Exit to loop when digit is 0
INC DPTR ;Increase data pointer value by 1
11 SJMP BACK ;Do it continue
EXIT:
13 SJMP $
ORG 30H ;Address of pointer pointed to data
15 MYDATA:
DB 1,2,3,4,5,6,7,8,9,0
17 END
✌
✆
Immediate Addressing
In immediate addressing mode, data is directly copied into a register or ac-
cumulator. Here source is data itself rather than register name or memory
address from where data is being copied. In immediate addressing data is
added to register or accumulator by using instruction MOV. The operand
data may be numeric constant, a symbolic variable and an arithmetic ex-
pression using constants, symbols and operators. Immediate operands are
preceded by symbol ‘#’. Assembler computes the value and substitutes the
immediate data into the instruction. Following is a way for immediate ad-
dressing of a hex value.
✞
1 MOV R0 ,#19H ; copy hex 19 in register R0 immediately.
✌
✆
The input operand data may be a decimal value for immediate addressing.
But it should be prefixed with symbol ‘#’ and does not suffixed with symbol
‘H’.
✞
1 MOV R0 ,#19 ; copy dec 19 in register R0 immediately.
✌
✆
20 Introduction
A signed number in decimal only can be immediately addressed if a sign (+
or –) is used between ‘#’ symbol and number.
✞
1 MOV R0 ,#+19 ; copy dec +19 in register R0 immediately.
MOV R1 ,# -19 ; copy dec -19 in register R1 immediately.
✌
✆
Remember that in immediate addressing, the value suffixed to symbol ‘#’
must be started with a number not with alphabet, as value with alphabet
is considered as symbol and there shall be compilation error. If input hex
values are like A1, AF or FF, then these values must be prefixed with number
0.
✞
MOV R0 ,# FFH ;Illegal operand and not acceptable
2 MOV R0 ,#0 FFH ;Legal operand and acceptable
✌
✆
For example
✞
MOV A ,#00H ;Store A with 0 value , all bit low
2 CLR C ;Reset carry flag to zero
SETB C ;Set carry flag to one
4 MOV P0,A ;Reset all bit of port P0 to 0
SETB P0.1 ;Set port bit P0.1 to 1
6 ;MOV A,# FFH ;Give error if not commented
MOV A,#0 FFH ;Accepted method , all bit of A are high
8 MOV P0,A ;Set all port bit of P0 to high
END ;End the program
✌
✆
An alpha-numeric symbol can also be used as input data. The symbol is put
in single quotes prefixed with # symbol.
✞
1 MOV A,#’Y’ ;Store A with symbol ’Y’
;’Y’ is equivalent to dec 89
3 MOV P0,A ;Set all port bit of P0 as
;binary value of dec 89
5 END
✌
✆
Address By Register (@)
Only R0 and R1 registers are used as a pointer to the data. Other registers
can not be used for this purpose. When R0 and R1 holds the address of
RAM they are preceded by ‘@’ symbol.
1.1. INTRODUCTION 21
✞
1 ;Set the address of Stack Pointer
MOV SP ,#31H
3 ;Store hex 0AH in R7
MOV R7 ,#0AH
5 ;Store hex 0BH in R6
MOV R6 ,#0BH
7 ;Store hex 0CH in R5
MOV R5 ,#0CH
9 ;Store hex 0DH in R4
MOV R4 ,#0DH
11 ;Push value of R7 at address location 32H
PUSH 7
13 ;Push value of R6 at address location 33H
PUSH 6
15 ;Push value of R5 at address location 34H
PUSH 5
17 ;Push value of R4 at address location 35H
PUSH 4
19 ;Copy RAM address location into R0 & R1
MOV R0 ,#33H
21 MOV R1 ,#34H
;Copy data of RAM address pointed by R0
23 ;into accumulator A
MOV A,@R0
25 ;Copy data of RAM address pointed by R1
;into secondary accumulator B
27 MOV B,@R1
;Halt the execution of program
29 END
✌
✆
Long Addressing
Long addressing is done by using LCALL and LJMP instructions. These
instructions includes 16 bit destination address. The address range for these
instructions is 0000H to FFFFH. The memory range in 8051 MCU is from
00H to FFH, this is why, this type of addressing is called long addressing.
22 Introduction
1.2 RAM
8051 MCU has 128 bytes of RAM and 4K bytes ROM. ROM is used for
program code. 8051 MCU also supports 64K bytes read only external code
memory. It can be accessed through PSEN. 8051 MCU also supported to
64K external data memory that can be read and write by RD and WR.
Code memory is selectable by EA(internal or external). External memory
space is access by using MOVX instruction. These two type of memory
spaces are not shared between code and data. RAM memory is divided into
three parts.
1. First 32 bytes are used as register bank.
2. Next 16 bytes are used for bit addressable read/write memory.
3. Rest of 80 bytes are used for read and write storage. This section is
called scratch pad.
Bytes Location From Location To Label
32 Bytes 00H 1FH Register bank
16 Bytes 20H 2FH Bit addressable
80 Bytes 30H 7FH Scratch pad
Table 1.3: Memory distribution.
Memory location of RAM from 0×30 to 0×7F are used as scratch pad and
also as registers. Addresses from 0×08 to 0×1F addresses are used for stack.
ROM which stores data is not bit addressable while RAM location 0×20 to
0×2F are bit and byte addressable. The instructions which are allowed to
access the RAM address for byte or bit addressing are SETB, CLR, CPL,
JB, JNB, JBC. For bit or byte addressing of RAM registers, the register
can be accessed by its location or by its address. Bit or byte addressing
by location, the location of address is identify by its counting position. For
example, the RAM address 0×20 is 32th
register of the RAM when counting
started from 0. Note that, in the computer counting (indexing) started from
0 instead of 1. In RAM each location is identified by hexadecimal values,
hence if location is identified as a decimal number then it first should be
1.2. RAM 23
converted into equivalent hexadecimal value and then RAM address should
be identified by using this hexa-decimal value. See the example below in
which register location and bit of register is used for bit addressing. Bit
addressable registers are located in RAM memory from 0×20 to 0×2F. This
is why, the first default bit addressable resister is 0×20.
✞
1 MOV 33 ,#200 ; Dec 30 or hex 0x21 address of RAM memory
MOV 21H ,#200; 0x21 address of RAM memory
3 CPL 33 ; D33 bit from 0x20 address of RAM memory
; Memory bit are counted from 0 so that
5 ; D33 bit is D1 bit of 5th byte.
CPL 33.1 ; D1 bit of 0x21 address of RAM memory
7 ; on applying of . (dot) 33 becomes
; RAM register address in decimal
9 ; (equivalent 0x21 in hex)
CPL 35.0 ; D0 bit of 0x23 address of RAM memory
11 ; on applying of . (dot) 35 becomes
; RAM register address in decimal
13 ; (equivalent 0x23 in hex)
END ; End of program
✌
✆
To kept out from this confusion, each decimal number should be converted
into equivalent hexadecimal numbers and then memory address should be
identify in the RAM register.
x0 x1 x2 x3 x4 x5 x6 x7
... 00 00 00 00 00 00 00 00
18 00 00 00 00 00 00 00 00
20 CA 00 01 02 00 00 00 00
28 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 1.4: Memory result.
In above example, we specify the bit of a register by using ‘.’ (dot). If
we do not use the ‘.’ (dot) as shown above then the address is consider as
bit number started from 0×20 to continue upto 0×2F in group of 8 bits. In
24 Introduction
the following example, fist we reset the value of RAM register identified by
its address 0×20.
✞
MOV 32 ,#00; 0x20 register of RAM memory
✌
✆
The above instruction is not compulsory as default bit addressable registers
are located in RAM from 0×20 to 0×2F. Original memory values at 0×20,
0×21 and 0×22 RAM registers are all zeros. Note that, here counting of bit
of binary numbers is in right-to-left direction and byte-by-byte wise.
✞
1 ;+---------------+----------------+----------------+
;| 0x20 | 0x21 | 0x22 |
3 ;+---------------+----------------+----------------+
;|0|0|0|0|0|0|0|0| 0|0|0|0|0|0|0|0| 0|0|0|0|0|0|0|0|
5 ;+---------------+----------------+----------------+
✌
✆
On complementing the D0 bit of 0×20 RAM register, the value of this register
is changed to 01H.
✞
1 CPL 0 ; D0 bit of 32(0x20) register of RAM memory
;+---------------+----------------+
3 ;| 0x20 | 0x21 |
;+---------------+----------------+
5 ;|0|0|0|0|0|0|0|1| 0|0|0|0|0|0|0|0|
;+---------------+----------------+
7 ;| 01D or 01H | 00D or 00H | SETB state
;+---------------+----------------+
✌
✆
✞
CPL 33.0 ; D0 bit of 0x21 register of RAM memory
2 ;+---------------+----------------+
;| 0x20 | 0x21 |
4 ;+---------------+----------------+
;|0|0|0|0|0|0|0|0| 0|0|0|0|0|0|0|1|
6 ;+---------------+----------------+
;| 00D or 00H | 01D or 01H | SETB state
8 ;+---------------+----------------+
✌
✆
1.2. RAM 25
x0 x1 x2 x3 x4 x5 x6 x7
... 00 00 00 00 00 00 00 00
18 00 00 00 00 00 00 00 00
20 01 00 01 00 00 00 00 00
28 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 1.5: Memory result.
Here, noted that, number succeeding to CPL instruction is the bit location
from the current address as CPL is bit addressable instruction. Therefore,
✞
CPL 16
✌
✆
meant that the complementing of 16th
bit (counted from 0) from the current
address. Now,
✞
1 CPL 1 ; D1 bit of 0x20 register of RAM memory
CPL 2 ; D2 bit of 0x20 register of RAM memory
✌
✆
complements the second and third bit from the current address. The memory
value of RAM register identified by RAM address 0×20 is
✞
;+---------------+----------------+
2 ;| 0x20 | 0x21 |
;+---------------+----------------+
4 ;|0|0|0|0|0|1|1|1| 0|0|0|0|0|0|0|0|
;+---------------+----------------+
6 ;| 07D or 07H | 00D or 00H | SETB state
;+---------------+----------------+
✌
✆
Similarly, other bit are complemented by using CPL instruction as shown
below:
✞
1 CPL 8 ; D0 bit of 33th (0 x21) register of RAM memory
;+---------------+----------------+
3 ;| 0x20 | 0x21 |
;+---------------+----------------+
26 Introduction
5 ;|0|0|0|0|0|1|1|1| 0|0|0|0|0|0|0|1|
;+---------------+----------------+
7 ;| 03D or 03H | 01D or 01H | SETB state
;+---------------+----------------+
9 CPL 16 ; D0 bit of 34th(0x22) register of RAM memory
;+---------------+----------------+----------------+
11 ;| 0x20 | 0x21 | 0x22 |
;+---------------+----------------+----------------+
13 ;|0|0|0|0|0|1|1|1| 0|0|0|0|0|0|0|1| 0|0|0|0|0|0|0|1|
;+---------------+----------------+----------------+
15 ;| 03D or 03H | 01D or 01H | 01D or 01H |
SETB state
;+---------------+----------------+----------------+
✌
✆
Full program codes are given below:
✞
MOV 33 ,#00;
2 CPL 0 ; D0 bit of first byte
CPL 1 ; D1 bit of first byte
4 CPL 2 ; D2 bit of first byte
CPL 8 ; D0 bit of second byte
6 CPL 16; D0 bit of third byte
✌
✆
The final values at RAM registers at memory addresses 0×20, 0×21 and
0×22 are shown in following memory map.
x0 x1 x2 x3 x4 x5 x6 x7
... 00 00 00 00 00 00 00 00
18 00 00 00 00 00 00 00 00
20 07 01 01 00 00 00 00 00
28 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 1.6: Memory result.
For bit or byte addressing of RAM register using addresses of registers is
possible by SETB instruction too. Byte or bit addressing of a RAM address
1.2. RAM 27
is allowed in group of two bytes. RAM addresses 0×20 and 0×21 (register
count numbers 32nd
and 33rd
) are taken as zero group, RAM addresses 0×22
and 0×23 (register count numbers 34th
and 35th
) are taken as first group and
so on. Counting of first register of a group started from 0 to 7 and second
register of the group started from 9 to F. Use of instruction
✞
SETB 14H ; 20 in decimal 2x8+4 = D4 bit
✌
✆
instructs that set the D4 bit of register at address 0 ×22 (register count
number 34th
) of first group. Similarly instruction
✞
1 SETB 1FH
✌
✆
instruct that set the D7 bit of register 0×23 (register count number 35th
)
of first group. Each character of operand of SETB instruction has unique
meaning. For example in instruction
✞
1 SETB 14H
✌
✆
14H has three characters, ‘1’, ‘4’ and ‘H’. Here, ‘1’ stands for first group of
RAM registers, i.e. RAM registers at the address of 0×22 and 0×23. ‘4’
represents to D4 bit of the register group. This bit is falls in the register
address 0×22. ‘H’ represents to hexa-decimal number. Now see the following
self explained example.
✞
1 ;+---------------+----------------+
;|0|0|0|0|0|0|0|0| 0|0|0|0|0|0|0|0| Original state
3 ;+---------------+----------------+
;
5 ;
SETB 14H ; Set the D4 bit of register 0x22 of group one
7 SETB 1FH ; Set the D7 bit of register 0x23 of group one
;+---------------+----------------+
9 ;| 0x22 | 0x23 |
;+---------------+----------------+
11 ;|0|0|0|1|0|0|0|0| 1|0|0|0|0|0|0|0|
;+---------------+----------------+
13 ;| 16D or 10H | 128D or 80H | SETB state
;+---------------+----------------+
15 SETB 24H ; Set the D4 bit of register 0x24 of group two
SETB 2BH ; Set the D3 bit of register 0x25 of group two
28 Introduction
17 ;+---------------+----------------+
;| 0x24 | 0x25 |
19 ;+---------------+----------------+
;|0|0|0|1|0|0|0|0| 0|0|0|0|1|0|0|0|
21 ;+---------------+----------------+
;| 16D or 10H | 08D or 08H | SETB state
23 ;+---------------+----------------+
SETB 31H ; Set the D1 bit of register 0x26 of group
three
25 SETB 38H ; Set the D0 bit of register 0x27 of group
three
;+---------------+----------------+
27 ;| 0x26 | 0x27 |
;+---------------+----------------+
29 ;|0|0|0|0|0|0|1|0| 0|0|0|0|0|0|0|1|
;+---------------+----------------+
31 ;| 02D or 02H | 01D or 01H | SETB state
;+---------------+----------------+
33 END
✌
✆
In above example, the bit is access by address. The memory of the MCU
after the execution of above program shall be looked like:
x0 x1 x2 x3 x4 x5 x6 x7
... 00 00 00 00 00 00 00 00
18 00 00 00 00 00 00 00 00
20 00 00 10 80 10 08 02 01
28 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 1.7: Memory result.
If operand of SETB is a decimal number then it represents to the bit
location from the D0 bit of 0×20 register. The counting starts from 0 to 7
for first register of the group and 8 to 15 for the second register of the group.
✞
1 SETB 14 ;8+6 => D6 bit of second byte
1.2. RAM 29
SETB 15 ;8+7 => D7 bit of second byte
3 SETB 16 ;8x2+0 => D0 bit of third byte
SETB 17 ;8x2+1 => D1 bit of third byte
5 ;+---------------+----------------+----------------+
;| 0x20 | 0x21 | 0x22 |
7 ;+---------------+----------------+----------------+
;|0|0|0|0|0|0|0|0| 1|1|0|0|0|0|0|0| 0|0|0|0|0|0|1|1|
9 ;+---------------+----------------+----------------+
;| 00D or 00H | 192D or C0H | 03D or 03H |
11 ;+---------------+----------------+----------------+
END
✌
✆
x0 x1 x2 x3 x4 x5 x6 x7
... 00 00 00 00 00 00 00 00
18 00 00 00 00 00 00 00 00
20 00 C0 03 00 00 00 00 00
28 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 1.8: Memory result.
To avoid this confusion of the bit position due to different starting
point of counting indices, first convert the location given in decimal
into hexa-decimal equivalent and then count the location of bit
starting from the index 0.
1.2.1 Register Bank
First 32 bytes are further divided in to four parts. Each part is of 8 bytes
long. Each byte of this part is called a register. A group of 8 bytes is called
register bank. The 8 bytes of a register bank are denoted by R0 to R7. Four
register banks are Bank 0, Bank 1, Bank 2 and Bank 3.
30 Introduction
Bank Range Registers
Bank 0 00H-07H R0 R1 R2 R3 R4 R5 R6 R7
Bank 1 08H-0FH R0 R1 R2 R3 R4 R5 R6 R7
Bank 2 10H-17H R0 R1 R2 R3 R4 R5 R6 R7
Bank 3 18H-1FH R0 R1 R2 R3 R4 R5 R6 R7
By default, register bank 0 is selected on power up. The following in-
structions
✞
MOV R0 ,#10 ; copy dec 10 to register
2 ; R0 of register bank 0
END
✌
✆
shall store the data into the register R0 of register bank 0. See RAM memory
x0 x1 x2 x3 x4 x5 x6 x7
00 0A 00 00 00 00 00 00 00
08 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 1.9: RAM memory result.
PSW is used to select the desired register banks. The PSW register is
shown below.
D7 D6 D5 D4 D3 D2 D1 D0
CY AC RS1 RS0 OV P
D3 and D4 bit of PSW register are set as per table given below to select
the register banks.
1.2. RAM 31
Selected Bank RS1 RS0
Bank 0 0 0
Bank 1 0 1
Bank 2 1 0
Bank 3 1 1
In the following example, RS0 bit is set high while RS1 bit is cleared.
Thus the register bank 1 is selected.
✞
1 SETB PSW.3 ; set D3 bit to 1
CLR PSW.4 ; set D4 bit to 0
3 MOV R0 ,#10 ; copy decimal 10 to register R0 of
; register bank 1, i.e. 08th RAM Byte
5 END
✌
✆
MOV R0,#10 stores the data in register R0 of register bank 1. See RAM
memory
RAM Address x0 x1 x2 x3 x4 x5 x6 x7
00H 00 00 00 00 00 00 00 00
08H 0A 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 1.10: RAM memory result.
1.2.2 Bit Addressable RAM
The RAM location from 20H to 2FH are bit addressible. It means, all in-
structions whose operands are bit addresses can accessit. bit of bytes from
RAM address 20H to 2FH are identified by their position indices, counted
from 0 to 127. The instruction sets SETB, CLR, CPL, JB, JNB and JBC
are bit addressing instructions.
32 Introduction
RAM Address x0 x1 x2 x3 x4 x5 x6 x7
20H 00 00 00 00 00 00 00 00
28H 00 00 00 00 00 00 00 00
Table 1.11: Bit Addressable RAM.
Each RAM address is 8 bit long. Between RAM addresses 20H to 2FH
(total 16 bytes), bit are counted from 0 to 127, taking first bit (D0 bit) of
RAM address 0×20 as reference position, i.e. zero position. In other words,
D0 bit of 20H RAM address is counted as index 0. Similarly, D1 bit of RAM
address 21H is 9th
bit of bit-addressable RAM from RAM address 0×20.
RAM Address D7 D6 D5 D4 D3 D2 D1 D0
20H 007 006 005 004 003 002 001 000
21H 016 014 013 012 011 010 009 008
... 031 030 029 028 027 026 025 024
2FH 127 125 125 124 123 122 121 120
Table 1.12: Indices of each bit of bit addressable RAM memory addresses.
See the following example
✞
1 SETB 03 ; set D3 bit at 0x20 RAM location
SETB 03H ; set D3 bit at 0x20 RAM location
3 SETB 24 ; set D4 bit at 0x23 RAM location
SETB 18H ; set D4 bit at 0x23 RAM location
5 END
✌
✆
The bit of RAM Address 0×20 and 0×23 will be set as shown in the below
table.
1.2. RAM 33
RAM Address D7 D6 D5 D4 D3 D2 D1 D0 Value
20H 0 0 0 0 1 0 0 0 08H
21H 0 0 0 0 0 0 0 0 00H
22H 0 0 0 0 0 0 0 0 00H
23H 0 0 0 0 0 0 0 1 01H
24H 0 0 0 0 0 0 0 0 00H
The bit indicex from 128 to 255 in decimal count are assigned to SFRs.
Note that these bit indices are also used as hexadecimal values. For example
7th
bit index is equal to 0×07 and 128th
bit index is equal to 0×80 from the
RAM Address 0×20. When an operand is used with BIT ADDRESSABLE
INSTRUCTIONS then it always represents to the indices of bit rather than
RAM address.
When operand of the bit addressing instruction is [128, 255], it access to
bit of Special Function Registers (SFR), i.e. ROM memory.
D7 D6 D5 D4 D3 D2 D1 D0
P0 135 134 133 132 131 130 129 128
TCON 143 142 141 140 139 138 137 136
P1 151 150 149 148 147 146 145 144
SCON 159 158 157 156 155 154 153 152
P2 167 166 165 164 163 162 161 160
IE 175 174 173 172 171 170 169 168
P3 183 182 181 180 179 178 177 176
IP 188 187 186 185 184
PSW 215 214 213 212 211 210 209 208
ACC 231 230 229 228 227 256 225 224
B 247 246 245 244 243 242 241 240
34 Introduction
The corresponding indices in hexa-decimal form are shown in the below
table.
D7 D6 D5 D4 D3 D2 D1 D0
P0 87 86 85 84 83 82 81 80
TCON 8F 8E 8D 8C 8B 8A 89 88
P1 97 96 95 94 93 92 91 90
SCON 9F 9E 9D 9C 9B 9A 99 98
P2 A7 A6 A5 A4 A3 A2 A1 A0
IE AF AE AD AC AB AA A9 A8
P3 B7 B6 B5 B4 B3 B2 B1 B0
IP BC BB BA B9 B8
PSW D7 D6 D5 D4 D3 D2 D1 D0
ACC E7 E6 E5 E4 E3 E2 E1 E0
B F7 F6 F5 F4 F3 F2 F1 F0
Remember that, registers A, B, PSW, IP, IE, ACC, SCON, TCON, all
input/output ports are bit addressable as well as SFR. ROM which holds
program code for execution and PCON are not bit addressable.
1.2.3 Scratch Pad
RAM location from 30H to 7FH are used as scratch pad for data.
x0 x1 x2 x3 x4 x5 x6 x7
30 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
7F 00 00 00 00 00 00 00 00
Table 1.13: Scratch Pad.
1.2. RAM 35
1.2.4 Stack Pointer
Stack is a section of RAM used to store information. This information may
be data or address. The register used to access the stack is called the stack
pointer (SP) register. It is 8 bit long register and can store values ranging
from 00H to FFH. On power on to 8051, SP contains the value 07H.
Bank Range Registers
Register Bank 0 00H-07H R0 R1 R2 R3 R4 R5 R6 R7
SP & Register Bank 1 08H-0FH R0 R1 R2 R3 R4 R5 R6 R7
Register Bank 2 10H-17H R0 R1 R2 R3 R4 R5 R6 R7
Register Bank 3 18H-1FH R0 R1 R2 R3 R4 R5 R6 R7
08H location of the RAM is first location which is used by the SP (from
Register Bank 1). PUSH instruction is used to add value in the stack and
POP is used to remove the value from the stack. SP is last location of stack,
this is why, SP is incremented by one on each PUSH and decremented by one
on each POP instruction. CPU uses stack to store the last address just after
the CALL instruction before handing over the control to the sub routine.
On RET instruction, control is transfer back to the caller. Caller resumes
the operation from the last address which was stored in stack by CALL. On
power up, register bank 1 and SP shares same memory location in the 8051
RAM. This is why, to use register bank 1, we first change the stack pointer
location to other RAM location by using MOV instruction as
✞
1 MOV SP ,<new RAM location >
✌
✆
Remember that, on each PUSH to stack, stack pointer register is incremented
by 1 and on each POP from the stack, stack pointer register is decremented by
1. There is no decrements protocol for stack pointer. Because, in decrements
protocol, SP may reach to the memory location of default register bank 0
and may violate the memory sharing protocol.
1.2.5 Program Counter
8051 has a program counter register. It is 8 bit long register. It is because,
8051 has 8 bit address line. It holds the address of opcode and operands
36 Introduction
which are to be executed (address of next opcode that would be fetched).
Program counter incremented by one when 8051 executed opcode at the
current address.
1.3 Subroutines
CALL instruction is used to call subroutine. Subroutines are used in seg-
mentation of large codes. Subroutines are group of instructions which are
frequently used in a program. It makes a program more structured and saves
the memory space. When subroutine is called, control is transferred to that
subroutine. After finishing of execution, RET return the control back to the
caller. See below codes
✞
1 ORG 0
;---------Start of Main -----------------
3 MAIN:
MOV A ,#11H
5 LCALL SUBROUT_1
LCALL SUBROUT_2
7 SJMP MAIN
;---------End of Main -------------------
9 ;
;---------Start of SUBROUT_1 ------------
11 SUBROUT_1:
MOV B ,#12H
13 RET ;Return to caller of SUBROUT_1
;---------End of SUBROUT_1 -------------
15 ;
;---------Start of SUBROUT_2 ------------
17 SUBROUT_2:
MOV R0 ,#13H
19 RET ;Return to caller of SUBROUT_2
;---------End of SUBROUT_2 -------------
21 END ;End of the program.
✌
✆
1.3. SUBROUTINES 37
1.3.1 Delay
8051 has two clocks. CPU takes certain number of clock cycle to execute an
instruction. It is called machine cycles. Length of machine cycle is depends
on the frequency of the crystal cycle. In 8051, one machine cycles lasts 12
oscillator period. If a CPU has a crystal frequency of 11.0592MHz then its
machine cycles frequency is computed as
MCF =
11.0592
12
= 921.6KHz
The machine cycle period is
T =
1
MCF
= 1.085µs
It means to read or write one byte, machine takes 1.085µs. Delay is a method
to lapse of time in MCU. Study following program:
✞
1 ;Start of DELAY subroutine
DELAY:
3 ;MOV is one byte instruction here. CPU takes
;one machine cycle to execute this instruction.
5 ;R3 register is stored value 200 immediately.
MOV R3 ,#200
7 ;Start HERE loop
HERE:
9 ;DJNZ is 2 byte instruction. Here DJNZ decrease
;R3 by one and checks whether it becomes zero
11 ;or not. If R3 is not zero , it again reach to
;HERE. By this way it performs 200 times loop
13 ;It means CPU takes 200x2 machine cycles to
;perform this line of instruction code.
15 DJNZ R3,HERE
;RET is two byte instruction. Here it takes two
17 ;machine cycles to execute it.
RET
19 ;By this way total machine cycles used to execute
;DELAY subroutines are 1+400+2 = 403 machine cycles.
✌
✆
38 Instructions
2.1. INSTRUCTION SETS 39
2Instructions
2.1 Instruction Sets
Instruction sets guide to MCU for execution of operands following to the
instruction as per instruction’s guideline. Some of the instructions like JBC,
CPL, INC and DEC are read-modify-write type of instruction, because they
read value from the port-bit, register or memory location then modify it
by performing an operation on them and again store it again into the same
port-bit, register or memory location.
2.1.1 ACALL
It is two byte long instruction. ACALL unconditionally calls a subroutine
at the indicated code address. ACALL pushes the address of the instruc-
tion that follows ACALL onto the stack, least-significant-byte first, most-
significant-byte second. The Program Counter is then updated so that pro-
gram execution continues at the indicated address. Since only 11 bit of the
Program Counter are affected by ACALL, calls may only be made to sub
routines located within the same 2K block where the first byte that follows
ACALL is located. The syntax of the instruction is
✞
ACALL <accumulator >, <data or address >
✌
✆
✞
1 HERE: ; HERE subroutine
MOV A,#1AH ; Store value 1A Hex to
Accumulator
3 AGAIN1: ; AGAIN1 subroutine
DEC A ; Decrease accumulator by 1
5 JNZ AGAIN1 ; Jump to AGAIN1 subroutine
ACALL THERE ; Call the DELAY subroutine
7 SJMP HERE ; Do it again and again
THERE: ; THERE subroutine
40 Instructions
9 MOV A,#1AH ; Store value 1A Hex to
Accumulator
AGAIN1: ; AGAIN subroutine
11 DEC A ; Decrease accumulator by 1
JNZ AGAIN1 ; Jump to AGAIN subroutine
13 RET ; Return control to caller
END ; End of program
✌
✆
2.1.2 ADD
It is acronym of ADD WITH ACCUMULATOR. If ADD uses registers or reg-
ister address as second operand then it is one byte long instruction, otherwise
it is two byte long instruction. This functions adds the operand value with
accumulator and stores result in accumulator. The operand value remains
unaffected on execution of this instruction. The syntax of the instruction is
✞
ADD <accumulator >,<data or address >
✌
✆
This instruction is used in following ways
✞
1 ADD A,# data
ADD A,<address >
3 ADD A,@R0
ADD A,@R1
5 ADD A,Rn
✌
✆
The carry flags are set accordingly to reflect the result. The Overflow (OV)
bit is set if there is a carry-out of bit 6 or out of bit 7, but not both.
Illustrated Example To add two hexadecimal numbers 10H and 62H are
given below.
✞
1 10H = 00001010
+62H = 01100010
3 ---------------------
72H = 01101100
✌
✆
The code for 8051 MCU is
✞
CLR C ;Clear carry
2 MOV A ,#10H ;Store 10H in accumulator
2.1. INSTRUCTION SETS 41
ADD A ,#62H ;Add hex 62 with accumulator
4 MOV R1 ,A ;Copy contents of accumulator to R1
END ;End the program
✌
✆
2.1.3 ADDC
It is acronym of ADD TO ACCUMULATOR WITH CARRY. If ADDC
uses registers or register address as second operand then it is one byte long
instruction, otherwise it is two byte long instruction. This functions adds
the operand value and carry with accumulator and stores the result value
in accumulator. The operand value remains unaffected. The syntax of the
instruction is
✞
1 ADDC <accumulator >,<data or address >
✌
✆
This instruction is used in following ways
✞
1 ADDC A,# data
ADDC A,<address >
3 ADDC A,@R0
ADDC A,@R1
5 ADDC A,Rn
✌
✆
ADDC reset the carry flag to zero. The Overflow (OV) bit is set if there is
a carry-out of bit 6 or out of bit 7, but not both.
✞
1 MOV A ,#20H ; A = 00100000
CLR C ; CY = 0
3 ADDC A ,#20H; A+CY +20H = 01000000
MOV R0 ,A ; R0 = 01000000
5 SETB C ; CY = 1
ADDC A ,#20H; A+CY +20H = 01100001
7 MOV R1 ,A ; R1 = 01100001
END ; End the program
✌
✆
2.1.4 AJMP
It is short name of ABSOLUTE JUMP. It is two bytes long instruction. The
instruction let the jump to compiler within 2K block unconditionally indi-
42 Instructions
cated by operand address. The new value for the Program Counter is cal-
culated by replacing the least-significant-byte of the Program Counter with
the second byte of the AJMP instruction, and replacing bit 0-2 of the most-
significant-byte of the Program Counter with 3 bit that indicate the page of
the byte following the AJMP instruction. bit 3-7 of the most-significant-byte
of the Program Counter remain unchanged. The syntax of the instruction is
✞
AJMP page <number from 0 to 7>
✌
✆
2.1.5 ANL
It is short name of BITWISE AND. If ANL uses registers or register address
as second operand then it is one byte long instruction, otherwise it is two
byte long instruction. The syntax of the instruction is
✞
1 ANL <operand a>,<operand b>
✌
✆
This instruction is used in following ways
✞
1 ANL <Internal RAM Address >,A
ANL <Internal RAM Address >,#datae
3 ANL A,# data
ANL A,<Internal RAM Address >
5 ANL A,@R0
ANL A,@R1
7 ANL A,Rn
ANL C,<bit Address >
9 ANL C,/<bit Address >
✌
✆
This instruction performs a bitwise “AND” operation between two operands.
The result is saved in accumulator or Carry flag. The second operand remains
unaffected. Logical “AND” gives result if both of the comparing bit are high,
otherwise bit is cleared.
Illustrated Example Bitwise AND is performed between bit of two num-
bers rather than number itself as a whole. The hexadecimal numbers F2H
and 20H are equals to their binary values 11110010 and 00100000. The Bit-
wise AND is performed bit-by-bit for all the corresponding bit of these two
binary numbers as shown below:
2.1. INSTRUCTION SETS 43
✞
1 F2H = 11110010
20H = 00100000
3 ------------------- AND
20H = 00100000
5 --------------
✌
✆
In above binary AND operation, the output is binary 1 if both operands are
binary 1 otherwise output is binary 0. The 8051MCU code for above AND
operation is given in following example.
✞
1 MOV A ,#0F2H ; A = 11110010
ANL A ,#20H ; A = 00100000
3 END ; End of program
✌
✆
2.1.6 CJNE
It is acronym of COMPARE AND JUMP IF NOT EQUAL. It is three byte
long instruction. The syntax of the instruction is
✞
1 CJNE <accumulator >,<data or address >,<relative address >
✌
✆
This instruction is used in following ways:
✞
1 CJNE A,#data ,<location >
CJNE A,<Internal RAM Address >,<location >
3 CJNE A,@R0 ,<location >
CJNE A,@R1 ,<location >
5 CJNE A,Rn ,<location >
✌
✆
CJNE compares the value of ‘accumulator’ with ‘data’ or value at ‘address’
and branches to the indicated ‘relative address’ if ‘accumulator’ and ‘data’
or value at ‘address’ are not equal otherwise program moves to next instruc-
tion. The Carry bit (C) is set if ‘accumulator’ is less than ‘data’ or value at
‘address’, otherwise it is cleared.
CY = 1 if A < Data
CY = 0 if A ≥ Data
CJNE either sets or clears the Carry bit so we can use JC, JNC, JB, JNB
and JBC etc bit addressable instructions. For this purpose, CJNE is used as
44 Instructions
✞
1 CJNE A,#data ,THERE
THERE:
3 JC HERE
HERE:
5 END
✌
✆
Illustrated Example Go through the following example:
✞
1 MOV R1 ,#12H
MOV R2 ,#25H
3 MOV R4 ,#10H
MOV A,R1
5 CJNE A ,#12H,HERE
MOV B,R2
7 CJNE A ,#11H,HERE
MOV B,R4
9 HERE:
END
✌
✆
First, registers R1, R2 and R4 are stored by hex values 12H, 25H and 10H
respectively. Now the value of register R1 is copied into accumulator A. Ac-
cumulator value is compared with 12H. If both values are equal then there
is no conditional jump to the address of HERE label and next instruction
MOV B,R2 is executed. In the next instruction accumulator value is com-
pared with 11H. Here both values are not equal hence, here a conditional
jump occurs to the address of label HERE. The instruction
✞
MOV B,R4
✌
✆
is skipped here.
2.1.7 CLR
It is acronym of CLEAR. It is two bytes long if it is used in bit addressable
mode, otherwise it is one byte long. CLR clears (sets to 0) all the bit(s) of the
indicated register. If the operand is a bit (including the carry bit), only that
specified bit is affected. Clearing the Accumulator sets the Accumulator’s
value to 0.
2.1. INSTRUCTION SETS 45
✞
1 CLR bit ;Bit
CLR C ;Carry flat
3 CLR A ; Accumulator
✌
✆
Illustrated Example In the following example, Accumulator, Carry flag
and LSB of port P0 are cleared (set to zero) by using CLR instruction.
✞
1 MOV A ,#0FFH ; A = 11111111
CLR A ; A = 00000000
3 SETB C ; Set carry flag high
CLR C ; C = 1
5 CLR P0.0 ; P0= 11111110
END ; End of program
✌
✆
2.1.8 CPL
It is acronym of COMPLEMENT. It complements the current value of bit or
register, i.e. it converts binary 0 to binary 1 and vice-versa. It is two bytes
long instruction if it is used as bit addressable mode, otherwise it is one byte
long instruction.
✞
CPL bit ;Bit
2 CPL C ;Carry flat
CPL A ; Accumulator
✌
✆
Illustrated Example CPL function toggles a bit or a register value. In the
following example, accumulator, Carry flag and LSB of port P0 are comple-
mented by instruction CPL.
✞
1 MOV A ,#0FFH ; A = 11111111
CPL A ; A = 00000000
3 CPL C ; C = 1
CPL P0.0 ; PO= 11111110
5 END ; End of program
✌
✆
46 Instructions
2.1.9 DA
It is short form of DECIMAL ADJUST ACCUMULATOR. It is one byte
long instruction. It is just called as DA. This instruction works only with
Accumulator (A).
1. The contents of the accumulator are changed from a binary value to two
4-bit binary coded decimal (BCD) digits. This is the only instruction
that uses the auxiliary flag to perform the binary to BCD conversion,
and the conversion procedure is described below.
2. If the value of the low-order 4-bits in the accumulator is greater than 9
or if Carry flag is set, the instruction adds 6 to the low-order four bits.
3. If the value of the high-order 4-bits in the accumulator is greater than
9 or if the Carry flag is set, the instruction adds 6 to the high-order
four bits.
The Carry bit (C) is set if the resulting value is greater than 0×99, otherwise
it is cleared. In hexadecimal number system, unit and tenth place digit
ranges from 0 to F but in decimal form of number system, unit and tenth
place digit ranges from 0 to 9. If unit place number is larger than 9 then
6 is added to it and carry is transferred to the tenth place digit. When we
convert the hexadecimal number by using DA instruction into BCD form of
number, the unit and tenth place digits are changes and they represent the
equivalent decimal number as 10 × t + d. Here, t is tenth place digit and
d is unit place digit. In hex form of number representation, the maximum
possible unit and tenth place digits shall be F.
✞
1 +---------------+---------------+
| Tenth place | Unit place |
3 +---------------+---------------+
| 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
5 +---------------+---------------+
| F | F |
7 +---------------+---------------+
✌
✆
But in BCD form of decimal numbers, the maximum binary arrangement
shall be as shown below.
✞
1 +---------------+---------------+
2.1. INSTRUCTION SETS 47
| Tenth place | Unit place |
3 +---------------+---------------+
| 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 |
5 +---------------+---------------+
| 9 | 9 |
7 +---------------+---------------+
✌
✆
This is why, when unit place digit becomes larger than 9 then 6 (binary
0110) is added. Addition of 6 in the LSB bit in hex larger than 9 returns the
remainder as per mathematics
d = (LSB + 06)%16
Carry is transferred to the left place digit. Consider a decimal number 90
(hex 5A) represented as
✞
1 +---------------+---------------+
| Tenth place | Unit place |
3 +---------------+---------------+
| 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 |
5 +---------------+---------------+
| 5 | A |
7 +---------------+---------------+
✌
✆
The four LSB bit represents to equivalent decimal 10 or hex A (binary 1010).
To convert it in BCD form, decimal 6 or hex 06 (binary 0110) is added to it.
Now the unit place digit becomes
✞
1 1010
+ 0110
3 -------
(1) 0000
5 -------
✌
✆
This carry (1) is added to four MSB bit as
✞
1 0101
+ 0001
3 -------
(0) 0110
5 -------
✌
✆
48 Instructions
Now the final number after DA instruction is ‘0110 0000’ which is decimal
equivalent to 60.
2.1.10 DEC
It is short form of DECREMENTS. It is two bytes long instruction if it is
used with internal RAM Address, otherwise it is one byte long instruction. It
decrements the value of operand by 1. If initial value is 0 then DEC cause to
reset the operand to FF in hex, i.e. operand overflows. It uses one Machine
Cycle (MC). This instruction is used in either of the following forms.
✞
1 DEC A
DEC <Internal RAM Address >
3 DEC @R0
DEC @R1
5 DEC Rn
✌
✆
Illustrated Example Accumulator has stored the value FFH and it is
subsequently decreases three times by one. The final value on accumulator
is FCH.
✞
1 MOV A,#0 FFH ; A = 11111111
DEC A ; A = 11111110
3 DEC A ; A = 11111101
DEC A ; A = 11111100
5 END ; End of program
✌
✆
2.1.11 DIV
It is short form of DIVISION. One byte long instruction. It divides primary
accumulator A by secondary accumulator B. It affects Carry and Over flow
flags to reflect the result. It is used as
✞
1 DIV AB
✌
✆
The quotient is placed in accumulator A and remainder is placed in the sec-
ondary accumulator B. This instruction always clears the Carry flag. Over-
flow flag is set if division is attempted by 0 otherwise it is cleared.
Illustrated Example
2.1. INSTRUCTION SETS 49
✞
1 MOV A ,#0FFH ; A = 11111111
MOV B ,#10H ; B = 00001010
3 DIV AB ; A = 00001111 & B = 00001111
END ; End of program
✌
✆
2.1.12 DJNZ
It is short name of DECREMENT AND JUMP IF NOT ZERO. Three bytes
long instruction if uses internal RAM Address otherwise it is two bytes long
instruction. Machine Cycle (MC) used by this instruction is 2. It is used as
either of the any forms given below.
✞
DJNZ <Int RAM Addr >,<relative address >
2 DJNZ Rn,<relative address >
✌
✆
DJNZ decrements the value of register by 1. If the initial value of register
is 0, decrementing register value by one resets this register to 255 (0×FF).
If the new value of register is not 0 the program will branch to the address
indicated by ‘relative address’. If the new value of register is 0 program flow
continues with the instruction following the DJNZ instruction.
Illustrated Example In the following example, port P1 is assign a hex value
55H. RAM memory location identified by 32H address is storing decimal
value 200. A loop is created to decrements the value stored in this address
and jump to LOOP1 is value at this memory address is not zero. Thus a
total 200 loops are performed by the following example.
✞
MOV P1 ,55H
2 MOV 32H ,#200
LOOP1:
4 CPL P1.1
DJNZ 32H,LOOP1
6 END
✌
✆
Another DJNZ example:
✞
MOV A ,#12H ; A = 00010010
2 MOV R0 ,#00H ; R0= 00000000
MOV R2 ,#05H ; R2= 00000101 Counter
4 LCALL DELAY ; Long Call to DELAY
50 Instructions
INC R0 ; Increment R0
6 DJNZ A, HERE ; If A not zero , Jump to HERE
HERE: ; HERE sub routine
8 MOV R1,A ; Copy value of A in R1
JMP THEEND ; JUMP to THEEND
10 DELAY: ; DELAY sub routine
MOV R3 ,#00H ; Set R3 to 0
12 MOV P0,R3 ; Change full port P0 as output port
RLC A ; A = 00100100, if first loop. In -
14 ; corresponding loops A has values -
; according to RLC A
16 MOV P0.1 ,C ; Copy carry flag to outport port
P0.1
DJNZ R2 , DELAY ; If R2 is not zero flow the loop
DELAY
18 RET ; Return the control to LCALL caller
THEEND: ; The end sub routine
20 END ; End of the program
✌
✆
2.1.13 INC
It increments the value of register by 1. It is two bytes long instruction if
uses Internal RAM Address otherwise it is one byte long instruction. It is
used in any of the following methods.
✞
INC A
2 INC <Internal RAM Address >
INC @R0
4 INC @R1
INC Rn
6 INC DPTR
✌
✆
If the initial value of register is 255 (0×FF), incrementing the value will cause
it to reset to 0. In the case of “INC DPTR”, the value two-bytes unsigned
integer value of DPTR is incremented. If the initial value of DPTR is 65535
(0×FFFF), incrementing the value will cause it to reset to 0. Again, the
Carry Flag is NOT set when the value of DPTR “rolls over” from 65535 to
0. In the following example, increment of accumulator take place four times.
✞
MOV A ,#12H ; A = 0001 0010
2.1. INSTRUCTION SETS 51
2 INC A ; A = 0001 0011
INC A ; A = 0001 0100
4 INC A ; A = 0001 0101
INC A ; A = 0001 0110
6 END
✌
✆
2.1.14 JB
JB describes to JUMP IF BIT SET. It is a three bytes long instruction. It
is used as
✞
JB <bit address >,<relative address >
✌
✆
It jumps to the ‘relative address’ if bit is set otherwise program executes con-
tinue with the instruction following the JB instruction. This is an instruction
which can be used to check the overflow flag via PSW register and jump to a
relative address. The overflow (OV) flag is represented by ‘PSW.2’. Syntax
for OV flag and JB instruction is
✞
1 JB PSW.2 ,<relative address >
✌
✆
Illustrated Example See the example below:
✞
1 MOV A ,#12H ; Set A to 12H
MOV P0 ,#00H ; Clear the port P0
3 JB P0.1 ,IFBIT ; If second bit of port P0 is hight then
; jump to IFBIT label. Here no jump occurs
5 SETB P0.1 ; Set second bit of port P0 high
JB P0.1 ,IFBIT ; If second bit of port P0 is hight then
7 ; jump to IFBIT label. Here jump occurs
MOV R0 ,#0 FFH ; No execution
9 MOV R1 ,#0FH ; No execution
IFBIT: ; IFBIT label
11 END ; End of program
✌
✆
In above example, Accumulator and port P0 are added the values 12H and
00H. JB is used to check the D1 bit of port P0. If D1 bit of port P0 is high
then pointer jumps to IFBIT label address.
52 Instructions
2.1.15 JBC
JBC is short form of JUMP IF BIT SET AND CLEAR BIT. It is a three
bytes long instruction. It is used as
✞
1 JBC <bit address >,<relative address >
✌
✆
It jumps to the ‘relative address’ if bit is high, i.e. is ‘1’, otherwise program
executes continue to the instruction following the JBC instruction. It also
reset the bit after execution of the instruction.
Illustrated Example See the example below:
✞
1 MOV A ,#12H ; Set A to 12H
MOV P0 ,#00H ; Clear the port P0
3 JBC P0.1 ,IFBIT ; If second bit of port P0 is hight then
; jump to IFBIT label. Here no jump occurs
5 SETB P0.1 ; Set second bit of port P0 high
JBC P0.1 ,IFBIT; If second bit of port P0 is hight then
7 ; jump to IFBIT label. Here jump occurs
; The target bit is reset to zero
9 MOV R0 ,#0 FFH ; No execution
MOV R1 ,#0FH ; No execution
11 IFBIT: ; IFBIT label
END ; End of program
✌
✆
In above example, Accumulator and port P0 are added the values 12H and
00H. JBC is used to check the D1 bit of port P0. If D1 bit of port P0 is high
then pointer jumps to IFBIT label address. Before jumping to the relative
address, the corresponding conditional bit (here, D1 bit of port P0) is cleared.
2.1.16 JC
JC instruction describes to JUMP IF CARRY. It is two bytes long instruc-
tion. It checks the status of carry bit when executed, and jumps to the
address specified by the operand if carry bit is high. It is used as
✞
JC <address >
✌
✆
If the Carry Bit is not set program execution continues with the instruction
following the JC instruction.
2.1. INSTRUCTION SETS 53
✞
1 MOV A ,#12H ; Set A to 12H
CLR C ; Clear carry flag
3 JC IFCARRY ; If carry flag is high or set to 1, jump
; to IFCARRY label. Here no jump occurs
5 SETB C ; Set carry flag high
JC IFCARRY ; If carry flag is high or set to 1, jump
7 ; to IFCARRY label. Here jump occurs
MOV R0 ,#0 FFH ; No execution
9 MOV R1 ,#0FH ; No execution
IFCARRY : ; IFCARRY label
11 END ; End of program
✌
✆
2.1.17 JMP
It describes to JUMP TO DATA POINTER PLUS ACCUMULATOR. It is
one byte long instruction. It jumps to the address represented by the sum of
the value of DPTR and the value of the accumulator specified by operand.
It is used as
✞
1 JMP @A+DPTR
✌
✆
Example is given below:
✞
1 MOV A ,#12H ; Set A to 12H
JMP IFNOBIT ; Jump to IFNOBIT label
3 MOV R0 ,#0 FFH ; No execution
MOV R1 ,#0FH ; No execution
5 IFNOBIT : ; IFNOBIT label
END ; End of program
✌
✆
2.1.18 JNB
It specified to JUMP IF BIT NOT SET. It is three bytes long bit addressable
instruction. It is used like
✞
JNB <bit address >,<relative address >
✌
✆
This instruction lets the jump to program to specified address if the indicated
bit is not set otherwise program executes continuously.
54 Instructions
Illustrated Example If bit is low, the instruction let the jump to the
program pointer to the label or to the address location. Here, Carry flag is
used to for comparing its bit level by JNB.
✞
1 MOV A ,#12H ; Set A to 12H
SETB C ; Set carry flag bit to high
3 JNB C,IFNOBIT ; If bit is low or set to 0 jump
; to IFNOBIT label. Here no jump occurs
5 CLR C ; Clear carry flag bit
JNB C,IFNOBIT ; If bit is low or set to 0, jump
7 ; to IFNOBIT label. Here jump occurs
MOV R0 ,#0 FFH ; No execution
9 MOV R1 ,#0FH ; No execution
IFNOBIT : ; IFNOBIT label
11 END ; End of program
✌
✆
2.1.19 JNC
It specified to JUMP IF CARRY NOT SET. Two byte long instruction. Used
as
✞
1 JNC <relative address >
✌
✆
This instruction let the jump to program to specified address if the carry flag
is not set otherwise executes continuously.
Illustrated Example In this example, JNC compare to carry bit. If carry
bit is low, this instruction jumps to the program pointer to the label or to
the address location. If it is high, program executed continuously to the next
instructions.
✞
1 MOV A ,#12H ; A = 00010010
CLR C ; CY = 0
3 RLC A ; A = 00100100 & CY = 0
JC ENDHERE ; If CY = 1
5 HERE:
RLC A ;1. A = 01001000 & CY = 0
7 ;2. A = 10010000 & CY = 0
;3. A = 00100000 & CY = 1
9 JNC HERE ; If CY = 0
ENDHERE :
2.1. INSTRUCTION SETS 55
11 MOV R0 ,A ; R0 = 00100000 & CY = 1
END
✌
✆
2.1.20 JNZ
It is short name to JUMP IF ACCUMULATOR IS NOT ZERO. It is two
bytes long instruction and used as
✞
JNZ <relative address >
✌
✆
This instruction let the jump to program to specified address if the accumu-
lator contains any value except zero otherwise executes continuously.
Illustrated Example JNZ jumps to a pointer or label when accumulator
is not zero. In the following program, accumulator is stored value 12H.
Accumulator is decrease by one and JNZ compare its value with zero. If
accumulator is not zero then the JNZ instruction jumps to the program
pointer to the label HERE.
✞
1 MOV A ,#12H ; A = 00010010
MOV R0 ,#00H ; R0 = 00000000
3 HERE:
DEC A ; Decrease A by one
5 INC R0 ; R0++
JNZ HERE ; If A>0
7 END ; End of program
✌
✆
2.1.21 JZ
It specified to JUMP IF ACCUMULATOR IS ZERO. It is two bytes long
instruction. Its operand is always accumulator. As there is no zero flag in
8051 MCU hence, accumulator’s zero value is used for comparison to zero.
Used as
✞
1 JZ <relative address >
✌
✆
This instruction let the jump to program to specified address if the accumu-
lator contains zero value otherwise executes continuously.
56 Instructions
Illustrated Example In this example, accumulator is stored value 02H
which is decremented one by one. Each decrements is followed by JZ instruc-
tion. When accumulator decremented to zero, JZ instruction is executed and
program counter reached to HERE label.
✞
1 MOV A ,#02H ; A = 0000 0010
JZ HERE ; No jump
3 DEC A ; A = 0000 0001
JZ HERE ; No jump
5 DEC A ; A = 0000 0000
JZ HERE ; Jump to HERE Label
7 DEC A ; No execution
HERE: ; HERE label
9 MOV A,#0 FFH; Set A = 1111 1111
END ; End of program
✌
✆
2.1.22 LCALL
It specified to LONG CALL. It is three bytes long instruction. Used as
✞
LCALL <code address >
✌
✆
LCALL calls a program subroutine. LCALL increments the program counter
by 3 and pushes that value onto the stack. The Program Counter is then
set to the 16-bit value which follows the LCALL opcode, causing program
execution to continue at that address. The target address for LCALL may
be anywhere within the 64K byte address.
Illustrated Example
✞
1 MOV A ,#12H ; A = 00010010
MOV R0 ,#00H ; R0= 00000000
3 MOV R2 ,#05H ; R2= 00000101 Counter
LCALL DELAY ; Long Call to DELAY
5 INC R0 ; Increment R0
DJNZ A, HERE ; If A not zero , Jump to HERE
7 HERE: ; HERE sub routine
MOV R1,A ; Copy value of A in R1
9 JMP THEEND ; JUMP to THEEND
DELAY: ; DELAY sub routine
11 MOV R3 ,#00H ; Set R3 to 0
2.1. INSTRUCTION SETS 57
MOV P0 ,R3 ; Change full port P0 as output port
13 RLC A ; A = 00100100, if first loop. In
; consecutive loops , A has values
15 ; according to RLC A
MOV P0.1 ,C ; Copy carry flag to outport port
P0.1
17 DJNZ R2, DELAY ; If R2 is not zero flow the loop
DELAY
RET ; Return the control to LCALL caller
19 THEEND: ; The end sub routine
END ; End of the program
✌
✆
2.1.23 LJMP
It specified to LONG JUMP. It is a three bytes long instruction. First byte
is opcode and next two bytes are address of location where jump occurs. The
maximum range of jump by this instruction is 0000H to FFFFH. Machine
Cycle (MC) used by this instruction is 2. It is used as
✞
LJMP <code address >
✌
✆
LJMP instructs to the program to jump unconditionally to the specified code
address.
2.1.24 MOV
It copies the contents into destination register directly, indirectly or imme-
diately. It also copies data from source register to destination register with
some restrictions. Machine Cycle (MC) used by this instruction is 1. Its
syntax is
✞
1 MOV <destination >,<source >
✌
✆
The destination may be a count number or hex number. For example
✞
1 MOV 32 ,#200
✌
✆
Here, memory location is specified in counting number. It stores value 200
in the at 32nd
register of RAM’s memory addresses. If second operand is a
58 Instructions
bit, then MOV instruction uses bit addressing of RAM register method to
store the data. For Example,
✞
1 MOV 05,P1.0
✌
✆
In above example, second operand is a bit hence MOV instruction copies the
D0 bit state of port 1 into the 05th
bit started from the 32nd
register of the
RAM or from 0×20 address of the RAM (See RAM section). Again in the
example given below:
✞
1 MOV 32H ,#200
✌
✆
Here memory location is specified in hex numbers. It stores value 200 in
the at 3 × 161
+ 2 × 160
= 50th
register of RAM’s memory addresses. The
MOV instruction uses direct addressing mode and register addressing mode
to address a register. The register bank locations are accessed by the register
names. For example
✞
1 MOV A,4 ; Direct addressing mode
MOV A,R4; Register addressing mode.
3 ; Both are same
✌
✆
Other method of use of MOV instructions are given below:
✞
1 MOV @Rn ,# data ;2 byte long instruction
MOV @Rn ,A ;1 byte long instruction
3 MOV @Rn ,<Internal RAM Address > ;2 byte long instruction
✌
✆
Here only registers R0 and R1 are pointer addressable. Therefore, @Rn
meant for @R0 and @R1.
✞
1 MOV A,# data ;2 byte long instruction
MOV A,@Rn ;1 byte long instruction
3 MOV A,Rn ;1 byte long instruction
MOV A,<Internal RAM Address > ;2 byte long instruction
5 MOV C,<bit Address > ;2 byte long instruction
MOV <bit address >,C ;2 byte long instruction
✌
✆
In above method of application of MOV instruction, accumulator can be
loaded either by direct addressing or by indirect addressing. Note that Carry
flag can be loaded with only bit value not with a byte value.
2.1. INSTRUCTION SETS 59
✞
MOV DPTR ,# data ;3 byte long instruction
✌
✆
Data pointer can handle two byte long data, hence while copying data to and
from DPTR, the source and destination shall of equal size.
✞
1 MOV Rn ,# data ;2 byte long instruction
MOV Rn ,A ;1 byte long instruction
3 MOV Rn ,<Internal RAM Address > ;2 byte long instruction
✌
✆
All registers can accept data from direct addressing method or they can the
stored data stored in the internal RAM location.
✞
1 MOV <Internal RAM Address >,#data ;3 byte long
instruction
MOV <Internal RAM Address >,@Rn ;2 byte long
instruction
3 MOV <Internal RAM Address >,Rn ;2 byte long
instruction
MOV <Internal RAM Address >,A ;2 byte long
instruction
5 MOV <Int RAM Addr >,<Int RAM Addr > ;2 byte long
instruction
✌
✆
MOV also copies data into internal ram address from registers, accumulators
and MOV can also copy data between internal RAM addresses. Source value
during the execution of this instruction does not change. No flags are affected
unless the instruction is moving the value of a bit into the carry bit in which
case the carry bit is affected or unless the instruction is moving a value into
the PSW register. Movement of data between registers by this instruction is
not allowed.
Illustrated Example See the following example, in which MOV instructino
is used to copy the data in accumulator and registers.
✞
1 MOV A ,#20H ; A = 00100000
CLR C ; CY = 0
3 ADDC A ,#20H; A+CY +20H = 01000000
MOV R0 ,A ; R0 = 01000000
5 SETB C ; CY = 1
ADDC A ,#20H; A+CY +20H = 01100001
7 MOV R1 ,A ; R1 = 01100001
60 Instructions
END ; End the program
✌
✆
2.1.25 MOVC
It is short name of MOVE CODE BYTE TO ACCUMULATOR. It is one
byte long instructio and can read internal code memory. Its syntax is
✞
MOVC <destination >, <source >
✌
✆
This instruction is used in the form as given below.
✞
1 MOVC A,@A+DPTR
MOVC A,@A+PC
✌
✆
MOVC moves a byte from Code Memory into the Accumulator. The Code
Memory address from which the byte will be moved is calculated by summing
the value of the Accumulator with either DPTR or the Program Counter
(PC). In the case of the Program Counter, PC is first incremented by 1
before being summed with the Accumulator.
✞
ORG 0
2 MOV DPTR ,# MYDATA
B1: CLR A
4 MOVC A,@A+DPTR
MOV P0,A
6 JZ EXIT
INC DPTR
8 SJMP B1
EXIT:
10 SJMP $
ORG 30H
12 MYDATA:
DB ’This is my world’ ,0
14 END
✌
✆
2.1.26 MOVX
It is MOVE DATA TO/FROM EXTERNAL MEMORY (XRAM). It is one
byte long instruction. Its syntax is
2.1. INSTRUCTION SETS 61
✞
MOVX <destination >,<source >
✌
✆
This instruction is used in the form as given below.
✞
1 MOVX @DPTR ,A
MOVX @R0 ,A
3 MOVX @R1 ,A
MOVX A,@DPTR
5 MOVX A,@R0
MOVX A,@R1
✌
✆
MOVX moves a byte to or from External Memory into or from the Accu-
mulator. If destination is @DPTR, the Accumulator is moved to the 16-bit
External Memory address indicated by DPTR. This instruction uses both P0
(port 0) and P2 (port 2) to output the 16-bit address and data. If source is
DPTR then the byte is moved from External Memory into the Accumulator.
If destination is @R0 or @R1, the Accumulator is moved to the 8-bit Ex-
ternal Memory address indicated by the specified Register. This instruction
uses only P0 (port 0) to output the 8-bit address and data. P2 (port 2) is
not affected. If source is @R0 or @R1 then the byte is moved from External
Memory into the Accumulator.
✞
XDATA EQU 2000H
2 XBYTES EQU 10
MOV DPTR ,# XDATA
4 MOV R2 ,# XBYTES
AGAIN:
6 MOVX A,@DPTR
MOV P1 ,A
8 INC DPTR
DJNZ R2,AGAIN
10 END
✌
✆
2.1.27 MUL
It is short form of MULTIPLY. One byte long instruction. It multiplies
accumulator with secondary accumulator. Machine Cycle (MC) used by this
instruction is 4. Its syntax is used as
62 Instructions
✞
MUL AB ; B source (times) and A destination (number)
2 ; AB = B x A ( mathematically)
; So , A is lower level.
✌
✆
It multiples the unsigned value of the Accumulator by the unsigned value
of the “B” register. The least significant byte of the result is placed in the
Accumulator and the most-significant-byte is placed in the “B” register. In
this operation, Carry flag is always reset to 0. The Overflow Flag is set if
the result is greater than 25, otherwise it is cleared.
Illustrated Example Assume two hex numbers 5FH and 10H whose byte
by byte product is 5FH × 10H = 5F0H. Accumulator stores lower byte
while secondary accumulator stores upper byte.
✞
1 MOV A,#5FH ; A = 01011111
MOV B ,#10H ; B = 00001010
3 MUL AB ; A = 11110000 & B = 00000101
END ; End of program
✌
✆
2.1.28 NOP
NOP, is acronym of NO OPERATION. It is one byte long instruction. NOP
is generally used only for timing purposes. Absolutely no flags or registers
are affected. Machine Cycle (MC) used by this instruction is 1. NOP is
an instruction that tells to MCU that there is nothing to execute. This
instruction is just skipped by the MCU. This operation is useful in delay
algorithm where each NOP delays the time equivalent to one clock cycle.
Illustrated Example In the following example, there are three preceding
and two succeeding NOP instruction to the MOV instruction. The preceding
instructions are skipped by the MCU and it takes three clock cycles to reach
MOV instruction. After entertaining to MOV instruction, MCU further waits
for two clock cycles before it reached to END instruction and execute it.
✞
NOP
2 NOP
NOP
4 MOV R3 ,#15H
NOP
2.1. INSTRUCTION SETS 63
6 NOP
END
✌
✆
2.1.29 ORG
ORG instruction is used to relocate the location of start of a program. It
sets the location counter to the value specified by the operand expression.
It is included in a program as first instruction. If no ORG instruction is
included before the first instruction or data type in the program, assembly
begins at location zero. Multiple ORG instructions can be included in a
single program.
✞
1 ORG 0000H ;Loading address
LJMP MAIN ;Jump to main program
3
ORG 0013H ;Timer 0 interrupt address
5 SETB P2.1 ;Set high to D1 pin of port P2
MOV R3 ,#10H ;Store R3 with hex 10H.
7 BACK: DJNZ R3 ,BACK;Loop upto 16 times
CLR P2.1 ;Clear the D2 pin of port P2
9 RETI ;Return from interrupt
11 ORG 0030H ;Main program location
MAIN:
13 MOV IE ,#84H ;HW based interrupt , EA high , EX1
HERE:SJMP HERE ;Return to BACK subroutine
15 END
✌
✆
2.1.30 ORL
ORL is short name of Bitwise OR. The OR output is binary 1 if either of
the two operands or both operands are binary 1 and binary 0 otherwise. It
is a binary operator and it operates on binary bit of the operands. See the
example below, in which two integer values 37 and 82 undergone binary OR
operation. First, these two decimal numbers are converted into their binary
equivalent and then bit-by-bit OR operation is performed. The binary result
is again converted into decimal equivalent. Here, the decimal equivalent of
binary result of OR operation on 37 and 82 is 119.
64 Instructions
✞
1 0010 0101 : X=37
0101 0010 : Y=82
3 ---------------- OR
0111 0111 : 119
✌
✆
This operator ORed the value at first register with the value at second register
and stores the result in first register.
✞
ORL <Internal RAM Address >,A ;2 byte long
instruction.
2 ORL <Internal RAM Address >,#data ;3 byte long
instruction.
ORL A,# data ;2 byte long
instruction.
4 ORL A,<Internal RAM Address > ;2 byte long
instruction.
ORL A,@R0 ;1 byte long
instruction.
6 ORL A,@R1 ;1 byte long
instruction.
ORL A,Rn ;1 byte long
instruction.
8 ORL C,<bit address > ;2 byte long
instruction.
✌
✆
See the example given below:
✞
;10H = 00010000
2 MOV A ,#10H ;Copy hex 10H into ACC
;11H = 00010001
4 MOV R1 ,#11H ;Copy hex 11H into R1
;10H = 00010000
6 ;11H = 00010001
;----------------OR
8 ;11H = 00010001
ORL A,R1 ;Get OR of 10H and 11H
✌
✆
2.1.31 POP
It is two bytes long instruction. Its syntax is
2.1. INSTRUCTION SETS 65
✞
1 POP <Internal RAM Address >
✌
✆
POP removes the last value placed on a stack and copied it into the internal
RAM address specified by its operand. Address of last value at stack pointer
is decreased by one after each POP.
✞
1 ;Begining of stack pointer register in RAM memory
MOV SP ,#31H
3 ;Store data into registers
MOV R2 ,#25H
5 MOV R1 ,#12H
MOV R4 ,#10H
7 ;Push data from register R2 into stack
;pointer addressed at 31H in internal RAM
9 PUSH 2 ; 25H
;Push data from register R1 into address
11 ;of next address of stack pointer
PUSH 1 ; 12H
13 ;Push data from register R4 into address
;of next address of stack pointer
15 PUSH 4 ; 10H
;Pop last value from stack pointer
17 ;into the addressed at 09H in RAM
;Here last value on stack is 10H
19 POP 09H
;Pop next last value from stack pointer
21 ;into the addressed at 10H in RAM
;Here next value in stack is 12H
23 POP 10H
;Pop next last value from stack pointer
25 ;into the addressed at 11H in RAM
;Here next value in stack is 25H
27 POP 11H
✌
✆
66 Instructions
x0 x1 x2 x3 x4 x5 x6 x7
00 00 12 25 10 00 00 00 00
08 00 10 00 00 00 00 00 00
10 12 25 00 00 00 00 00 00
18 00 00 00 00 00 00 00 00
20 00 00 01 00 00 00 00 00
28 00 00 00 00 00 00 00 00
30 00 00 25 12 10 00 00 00
... 00 00 00 00 00 00 00 00
Table 2.1: Memory result.
The output can be view in the RAM address started from 32H. Only direct
addressing mode is allowed for popping the stack. Note that while using
POP instruction, we should have knowledge of number of elements present
in stack pointer, otherwise POP starts fetching the value beyond the stake
pointer values.
✞
1 MOV SP ,#31H
MOV R2 ,#25H
3 MOV R1 ,#12H
MOV R4 ,#10H
5 PUSH 2 ; Value 25H
PUSH 1 ; Value 12H
7 PUSH 4 ; Value 10H
POP 10H ; Value 10H
9 POP 11H ; Value 12H
POP 12H ; Value 25H
11 POP 13H ; Fetches value from RAM address 0x31H
POP 14H ; Fetches value from RAM address 0x30H
13 POP 15H ; Fetches value from RAM address 0x2FH
END
✌
✆
2.1. INSTRUCTION SETS 67
2.1.32 PUSH
It is two byte long instruction. Its syntax is
✞
PUSH <Internal RAM Address >
✌
✆
PUSH “pushes” the value of the specified ‘Int. RAM Address’ onto the stack.
PUSH first increments the value of the Stack Pointer by 1, then takes the
value stored in ‘Int. RAM Address’ and stores it in Internal RAM at the
location pointed to by the incremented Stack Pointer.
✞
1 ;Begining of stack pointer
;register in RAM memory
3 MOV SP ,#31H
;Add data into registers
5 MOV R2 ,#25H
MOV R1 ,#12H
7 MOV R4 ,#10H
;Push data from register R2 into stack
9 ;pointer at address 31H in internal RAM
PUSH 2
11 ;Push data from register R1 into stack
;pointer at next address of stack pointer
13 PUSH 1
;Push data from register R4 into stack
15 ;pointer at next address of stack pointer
PUSH 4
✌
✆
The output can be view in the RAM address started from 32H. Only direct
addressing mode is allowed for pushing the stack.
68 Instructions
x0 x1 x2 x3 x4 x5 x6 x7
00 00 12 25 10 00 00 00 00
08 00 00 00 00 00 00 00 00
10 00 00 00 00 00 00 00 00
18 00 00 00 00 00 00 00 00
20 00 00 01 00 00 00 00 00
28 00 00 00 00 00 00 00 00
30 00 00 25 12 10 00 00 00
... 00 00 00 00 00 00 00 00
Table 2.2: Memory result.
2.1.33 RET
It is short form of RETURN FROM SUBROUTINE. It is two bytes instruc-
tion. RET is used to return from a subroutine previously called by LCALL
or ACALL. Program execution continues at the address that is calculated
by popping the topmost 2 bytes off the stack. The most-significant-byte is
popped off the stack first, followed by the least-significant-byte. Machine
Cycle (MC) used by this instruction is 2. It is the last instruction of a
sub-routine. It syntax is
✞
RET
✌
✆
Illustrated Example
✞
1 MOV A ,#12H ; A = 00010010
MOV R2 ,#05H ; R2= 00000101 Counter
3 LCALL DELAY ; Long Call to DELAY
JMP THEEND ; JUMP to THEEND
5 DELAY: ; DELAY sub routine
MOV R3 ,#00H ; Set R3 to 0
7 MOV P0,R3 ; Change full port P0 as output port
RLC A ; A = 00100100, if first loop. In -
9 ; corresponding loops A has values -
2.1. INSTRUCTION SETS 69
; according to RLC A
11 MOV P0.1 ,C ; Copy carry flag to outport port
P0.1
DJNZ R2, DELAY ; If R2 is not zero flow the loop
DELAY
13 RET ; Return the control to LCALL caller
THEEND: ; The end sub routine
15 END ; End of the program
✌
✆
2.1.34 RETI
It is acronym of RETURN FROM INTERRUPT. It is one byte long in-
struction. RETI is used to return from an interrupt service routine. RETI
first enables interrupts of equal and lower priorities to the interrupt that is
terminating. Program execution continues at the address that is calculated
by popping the topmost 2 bytes off the stack. The most-significant-byte
is popped off the stack first, followed by the least-significant-byte. RETI
functions identically to RET if it is executed outside of an interrupt service
routine. It s syntax is
✞
1 RETI
✌
✆
On execution of RETI, TCON.1 and TCON.3 bit are cleared, indicating that
the interrupt is finished and MCU is ready for another interrupt. During
the execution of interrupt subroutine, interrupt pins are ideally ignored, no
matter how many times and how long it makes high-to-low transitions.
Illustrated Example For interrupt of MCU via Interrupt Enable (IE) Spe-
cial Function Register (SFR), we use the roll over state of the Timer 0. On
roll over, TF0 flag is set to high. If interrupt flag (ET0) for Timer 0 (TF0)
is set high then on rollover, MCU assumed that it is interrupted for Timer
0. TF0 reset to low on call of interrupt. The MCU gets the address of ROM
location fixed for Timer 0 (TF0) interrupt. It is 0003H. Now MCU executes
the subroutine started from the ROM address 0×0003. On execution of RETI
instruction, it returns to the original address from where it was interrupted.
✞
1 ORG 0000H ;Loading address
LJMP MAIN ;Jump to main program
3
70 Instructions
ORG 000BH ;Timer 0 interrupt address
5 CPL P2.1 ;CPL to D1 pin of port P2
RETI ;Return from interrupt
7
ORG 0030H ;Main program location
9 MAIN:
MOV TMOD ,#02H ;Timer 1 mode 2
11 MOV P0 ,#0 FFH ;Make port P0 input port
MOV TH0 ,#90H ;Load TH of Timer 0 with 90H
13 ;+----------------------------------------------+
;| IE |
15 ;+-----+-----+-----+----+-----+-----+-----+-----+
;| EA | - | ET2 | ES | ET1 | EX1 | ET0 | EX0 |
17 ;+-----+-----+-----+----+-----+-----+-----+-----+
;| 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
19 ;+-----+-----+-----+----+-----+-----+-----+-----+
MOV IE ,#82H ;SW based interrupt , EA high ,
21 ;ET0 high enables interrupt
;via TF0 flag.
23 SETB TR0 ;Start Timer 0
25 BACK:
MOV A,P0 ;Copy input into ACC from P0
27 MOV P1,A ;Mov data of ACC into P1
SJMP BACK ;Return to BACK subroutine
29 END
✌
✆
There is another example in which external interrupt is applicable.
✞
1 ORG 0000H ;Loading address
LJMP MAIN ;Jump to main program
3
ORG 0013H ;Timer 0 interrupt address
5 SETB P2.1 ;Set high to D1 pin of port P2
MOV R3 ,#10H ;Store R3 with hex 10H.
7 BACK: DJNZ R3,BACK;Loop upto 16 times
CLR P2.1 ;Clear the D2 pin of port P2
9 RETI ;Return from interrupt
11 ORG 0030H ;Main program location
;+----------------------------------------------+
2.1. INSTRUCTION SETS 71
13 ;| IE |
;+-----+-----+-----+----+-----+-----+-----+-----+
15 ;| EA | - | ET2 | ES | ET1 | EX1 | ET0 | EX0 |
;+-----+-----+-----+----+-----+-----+-----+-----+
17 ;| 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 |
;+-----+-----+-----+----+-----+-----+-----+-----+
19 MAIN:
MOV IE ,#84H ;HW based interrupt , EA high ,
21 ;EX1 high enables interrupt
;via IE1 flag.
23 HERE:SJMP HERE ;Return to BACK subroutine
END
✌
✆
In above example, EA and EX1 flags of IE SRF are set high. EA allows
interrupts and EX1 allows the external interrupt via IE1 flag which is D3 bit
of TCON SRF. On interrupt via IE1 flag or TCON.3 flag, the MCU gets the
address of External Hardware Interrupt 1 (INT1), which is 0013H. On inter-
rupt, MCU starts execution from the address of 0×0013 and returns to the
location from where it was interrupted when RETI instruction is executed.
2.1.35 RL
It is short form of ROTATE ACCUMULATOR LEFT. It is one byte long
instruction. Syntax for this instruction is
✞
RL A
✌
✆
It shifts the bit of accumulator to left. The bit D7 bit is removed from its
place and postfixed after the bit D0.
Illustrated Example Consider an example as given below:
✞
1 MOV A ,#25H ; A = 0010 0101
RL A ; A = 0100 1010
3 RL A ; A = 1001 0100
END ; End of program
✌
✆
In above example, D7 bit is removed from its place and placed just after the
bit D0. The new byte thus formed is the result of RL instruction. If RL
instruction is performed by four times then four bit of LSB side and four bit
of MSB side are swapped (like SWAP instruction).
72 Instructions
✞
MOV A ,#25H ; A = 0010 0101
2 RL A ; A = 0100 1010
RL A ; A = 1001 0100
4 RL A ; A = 0010 1001
RL A ; A = 0101 0010 (swapped )
6 END ; End of program
✌
✆
2.1.36 RLC
It is short form of ROTATE ACCUMULATOR LEFT THROUGH CARRY.
It is one byte long instruction. Its syntax is
✞
RLC A
✌
✆
It shifts the bit of the Accumulator to the left. Most significant bit D0 is
loaded on Carry flag and original value of Carry flag is post-fixed after bit D0.
This function can be used to quickly multiply a byte by 2. This instruction
can also be used to get in or out the bytes from I/O ports or port pins
through carry bit taking MSB first. Assume initially, a register has values in
hex represented by bit D7 to D0 where D7 is MSB. These places are assumed
as original places of data bit and Carry. The carry bit is represented by CY .
D7 D6 D5 D4 D3 D2 D1 D0
CY
I/O
Pn
After the execution of instruction RLC for the register, all bit shift to
leftward. The CY bit acquires the original place of bit D0, D0 acquires the
original place of D1 and so on. Bit D7 acquires the place of CY. If we want
to put the MSB at output port. Instruction MOV is used as
✞
1 MOV Pn.X ,C
✌
✆
Here Pn is port either P0 or P2, X is any port bit of its 8 port bits. By this
way at output port, bit D7 is obtained.
2.1. INSTRUCTION SETS 73
D6 D5 D4 D3 D2 D1 D0 CY
D7
I/O
Pn D7
After the execution of instruction RLC for the register again, all bit shift
to leftward. The CY bit acquires the original place of bit D1, D1 acquires
the original place of D2, D7 acquires the original place of D0 and so on. Bit
D6 acquires the place of CY. If we want to put the MSB at output port.
Instruction MOV is used as
✞
1 MOV Pn.X ,C
✌
✆
By this way at output port, bit D6 is obtained. Now, two bit D7 and D6 are
obtained at output port in which MSB comes first.
D5 D4 D3 D2 D1 D0 CY D7
D6
I/O
Pn D7
D6
Next step is performed similarly and three consecutive bit are obtained
at output port in which MSB comes first.
D4 D3 D2 D1 D0 CY D6
D7
D5
I/O
Pn D7
D6
D5
Illustrated Example
✞
1 MOV A ,#25H ; A = 00100101, C=0
RLC A ; A = 01001010, C=0
3 RLC A ; A = 10010100, C=0
74 Instructions
RLC A ; A = 00101000, C=1
5 RLC A ; A = 01010001, C=0
RLC A ; A = 10100010, C=0
7 END ; End the program
✌
✆
2.1.37 RR
It is acronym of ROTATE ACCUMULATOR RIGHT. It is one byte long
instruction. Syntax of this instruction is
✞
1 RR A
✌
✆
It shifts the bit of the Accumulator to the right. The least significant bit D0
is removed from its place and prefixed before the most significant bit D7.
Illustrated Example Consider an example as given below:
✞
1 MOV A ,#25H ; A = 0010 0101
RR A ; A = 1001 0010
3 RR A ; A = 0100 1001
END
✌
✆
In above example, D0 bit is removed from its place and placed just before
the bit D7. The new byte thus formed is the result of RR instruction. If RR
instruction is performed by four times then four bit of LSB side and four bit
of MSB side are swapped (like SWAP instruction).
✞
MOV A ,#25H ; A = 0010 0101
2 RR A ; A = 1001 0010
RR A ; A = 0100 1001
4 RR A ; A = 1010 0100
RR A ; A = 0101 0010 (swapped )
6 END
✌
✆
2.1.38 RRC
It is short form of ROTATE ACCUMULATOR RIGHT THROUGH CARRY.
It is one byte long instruction. Its syntax is
✞
RRC A
✌
✆
2.1. INSTRUCTION SETS 75
It shifts the bit of the Accumulator to the left. Most significant bit D0 is
loaded on Carry flag and original value of Carry flag is prefixed after bit D0.
This function can be used to quickly multiply a byte by 2. This instruction
can also be used to get in or out the bytes from I/O ports or port pins
through carry bit taking LSB first. Assume initially, a register has values in
hex represented by bit D7 to D0 where D0 is LSB. These places are assumed
as original places of data bit and Carry. The carry bit is represented by CY .
D7 D6 D5 D4 D3 D2 D1 D0 CY
I/O
Pn
After the execution of instruction RRC, all bit shift to rightward. D0
acquires the original place of CY , CY acquires the original place of D7 and
so on. If we want to put the LSB at output port. Instruction MOV is used
as
✞
1 MOV Pn.X ,C
✌
✆
Here Pn represents to a port of specific number n ranging from zero to four.
For example, P0 is first port, P2 is third port etc. X is any port pin of its 8
port pins. By this way at output port, bit D0 is obtained.
D7 D6 D5 D4 D3 D2 D1 D0
I/O
Pn
CY
D0
After the execution of instruction RRC again, all bit shift to rightward.
The CY bit acquires the original place of bit D6, D6 acquires the original
place of D5, D0 acquires the original place of D7 and so on. Bit D1 acquires
the original place of CY . If we want to put the next LSB at output port.
Instruction MOV is used as
✞
1 MOV Pn.X ,C
✌
✆
76 Instructions
By this way at output port, bit D1 is obtained. Now, two bit D0 and D1 are
obtained at output port in which LSB comes first.
D7 D6 D5 D4 D3 D2 D1
I/O
Pn
D0 CY
D0
D1
By this way, we can get the other bit consecutively.
✞
1 MOV A ,#25H ; A = 00100101, C=0
RRC A ; A = 00010010, C=1
3 RRC A ; A = 10001001, C=0
RRC A ; A = 01000100, C=1
5 RRC A ; A = 10100010, C=0
RRC A ; A = 01010001, C=0
7 END ; End the program
✌
✆
2.1.39 SETB
It sets the carry bit or bit address. It is used as
✞
1 SETB C ;1 byte long instruction.
SETB <bit address > ;2 bytes long instruction.
✌
✆
Illustrated Example Here is an example in which Carry flag is first clear
and then it is set to high. The carry bit is sent to the first bit of port P0.
✞
MOV A ,#00H
2 CLR C
SETB C
4 MOV P0,A
SETB P0.1
6 END
✌
✆
2.1. INSTRUCTION SETS 77
2.1.40 SJMP
It is two bytes long instruction. First byte is opcode and second byte is ad-
dress. SJMP jumps unconditionally to the address specified relative address.
The maximum range of SHORT JUMP is from 00H to FFH. Relative address
must be within -128 (for backward jump) or +127 (for forward jump) bytes
of the instruction that follows the SJMP instruction. Machine Cycle (MC)
used by this instruction is 2. Syntax of this instruction is
✞
SJMP <relative address >
✌
✆
Compiler shows error as jump is out of range if ‘relative address’ is beyond
the range. The following example is self explanatory.
✞
1 MOV A ,#25 ; Accumulator
HERE: ; HERE subroutine
3 CPL P1.0 ; Toggle the 1st bit of P0 port
SJMP HERE ; Do it again and again
5 END ; End of program
✌
✆
‘$’ symbol is used for ‘stay here’ to the SJMP instruction, i.e. ‘$’ symbol tells
to MCU that the jump is to the current address. Above example is modified
to reflect the stay here phenomenon in the 8051MCU.
✞
1 MOV A ,#25 ; Accumulator
HERE: ; HERE subroutine
3 CPL P1.0 ; Toggle the 1st bit of P0 port
SJMP $ ; Stay here. No further execution
5 SJMP HERE ; Do it again and again
END ; End of program
✌
✆
2.1.41 SUBB
It is short form of SUBTRACT FROM ACCUMULATOR WITH BORROW.
Its syntax is
✞
SUBB <destination >,<source >
✌
✆
Its destination is always accumulator while source may be data, register or
Int. RAM Address. It is used as
78 Instructions
✞
1 SUBB A,# data ;2 byte long instruction.
SUBB A,<Internal RAM Address > ;2 byte long instruction.
3 SUBB A,@R0 ;1 byte long instruction.
SUBB A,@Rn ;1 byte long instruction.
✌
✆
It resets the Carry flag, Auxiliary Carry flag and Overflow flags accordingly
to reflect the result. If destination is lesser than the source value, where
values are represented in hex, it borrows the carry equal to 100H and sets
the carry flag high. In any step of the borrow subtraction, previous carry is
also subtracted in the current borrow subtraction.
Illustrated Example Assume two one bytes long hexadecimal numbers
62H and 96H, which are to be subtracted from each other. In first case, 96H
is being subtracted from 62H.
✞
62H = 01100010 (smaller )
2 -96H = 10010110 (larger)
- 0H = 0 (Previous Carry)
4 -------------------------------
✌
✆
Here 62H is smaller than 96H. So there is borrows a number 100H. Now the
subtraction is
✞
100H = 100000000
2 +62H = 01100010 (smaller )
------------------------------
4 162H = 101100010
-96H = 10010110 (larger)
6 - 0H = 0 (Previous Carry)
-------------------------------
8 CCH = 11001100
✌
✆
In Borrow subtraction, on state of borrow, carry flag is set. The carry set
in first borrow subtraction, it is subtracted in consecutive next subtraction.
Again to subtract 2045H from 2221H we use following steps.
✞
2221H
2 -2045H
✌
✆
This subtraction is performed for lower and higher bytes respectively. Ini-
tially, carry bit is reset to zero. Now subtraction of lower byte is
2.1. INSTRUCTION SETS 79
✞
100H = 100000000
2 +21H = 100001 (smaller )
------------------------------
4 121H = 100100001
-45H = 1000101 (larger)
6 - 0H = 0 (Previous Carry)
-------------------------------
8 DCH = 11011100
✌
✆
Now the carry is set to 1. Now the subtraction of higher byte is
✞
+22H = 00100010 (larger)
2 -20H = 00100000 (smaller )
- 1H = 1 (Previous Carry)
4 -------------------------------
01H = 00000001
✌
✆
Now the subtration result is 01DCH.
✞
1 2221H
-2145H
3 -------
01DCH
✌
✆
The equivalent assembly code for 8051 MCU is given below:
✞
CLR C
2 MOV A ,#21H
SUBB A ,#45H
4 MOV R7 ,A
MOV A ,#22H
6 SUBB A ,#20H
MOV R6 ,A
8 END
✌
✆
2.1.42 SWAP
It is short form of SWAP ACCUMULATOR NIBBLES. It is one byte long
instruction. It is used as
80 Instructions
✞
SWAP A
✌
✆
SWAP swaps bit D0 to D3 of the Accumulator with bit D4 to D7 of the
Accumulator. This instruction works only with Accumulator.
Illustrated Example
✞
1 MOV A ,#72H ;A: 0111 0010
SWAP A ;A: 0010 0111
✌
✆
This instruction is also equal to four times execution of RR or RL instruction.
Example with RL instruction is given below.
✞
MOV A ,#72H ;A: 0111 0010
2 RL A ;A: 1110 0100
RL A ;A: 1100 1001
4 RL A ;A: 1001 0011
RL A ;A: 0010 0111
✌
✆
Example with RR instruction is given below.
✞
1 MOV A ,#72H ;A: 0111 0010
RR A ;A: 0011 1001
3 RR A ;A: 1001 1100
RR A ;A: 0100 1110
5 RR A ;A: 0010 0111
✌
✆
2.1.43 XCH
It is acronym of EXCHANGE. It has syntax like
✞
1 XCH <destination >,<source >
✌
✆
It is used as
✞
1 XCH A,@R0 ;1 byte long instruction.
XCH A,@R1 ;1 byte long instruction.
3 XCH A,Rn ;1 byte long instruction.
XCH A,<Internal RAM Address > ;2 byte long instruction.
✌
✆
2.1. INSTRUCTION SETS 81
It exchanges the values between the accumulator and registers. This instruc-
tion is also used to exchange the values between the accumulator and the
RAM address indicated by the value stored in register R0 or R1. To use
register R0 and R1 as RAM addresses, they are prefixed by ‘@’ symbol as
@R0 and @R1.
Illustrated Example Assume that accumulator has value 25H and register
R0 has value 52H. On call of XCH, accumulator and register R0 exchange
their values mutually.
✞
MOV A ,#25H ; A = 0010 0101
2 MOV R0 ,#52H ; R0= 0101 0010
XCH A,R0 ; A = 0101 0010
4 ; R0= 0010 0101
END ; End of program
✌
✆
Another example is
✞
1 MOV A ,#24H ; A=24H = 0010 0100
MOV R0 ,#65H ; Store address value to R0
3 ; Initially value at RAM address
; 65H is decimal zero (1000 1110)
5 XCH A,@R0 ; Exchange four bit from LSB side.
; Now , A = 1000 1110 and value at
7 ; RAM Address 65H = 0010 0100
END ; End the program
✌
✆
In above example, hex value 24H is stored in accumulator. A auxiliary RAM
address is stored in register R0. The value at RAM address 0×65 (equal to the
value of R0) is 8E. On calling of instruction XCH, the values of accumulator
and RAM address 0×65 are exchanged mutually. Now the new values of
accumulator and RAM address 0×65 are 8EH and 24H respectively. @R0
in XCH represents that we have first retrieved the value from RAM address
equivalent to value of R0.
2.1.44 XCHD
It is short name of EXCHANGE BITS. It is one byte long instruction. Its
syntax is
✞
XCHD A,[@R0/@R1]
✌
✆
82 Instructions
And it is used as
✞
1 XCHD A,@R0
XCHD A,@R1
✌
✆
This instruction exchanges bit D0 to D3 of the Accumulator with bit D0 to
D3 of the Internal RAM Address pointed to indirectly by R0 or R1. Bits D4
to D7 of each register are unaffected.
✞
MOV A ,#24H ; A=24H = 0010 0100
2 MOV R0 ,#65H ; Assign RAM address value to R0
; Initially value at RAM Address
4 ; 0x65 is decimal zero (0000 0000)
XCHD A,@R0 ; Exchange four bit from LSB side.
6 ; Now , A = 0010 0000 and value at
; RAM Address 65H = 0000 0100
8 END ; End the program
✌
✆
In above example, hex value 24H is stored in accumulator. An auxiliary
RAM address (0×65) is stored in register R0. Value at this RAM address is
00H. Now, XCHD instruction is used to exchange the 4 bit from LSB side in
the values of accumulator and RAM address 0×65 (equal to the value stored
in register R0). Now the new values of accumulator and RAM address 0×65
are 20H and 04H respectively. @R0 in XCHD represents that we have first
retrieved the value from RAM address equivalent to value of R0.
2.1.45 XRL
It describes to BITWISE EXCLUSIVE OR. It gives the exclusive OR of two
given values. It is used as
✞
XRL <Internal RAM Address >,A ;2 byte long instruction.
2 XRL <Internal RAM Address >,#data ;3 byte long
instruction.
XRL A,# data ;2 byte long instruction.
4 XRL A,<Internal RAM Address > ;2 byte long instruction.
XRL A,@R0 ;1 byte long instruction.
6 XRL A,@R1 ;1 byte long instruction.
XRL A,Rn ;1 byte long instruction.
✌
✆
The result is stored in the accumulator.
2.1. INSTRUCTION SETS 83
✞
1 MOV A ,#25H ; A = 0010 0101
MOV R0 ,#52H ; R0= 0101 0010
3 XRL A,R0 ; A = 0111 0111
END ; End of program
✌
✆
84 µC Structure
Instruction M Cycle Remarks Instruction M Cycle Remarks
ACALL 2 MOV 1
ADD 1 MOV 2
ADDC 1 MOVC 2
AJMP 2 MOVX 2
ANL 1 MUL 4
CJNE 2 NOP 1
CJNE 2 ORL 1
CLR 1 ORL 2
CPL 1 POP 2
DA 1 PUSH 2
DEC 1 RET 2
DIV 4 RETI 2
DJNZ 2 RL 1
INC 1 RLC 1
JB 2 RR 1
JBC 2 RRC 1
JC 2 SETB 1
JMP 2 SJMP 2
JNB 2 SUBB 1
JNC 2 SWAP 1
JNZ 2 XCH 1
JZ 2 XCHD 1
LCALL 2 XRL 1
LJMP 2 XRL 2
Table 2.3: Table of Instruction Cycles
3.1. TIMER 85
3µC Structure
3.1 Timer
The 8051 MCU has two timers known as Timer 0 and Timer 1. Each of these
two timers is 16 bit long. The lower 8 bit of Timer 0 are called as TL0 and
higher 8 bit of Timer 0 are called TH0. Similarly, lower and higher 8 bit of
Timer 1 are called TL1 and TH1 respectively. In Timer 0, bit are represented
as
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
TH0
TL0
Similarly, in Timer 1, bit are represented as
D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0
TH1
TL1
Port 3 can be used as input or output port. As input, Port 3 is used
to handle serial communication, timer/counter and read write signal. The
specifications of Port 3 are given below:
86 µC Structure
P3 Function Pin No Description
P3.0 RxD 10 Receive data from serial communication.
P3.1 TxD 11 Transmit data to serial communication.
P3.2 INT0 12 External Interrupt for timer 0.
P3.3 INT1 13 External Interrupt for timer 1.
P3.4 T0 14 Externally pulse feed to timer 0.
P3.5 T1 15 Externally pulse feed to timer 1.
P3.6 WR 16 Write to external data memory.
P3.7 RD 17 Read from external data memory.
3.1.1 TMOD
Both the timer 0 and the timer 1 use the same register, called TMOD (Timer
Mode). TMOD is 8 bit register of which lower 4 bit are meant for Timer
0 and upper 4 bit are meant for Timer 1. TMOD bit are shown in figure
below.
GATE C/T M1 M0
Timer 1
MSB
GATE C/T M1 M0
Timer 0
LSB
Of these lower and upper 4 bit of TMOD, lower 2 bit are used to set the
timer mode and upper 2 bit are used for specification of the operation.
3.1. TIMER 87
M1 M0 Mode Operating Mode
0 0 0 13 bit timer mode operation. 8 bit for
TH and 5 bit prescalar for TL.
0 1 1 16 bit timer mode operation. TH and
TL are cascaded. No prescalar.
1 0 2 8 bit auto reload operation. TH holds a
value which reloaded into TL each time
when TL overflows from FFH to 00H.
1 1 3 Split time mode
Mode 2 is auto reload mode. It means when timer is roll over or inter-
rupted, TLn is automatically reloaded by the initial value of THn. In other
modes, timer is not reloaded automatically but reset to 00H. To load the
timer with initial values of THn and TLn, they are required to readdressed
by their initial values on interrupt or on roll over of timer. The following
instruction sets the Timer 1 in operation mode 2 while Timer 0 in operation
mode 0.
TMOD
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
0 0 1 0 0 0 0 0
In above example, the Timer 1, in which M1 is set high and M0 is set low.
Which is operation mode 2 of the Timer 1. Similarly, following instruction
on TMOD register
TMOD
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
0 0 1 0 0 0 0 1
✞
MOV TMOD ,#21H ; 21H = 0010 0001
✌
✆
88 µC Structure
selects the Timer 1 in operation mode 2 and Timer 0 in operation mode 1.
GATE is used to start or stop timer either by hardware mode or by software
mode. If GATE is set to 1, timer can be start or stopped by external source.
At the state of GATE set to 1, timer/counter is enabled only when the INT
pin is high and the TR control pin is set (TR). If GATE is set to 0, TR
instruction is used to start or stop the timer. To start Timer 0 or Timer
1, TR instruction is used as TR0 or TR1 respectively. If TR is set to high,
timer is started. If TR is cleared, timer is stopped.
✞
1 SETB TRx ;Start a Timer x
CLR TRx ;Stop the Timer x
✌
✆
Each timer has its own flag, known as TF. TF0 and TF1 for Timer 0 and
Timer 1 respectively. These flags are set to high when 16bit Timer are
overflows. Timers are used for time delay. C/T bit in TMOD used to get
the pulses from the outside of 8051 when C/T is set high. If C/T is zero,
then timer is used as timer and if C/T is one, then timer is used as counter.
Port pins P3.4 and P3.5 are used by Timer 0 and Timer 1 to get the pulses
from the external source clock. When GATE is high then we can interrupt
the timer externally. To start or stop the Timer 0 and Timer 1, pin P3.2 and
P3.3 are used respectively.
Mode 1 Example
✞
MOV TMOD ,#01H ; Timer 0 in mode 1 (16-bit)
2 MOV TL0 ,#0F2H ; TL0 = F2 Hex
MOV TH0 ,#0FFH ; TH0 = FF Hex
4 SETB TR0 ; Start the timer 0
AGAIN: ; AGAIN subroutine
6 JNB TF0 ,AGAIN ; Monitor timer flag 0
; until it rolls over
8 CPL P1.0 ; Toggle the 1st bit of P0 port
CLR TR0 ; Stop the timer 0
10 CLR TF0 ; Clear the timer 0 flag
END ; End of program
✌
✆
Mode 2 Example It is 8-bit timer. It allows only values from 00H to
FFH in the THx register. After the timer started, it started counting up by
incrementing the TLx register. When TLx register is rolls over FFH to 00H,
the timer flag is set to high. TLx is automatically reloaded with original value
3.1. TIMER 89
kept by the THx register. Observe this roll over and execute the subroutine.
✞
1 MOV TMOD ,#02H ; Timer 0 in mode 2
; (8-bit/auto reload)
3 MOV TH0 ,#0 FFH ; TH0 = FF Hex
SETB TR0 ; Start the timer 0
5 AGAIN: ; AGAIN subroutine
JNB TF0 ,AGAIN ; Monitor timer flag 0
7 ; until it rolls over
CPL P1.0 ; Toggle the 1st bit of P0 port
9 CLR TR0 ; Stop the timer 0
CLR TF0 ; Clear the timer 0 flag
11 END ; End of program
✌
✆
Timer as Delay
Timers are used to generate time delay. In 8051 MCU, XTAL is used to
generate oscillations (clock frequency). 12 oscillation called one machine
cycle. The frequency of XTAL is 11.0592 MHz. The time period of each
machine cycle is 1.085µs. Number of counts are measured in half of the
pulse hence entire period of time delay is measured by doubling the number
of counts.
Illustrated Example To understand the amount of time delay in the DE-
LAY subroutine generated by the timer, following example is given.
✞
1 MOV TMOD ,#01H ; Timer 0 in mode 1 (16-bit)
HERE: ; HERE subroutine
3 MOV TL0 ,#0 F2H ; TL0 = F2 Hex
MOV TH0 ,#0 FFH ; TH0 = FF Hex
5 CPL P1.0 ; Toggle the D0 bit of P0 port
ACALL DELAY ; Call the DELAY subroutine
7 SJMP HERE ; Do it again and again
DELAY: ; Delay subroutine
9 SETB TR0 ; Start the Timer 0 ;1
Machine Cycle
AGAIN: ; AGAIN subroutine
11 JNB TF0 ,AGAIN ; Monitor timer flag 0 ;14
Machine Cycles
; until it rolls over
90 µC Structure
13 CLR TR0 ; Stop the timer 0 ;1
Machine Cycles
CLR TF0 ; Clear the timer 0 flag ;1
Machine Cycles
15 RET ; Return to subroutine caller ;2
Machine Cycles
END ; End of program
✌
✆
In above example a time delay is generated by using DELAY subroutine.
In a subroutine execution, total machine cycles are 19. So, time delay is of
19 × 1.085 = 20.615s.
Illustrated Example Time delay is also used to generate a square wave of
required time period. In one complete time period cycle of a square wave,
wave remains low during the first half cycle and remains high during the
second half cycle or vice-versa. This is why during the generation of square
wave, we always computes the number of cycle required for either high state
or low state of the square wave. In this explanation, we have to compute
the time period of the square wave when initial value of Timer 1 is FFF2H.
For MCU 8051, the XTAL frequency is 11.0592 MHz and clock frequency of
timer is one twelfth of it. So, timer frequency is
11.0592MHz
12
= 921.6kHz
The clock period is
Tc =
1
921.6kHz
= 1.085µs
Now the number of count are FFFFH − FFF2H = 0DH. It is equal to 13
in decimal. One more count required for roll over from FFFFH to 0000H
to raised the TF flag. Hence total counts are 13 + 1 = 14. This gives the
time period for half pulse equal to
14 × 1.085µs = 15.19µs
For the entire period it is
T = 2 × 15.19µs = 30.38µs
as the time delay generated by the timer. See the below example which uses
Timer 1 for square wave generation.
3.1. TIMER 91
✞
MOV TMOD ,#10H ; Timer 1 in mode 1 (16-bit)
2 HERE: ; HERE subroutine
MOV TL1 ,#0 F2H ; TL1 = F2 Hex
4 MOV TH1 ,#0 FFH ; TH1 = FF Hex
CPL P1.0 ; Toggle the D0 bit of P0 port
6 SETB TR1 ; Start the Timer 1
AGAIN: ; AGAIN subroutine
8 JNB TF1 ,AGAIN ; Monitor timer flag 0 ;14 Machine
Cycles
; until it rolls over
10 CLR TR1 ; Stop the timer 0
CLR TF1 ; Clear the timer 0 flag
12 SJMP HERE ; Jump to HERE
END ; End of program
✌
✆
Illustrated Example Similarly, other operation mode of Timers can be
used to generate the time delay or square wave. But the time period or
time delay is restricts to the values of THn and TLn in the corresponding
operation mode. In operation mode 2 of a timer, the initial count value is
stored in TH and it is automatically copied into TL when timer is started.
Timer counting started next to the value current value of TL. When timer
is roll over, TL is reset to the value of TH. See the example below:
✞
1 MOV TMOD ,#20H ; Timer 1 in mode 2 (16-bit)
MOV TH1 ,#05H ; TH1 = 05 Hex. It auto reload
3 ; to TL1 when timer roll over
HERE: ; HERE subroutine
5 CPL P1.0 ; Toggle the 1st bit of P0 port
SETB TR1 ; Start the timer 1.
7 ; with value of TH1
AGAIN: ; AGAIN subroutine
9 JNB TF1 ,AGAIN ; Monitor timer flag 0
; until it rolls over
11 CLR TR1 ; Stop the timer 0
CLR TF1 ; Clear the timer 0 flag
13 SJMP HERE ; Jump to HERE , Timer Mode 2
; automatically reloaded.
15 END ; End of program
✌
✆
92 µC Structure
Illustrated Example Now a question rises that how we set the Timer values
initially to get the specific time delay. For example we want a time delay of
15.19µs, then what should be the initial values of the Timer 1 in operation
mode 1. To do this first we get the number of cycles to be performed by the
machine for time delay of 15.19µs.
n =
15.19
1.085
= 14
In operation mode 1, the final value of Timer 1 is FFFFH. So first we reduce
the number of cycles from the value of FFFFH in decimal form, which is
65535. So, 65535 − 14 = 65521. To roll over of the timer, we need one more
cycle so the final value is 65521 + 1 = 65522. 65522 is equivalent to FFF2H.
Now the initial timer values should be TLn=F2H and THn=FFH. The timer
delay example shall be
✞
1 MOV TMOD ,#10H ; Timer 1 in mode 1 (16-bit)
HERE: ; HERE subroutine
3 MOV TL1 ,#0 F2H ; TL1 = F2 Hex
MOV TH1 ,#0 FFH ; TH1 = FF Hex
5 CPL P1.0 ; Toggle the 1st bit of P0 port
SETB TR1 ; Start the timer 1
7 AGAIN: ; AGAIN subroutine
JNB TF1 ,AGAIN ; Monitor timer flag 0
9 ; until it rolls over
CLR TR1 ; Stop the timer 0
11 CLR TF1 ; Clear the timer 0 flag
SJMP HERE ; Jump to HERE
13 END ; End of program
✌
✆
Illustrated Example For time delay we use loops for delaying the execution
of instruction out of the loop. Delay of time is counted between start of timer
and stop of timer. In MCU instruction programs, there are some additional
instructions other than loops and start and stop of timer. These additional
instruction causes the extra time delay counted by the MCU. This is why
a proper care is required in time delay computation. This extra time delay
is called overhead time delay. We want to make a time delay of 10.85µs
between the loop and stopping of timer. See example below:
✞
1 MOV TMOD ,#01H ;2 Machine Cycle = 2.17 micro second
3.1. TIMER 93
MOV TH0 ,#0 FFH ;2 Machine Cycle = 2.17 micro second
3 MOV TL0 ,#0 F6H ;2 Machine Cycle = 2.17 micro second
SETB TR0 ;1 Machine Cycle = 1.085 micro second
5 BACK:
JNB TF0 ,BACK ;1 Machine Cycle = 1.085 micro second
7 ;total 10 Cycles =10 .85 micro second
CLR TR0 ;1 Machine Cycle = 1.085 micro second
9 END
✌
✆
If an extra instruction is added as shown below
✞
1 MOV TMOD ,#01H ;2 Machine Cycle = 2.17 micro second
MOV TH0 ,#0 FFH ;2 Machine Cycle = 2.17 micro second
3 MOV TL0 ,#0 F6H ;2 Machine Cycle = 2.17 micro second
SETB TR0 ;1 Machine Cycle = 1.085 micro second
5 BACK:
JNB TF0 ,BACK ;1 Machine Cycle = 1.085 micro second
7 ;total 10 Cycles =10 .85 micro second
MOV A,P0 ;1 Machine Cycle = 1.085 micro second
9 CLR TR0 ;1 Machine Cycle = 1.085 micro second
END ;
✌
✆
The time delay between the loop and stopping of timer is excess by one
machine cycle. This is called overhead time delay. In the instruction lines
✞
BACK:
2 JNB TF0 ,BACK ;1 Machine Cycle = 1.085 micro second
;total 10 Cycles = 10 .85 micro second
4 MOV A,P0 ;1 Machine Cycle = 1.085 micro second
CLR TR0 ;1 Machine Cycle = 1.085 micro second
✌
✆
One extra cycle is consumed by MOV instruction which is added in the time
delay before the timer is stopped. This extra time is called overhead time
delay. To compensate the overhead time delay to the loop time delay we
have to set initial values of timer such a way that excess overhead time delay
is compensated. Above example is rewritten as
✞
1 MOV TMOD ,#01H ;2 Machine Cycle = 2.17 micro second
MOV TH0 ,#0 FFH ;2 Machine Cycle = 2.17 micro second
3 ;-----------Reset the TL0 of Timer 0--------------;
MOV TL0 ,#0 F7H ;2 Machine Cycle = 2.17 micro second
94 µC Structure
5 SETB TR0 ;1 Machine Cycle = 1.085 micro second
BACK:
7 JNB TF0 ,BACK ;1 Machine Cycle = 1.085 micro second
;Total 10 Cycles = 10.85 micro second
9 MOV A,P0 ;1 Machine Cycle = 1.085 micro second
CLR TR0 ;1 Machine Cycle = 1.085 micro second
11 END
✌
✆
Timer as Counter
Timer can also be used as counters. It counts the events happening outside
the 8051. When it is used as counter, outside pulse increments the TH and
TL registers. The C/T bit in the TMOD register decides the source of the
clock. If C/T is 0 then the source of the clock is internal XTAL. If C/T is
1 then external source of clock is selected. The external source of pulse is
connected with P3.4 and P3.5 port pins. P3.4 pin is used for timer/counter
0 and P3.5 pin is used for timer/counter 1.
Solved Problem 3.1 A programmer wants to use Timer 1 in auto reload
mode. What should be the value for TMOD register?
Solution Upper 4 bit are used to control the Timer 1. M1 and M0 are
used to select the timer mode. Mode 2 of Timer 1 is used as auto reload
operation. So M1 of Timer 1 should be high and M0 of Timer 1 should be
low. So, the value of TMOD should be 20H.
TMOD
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
0 0 1 0 0 0 0 0
✞
1 MOV TMOD ,#20H ; 20H = 0010 0000
✌
✆
Solved Problem 3.2 A programmer wants to use Timer 0 in 16 bit operation.
What should be the value for TMOD register? If timer gets roll over in 10
machine cycles then find the initial values for TH and TL register of the
timer.
3.1. TIMER 95
Solution Lower 4 bit are used to control the Timer 0. M1 and M0 are
used to select the timer mode. Mode 1 of Timer 0 is used for 16 bit operation.
So M1 of Timer 0 should be low and M0 of Timer 0 should be high. So, the
value of TMOD should be 01H.
TMOD
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
0 0 0 0 0 0 0 1
✞
1 MOV TMOD ,#01H ; 20H = 0000 0001
✌
✆
To get rollover in 10 machine cycles, the TH0 and TL0 values should be FFH
and 06H respectively.
3.1.2 TCON
Timer Control (TCON) register is a 8 bits, bit-addressable register. Its upper
4 bit controls the TF (Timer Flag) and TR (Timer Run) bit of both timers
while lower 4 bit are used for controlling interrupts externally and interrupt
timers.
TF1 TR1 TF0 TR0
MSB
IE1 IT1 IE0 IT0
LSB
If GATE is 1, then start and stop of timers are done externally through
pins P3.2 and P3.3 for Timer 0 and Timer 1 respectively. TF1 and TF0 flags
are used to check whether a timer is rolled over or not. A timer is 16 bit
register having two 8 bit groups TL and TH. The range of timer (Timer 0 or
Timer 1) is 0000H to FFFFH. When timer registers is at FFFFH state then
at next counting it becomes 0000H. This is called roll over. If a timer is roll
over then it sets Timer Flag (TF0 for Timer 0 and TF1 for Timer 1) to high.
TR1 or TR0 is set high when Timer 1 or Timer 0 is started respectively. IE0
or IE1 is set to high when Timer 0 or Timer 1 is interrupted via hardware
(externally from P3.2 for Timer 0 and P3.3 for Timer 1).
96 µC Structure
3.2 Interrupt
Interrupt is an action taken by internally or externally where current pro-
cessing is stopped immediately and new process is started. An interrupt is
an external or internal event that interrupts the micro-controller to inform
that device needs its service. The interrupts must be enabled by software in
order for micro-controller to respond to them.
IE
EA - ET2 ES ET1 EX1 ET0 EX0
1 0 0 0 0 0 1 0
The instruction IE register is used for this purpose. IE register is bit
addressable. IE register has 8 bit with following meanings.
Bit Name IE Bit Description
EA IE.7 Enable/Disable all interrupts.
– IE.6 Reserved.
ET2 IE.5 Enables or disables timer 2 overflow interrupt.
ES IE.4 Enables or disables serial port interrupt.
ET1 IE.3 Enables or disables timer 1 overflow interrupt.
EX1 IE.2 Enables or disables external interrupt for T1.
ET0 IE.1 Enables or disables timer 0 overflow interrupt.
EX0 IE.0 Enables or disables external interrupt for T0.
In 8051, two pins P3.2 and P3.3 are designated as INT0 and INT1 and
used as external hardware interrupts of the Timer 0 and Timer 1 respectively.
3.2.1 External Interrupt
For each interrupt, there must be an Interrupt Service Routine (ISR) or
interrupt handler. On invoke of an interrupt, the micro-controller runs the
interrupt service routine. For every interrupt, there is a fixed location in
3.2. INTERRUPT 97
memory that holds the address of ISR. These memory location is called
Interrupt Vector Table. On activation of an interrupt, MCU jumps to a
fixed location in memory and upon execution of RETI instruction, it returns
to the place where it was interrupted. Go through the following example in
which external interrupt is used.
✞
1 ORG 0000H ;Loading address
LJMP MAIN ;Jump to main program
3
ORG 0013H ;Timer 0 interrupt address
5 SETB P2.1 ;Set high to D1 pin of port P2
MOV R3 ,#10H ;Store R3 with hex 10H.
7 BACK: DJNZ R3 ,BACK;Loop upto 16 times
CLR P2.1 ;Clear the D2 pin of port P2
9 RETI ;Return from interrupt
11 ORG 0030H ;Main program location
MAIN:
13 MOV IE ,#84H ;HW based interrupt , EA high , EX1
HERE:SJMP HERE ;Return to BACK subroutine
15 END
✌
✆
3.2.2 Interrupt Priority Register
By default the highest to lowest priorities of interrupt or internal polling
sequence of MCU is given in the table below.
Interrupt Priority Symbol
External Interrupt 0 INT0
Timer Interrupt 0 TF0
External Interrupt 1 INT1
Timer Interrupt 1 TF1
Serial Communication (RI + TI)
These default priorities are poled at the time of power on of 8051 MCU.
We can change the sequence of interrupt priority by assigning the higher
98 µC Structure
priority to any one of the interrupts by programming the Interrupt Priority
Register. To give a higher priority to an interrupt, its corresponding pin set
to high. The interrupt priority register has following bits.
Bit Name IP Bit Explanation
– IP.7 Reserved.
– IP.6 Reserved.
PT2 IP.5 Timer 2 interrupt priority.
PS IP.4 Serial port interrupt priority.
PT1 IP.3 Tmer 1 interrupt priority.
PX1 IP.2 External interrupt 1 priority.
PT0 IP.1 Timer 0 interrupt priority.
PX0 IP.0 External interrupt 0 priority.
When two or more interrupts are activated at a time, then they are ser-
viced according to their default priority.
3.2.3 Interrupt Vector Table
There are six interrupts, (i) one interrupt for reset, (ii) two interrupts for
timer, (iii) two external interrupts and (iv) one serial communication inter-
rupts. On interrupts, MCU jumps to the fixed memory locations of ROM.
The group of all memory locations fixed for interrupts are called Interrupt
Vector Table. These memory locations are also called Interrupt Service Rou-
tine (ISR). The interrupt vector table is given below:
3.2. INTERRUPT 99
Interrupt ROM Memory (Hex) Pin
Reset 0000H Pin 9
External HW (INT0) 0003H P3.2
Timer 0 (TF0) 000BH
External HW (INT1) 0013H P3.3
Timer 1 (TF1) 001BH
Serial COM (RI & TI) 0023H
On reset MCU jumps to the fixed memory location specified for the in-
terrupt. It starts execution of the Interrupt Service Subroutine until it is
not instructed to return from interrupt by instruction RETI. After return-
ing from the interrupt, MCU starts execution from the place where it was
interrupted.
✞
1 ORG 0000H ;Loading address
LJMP MAIN ;Jump to main program
3
ORG 000BH ;Timer 0 interrupt address
5 CPL P2.1 ;CPL to D1 pin of port P2
RETI ;Return from interrupt
7
ORG 0030H ;Main program location
9 MAIN:
MOV TMOD ,#02H ;Timer 1 mode 2
11 MOV P0 ,#0 FFH ;Make port P0 input port
MOV TH0 ,#90H ;Load TH of Timer 0 with 90H
13 MOV IE ,#82H ;SW based interrupt , EA high
SETB TR0 ;Start Timer 0
15
BACK:
17 MOV A,P0 ;Copy input into ACC from P0
MOV P1 ,A ;Mov data of ACC into P1
19 SJMP BACK ;Return to BACK subroutine
END
✌
✆
On reset interrupt or power on of the MCU, all the values in the RAM and
100 µC Structure
register are lost. On power up, the default values of MCU registers are as
given below:
Register Value
PC 0000
DPTR 0000
ACC 00
PSW 00
SP 07
B 00
P0-P3 FF
To activate a reset input to be effective, it must be pushed at least two
duration of machine cycles.
3.3 Communication
To communicate between PC & Micro controller, DB-9 version of serial I/O
standards are used. It has nine pins.
3.3. COMMUNICATION 101
Pin Description
1 Data Carrier Detect (DCD)
2 Received Data (RxD)
3 Transmitted Data (TxD)
4 Data Terminal Ready (DTR)
5 Signal Ground (GND)
6 Data Set Ready (DSR)
7 Request To Sent (RTS)
8 Clear To SenT (CTS)
9 Ring Indicator (RI)
We required minimum 3 pins. TxD, RxD and ground. Description of
other pins are:
DTR When terminal is turned on, it sends out signal DTR to indicate
that it is ready for communication.
DSR When DCE is turned on and has gone through the self-test, it assert
DSR to indicate that it is ready to communicate RTS (request to send)
RTS When the DTE device has byte to transmit, it assert RTS to signal
the modem that it has a byte of data to transmit CTS (clear to send)
CTS When the modem has room for storing the data it is to receive, it
sends out signal CTS to DTE to indicate that it can receive the data now
DCD The modem asserts signal DCD to inform the DTE that a valid
carrier has been detected and that contact between it and the other modem
is established RI (ring indicator).
RI An output from the modem and an input to a PC indicates that the
telephone is ringing. It goes on and off in synchronous with the ringing sound
102 µC Structure
3.3.1 Baud Rate
Baud rate is bits per second transmitted during communication. 8051 MCU
UART is 32. The machine cycle frequency of MCU is 921.6 kHz. If TMOD is
set to low, timer frequency is given by 921.6kHz ÷ 32 = 28,800. If TMOD is
set to high, timer frequency is given by 921.6kHz ÷ 16 = 57,600. By default
TMOD is low. To set the baud rate of MCU communication, 28,800 or 57,600
is divided by an integer. If THn is set to 3 then baud rate shall be 28,800
÷ 3 = 9600 bits per second and so on. Baud rate is required to construct
a delay sub routine as a bit in serial communication requires a specific time
for its transmission. For example, if baud rate is 100K, i.e. 100K bits per
second then one bit will last in 1/100K second or in 10µs (bit time). The
delay loop of 10µs is setup and transmission line is setup for the different
bits for this duration of delay.
3.3.2 SBUF
SBUF is acronym of Serial Data Buffer. It is an 8-bit register, solely used for
serial communication. To transfer a byte data via the TxD line, it must be
placed in the SBUF register. When the data is placed in the SBUF register,
it is framed with start and stop bit and transferred serially via TxD line.
SBUF also receives the one byte data via RxD line. At the moment of data
received via RxD, data is deframed by eliminating stop and start bit and is
placed in SBUF register. By using MOV instruction, data can be directly
transfer into the SBUF or data can transfer mutually between accumulator
and SBUF.
✞
MOV SBUF ,#’D’
2 MOV A,SBUF
MOV SBUF ,A
✌
✆
Illustrated Example In following example one byte data equivalent to
letter ‘A’ is transmitted serially via TxD line. The timer mode is set for
mode 2 operation. Timer is started. Serial Communication register is set
for 8 bit, 1 stop and REN enabled transfer mode. In serial control (SCON),
the data moves in or moves out through only SBUF register. This is why
in the loop sub-routine data ‘A’ is first copied into the SBUF register and
then it is transferred serially. Wait until data is not completely transferred
serially and TI is not set to high. When TI is set to high data transmission
3.3. COMMUNICATION 103
completed. Clear TI so that TI flag is ready for next data transmission loop.
Reach again at the beginning of loop for transferring of the data serially.
✞
1 MOV TMOD ,#20H ; Timer 0 in mode 2
; (8-bit/auto reload)
3 MOV TH1 ,#6 ; TH0 = FF Hex
MOV SCON ,#50 ; 8-bit , 1 stop , REN enable
5 SETB TR1 ; Start the timer
AGAIN: ; AGAIN subroutine
7 MOV SBUF ,#’A’ ; Transmit letter A
HERE:
9 JNB TI ,HERE ; Monitor for transfer interrupt
CLR TI ; Clear the transfer interrupt
11 SJMP AGAIN ; Do it again
✌
✆
In following example a byte data equivalent to letter ‘FFH’ is received serially
via RxD line.
✞
1 MOV TMOD ,#20H ; Timer 0 in mode 2
; (8-bit/auto reload)
3 MOV TH1 ,#6 ; TH0 = FF Hex
MOV SCON ,#50 ; 8-bit , 1 stop , REN enable
5 SETB TR1 ; Start the timer
HERE:
7 JNB RI ,HERE ; Monitor for receive interrupt
MOV A,SBUF ; Copy SBUF data to accumulator
9 MOV P1 ,A ; Send accumulator data to port P1
CLR RI ; Clear the receive interrupt
11 SJMP HERE ; Do it again
✌
✆
3.3.3 SCON
SCON is acronym of Serial Control. It is 8-bit register. It is used in data
framing that consists the start bit, stop bit and data bits.
SM0 SM1 SM2 REN TB8 RB8 TI RI
MSB LSB
104 µC Structure
The pin description of SCON register are given in the following table.
Pin Description
SM0 or SCON.7 Serial port mode specifier.
SM1 or SCON.6 Serial port mode specifier.
SM2 or SCON.5 Used for multiprocessor communication.
REN or SCON.4 Set/cleared by software to enable/disable reception.
TB8 or SCON.3 Not used.
RB8 or SCON.2 Not used.
TI or SCON.1 Transmit interrupt flag.
RI or SCON.0 Receive interrupt flag.
SM0, SM1 These bit selects the framing/encapsulation of data by spec-
ifying the number of bit per character and the start and the stop bit if
required. There are four types of data framing which are identified by Mode
0 to Mode 4 operations.
SM0 SM1 Mode
0 0 Serial Mode 0
0 1 Serial mode 1, 8-bit data, 1 stop bit, 1
start bit
1 0 Serial mode 2
1 1 Serial mode 3
SM2 It enables the multiprocessing capabilities of 8051.
REN SCON is a bit addressable register. Therefore, the REN pin can
be assessed by SCON.4 address. When it is high, it allows to receive data on
RxD pin.
3.3. COMMUNICATION 105
TI When 8051 finishes the transfer of 8-bit character, TI becomes high,
it indicates that it is ready to transfer another byte. TI bit is raised at the
beginning of the stop bit. By monitoring TI flag, we can make sure that
SBUF is not overloading. If we write another byte into SBUF before TI
raised to high, the un-transmitted portion of the previous byte will be lost.
Before loading SBUF with new data byte, TI must be reset to 0.
RI When 8051 receives data serially via RxD, it strips off start and stop
bit and places the byte in SBUF register. It raised the RI flag bit to high
and indicates that a byte has been received and should be picked up before
it is lost. RI is raised halfway through the stop bit.
Illustrated Example The minimum structure for serial transmission is re-
quired the following lines of coding. We need proper initialization of TMOD,
SCON registers. SBUF register is used to load input as it is the register
through which data is either transmitted or received.
✞
1 MOV TMOD ,#20H ; Timer 1, mode 2 (auto reload)
MOV TH1 ,#3 ; 9600 Baud rate
3 MOV SCON ,#50H ; 8-bit , 1 stop , REN enabled
SETB TR1 ; Start Timer 1
5 AGAIN:
MOV SBUF ,#’Y’ ; Load SBUF with letter Y
7 HERE:
JNB TI ,HERE ; Wait for last byte
9 CLR TI ; Clear Transfer Interrupt
SJMP AGAIN ; Do again
11 END ; End program
✌
✆
Following is a program which transmits the word NOT serially. This program
is slightly modified form of above program. TMOD and SCON registers are
initialized as shown in above example. SBUF register is used to transfer the
bytes from Accumulator to output port.
✞
1 MOV TMOD ,#20H ; Timer 1, mode 2 (auto reload)
MOV TH1 ,#3 ; 9600 Baud rate
3 ;+-----------------------------------------------+
;| SCON |
5 ;+-----+-----+-----+-----+-----+-----+-----+-----+
;| SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI |
7 ;+-----+-----+-----+-----+-----+-----+-----+-----+
106 µC Structure
;| 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 |
9 ;+-----+-----+-----+-----+-----+-----+-----+-----+
MOV SCON ,#50H ; 8-bit , 1 stop , REN enabled
11 SETB TR1 ; start timer 1
AGAIN:
13 MOV A,#’N’ ; Transfer N
ACALL TRANS
15 MOV A,#’O’ ; Transfer O
ACALL TRANS
17 MOV A,#’T’ ; Transfer T
ACALL TRANS
19 SJMP AGAIN
TRANS:
21 MOV SBUF ,A ; Load SBUF
HERE:
23 JNB TI ,HERE ; Wait for last byte
CLR TI ; Get ready for next byte
25 RET
END
✌
✆
Here, SBUF data is reloaded into Accumulator. After complete receiving of
data from SBUF register, Receive Interrupt (RI) is cleared and the program
is ready for receiving of new byte.
✞
MOV TMOD ,#20H ; Timer 1, mode 2 (auto reload)
2 MOV TH1 ,#6 ; 9600 Baud rate
MOV SCON ,#51H ; 8-bit , 1 stop , REN enabled
4 SETB TR1 ; Start Timer 1
AGAIN:
6 JNB RI,AGAIN; Wait untill whole byte
; not received.
8 MOV A,SBUF ; Load Accumulator with SBUF value
CLR RI ; Clear Receive Interrupt
10 SJMP AGAIN ; Do again
END ; End program
✌
✆
3.3.4 PCON
It is acronym of Power Control. It is used to change the baud rate of the
MCU. It is a 8-bit register. When 8051 is powered up, SMOD is zero. We can
3.3. COMMUNICATION 107
set it to high by software and thereby double the baud rate. SMOD1 uses
57600Hz and SMOD0 uses 28800Hz. The PCON register is shown below.
SMOD1SMOD0 – POF GF1 GF0 PD IDL
When PCON.0 sets high, MCU goes under idle condition. PCON is not
a bit addressable register, hence a manipulation is required for it. First we
copied the PCON into A (Accumulator) and then its bit values are altered
by using SETB instruction as
✞
1 SETB ACC.7
✌
✆
After that, the accumulator value is copied into PCON register. The com-
plete routine is
✞
1 MOV A,PCON
SETB ACC.7
3 MOV PCON ,A
✌
✆
By this way the baud rate is doubled. Following is an example for transmit-
ting NOT serially in double baud rate.
✞
1 MOV A,PCON
SETB ACC.7
3 MOV PCON ,A
MOV TMOD ,#20H ; Timer 1, mode 2 (auto reload)
5 MOV TH1 ,#6 ; 9600 Baud rate
MOV SCON ,#51H ; 8-bit , 1 stop , REN enabled
7 SETB TR1 ; Start Timer 1
AGAIN:
9 JNB RI ,AGAIN; Wait untill whole byte
; not received.
11 MOV A,SBUF ; Load Accumulator with SBUF value
MOV P1 ,A ; Send accumulator into P1
13 CLR RI ; Clear RI
SJMP AGAIN ; Do again
15 END ; End program
✌
✆
In idle mode, The ALE and PSEN output are held high. Last instruction
executed before going into the Idle mode. The internal CPU clock is gated
108 µC Structure
off. Interrupt, Timer, and Serial Port functions act normally. All of registers,
ports and internal RAM maintain their data during Idle. An instruction that
sets PCON.1 causes power down mode. Last instruction executed before
going into the power down mode. The on-chip oscillator is stopped. All
functions are stopped, the contents of the on-chip RAM and Special Function
Registers are maintained. The ALE and PSEN output are held low. The
reset that terminates the Power Down.
3.4 Assembly Directives
Directives in assembly language provide the way to assign a number or string
to a variable for further use. Classifications and their use is explained in the
following sections.
3.4.1 Symbol Definition
The assembler automatically assigns values to variables that appear as in-
struction labels. This value is the current setting of the location counter
when the instruction is assembled. Symbols and assigning a value to them
by using EQU directive.
✞
1 <symbol name > EQU <value or expression >
✌
✆
The example of EQU definition is given below:
✞
1 AGE EQU 10
MOV P0 ,# AGE
3 MOV P1 ,# AGE
END
✌
✆
3.4.2 Data Definition
The DB (Define Byte) and DW (Define Word) directive enable you to define
data to be stored in your program. Data can be in the form of 8-bit or 16-bit
values or in a string of text characters. The synopses of data definition is
✞
<symbol name > DB <string expression >
✌
✆
3.5. ASSEMBLY IN C 109
DB directive should be at different location to avoid the shortage of registers
for program’s internal process. See the example below:
✞
1 ORG 0000H
<program codes >
3 ORG 200H ;Different location of data
MYDATA: DB "This is my name" ,0
✌
✆
Here, number 0 at the end of data definition ‘MYDATA’ represents the end
of string. Through DPTR pointer, number 0 is copied into accumulator
and by JZ instruction we exit from the loop which is reading data bytes of
‘MYDATA’. In case of numerical data definition, this method of termination
of data is not suitable if the data bytes are numbers as 0 itself is a number.
In that case datasize is computed by the following method.
✞
ORG 0
2 MOV DPTR ,# MYDATA ; Get Address of MYDATA
MOV R6 , DPL ; Copy address to R6
4 MOV DPTR ,# MYDATAEND ; Get Address of MYDATAEND
MOV A,DPL ; Copy address to Accumulator
6 SUBB A,R6 ; Get size of data
MOV R7 ,A ; Copy data size into Register R7
8 ; Value is 06 in this example
MYDATA: ; ROM data
10 DB 10,5,0,8,5,8
MYDATAEND: ; End mark of ROM data
12 END
✌
✆
Note that here, data is one byte long. Hence data size of user data is 06. If
it is two bytes long, then the number of times data being read is half of the
total data size and in each iteration, two bytes shall be read cumulatively.
3.5 Assembly in C
Here we use Small Devices C Compiler (SDCC) for compilation of the C
assembly program for 8051. The Internal registers instruction is included
from the header file <8051.h>. A simple program that writes the character
codes ranging from 0 to 255 is given below.
✞
#include <8051.h>
110 µC Structure
2
void main(void) {
4 unsigned char z; /* Declaration of character
variable .*/
P1 = 0x00; /* Reset the port P1 to low */
6 for (z = 0; z <= 255; z++) /* For loop */
P1 = z; /* Put the char code in port P1.*/
8 }
✌
✆
During the further discussion, it is assumed that the reader are well aware
of C programming. The data type in SDCC are int and char either signed
or unsigned. By default, all the data types are signed.
✞
#include <8051.h>
2
void main(void) {
4 unsigned char z[]="0123456789abcdefghij";
unsigned int i;
6 for(i=0;i <20;i++){
P1=z[i];
8 }
}
✌
✆
Data Type Size Range
unsigned char 8-bit 0 to 255
signed char 8-bit -128 to +127
unsigned int 16-bit 0 to 65535
signed int 16-bit -32768 to 32767
sbit 1-bit SFR bit addressable only
bit 1-bit RAM bit addressable only
sfr 8-bit RAM address 80H to FFH only
Table 3.1: Data type in SDCC for 8051 MCU.
3.5. ASSEMBLY IN C 111
We only discuss that how the internal architecture of the 8051 is to be ac-
cessed via a C program. The following program is for infinite loop.
✞
1 #include <8051.h>
3 void main(void) {
P1 = 0x00;
5 for (;;) { /* Infinite loop. */
P1 = 0xFF;
7 P1 = 0x00;
}
9 }
✌
✆
Arrays can be used as shown in the example below.
✞
1 #include <8051.h>
3 void main(void) {
unsigned char myArr[] = "abcd";
5 unsigned char i;
for (i = 0; i < 4; i++) {
7 P1 = myArr[i];
}
9 }
✌
✆
Following is an example for integer datatype.
✞
1 #include <8051.h>
3 void main(void) {
unsigned int msg[] = {0x62 ,0 x5b};
5 unsigned char z;
while (1){
7 for (z = 0; z < 2; z++) {
P0 = msg[z];
9 P1 = z;
}
11 }
}
✌
✆
In SDCC, bytes register and bit registers are directly accessed by their name.
Byte registers are assigned by their true name. Bit registers are assigned by
112 µC Structure
their byte name and bit position joined with ‘ ’ symbol (underscore). For
example all bit of port P0 is accessed as
✞
P0 = 0xAF; /* Assigned 0xAF to port P0.*/
✌
✆
✞
1 #include <8051.h>
3 void main(void) {
for (;;) {
5 P0 = 0xAF;
P0 = 0x00;
7 }
}
✌
✆
While a single bit of port pin is assessed as
✞
P0_1 /* For port bit 1 of port 0.*/
2 P0_2 /* For port bit 2 of port 0.*/
P1_1 /* For port bit 1 of port 1.*/
✌
✆
To access the first port bit of port P0, following method is used.
✞
1 #include <8051.h>
3 void main(void) {
unsigned char z;
5 for (z = 0; z < 255; z++) {
P2_0 = 1;
7 P2_0 = 0;
}
9 }
✌
✆
Similarly, other bit registers which are given specific name are accessed by
their name rather that bit value. In following example, Carry flags is accessed
by its name CY rather than its bit value “PSW.7”.
✞
1 #include <8051.h>
3 void main(void) {
for (;;) {
5 CY = 1;
CY = 0;
3.5. ASSEMBLY IN C 113
7 }
}
✌
✆
The byte register name and bit register name are listed in the header file
<8051.h>. Nearly, all the C like operations, i.e. if, if-else or if-else-if, switch-
case, for loop, while loop, increments & decrements, array, arithmetic oper-
ations, binary operations and bit shifts etc can be performed in 8051 with
SDCC.
✞
#include <8051.h>
2
void main(void) {
4 unsigned int i;
while (1) {
6 if (i > 3) {
P0 = 0x50;
8 break;
} else {
10 P0 = 0x01;
}
12 i++;
}
14 }
✌
✆
Example of bit shift
✞
#include <8051.h>
2
void main(void) {
4 unsigned int i;
P1=0x01;
6 while (1) {
if (i > 3) {
8 P0 = 0x50;
break;
10 } else {
P1 = P1 << 1;
12 }
i++;
14 }
}
✌
✆
114 µC Structure
In following example, port P1 is reset to store given data A1H. The bit at
port P1 are shifted leftward. The MSB bit of data at Port P1 is serially
accepted and sent to the LSB bit of port P0 at first.
✞
1 #include <8051.h>
3 void main(void) {
unsigned int i;
5 P0 = 0x00;
P1 = 0xA1;
7 for (i = 0; i < 8; i++) {
P0_0 = P1_7;
9 P1 = P1 << 1;
}
11 }
✌
✆
In following example, array arithmetic is executed to get the sum of three
numbers.
✞
1 #include <8051.h>
3 void main(void) {
unsigned int i;
5 unsigned char sum=0;
unsigned char data[]={0x10 , 0x20 , 0x30};
7 for (i = 0; i < 3; i++) {
sum = sum+data[i];
9 }
P0=sum;
11 }
✌
✆
After execution of program, we get the 60H value as result at port P0. Re-
member that in 8051 C assembly, in the divisions of decimal numbers, quo-
tient is always an integer. i.e. division does not takes place upto decimal
place. This property is used in the conversion of numbers from one form to
another form.
✞
1 #include <8051.h>
3 void main(void) {
unsigned char x,by ,d1 ,d2,d3;
3.5. ASSEMBLY IN C 115
5 by=0xAD; /* 0xAD = 173d*/
x=by /10; /* 173/10 = 17d*/
7 d1=by %10;/* 137%10 = 3d*/
d2=x%10; /* 17%10 = 7d*/
9 d3=x/10; /* 17/10 = 1d*/
P0=d1; /* 3d in unit place*/
11 P1=d2; /* 7d in tenth place*/
P2=d3; /* 1d in hundredth place*/
13 }
✌
✆
If we want to catch the interrupt raised by timer, then we should use the
following syntax:
✞
1 void Timer <n>_ISR(void) __interrupt (<IE bit number for
Tn >){
<statements >
3 }
✌
✆
For example, for Time 0 interrupt, above syntax becomes
✞
1 void Timer0_ISR(void) __interrupt (1){
<statements >
3 }
✌
✆
Here, ‘1’ inside parentheses is bit number of IE register. To catch the in-
terrupt arises from Timer 0 flag, we use ‘1’. To catch the interrupt arises
from Timer 1 flag, we use ‘3’. Similarly, we can catch other interrupts. This
function is used outside the main part of the C program. The syntax for
serial interrupt is given below:
✞
1 void Serial0_ISR(void) __interrupt (4){
<statements >
3 }
✌
✆
An example using ISR is given below:
✞
1 #include <8051.h>
3 void Timer0_ISR(void) __interrupt (1){
if(P1_1==0)
5 P1_1=1;
else
116 µC Structure
7 P1_1=0;
}
9
void main(void) {
11 TMOD=0x02;
TH0=0XA4;
13 IE=0 x82;
TR0 =1;
15 while(1){
P0 = P1;
17 }
}
✌
✆
8051 C Compiler allocates RAM location for Register Bank 0 from address
of 0 to 7. Individual variables are stored from address 08 and beyond. Array
element are stored right after the variables’ address. Stack addresses are
right after array elements. In the following example, we shall try to get the
code space locations stored in the RAM.
✞
#include <8051.h>
2
void main(void) {
4 unsigned char mydata [100];
unsigned char x, z = 0;
6 for (x = 0; x < 100; x++) {
z--;
8 mydata[x] = z;
P1 = z;
10 }
}
✌
✆
Simulate the above program and see the result. See another example
✞
1 #include <8051.h>
3 void main(void) {
unsigned char mydata[] = "ABCD";
5 unsigned char x, z = 0;
for (x = 0; x < 4; x++) {
7 P1 = mydata[x];
}
3.5. ASSEMBLY IN C 117
9 }
✌
✆
After successfull execution of simulation, the RAM data is looked like
x0 x1 x2 x3 x4 x5 x6 x7
00 00 0B 00 00 00 00 00 04
08 41 42 43 44 00 00 00 00
10 00 00 00 00 00 00 00 00
... 00 00 00 00 00 00 00 00
Table 3.2: RAM memory result.
3.5.1 Byte Register
Some registers in 8051 are byte addressable via SDCC. The names of byte
addressable registers are P0, P1, P2, P3, SP, DPL, DPH, PCON, TCON,
TMOD, TL0, TL1, TH0, TH1, SCON, SBUF, IE, IP, Process State Word
(PSW), ACC and B.
3.5.2 Bit Register
Some registers in 8051 are bit addressable via SDCC. These bit are P0 0,
P0 1, P0 2, P0 3, P0 4, P0 5, P0 6, P0 7, P1 0, P1 1, P1 2, P1 3, P1 4,
P1 5, P1 6, P1 7, P2 0, P2 1, P2 2, P2 3, P2 4, P2 5, P2 6, P2 7, P3 0,
P3 1, P3 2, P3 3, P3 4, P3 5, P3 6, P3 7, ACC, IT0, IE0, IT1, IE1, TR0,
TF0, TR1, TF1, RI, TI, RB8, TB8, REN, SM2, SM1, SM0, EX0, ET0, EX1,
ET1, ES, EA, RXD, TXD, INT0, INT1, T0, T1, WR, RD, PX0, PT0, PX1,
PT1, PS, P, F1, OV, RS0, RS1, F0, AC and CY. In SDCC, we can access a
register bit-by-bit using sbit operator. This operator is used as
✞
1 __sbit __at (<bit address >) <bit name >;
✌
✆
For example, to access the ACC.0 address bit we use
✞
1 __sbit __at (0xE0) ACC_0;
✌
✆
Now ACC 0 can modify to D0 bit of the accumulator. See example below:
118 µC Structure
✞
1 #include <8051.h>
__sbit __at(0xE0) ACC_0;
3
void main(void) {
5 int i;
while (1) {
7 for (i = 0; i < 1; i++);
ACC_0 = 1;
9 for (i = 0; i < 1; i++);
ACC_0 = 0;
11 }
}
✌
✆
Solved Problem 3.3 Write a C program for 8051 to transfer the letter “Z
serially at 4800 baud continuously. Use 8-bit data and 1 stop bit.
Solution This problem is solved by setting the timer, baud rate and
creating a loop for continuous transmission of symbol ‘Z’. The C program is
given below:
✞
#include <8051.h>
2
void main(void) {
4 //+---------------------+ +-----------------------+
//| Timer 1 | | Timer 0 |
6 //+-----+-----+----+----+ +------+-----+-----+----+
//|TMOD | C/T | M1 | M0 | | TMOD | C/T | M1 | M0 |
8 //+-----+-----+----+----+ +------+-----+-----+----+
//| 0 | 0 | 1 | 0 | | 0 | 0 | 0 | 0 |
10 //+-----+-----+----+----+ +------+-----+-----+----+
TMOD = 0x20;
12 TH1 = 0xFA;
14 //+---------------------------------------------+
//| SCON |
16 //+-----+-----+-----+-----+-----+-----+----+----+
//| SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI |
18 //+-----+-----+-----+-----+-----+-----+----+----+
//| 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
3.5. ASSEMBLY IN C 119
20 //+-----+-----+-----+-----+-----+-----+----+----+
SCON = 0x50;
22 TR1 = 1;
while (1) {
24 SBUF = ’Z’;
while (TI == 0);
26 TI = 0;
}
28 }
✌
✆
Solved Problem 3.4 Write an 8051 C program to transfer the message “NO”
serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously.
Solution Here we use the Timer 1 for time count. “No” is made of two
characters “N” and “O”. We use while loop to transmit these two characters
continuously. A character is transmit serially in binary form. First it is stored
in SBUF register and then transmits. Initially Transfer Interrupt (TI) flag
is set to low. In each transmit of the character we wait until the whole byte
is not transmitted. When a character is transmitted completely, i.e. at the
end of 8th
bit, TI flag is set to high.
✞
#include <8051.h>
2
void SerTx(unsigned char);
4
void main(void) {
6 //+---------------------+ +-----------------------+
//| Timer 1 | | Timer 0 |
8 //+-----+-----+----+----+ +------+-----+-----+----+
//|TMOD | C/T | M1 | M0 | | TMOD | C/T | M1 | M0 |
10 //+-----+-----+----+----+ +------+-----+-----+----+
//| 0 | 0 | 1 | 0 | | 0 | 0 | 0 | 0 |
12 //+-----+-----+----+----+ +------+-----+-----+----+
14 TMOD = 0x20;
TH1 = 0xFD;
16 //+---------------------------------------------+
//| SCON |
18 //+-----+-----+-----+-----+-----+-----+----+----+
//| SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI |
120 µC Structure
20 //+-----+-----+-----+-----+-----+-----+----+----+
//| 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
22 //+-----+-----+-----+-----+-----+-----+----+----+
24 SCON = 0x50;
TR1 = 1;
26 while (1) {
SerTx(’N’);
28 SerTx(’O’);
}
30 }
32 void SerTx(unsigned char x) {
SBUF = x;
34 while (TI == 0);
TI = 0;
36 }
✌
✆
Solved Problem 3.5 Write a C program for 8051 MCU to toggle D1 bit of
port P2 after each 250µs. Use Mode 1 of Timer 1 for this purpose.
Solution The number of cycles required to be wait for time delay of 250µs
is
N =
250µs
1.085µs
= 230
The timer should move 230 cycles to becomes FFFFH (65535 in decimal).
The timer require one more cycle to get roll over, i.e. time value should have
65536 in decimal. Now, the initial values of Timer 1 should be 65536-230 =
65306 in decimal or FF1AH. Now the TH1 is FFH and TL1 is 1AH. When
timer is roll over, it sets TF1 high. We read the TF1 flag to check the time
delay of 250µs. When the bit is roll over, toggle the D1 bit of port P2.
✞
#include <8051.h>
2
void main(void) {
4 //+---------------------+ +-----------------------+
//| Timer 1 | | Timer 0 |
6 //+-----+-----+----+----+ +------+-----+----+-----+
//|TMOD | C/T | M1 | M0 | | TMOD | C/T | M1 | M0 |
8 //+-----+-----+----+----+ +------+-----+----+-----+
3.5. ASSEMBLY IN C 121
//| 0 | 0 | 1 | 0 | | 0 | 0 | 0 | 0 |
10 //+-----+-----+----+----+ +------+-----+----+-----+
TMOD = 0x20; // Timer 1 Mode 1
12
//+---------------------+ +----------------------+
14 //| TH1 | | TH1 |
//+---------------------+ +----------------------+
16 //| 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 |
//+---------------------+ +----------------------+
18 TH1 = 0xFF; // Timer 1, TH1 values
20 //+---------------------+ +----------------------+
//| TL1 | | TL1 |
22 //+---------------------+ +----------------------+
//| 0 | 0 | 0 | 1 | | 1 | 0 | 1 | 0 |
24 //+---------------------+ +----------------------+
TL1 = 0x1A; // Timer 1, TL1 values
26 // start the timer
TR1 = 1;
28 while (1) { // infinite loop
if (TF1 == 1) { // catch the roll over
30 if (P2_1 == 1) { //if P0_1 is high
P2_1 = 0; // set it low
32 } else { // otherwise
P2_1 = 1; // set it high
34 }
TR1 = 0; // stop the timer
36 TF1 = 0; // clear rollover flag
TH1 = 0xFF; // set initial TH1
38 TL1 = 0x1A; // set initial TL1
TR1 = 1; // start the timer
40 }
}
42 }
✌
✆
Above programm shall toggle D1 bit of port P2 after each 250µs.
122 Numerical Problems
3.6 Appendix
3.6.1 Hex File
Hex file is the file which is loaded on MCU by the loader. Hex file provides
the following information
✞
:CC AAAA TT DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD SS
✌
✆
In hex file, each line is started with colon (:). It is followed by CC which
tells how many data bytes to be loaded. Next, AAAA are the address of
location where data of the current line is to be loaded by loaders. TT tells
that whether there is a next line following to the current line or not. If TT
is 00 then there is next line to be loaded and if TT is 01 then the current
line is last one. Next 16 bytes are data bytes in hex form. SS is check-sum
byte of whatever is there in the current line.
3.7 Glossary
Masking If an interrupt or device is disabled either by software or by
hardware, then it is said masking. A masked device or masked interrupt shall
never be entertained by the micro controller.
3.7. GLOSSARY 123
4Numerical Problems
Solved Problem 4.1 During execution of program by 8051MCU, its carry
and parity flags are set to high. What is the value of Process Status Word?
Solution PSW is a bit addressable register. The Process Status Word
(PSW) has 6 conditional flags. The position of these conditional flags and
their corresponding bit positions are shown in the following table.
D7 D6 D5 D4 D3 D2 D1 D0
CY AC RS1 RS0 OV P
When carry and parity flags are set high the binary value of PSW register
becomes 10000001.
CY AC RS1 RS0 OV P
D7 D6 D5 D4 D3 D2 D1 D0
1 0 0 0 0 0 0 1
This binary is equivalent to decimal 129 and also equivalent to hexa-
decimal 81H. Thus the value of PSW is 81H.
Solved Problem 4.2 Set the value of timer register (TMOD) so that T1 timer
works in Mode 0 operation.
Solution Here the programming problem is to set the timer register so
that its T1 timer works in mode 0 operation. The TMOD register bit are
represented like
GATE C/T M1 M0
Timer 1
MSB
GATE C/T M1 M0
Timer 0
LSB
124 Numerical Problems
A Mode 0 operation is a 13 bit timer mode in which 8 bit are reserved
for TH and 5 bit are prescalar for TL. For Mode 0 operation of timer T1, its
M1 and M0 bit of should be binary zeros. Now, the value of TMOD is
TMOD
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
0 0 0 0 0 0 0 0
Solved Problem 4.3 Set the value of timer register (TMOD) so that timer
T1 works in 16 bit operation.
Solution Here the programming problem is to set the timer register so
that its T1 timer works in 16 bit operations. The TMOD register bit are
represented like
GATE C/T M1 M0
Timer 1
MSB
GATE C/T M1 M0
Timer 0
LSB
A Mode 1 operation is a 16 bit timer operation in which TH and TL are
cascaded without prescalar. For Mode 1 operation of timer T1, its M1 bit
should be binary zero and M0 bit should be binary 1. Now, the value of
TMOD is
TMOD
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
0 0 0 1 0 0 0 0
The TMOD should be assigned value
✞
1 MOV TMOD ,#10H ; 10H = 0001 0000B
✌
✆
Solved Problem 4.4 How will you configure SCON register if you want to
operate Mode 1 communication and Transfer Interrupts?
3.7. GLOSSARY 125
Solution The SCON register has following bit which are independently
addressable.
SM0 SM1 SM2 REN TB8 RB8 TI RI
MSB LSB
SM0 and SM1 bit are used to determine the data framing, i.e. the data
communication mode. For Mode 1 communication, SM0 and SM1 bit should
be 0 and 1 respectively. The transfer interrupt is enabled by setting TI bit
high. Now the actual binary value of SCON register is 01000010, i.e. 42H or
66 in decimal.
✞
1 MOV SCON ,#42H ; 8-bit , 1 stop , REN enabled
END ; End program
✌
✆
Solved Problem 4.5 Set the value of timer register (TMOD) so that timer
T0 works in 8 bit auto reload operation mode.
Solution Here the programming problem is to set the timer register so
that its T0 timer works in mode 2 operation, i.e. 8 bit auto reload operations.
The TMOD register bit are represented like
GATE C/T M1 M0
Timer 1
MSB
GATE C/T M1 M0
Timer 0
LSB
A Mode 2 operation is a 8 bit auto reload operation. It means, the TH of
the timer T0 hold an initial value and on timer run, TL of the timer T0 is
counted upward. When TL is overflown or rolls-over, initial value of TH is
copied into the TL as its initial value just after rollover.
TMOD
Timer 1 Timer 0
GATE C/T M1 M0 GATE C/T M1 M0
0 0 0 0 0 0 1 0
126 Numerical Problems
✞
MOV TMOD ,#02H ; 10H = 0000 0010B
✌
✆
Solved Problem 4.6 Retrieve the numbers stored in ROM and add them
cumulatively. Also show the result at Register R1.
Solution In this problem, we first store the data in ROM. A size module is
written to get the actual size of data. Size of data is obtained by subtracting
the offsets addresses of start and end of data. Now a loop B1 is declared.
This loop iterates the same number of times as the data size is. In each
iteration, data is retrieved from ROM and copied into accumulator. This
retrieved data is added to the previously stored data at register R1 and then
again sum result is copied into R1. When all data is cumulatively summed,
the iteration loop is exits.
✞
1 ORG 0
;----Size module begin ----------
3 MOV DPTR ,# MYDATA ;Get Address of MYDATA
MOV R6, DPL ;Copy address to R6
5 MOV DPTR ,# MYDATAEND ;Get Address of MYDATAEND
MOV A,DPL ;Copy address to Accumulator
7 SUBB A,R6 ;Get size of data
MOV R7,A ;Copy data size into Register R7
9 ;It is size of data and acts as
counter
;----Size module end ----------
11 MOV DPTR ,# MYDATA ;Get Address of MYDATA
MOV R1 ,0 ;Reset the value of R1 to 0
13 B1:
CLR A ;Clear accumulator
15 MOVC A,@A+DPTR ;Get ROM data to Accumulator
ADD A,R1 ;Add R1 value to accumulator
17 MOV R1,A ;Copy accumulator value to R1
INC DPTR ;Increase data pointer
19 DJNZ R7 ,B1 ;Jump to B1 if R7 is not zero
;after decrements by one
21 ORG 30H ;New location of RAM
MYDATA: ;ROM data
23 DB 10,5,12,8,5,8
MYDATAEND:
3.7. GLOSSARY 127
25 EXIT:
END
✌
✆
Solved Problem 4.7 Write a program that may able to transmit 8 bit data
at output port P0 and transmit it with LSB comes first at this output port.
Solution To solve this problem, we have stored an specific value in accu-
mulator, i.e. register A. The bit of accumulator data are to be transmitted
serially at output port P0. The loop times is controlled by value stored in
register R1. In each loop, value of accumulator is rotated rightward through
carry (RRC). The value at carry is moved (MOV) to D1 bit of port P0. The
value of R1 is decremented and then jumped (DJNZ) to next iteration or
loop until R1 is not become zero.
✞
MOV A ,#00H
2 MOV P0 ,A ;Reset port
MOV A ,#72H ;Data to be transmitted serially
4 MOV R1 ,#8
HERE:
6 RLC A ;Rotate accumulator right through carry
MOV P0.1 ,C ;Transmit data to D1 bit of P0 port
8 DJNZ R1,HERE;Jump to HERE if R1 is not zero after
;decrements by one
10 END
✌
✆
Solved Problem 4.8 Write a program that may able to transmit 8 bit data
at output port P0 and transmit it with MSB comes first at this output port.
Solution To solve this problem, we have stored an specific value in accu-
mulator, i.e. register A. The bit of accumulator data are to be transmitted
serially at output port P0. The loop times is controlled by value stored in
register R1. In each loop, value of accumulator is rotated leftward through
carry (RLC). The value at carry is moved (MOV) to D1 bit of port P0. The
value of R1 is decremented and then jumped (DJNZ) to next iteration or
loop until R1 is not become zero.
✞
MOV A ,#00H
2 MOV P0 ,A ;Reset port
MOV A ,#72H ;Data to be transmitted serially
128 Numerical Problems
4 MOV R1 ,#8
HERE:
6 RLC A ;Rotate accumulator left through carry
MOV P0.1 ,C ;Transmit data to D1 bit of P0 port
8 DJNZ R1 ,HERE;Jump to HERE if R1 is not zero after
;decrements by one
10 END
✌
✆
Solved Problem 4.9 Write a program that may able to receive 8 bit input
data at port P2 and transmit it with LSB comes first at any of the output
port.
Solution To solve this problem, we have accepts an specific value from
port P0 and stored it in accumulator. The bit of accumulator data are to
be transmitted serially at output port P2. The loop times is controlled by
value stored in register R1. In each loop, value of accumulator is rotated
rightward through carry (RRC). The value at carry is moved (MOV) to D1
bit of port P2. The value of R1 is decremented and then jumped (DJNZ) to
next iteration or loop until R1 is not become zero.
✞
MOV A ,#00H
2 MOV P2,A ;Reset port
MOV A,P0 ;Acquire data from port P0
4 MOV R1 ,#8
HERE:
6 RRC A ;Rotate accumulator right through carry
MOV P2.1 ,C ;Transmit data to D1 bit of P2 port
8 DJNZ R1 ,HERE;Jump to HERE if R1 is not zero after
;decrements by one
10 END
✌
✆
Solved Problem 4.10 Write a program that may able to receive 8 bit input
data at port P2 and transmit it with MSB comes first at any of the output
port.
Solution To solve this problem, we have accepts an specific value from
port P0 and stored it in accumulator. The bits of accumulator data are to
be transmitted serially at output port P2. The loop times is controlled by
value stored in register R1. In each loop, value of accumulator is rotated
3.7. GLOSSARY 129
leftward through carry (RRC). The value at carry is moved (MOV) to D1
bit of port P2. The value of R1 is decremented and then jumped (DJNZ) to
next iteration or loop until R1 is not become zero.
✞
MOV A ,#00H
2 MOV P2 ,A ;Reset port
MOV A,P0 ;Acquire data from port P0
4 MOV R1 ,#8
HERE:
6 RLC A ;Rotate accumulator left through carry
MOV P2.1 ,C ;Transmit data to D1 bit of P2 port
8 DJNZ R1,HERE;Jump to HERE if R1 is not zero after
;decrements by one
10 END
✌
✆
Solved Problem 4.11 We want the output ‘47H’ (in BCD) in the accumulator
by pressing two keys ‘4’ and ‘7’ from keyboard. Write the program for the
same.
Solution The hex number 47H is made of two digits ‘4’ and ‘7’. The
numeric number ‘4’ pressed from key board of the computer is represented
by char code 52 (decimal) or 34 (hex) in computer language. Similarly,
numeric number ‘7’ is represented by char code 55 (decimal) or 37 (hex).
First store the key data in accumulator and register.
✞
MOV A,#’4’ ;key 4 = ASCII 52 = 34H = 0011 0100
2 MOV R0 ,#’7’ ;key 7 = ASCII 55 = 37H = 0011 0111
✌
✆
We need only lower nibbles of the two numbers to make 47H. So we have to
mask the upper nibbles.
✞
ANL A ,#0FH ; 0011 0100 AND 0000 1111 = 0000 0100
2 ANL R0 ,#0FH ; 0011 0111 AND 0000 1111 = 0000 0111
✌
✆
Remember that ANL can not be performed with registers other than accu-
mulator or carry. So we have to add additional steps to move data from
accumulator to register and vice versa. Here, only indicative steps are used.
See complete code given at the last of the solution. Now the binary 0000
0100 should make the upper nibble of 47H. So we swap the lower and upper
nibbles with each other.
130 Numerical Problems
✞
SWAP A ; 0000 0100 = 0100 0000
✌
✆
Now add the accumulator with R0 to store 47H in accumulator.
✞
1 ORL A,R0 ; 0100 0000 OR 0000 0111 = 0100 0111
✌
✆
The whole program is written as
✞
1 MOV A,#’4’
MOV R0 ,#’7’
3 ANL A,#0FH
MOV R1,A
5 MOV A,R0
ANL A,#0FH
7 MOV R0,A
MOV A,R1
9 SWAP A
ORL A,R0
11 END
✌
✆
Solved Problem 4.12 Write a C program for 8051 MCU that may able to
send a message to LCD through port pin P1.
Solution Solution of this problem is explained in below C program. Here,
a string i.e. a message is declared as an array. Read the message byte by
byte and transfer it into the port P1.
✞
1 #include <8051.h >
3 void main(void) {
unsigned char msg[] = "The Earth";
5 unsigned char z;
for (z = 0; z < 9; z++) {
7 P1 = msg[z];
}
9 }
✌
✆
Solved Problem 4.13 Write a C program for 8051 MCU to toggle D1 digit
of the port P0 upto any specific times of loop.
3.7. GLOSSARY 131
Solution In this program, a for loop is used to toggle the D1 digit of
the port P0 upto specific times. As the pin is toggling between binary 0 to
binary 1, therefore we have used a condition inside the for loop to check the
current value of D1 digit of the port P0 and toggle it to other level.
✞
1 #include <8051.h>
3 void main(void) {
unsigned int j;
5 for(j=0;j <1085;j++){ // Toggle 1085 times
if(P0_1==1) //if P0_1 pin is high
7 P0_1=0x00; // set pin to low
else // otherwise
9 P0_1=0x01; // set pin to high
}
11 }
✌
✆
Solved Problem 4.14 Write a C program for 8051 MCU to toggle D1 digit
of the port P2 high when there is an odd number and low when there is an
even number.
Solution In this program, a for loop is used to toggle the D1 digit of the
port P2 high when there is an odd number and toggle to low when there is
an even number. The remainder rule is used to check the number whether it
is odd or whether it is even and according to the type of number, we toggle
the D1 digit of port P2.
✞
1 #include <8051.h>
3 void main(void) {
int i = 0;
5 while (i < 100) {
if (i % 2 == 0) {
7 P2_1 = 0;
} else {
9 P2_1 = 1;
}
11 i++;
}
13 }
✌
✆
132 Numerical Problems
Solved Problem 4.15 Write a C program for 8051 MCU to toggle D1 digit
of the port P2 low when there is a prime number and high when there is non
prime number. Also show the number at port P1.
Solution The complete C program for this problem is given below. This
program is self explanatory. A number is a prime number if its factors are
either itself or one only. For first 100 numbers, we first checked whether the
number is a prime number or not. After that, if number is prime number
then D1 digit of port P2 is toggled low. Similarly, if number is not a prime
number then D1 digit of port P2 is toggled to high. The number is also
transferred to port P1 for its display.
✞
1 #include <8051.h>
int isprime (int);
3 void main(void) {
int i = 3;
5 while (i < 100) {
if (isprime (i)) {
7 P2_1 = 0;
} else {
9 P2_1 = 1;
}
11 P1 = i;
i++;
13 }
}
15 int isprime (int i) {
int j = 0;
17 int k = 0;
19 if (i < 3) {
return 0;
21 }
if (i == 3) {
23 return 1;
}
25 if (i % 2 == 0) {
return 0;
27 } else {
int l = (i / 2) + 1;
3.7. GLOSSARY 133
29 for (j = 3; j <= l; j += 2) {
if (i % j == 0) {
31 return 0;
} else {
33 k = 1;
}
35 }
}
37 return k;
}
✌
✆

More Related Content

PDF
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
PDF
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
PDF
c programming
PDF
Notes of 8051 Micro Controller for BCA, MCA, MSC (CS), MSC (IT) & AMIE IEI- b...
PDF
Assembly Codes in C Programmes - A Short Notes by Arun Umrao
PDF
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 1 of 5 by...
PDF
Modlica an introduction by Arun Umrao
PDF
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...
Notes of 8085 micro processor Programming for BCA, MCA, MSC (CS), MSC (IT) &...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
c programming
Notes of 8051 Micro Controller for BCA, MCA, MSC (CS), MSC (IT) & AMIE IEI- b...
Assembly Codes in C Programmes - A Short Notes by Arun Umrao
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 1 of 5 by...
Modlica an introduction by Arun Umrao
Think Like Scilab and Become a Numerical Programming Expert- Notes for Beginn...

What's hot (17)

PDF
Advanced cardiovascular exercites
PDF
Maxima CAS an introduction
PDF
Basic ForTran Programming - for Beginners - An Introduction by Arun Umrao
PDF
Java Programming Notes for Beginners by Arun Umrao
PDF
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
PDF
Notes and Description for Xcos Scilab Block Simulation with Suitable Examples...
PDF
The C Preprocessor
PDF
Tinyos programming
PDF
Electrónica digital: Logicsim
PDF
Thats How We C
PDF
Hats guia de_macro
PDF
Advanced macro guide
PDF
Circuitikzmanual
PDF
Reading Materials for Operational Research
PDF
An Introduction to Computational Networks and the Computational Network Toolk...
PDF
Circuitikzmanual (1)
PDF
Cac he dong luc hoc rat hay
Advanced cardiovascular exercites
Maxima CAS an introduction
Basic ForTran Programming - for Beginners - An Introduction by Arun Umrao
Java Programming Notes for Beginners by Arun Umrao
Notes for GNU Octave - Numerical Programming - for Students 01 of 02 by Arun ...
Notes and Description for Xcos Scilab Block Simulation with Suitable Examples...
The C Preprocessor
Tinyos programming
Electrónica digital: Logicsim
Thats How We C
Hats guia de_macro
Advanced macro guide
Circuitikzmanual
Reading Materials for Operational Research
An Introduction to Computational Networks and the Computational Network Toolk...
Circuitikzmanual (1)
Cac he dong luc hoc rat hay
Ad

Similar to 8051 micro controller (20)

PDF
Fanuc pmc programming manual
PDF
Manual completo ARM
PDF
Mc68 hc908jl3e
PDF
MFR4310.pdf
PDF
Dm00046982
PDF
Manual programação stm 32 f4
PDF
8S003F3_STMicroelectronics.pdf
PDF
ST7MC1XXcomponente eletronico de boa qualidade
PDF
En.dm00024550 stm8 s003f3p6
PDF
En.cd00161566
PDF
Stm32 f103c8t6
PDF
PDF
PDF
Discovering st32
PDF
Stm32 reference manual
PDF
27911 gyroscope appnote2
PDF
Bucher lift control guide
PDF
Modbus protocol reference guide
PDF
SPI Concepts.pdf
Fanuc pmc programming manual
Manual completo ARM
Mc68 hc908jl3e
MFR4310.pdf
Dm00046982
Manual programação stm 32 f4
8S003F3_STMicroelectronics.pdf
ST7MC1XXcomponente eletronico de boa qualidade
En.dm00024550 stm8 s003f3p6
En.cd00161566
Stm32 f103c8t6
Discovering st32
Stm32 reference manual
27911 gyroscope appnote2
Bucher lift control guide
Modbus protocol reference guide
SPI Concepts.pdf
Ad

More from Arun Umrao (20)

PDF
maths_practice_sheet_28.12.23 (copy).pdf
PDF
Function Analysis v.2
PDF
Average value by integral method
PDF
Xcos simulation
PDF
Work and energy
PDF
Units of physical quantities
PDF
Scilab help book 2 of 2
PDF
Scilab help book 1 of 2
PDF
Notes of Java
PDF
Modelica programming help book
PDF
Measurements and errors
PDF
Linear motion for k12 students
PDF
Gnu octave help book 02 of 02
PDF
Gnu octave help book 01 of 02
PDF
Fortran programming help book
PDF
Force and its application for k12 students
PDF
Electric field for k12 student
PDF
Dictionary of physics
PDF
Decreasing and increasing function
PDF
Circular motion
maths_practice_sheet_28.12.23 (copy).pdf
Function Analysis v.2
Average value by integral method
Xcos simulation
Work and energy
Units of physical quantities
Scilab help book 2 of 2
Scilab help book 1 of 2
Notes of Java
Modelica programming help book
Measurements and errors
Linear motion for k12 students
Gnu octave help book 02 of 02
Gnu octave help book 01 of 02
Fortran programming help book
Force and its application for k12 students
Electric field for k12 student
Dictionary of physics
Decreasing and increasing function
Circular motion

Recently uploaded (20)

PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PPTX
Transform Your Business with a Software ERP System
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
medical staffing services at VALiNTRY
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
assetexplorer- product-overview - presentation
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
history of c programming in notes for students .pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
top salesforce developer skills in 2025.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
wealthsignaloriginal-com-DS-text-... (1).pdf
Transform Your Business with a Software ERP System
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
How to Choose the Right IT Partner for Your Business in Malaysia
PTS Company Brochure 2025 (1).pdf.......
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Adobe Illustrator 28.6 Crack My Vision of Vector Design
medical staffing services at VALiNTRY
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
CHAPTER 2 - PM Management and IT Context
Understanding Forklifts - TECH EHS Solution
assetexplorer- product-overview - presentation
2025 Textile ERP Trends: SAP, Odoo & Oracle
Digital Systems & Binary Numbers (comprehensive )
Computer Software and OS of computer science of grade 11.pptx
history of c programming in notes for students .pptx
ai tools demonstartion for schools and inter college
top salesforce developer skills in 2025.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)

8051 micro controller

  • 1. 1 8051 MCU AN INTRODUCTION Arun Umrao https://sites.google.com/view/arunumrao DRAFT COPY - GPL LICENSING
  • 2. 2 Contents 1 Introduction 5 1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.1.1 Registers . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.1.2 DPTR . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.1.3 Special Function Register (SFR) . . . . . . . . . . . . . 9 1.1.4 Input/Output Ports . . . . . . . . . . . . . . . . . . . 11 1.1.5 bit addressing . . . . . . . . . . . . . . . . . . . . . . . 13 1.1.6 Status Flags . . . . . . . . . . . . . . . . . . . . . . . . 14 1.1.7 Addressing . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.2 RAM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.2.1 Register Bank . . . . . . . . . . . . . . . . . . . . . . . 29 1.2.2 Bit Addressable RAM . . . . . . . . . . . . . . . . . . 31 1.2.3 Scratch Pad . . . . . . . . . . . . . . . . . . . . . . . . 34 1.2.4 Stack Pointer . . . . . . . . . . . . . . . . . . . . . . . 35 1.2.5 Program Counter . . . . . . . . . . . . . . . . . . . . . 35 1.3 Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 1.3.1 Delay . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 2 Instructions 39 2.1 Instruction Sets . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.1.1 ACALL . . . . . . . . . . . . . . . . . . . . . . . . . . 39 2.1.2 ADD . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2.1.3 ADDC . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2.1.4 AJMP . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 2.1.5 ANL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 2.1.6 CJNE . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 2.1.7 CLR . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 2.1.8 CPL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 2.1.9 DA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 2.1.10 DEC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.1.11 DIV . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.1.12 DJNZ . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 2.1.13 INC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 2.1.14 JB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 2.1.15 JBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
  • 3. 3 2.1.16 JC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 2.1.17 JMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.1.18 JNB . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 2.1.19 JNC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 2.1.20 JNZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2.1.21 JZ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2.1.22 LCALL . . . . . . . . . . . . . . . . . . . . . . . . . . 56 2.1.23 LJMP . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 2.1.24 MOV . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 2.1.25 MOVC . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 2.1.26 MOVX . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 2.1.27 MUL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 2.1.28 NOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 2.1.29 ORG . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.1.30 ORL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.1.31 POP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 2.1.32 PUSH . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 2.1.33 RET . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 2.1.34 RETI . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 2.1.35 RL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 2.1.36 RLC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 2.1.37 RR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 2.1.38 RRC . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 2.1.39 SETB . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 2.1.40 SJMP . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 2.1.41 SUBB . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 2.1.42 SWAP . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 2.1.43 XCH . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 2.1.44 XCHD . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 2.1.45 XRL . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 3 µC Structure 85 3.1 Timer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 3.1.1 TMOD . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 3.1.2 TCON . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 3.2 Interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 3.2.1 External Interrupt . . . . . . . . . . . . . . . . . . . . 96 3.2.2 Interrupt Priority Register . . . . . . . . . . . . . . . . 97
  • 4. 4 Introduction 3.2.3 Interrupt Vector Table . . . . . . . . . . . . . . . . . . 98 3.3 Communication . . . . . . . . . . . . . . . . . . . . . . . . . . 100 3.3.1 Baud Rate . . . . . . . . . . . . . . . . . . . . . . . . . 102 3.3.2 SBUF . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 3.3.3 SCON . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 3.3.4 PCON . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 3.4 Assembly Directives . . . . . . . . . . . . . . . . . . . . . . . . 108 3.4.1 Symbol Definition . . . . . . . . . . . . . . . . . . . . . 108 3.4.2 Data Definition . . . . . . . . . . . . . . . . . . . . . . 108 3.5 Assembly in C . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 3.5.1 Byte Register . . . . . . . . . . . . . . . . . . . . . . . 117 3.5.2 Bit Register . . . . . . . . . . . . . . . . . . . . . . . . 117 3.6 Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 3.6.1 Hex File . . . . . . . . . . . . . . . . . . . . . . . . . . 122 3.7 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 4 Numerical Problems 123
  • 5. 1.1. INTRODUCTION 5 1Introduction 1.1 Introduction During the compilation and execution of codes, we use MCU 8051 IDE for the purpose. Codes given below are never ported into real devices. As we know that in 8051 MCU, data can be provided in decimal form or in hex form. If a number is not suffixed with ‘H’ then it is taken as decimal value. In MCU processor, it is first converts into the hex value and then it is executed. For example, port P0 in MCU uses memory address 80H. If an instruction has to access this port, it must identify it by its memory address, that is 0×80 (80H). If instruction is followed by 80 only, then it shall be treated as decimal 80 or 0×50 (50H) and instruction will access memory location other than port P0. ✞ 1 MOV P0 ,#0 FFH ; Set port P0 to input port CPL 80 ;80th register of RAM 3 ;in hex it is 50H which is not ;address of port P0 5 END ✌ ✆ Above example does not complement to the value of port P0 and it comple- ments to 80th register, i.e. 80 is in decimal value and it is not the address of port P0. Again if above program is modified as given below ✞ 1 MOV P0 ,#0 FFH ; Set port P0 to input port CPL 80H ;80H is 128 in decimal. i.e. 3 ;128th register of RAM. It is ;address of port P0 5 END ✌ ✆ then it complements to the value at port P0. As 80H represents to 128th register of RAM that is address of port P0. So be careful while you are using address as count form (decimal) or in hex form. User’s input data should
  • 6. 6 Introduction be started with a numeric digit and prefixed with # symbol. Numeric input data should be started with a numeric digit. See example below: ✞ 1 MOV P0 ,#0 FFH ; Valid MOV P0 ,# FFH ; Invalid 3 MOV P0 ,#80H ; Valid END ✌ ✆ Second line in above codes has invalid input. The input is consider as group of characters rather than numerical input value. If an input is not prefixed with # symbol then it is considered as RAM memory address or port address. 20 21 19 22 18 23 17 24 16 25 15 26 14 27 13 28 12 29 11 30 10 31 9 32 8 33 7 34 6 35 5 36 4 37 3 38 2 39 1 40 P1.0 P1.1 P1.2 P1.3 P1.4 P1.5 P1.6 P1.7 RST P0.0 P0.1 P0.2 P0.3 P0.4 P0.5 P0.6 P0.7 Vcc P3.0 P3.1 P3.2 P3.3 P3.4 P3.5 P3.6 P3.7 XTAL2 XTAL1 GND P2.0 P2.1 P2.2 P2.3 P2.4 P2.5 P2.6 P2.7 EA ALE PSEN Figure 1.1: 8051 pin diagram. EA is acronym of External Access Enable. This pin is active when it is low and it is used to access external memory locations 0 to 4K. This pin must be connected to VCC if MCU wants to access internal ROM and should connect to GND to access external ROM. 8051 micro controller has two 16 bit registers (PC & DPTR). PSEN is acronym of Program Store Enable which controls the read signal for external program memory (active when it is low). ALE is acronym of Address Latch Enable, to latch address outputs at Ports P0 and P2. XTAL1 and XTAL2 pins are used for crystal inputs for internal oscillator.
  • 7. 1.1. INTRODUCTION 7 1.1.1 Registers Registers are used to store information temporarily. 8051 micro controller unit (MCU) has 8 numbers of 8 bit registers. These eight registers are named from R0 to R7 to process data. These are default registers. The registers are used to access the stack pointer registers also. A register can be used as a pointer to the data. For this purpose only register R0 and R1 are used. Other registers can not be used to hold the address of operand located in RAM. To hold the address of RAM location, the registers R0 and R1 must be prefixed with symbol ‘@’. Special Function Register (SFR) can be accessed by their names or by their address. Address of register A is 0E0H and of register B is 0F0H. The Carry flag is denoted by ‘C’ in 8051. 8051 MCU as one primary accumulator and one secondary accumulator. They are represented by A and B respectively. In 8051 MCU there are two other register banks named as Bank 0 and Bank 1. Each bank has eight resisters named from R0 to R7. Bank 0 or Bank 1 can be selected by using instructions like given below: ✞ SET B RS0 2 ; OR SET B RS1 ✌ ✆ Application of register 1 and register 2 as RAM address is shown in the following codes. ✞ 1 MOV R1 ,#20H ;Value at register R1 is 0x20 MOV @R1 ,#30H;Use register 1 (R1) value as 3 ;RAM Address , i.e. 0x20 ✌ ✆ x0 x1 x2 x3 x4 x5 x6 x7 00 00 20 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 20 30 00 01 00 00 00 00 00 28 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 1.1: Memory result.
  • 8. 8 Introduction 1.1.2 DPTR Data Pointers i.e. DPTR is used either to store or to access 16bit data from the internal RAM/ROM memory. While using the DPTR, source and destination registers must have equal size. For example: ✞ 1 ;Copy data in DPTR by direct addressing. MOV DPTR ,#1012H 3 ;Copy low byte into R0 register MOV R0,DPL 5 ;Copy high byte into R1 register MOV R1,DPH 7 ;End the program execution END ✌ ✆ The data stored in DPTR is two bytes long. Its Low byte is stored in DPL register and High byte is stored in DPH register. DPTR is a Special Function Register (SFR). In instruction ✞ MOV DPTR ,#1012H ✌ ✆ Low byte is 12H and high byte is 10H. Therefore DPL register has value 12H and DPH register has 10H. DPTR acts an intermediate buffer where data is stored temporarily before it moves from memory to register or register to memory. See another example: ✞ 1 ORG 0 MOV DPTR ,# MYDATA 3 MOV R1 ,#80H ;Start the point of data copying B1: 5 CLR A ;Clear accumulator MOVC A,@A+DPTR ;Get ROM data to Accumulator 7 MOV @R1 ,A ;Copy accumulator data to R1 MOV P0,A ;Copy accumulator data to P0 9 JZ EXIT INC DPTR ;Increase data pointer 11 INC R1 ;Increase ROM location SJMP B1 13 EXIT: SJMP $ ;Stay here 15 ORG 30H ;New location of RAM MYDATA: ;ROM data
  • 9. 1.1. INTRODUCTION 9 17 DB ’This is my word’ ,0 END ✌ ✆ 1.1.3 Special Function Register (SFR) Special Function Register (SFR) are used by MCU internally to perform op- erations. The SFR registers are P0, SP, DPL, DPH, PCON, TCON, TMOD, TL0, TL1, P1, SCON, SBUG, P2, IE, P3, IP, PSW, ACC and B. In which only ACC, B, PSW, IP, IE, SCON, TCON and input/output ports are bit addressable. The bit indices, in hexa-decimal numbers, for the location of each bit of SFR is given in the following table. These indices are used with bit addressing instructions only. D7 D6 D5 D4 D3 D2 D1 D0 P0 87 86 85 84 83 82 81 80 TCON 8F 8E 8D 8C 8B 8A 89 88 P1 97 96 95 94 93 92 91 90 SCON 9F 9E 9D 9C 9B 9A 99 98 P2 A7 A6 A5 A4 A3 A2 A1 A0 IE AF AE AD AC AB AA A9 A8 P3 B7 B6 B5 B4 B3 B2 B1 B0 IP BC BB BA B9 B8 PSW D7 D6 D5 D4 D3 D2 D1 D0 ACC E7 E6 E5 E4 E3 E2 E1 E0 B F7 F6 F5 F4 F3 F2 F1 F0 Table 1.2: Special function registers and indices of their corresponding bit in hexa-decimal form. Solved Problem 1.1 Load 24H in the accumulator of 8051MCU and access its D3 bit as bit addressable mode.
  • 10. 10 Introduction Solution Accumulator is a bit addressable register and its bit can be accessed independently. According to this problem, we first load a value 24H into the accumulator. Then we complement the fourth bit D3 bit of the accumulator value by using CPL instruction. See the program given below: ✞ MOV A ,#24H ; A=24H = 0010 0100 2 CPL A.3 ; A=2CH = 0010 1100 END ; End the program ✌ ✆ Solved Problem 1.2 Write a 8051MCU assembly program for continuous toggling of D5 port pin of port P0 by accessing address location of port P0. Solution The address location of port P0 ranges from RAM address 0×80 to 0×87 RAM location and they represent to the corresponding bit from D0 to D7 of the port P0. To toggle the D5 pin we have to complement the value at 0×85 RAM Location. ✞ 1 ORG 0000H HERE: 3 CPL 85H ; Complement D_5 pin of port P0 CPL 85H ; Complement D_5 pin of port P0 5 JMP HERE; Continuous loop END ; End the program ✌ ✆ Solved Problem 1.3 Write a 8051MCU assembly program for continuous toggling of D6 bit of accumulator by accessing address location method. Solution The address location of accumulator ranges from RAM address 0×E0 to RAM address 0×E7 and they represent to the corresponding bits, from D0 to D7 of the accumulator. To toggle the D6 bit of accumulator we have to complement the value at 0×E6 RAM Location. ✞ ORG 00H 2 HERE: CPL 0E6H ; Complement D_6 of accumulator 4 CPL 0E6H ; Complement D_6 of accumulator JMP HERE ; Continuous loop 6 END ; End the program ✌ ✆
  • 11. 1.1. INTRODUCTION 11 1.1.4 Input/Output Ports 8051 has four 8-bit long I/O ports named from P0 to P3 and uses 8 pins. When all or one of the 8 bit are in 0 state, then port or corresponding port bit acts as output port. Similarly, when all or one of the 8 bit are in state 1, then the port or port bit acts as input port. By default all ports are input ports. Before we may start using port, it must be reconfigured. Each bit of an Port is numbered from ‘Pn.0’ to ‘Pn.7’. Each bit pin of a port (say P0 for example) can be accessed by its name or addresses, as pins have designated addresses from 0×80 to 0×87. Port pin bit of port P1 can be accessed by using either their name or addresses, as pins have designated addresses from 0×90 to 0×97. Similarly port pin bit of port P2 and P3 can be accessed by using either their name or address as pins have designated addresses from 0×A0 to 0×A7 and from 0×B0 to 0×B7 respectively. Port P0 must be connected with 10K ohm pull-up register as it is like open drain. Only port P0 required pull up registers. Port P2 is used with port P0 as upper byte and provides sixteen bit port address. ✞ MOV A ,#0FFH ;Set A=FF hex 2 MOV P0 ,A ;Reset all bit of port P0 to 1 ;Hence it is made to input port. 4 END ;End of program ✌ ✆ A port pins can be addressed either by its name or by its address. In the following example, port pins are accessed by using their addresses. ✞ MOV P0 ,#0 FFH ; set port to FFH 2 CPL 80H ; Compliment the first LSB to 0 MOV P1 ,#0 FFH ; set port to FFH 4 CPL 90H ; Compliment the first LSB to 0 MOV P2 ,#0 FFH ; set port to FFH 6 CPL 0A0H ; Compliment the first LSB to 0 MOV P3 ,#0 FFH ; set port to FFH 8 CPL 0B0H ; Compliment the first LSB to 0 END ✌ ✆
  • 12. 12 Introduction Port Hex Value D7 D6 D5 D4 D3 D2 D1 D0 P0 FE 1 1 1 1 1 1 1 0 P1 FE 1 1 1 1 1 1 1 0 P2 FE 1 1 1 1 1 1 1 0 P3 FE 1 1 1 1 1 1 1 0 87H P0.7 86H P0.6 85H P0.5 84H P0.4 83H P0.3 82H P0.2 81H P0.1 80H P0.0 97H P1.7 96H P1.6 95H P1.5 94H P1.4 93H P1.3 92H P1.2 91H P1.1 90H P1.0 0A7H P2.7 0A6H P2.6 0A5H P2.5 0A4H P2.4 0A3H P2.3 0A2H P2.2 0A1H P2.1 0A0H P2.0 0B7H P3.7 0B6H P3.6 0B5H P3.5 0B4H P3.4 0B3H P3.3 0B2H P3.2 0B1H P3.1 0B0H P3.0 Figure 1.2: Port pin addresses of ports P0, P1, P2 and P3. Similarly, in the following example, port pins are accessed by using their port name. For example, ‘P1.0’ for first pin of port P1 from LSB side, ‘P3.3’ for D3 pin of port P3 from LSB side etc. ✞ 1 MOV P0 ,#0 FFH ; set port to FFH CPL P0.0 ; Compliment the first LSB to 0 3 MOV P1 ,#0 FFH ; set port to FFH CPL P1.0 ; Compliment the first LSB to 0 5 MOV P2 ,#0 FFH ; set port to FFH CPL P2.0 ; Compliment the first LSB to 0 7 MOV P3 ,#0 FFH ; set port to FFH CPL P3.0 ; Compliment the first LSB to 0 9 END ✌ ✆
  • 13. 1.1. INTRODUCTION 13 Port Hex Value D7 D6 D5 D4 D3 D2 D1 D0 P0 FE 1 1 1 1 1 1 1 0 P1 FE 1 1 1 1 1 1 1 0 P2 FE 1 1 1 1 1 1 1 0 P3 FE 1 1 1 1 1 1 1 0 1.1.5 bit addressing We can access the entire 8-bits of a port or a specific bit of any port without any alteration in other bits. To access a particular bit of a port, code syntax is used like : ✞ 1 SETB X.Y ✌ ✆ Here ‘X’ is port name and ‘Y’ is specific bit number ranging from 0 to 7. For example, first and second bit of port P1 are accessed by ✞ 1 BACK: CPL P1.0 3 CPL P1.1 SJMP BACk 5 END ✌ ✆ Addresses of bit of ports P0 to P3 are given below: 1. Addresses of port bit of port P0 ranges from 80H to 87H 2. Addresses of port bit of port P1 ranges 90H to 97H 3. Addresses of port bit of port P2 ranges A0H to A7H 4. Addresses of port bit of port P3 ranges B0H to B7H Registers A, B, Process State Word (PSW), IP, IE, ACC, SCON and TCON are bit addressable. D7 D6 D5 D4 D3 D2 D1 D0 CY AC RS1 RS0 OV P
  • 14. 14 Introduction For example to check the Overflow bit, we use instruction as ✞ 1 JB PSW.2 <target > ✌ ✆ 1.1.6 Status Flags The ALU of 8051 includes five flip-flops which are set or reset after an oper- ation according to data conditions of the result in the accumulator and other registers. They are called Carry (CY), Auxiliary Carry (AC), RS1, RS0, Over Flow (OV) and Parity (P) flags. All these flags are arranged as shown below and they form a 8-bits long word (i.e. one byte long) that is called as Process State Word (PSW). D7 D6 D5 D4 D3 D2 D1 D0 CY AC RS1 RS0 OV P Carry Flag The carry flag is commonly used to indicate whether an addition causes a ‘carry’ into the next higher order digit. The carry flag is also used as a ‘borrow’ flag in subtractions. To set the carry flag, syntax is used as ✞ 1 SETB C ; OR 3 SETB CY ✌ ✆ Or via bit addressing of PSW as ✞ 1 SETB PSW.7 ✌ ✆ Auxiliary Carry Flag The auxiliary carry flag indicates a carry out of third bit of the accumulator. It is present to enable the Decimal Adjust Accumulator (DAA) to perform its functions. The AC flag can be accessed by bit addressing of PSW like ✞ 1 SETB PSW.6 ✌ ✆ Register Bank 1 Select Control Bit (RS1) When RS1 is set to high then register bank 1 is selected. In this bank there are eight registers those can
  • 15. 1.1. INTRODUCTION 15 be accessed by using register’s name, R0 to R7. Both resister banks of the 8051 MCU can not be used simultaneously, hence before selecting of resister bank 1, resister bank 0 must be deselected. The RS1 bit can be addressed via bit addressing of PSW like ✞ 1 SETB PSW.4 ✌ ✆ Register Bank 0 Select Control Bit (RS0) When RS0 is set to high then register bank 0 is selected. In each bank there are eight registers can be accessed by register name, R0 to R7. Both resister banks of the 8051 MCU can not be used simultaneously, hence before selecting of resister bank 0, resister bank 1 must be deselected. The RS0 bit can be addressed via bit addressing of PSW like ✞ 1 SETB PSW.3 ✌ ✆ Overflow Flag In additions, the Overflow (OV) bit is set if there is a carry out-of D6 or out-of D7, but not both. The OV flag can be accessed by bit addressing of PSW like ✞ 1 SETB PSW.2 ✌ ✆ ✞ 1 MOV A ,#+96 ;96D = 0110 0000 MOV R0 ,#+70 ;70D = 0100 0110 3 ADD A,R0 ;166D= 1010 0110 END ; End of program ✌ ✆ In above sum, sum of D6 bit of both numbers sends a carry to D7 bits. But on the sum of D7 bit of the numbers there is no further carry. Hence overflow flag is set to high. Parity Flag Parity is determined by counting the number of one bit set in the result in the accumulator. Instructions that affect the parity flag set the flag to one for even parity and reset the flag to zero to indicate odd parity. The parity flag can be accessed by bit addressing of PSW like ✞ SETB PSW.0 ✌ ✆ In the following table, some mathematical mnenomics are listed which uses the PSW flags.
  • 16. 16 Introduction Operator C AC OV P ADD Y Y ADDC Y Y DA Y Y DIV Y Y MUL Y Y RLC RRC SETB SUBB 1.1.7 Addressing In 8051 MCU data is copied or moved from register to accumulator or vice- versa. Data transfer between registers is not acceptable and cause errors. During data transfter, size of the source register and the destination register should be same. Before going through the addressing, remember that the values which are not ends with ‘H’ are taken as decimal number by truncating trailing characters other than numeric digits. For example ✞ 1 MOV A ,#10 ;Copy decimal 10 (hex A) to accumulator MOV A,#1A ;"Invalid numeric base ‘A ’" if used 3 ; directly. If used via EQU directive ;Copy decimal 1 (hex 1) to accumulator 5 ;and truncate trailing character ’A’ MOV A,#A1 ;A1 is considered as label rather than value 7 ;There shall be error of "symbol not defined" ✌ ✆ See the example below: ✞ 1 AGE EQU 1A ;EQU directive for AGE MOV P0 ,# AGE ;AGE is used by immediate addressing 3 MOV P1 ,#15 ;Copy decimal 15 (hex F) to port P1 MOV P2 ,#15H ;Copy hexa -decimal 15 to port P2
  • 17. 1.1. INTRODUCTION 17 5 END ✌ ✆ After execution of the above code, the port values will be as given below: Port Hex Value D7 D6 D5 D4 D3 D2 D1 D0 P0 01 0 0 0 0 0 0 0 1 P1 0F 0 0 0 0 1 1 1 1 P2 15 0 0 0 1 0 1 0 1 P3 00 0 0 0 0 0 0 0 0 With above examples it is clear that a data value is either copied from the port or resister, or it moved to port or register. Direct Addressing In direct addressing mode data is transferred from register to accumulator or vice-versa by identifying the source and destination as indicated in operands. In direct addressing, source or destination is identified by its name. ✞ 1 MOV A ,04H ✌ ✆ Here 04H represents to the register R4. Pushing and popping the stack is allowed only in direct addressing mode. PUSH A is not valid while PUSH 0E0H is valid. Here 0E0H is address of the accumulator. Looping is not allowed in direct addressing mode. The RAM locations 30H to 7FH are only accessible by direct addressing mode only. The direct addressing can access to on-chip memory location. Indirect Addressing Mode Registers R0 and R1 are operated as pointer registers. Indirect addressing is represented by an ‘@’ symbol before R0 and R1. In indirect addressing, source or destination is identified by its address rather than its name. For example ✞ 1 MOV A,@R0 ✌ ✆
  • 18. 18 Introduction Register Direct Addressing In register direct addressing mode, data is addressed by register name. In register direct addressing mode, a register is either target for data pasting or source for data copying. For example ✞ 1 MOV A,R4 ✌ ✆ Here, register R4 of a register bank is addressed directly and its contents are copied into the accumulator. Register Indirect Addressing This mode is similar to the direct address. In this type of addressing, contents of the memory location registers are copied into the internal registers, i.e. accumulator or secondary accumulator. The registers in register indirect addressing mode, register is identified by its address rather than register name. In This addressing mode, source or destination is the sum of the base address and the accumulator (acts as index). The base address may be DPTR or PC. Example with DPTR base: ✞ 1 MOV DPTR , #20H MOV A, #15H 3 MOVC A, @A+DPTR ;A <- M[35H] ✌ ✆ Example with PC base: ✞ 1 ORG 0100H 100: MOV A, #05H 3 102: MOVC A, @A+PC ; A <- M[108] 103: NOP ; PC Location ✌ ✆ Index Addressing Index addressing mode is widely used in accessing data elements of the look- up table entries located in the program ROM. The syntax use is ✞ MOVC A,@A+DPTR ✌ ✆ In the following example, index addressing is used to return the index values starting from numeric 1 to 9. Last digit 0 is used to halt the process.
  • 19. 1.1. INTRODUCTION 19 ✞ 1 ORG 0 MOV DPTR ,#30H ;Fetch the data pointer from address 03H 3 ;It may be used as MOV DPTR ,# MYDATA also BACK: 5 CLR A ;Clear the accumulator MOVC A,@A+DPTR;Get the data pointer value and copy 7 ;in accumulator MOV P0 ,A ;Copy accumulator value to port P0 9 JZ EXIT ;Exit to loop when digit is 0 INC DPTR ;Increase data pointer value by 1 11 SJMP BACK ;Do it continue EXIT: 13 SJMP $ ORG 30H ;Address of pointer pointed to data 15 MYDATA: DB 1,2,3,4,5,6,7,8,9,0 17 END ✌ ✆ Immediate Addressing In immediate addressing mode, data is directly copied into a register or ac- cumulator. Here source is data itself rather than register name or memory address from where data is being copied. In immediate addressing data is added to register or accumulator by using instruction MOV. The operand data may be numeric constant, a symbolic variable and an arithmetic ex- pression using constants, symbols and operators. Immediate operands are preceded by symbol ‘#’. Assembler computes the value and substitutes the immediate data into the instruction. Following is a way for immediate ad- dressing of a hex value. ✞ 1 MOV R0 ,#19H ; copy hex 19 in register R0 immediately. ✌ ✆ The input operand data may be a decimal value for immediate addressing. But it should be prefixed with symbol ‘#’ and does not suffixed with symbol ‘H’. ✞ 1 MOV R0 ,#19 ; copy dec 19 in register R0 immediately. ✌ ✆
  • 20. 20 Introduction A signed number in decimal only can be immediately addressed if a sign (+ or –) is used between ‘#’ symbol and number. ✞ 1 MOV R0 ,#+19 ; copy dec +19 in register R0 immediately. MOV R1 ,# -19 ; copy dec -19 in register R1 immediately. ✌ ✆ Remember that in immediate addressing, the value suffixed to symbol ‘#’ must be started with a number not with alphabet, as value with alphabet is considered as symbol and there shall be compilation error. If input hex values are like A1, AF or FF, then these values must be prefixed with number 0. ✞ MOV R0 ,# FFH ;Illegal operand and not acceptable 2 MOV R0 ,#0 FFH ;Legal operand and acceptable ✌ ✆ For example ✞ MOV A ,#00H ;Store A with 0 value , all bit low 2 CLR C ;Reset carry flag to zero SETB C ;Set carry flag to one 4 MOV P0,A ;Reset all bit of port P0 to 0 SETB P0.1 ;Set port bit P0.1 to 1 6 ;MOV A,# FFH ;Give error if not commented MOV A,#0 FFH ;Accepted method , all bit of A are high 8 MOV P0,A ;Set all port bit of P0 to high END ;End the program ✌ ✆ An alpha-numeric symbol can also be used as input data. The symbol is put in single quotes prefixed with # symbol. ✞ 1 MOV A,#’Y’ ;Store A with symbol ’Y’ ;’Y’ is equivalent to dec 89 3 MOV P0,A ;Set all port bit of P0 as ;binary value of dec 89 5 END ✌ ✆ Address By Register (@) Only R0 and R1 registers are used as a pointer to the data. Other registers can not be used for this purpose. When R0 and R1 holds the address of RAM they are preceded by ‘@’ symbol.
  • 21. 1.1. INTRODUCTION 21 ✞ 1 ;Set the address of Stack Pointer MOV SP ,#31H 3 ;Store hex 0AH in R7 MOV R7 ,#0AH 5 ;Store hex 0BH in R6 MOV R6 ,#0BH 7 ;Store hex 0CH in R5 MOV R5 ,#0CH 9 ;Store hex 0DH in R4 MOV R4 ,#0DH 11 ;Push value of R7 at address location 32H PUSH 7 13 ;Push value of R6 at address location 33H PUSH 6 15 ;Push value of R5 at address location 34H PUSH 5 17 ;Push value of R4 at address location 35H PUSH 4 19 ;Copy RAM address location into R0 & R1 MOV R0 ,#33H 21 MOV R1 ,#34H ;Copy data of RAM address pointed by R0 23 ;into accumulator A MOV A,@R0 25 ;Copy data of RAM address pointed by R1 ;into secondary accumulator B 27 MOV B,@R1 ;Halt the execution of program 29 END ✌ ✆ Long Addressing Long addressing is done by using LCALL and LJMP instructions. These instructions includes 16 bit destination address. The address range for these instructions is 0000H to FFFFH. The memory range in 8051 MCU is from 00H to FFH, this is why, this type of addressing is called long addressing.
  • 22. 22 Introduction 1.2 RAM 8051 MCU has 128 bytes of RAM and 4K bytes ROM. ROM is used for program code. 8051 MCU also supports 64K bytes read only external code memory. It can be accessed through PSEN. 8051 MCU also supported to 64K external data memory that can be read and write by RD and WR. Code memory is selectable by EA(internal or external). External memory space is access by using MOVX instruction. These two type of memory spaces are not shared between code and data. RAM memory is divided into three parts. 1. First 32 bytes are used as register bank. 2. Next 16 bytes are used for bit addressable read/write memory. 3. Rest of 80 bytes are used for read and write storage. This section is called scratch pad. Bytes Location From Location To Label 32 Bytes 00H 1FH Register bank 16 Bytes 20H 2FH Bit addressable 80 Bytes 30H 7FH Scratch pad Table 1.3: Memory distribution. Memory location of RAM from 0×30 to 0×7F are used as scratch pad and also as registers. Addresses from 0×08 to 0×1F addresses are used for stack. ROM which stores data is not bit addressable while RAM location 0×20 to 0×2F are bit and byte addressable. The instructions which are allowed to access the RAM address for byte or bit addressing are SETB, CLR, CPL, JB, JNB, JBC. For bit or byte addressing of RAM registers, the register can be accessed by its location or by its address. Bit or byte addressing by location, the location of address is identify by its counting position. For example, the RAM address 0×20 is 32th register of the RAM when counting started from 0. Note that, in the computer counting (indexing) started from 0 instead of 1. In RAM each location is identified by hexadecimal values, hence if location is identified as a decimal number then it first should be
  • 23. 1.2. RAM 23 converted into equivalent hexadecimal value and then RAM address should be identified by using this hexa-decimal value. See the example below in which register location and bit of register is used for bit addressing. Bit addressable registers are located in RAM memory from 0×20 to 0×2F. This is why, the first default bit addressable resister is 0×20. ✞ 1 MOV 33 ,#200 ; Dec 30 or hex 0x21 address of RAM memory MOV 21H ,#200; 0x21 address of RAM memory 3 CPL 33 ; D33 bit from 0x20 address of RAM memory ; Memory bit are counted from 0 so that 5 ; D33 bit is D1 bit of 5th byte. CPL 33.1 ; D1 bit of 0x21 address of RAM memory 7 ; on applying of . (dot) 33 becomes ; RAM register address in decimal 9 ; (equivalent 0x21 in hex) CPL 35.0 ; D0 bit of 0x23 address of RAM memory 11 ; on applying of . (dot) 35 becomes ; RAM register address in decimal 13 ; (equivalent 0x23 in hex) END ; End of program ✌ ✆ To kept out from this confusion, each decimal number should be converted into equivalent hexadecimal numbers and then memory address should be identify in the RAM register. x0 x1 x2 x3 x4 x5 x6 x7 ... 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00 00 20 CA 00 01 02 00 00 00 00 28 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 1.4: Memory result. In above example, we specify the bit of a register by using ‘.’ (dot). If we do not use the ‘.’ (dot) as shown above then the address is consider as bit number started from 0×20 to continue upto 0×2F in group of 8 bits. In
  • 24. 24 Introduction the following example, fist we reset the value of RAM register identified by its address 0×20. ✞ MOV 32 ,#00; 0x20 register of RAM memory ✌ ✆ The above instruction is not compulsory as default bit addressable registers are located in RAM from 0×20 to 0×2F. Original memory values at 0×20, 0×21 and 0×22 RAM registers are all zeros. Note that, here counting of bit of binary numbers is in right-to-left direction and byte-by-byte wise. ✞ 1 ;+---------------+----------------+----------------+ ;| 0x20 | 0x21 | 0x22 | 3 ;+---------------+----------------+----------------+ ;|0|0|0|0|0|0|0|0| 0|0|0|0|0|0|0|0| 0|0|0|0|0|0|0|0| 5 ;+---------------+----------------+----------------+ ✌ ✆ On complementing the D0 bit of 0×20 RAM register, the value of this register is changed to 01H. ✞ 1 CPL 0 ; D0 bit of 32(0x20) register of RAM memory ;+---------------+----------------+ 3 ;| 0x20 | 0x21 | ;+---------------+----------------+ 5 ;|0|0|0|0|0|0|0|1| 0|0|0|0|0|0|0|0| ;+---------------+----------------+ 7 ;| 01D or 01H | 00D or 00H | SETB state ;+---------------+----------------+ ✌ ✆ ✞ CPL 33.0 ; D0 bit of 0x21 register of RAM memory 2 ;+---------------+----------------+ ;| 0x20 | 0x21 | 4 ;+---------------+----------------+ ;|0|0|0|0|0|0|0|0| 0|0|0|0|0|0|0|1| 6 ;+---------------+----------------+ ;| 00D or 00H | 01D or 01H | SETB state 8 ;+---------------+----------------+ ✌ ✆
  • 25. 1.2. RAM 25 x0 x1 x2 x3 x4 x5 x6 x7 ... 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00 00 20 01 00 01 00 00 00 00 00 28 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 1.5: Memory result. Here, noted that, number succeeding to CPL instruction is the bit location from the current address as CPL is bit addressable instruction. Therefore, ✞ CPL 16 ✌ ✆ meant that the complementing of 16th bit (counted from 0) from the current address. Now, ✞ 1 CPL 1 ; D1 bit of 0x20 register of RAM memory CPL 2 ; D2 bit of 0x20 register of RAM memory ✌ ✆ complements the second and third bit from the current address. The memory value of RAM register identified by RAM address 0×20 is ✞ ;+---------------+----------------+ 2 ;| 0x20 | 0x21 | ;+---------------+----------------+ 4 ;|0|0|0|0|0|1|1|1| 0|0|0|0|0|0|0|0| ;+---------------+----------------+ 6 ;| 07D or 07H | 00D or 00H | SETB state ;+---------------+----------------+ ✌ ✆ Similarly, other bit are complemented by using CPL instruction as shown below: ✞ 1 CPL 8 ; D0 bit of 33th (0 x21) register of RAM memory ;+---------------+----------------+ 3 ;| 0x20 | 0x21 | ;+---------------+----------------+
  • 26. 26 Introduction 5 ;|0|0|0|0|0|1|1|1| 0|0|0|0|0|0|0|1| ;+---------------+----------------+ 7 ;| 03D or 03H | 01D or 01H | SETB state ;+---------------+----------------+ 9 CPL 16 ; D0 bit of 34th(0x22) register of RAM memory ;+---------------+----------------+----------------+ 11 ;| 0x20 | 0x21 | 0x22 | ;+---------------+----------------+----------------+ 13 ;|0|0|0|0|0|1|1|1| 0|0|0|0|0|0|0|1| 0|0|0|0|0|0|0|1| ;+---------------+----------------+----------------+ 15 ;| 03D or 03H | 01D or 01H | 01D or 01H | SETB state ;+---------------+----------------+----------------+ ✌ ✆ Full program codes are given below: ✞ MOV 33 ,#00; 2 CPL 0 ; D0 bit of first byte CPL 1 ; D1 bit of first byte 4 CPL 2 ; D2 bit of first byte CPL 8 ; D0 bit of second byte 6 CPL 16; D0 bit of third byte ✌ ✆ The final values at RAM registers at memory addresses 0×20, 0×21 and 0×22 are shown in following memory map. x0 x1 x2 x3 x4 x5 x6 x7 ... 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00 00 20 07 01 01 00 00 00 00 00 28 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 1.6: Memory result. For bit or byte addressing of RAM register using addresses of registers is possible by SETB instruction too. Byte or bit addressing of a RAM address
  • 27. 1.2. RAM 27 is allowed in group of two bytes. RAM addresses 0×20 and 0×21 (register count numbers 32nd and 33rd ) are taken as zero group, RAM addresses 0×22 and 0×23 (register count numbers 34th and 35th ) are taken as first group and so on. Counting of first register of a group started from 0 to 7 and second register of the group started from 9 to F. Use of instruction ✞ SETB 14H ; 20 in decimal 2x8+4 = D4 bit ✌ ✆ instructs that set the D4 bit of register at address 0 ×22 (register count number 34th ) of first group. Similarly instruction ✞ 1 SETB 1FH ✌ ✆ instruct that set the D7 bit of register 0×23 (register count number 35th ) of first group. Each character of operand of SETB instruction has unique meaning. For example in instruction ✞ 1 SETB 14H ✌ ✆ 14H has three characters, ‘1’, ‘4’ and ‘H’. Here, ‘1’ stands for first group of RAM registers, i.e. RAM registers at the address of 0×22 and 0×23. ‘4’ represents to D4 bit of the register group. This bit is falls in the register address 0×22. ‘H’ represents to hexa-decimal number. Now see the following self explained example. ✞ 1 ;+---------------+----------------+ ;|0|0|0|0|0|0|0|0| 0|0|0|0|0|0|0|0| Original state 3 ;+---------------+----------------+ ; 5 ; SETB 14H ; Set the D4 bit of register 0x22 of group one 7 SETB 1FH ; Set the D7 bit of register 0x23 of group one ;+---------------+----------------+ 9 ;| 0x22 | 0x23 | ;+---------------+----------------+ 11 ;|0|0|0|1|0|0|0|0| 1|0|0|0|0|0|0|0| ;+---------------+----------------+ 13 ;| 16D or 10H | 128D or 80H | SETB state ;+---------------+----------------+ 15 SETB 24H ; Set the D4 bit of register 0x24 of group two SETB 2BH ; Set the D3 bit of register 0x25 of group two
  • 28. 28 Introduction 17 ;+---------------+----------------+ ;| 0x24 | 0x25 | 19 ;+---------------+----------------+ ;|0|0|0|1|0|0|0|0| 0|0|0|0|1|0|0|0| 21 ;+---------------+----------------+ ;| 16D or 10H | 08D or 08H | SETB state 23 ;+---------------+----------------+ SETB 31H ; Set the D1 bit of register 0x26 of group three 25 SETB 38H ; Set the D0 bit of register 0x27 of group three ;+---------------+----------------+ 27 ;| 0x26 | 0x27 | ;+---------------+----------------+ 29 ;|0|0|0|0|0|0|1|0| 0|0|0|0|0|0|0|1| ;+---------------+----------------+ 31 ;| 02D or 02H | 01D or 01H | SETB state ;+---------------+----------------+ 33 END ✌ ✆ In above example, the bit is access by address. The memory of the MCU after the execution of above program shall be looked like: x0 x1 x2 x3 x4 x5 x6 x7 ... 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00 00 20 00 00 10 80 10 08 02 01 28 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 1.7: Memory result. If operand of SETB is a decimal number then it represents to the bit location from the D0 bit of 0×20 register. The counting starts from 0 to 7 for first register of the group and 8 to 15 for the second register of the group. ✞ 1 SETB 14 ;8+6 => D6 bit of second byte
  • 29. 1.2. RAM 29 SETB 15 ;8+7 => D7 bit of second byte 3 SETB 16 ;8x2+0 => D0 bit of third byte SETB 17 ;8x2+1 => D1 bit of third byte 5 ;+---------------+----------------+----------------+ ;| 0x20 | 0x21 | 0x22 | 7 ;+---------------+----------------+----------------+ ;|0|0|0|0|0|0|0|0| 1|1|0|0|0|0|0|0| 0|0|0|0|0|0|1|1| 9 ;+---------------+----------------+----------------+ ;| 00D or 00H | 192D or C0H | 03D or 03H | 11 ;+---------------+----------------+----------------+ END ✌ ✆ x0 x1 x2 x3 x4 x5 x6 x7 ... 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00 00 20 00 C0 03 00 00 00 00 00 28 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 1.8: Memory result. To avoid this confusion of the bit position due to different starting point of counting indices, first convert the location given in decimal into hexa-decimal equivalent and then count the location of bit starting from the index 0. 1.2.1 Register Bank First 32 bytes are further divided in to four parts. Each part is of 8 bytes long. Each byte of this part is called a register. A group of 8 bytes is called register bank. The 8 bytes of a register bank are denoted by R0 to R7. Four register banks are Bank 0, Bank 1, Bank 2 and Bank 3.
  • 30. 30 Introduction Bank Range Registers Bank 0 00H-07H R0 R1 R2 R3 R4 R5 R6 R7 Bank 1 08H-0FH R0 R1 R2 R3 R4 R5 R6 R7 Bank 2 10H-17H R0 R1 R2 R3 R4 R5 R6 R7 Bank 3 18H-1FH R0 R1 R2 R3 R4 R5 R6 R7 By default, register bank 0 is selected on power up. The following in- structions ✞ MOV R0 ,#10 ; copy dec 10 to register 2 ; R0 of register bank 0 END ✌ ✆ shall store the data into the register R0 of register bank 0. See RAM memory x0 x1 x2 x3 x4 x5 x6 x7 00 0A 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 1.9: RAM memory result. PSW is used to select the desired register banks. The PSW register is shown below. D7 D6 D5 D4 D3 D2 D1 D0 CY AC RS1 RS0 OV P D3 and D4 bit of PSW register are set as per table given below to select the register banks.
  • 31. 1.2. RAM 31 Selected Bank RS1 RS0 Bank 0 0 0 Bank 1 0 1 Bank 2 1 0 Bank 3 1 1 In the following example, RS0 bit is set high while RS1 bit is cleared. Thus the register bank 1 is selected. ✞ 1 SETB PSW.3 ; set D3 bit to 1 CLR PSW.4 ; set D4 bit to 0 3 MOV R0 ,#10 ; copy decimal 10 to register R0 of ; register bank 1, i.e. 08th RAM Byte 5 END ✌ ✆ MOV R0,#10 stores the data in register R0 of register bank 1. See RAM memory RAM Address x0 x1 x2 x3 x4 x5 x6 x7 00H 00 00 00 00 00 00 00 00 08H 0A 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 1.10: RAM memory result. 1.2.2 Bit Addressable RAM The RAM location from 20H to 2FH are bit addressible. It means, all in- structions whose operands are bit addresses can accessit. bit of bytes from RAM address 20H to 2FH are identified by their position indices, counted from 0 to 127. The instruction sets SETB, CLR, CPL, JB, JNB and JBC are bit addressing instructions.
  • 32. 32 Introduction RAM Address x0 x1 x2 x3 x4 x5 x6 x7 20H 00 00 00 00 00 00 00 00 28H 00 00 00 00 00 00 00 00 Table 1.11: Bit Addressable RAM. Each RAM address is 8 bit long. Between RAM addresses 20H to 2FH (total 16 bytes), bit are counted from 0 to 127, taking first bit (D0 bit) of RAM address 0×20 as reference position, i.e. zero position. In other words, D0 bit of 20H RAM address is counted as index 0. Similarly, D1 bit of RAM address 21H is 9th bit of bit-addressable RAM from RAM address 0×20. RAM Address D7 D6 D5 D4 D3 D2 D1 D0 20H 007 006 005 004 003 002 001 000 21H 016 014 013 012 011 010 009 008 ... 031 030 029 028 027 026 025 024 2FH 127 125 125 124 123 122 121 120 Table 1.12: Indices of each bit of bit addressable RAM memory addresses. See the following example ✞ 1 SETB 03 ; set D3 bit at 0x20 RAM location SETB 03H ; set D3 bit at 0x20 RAM location 3 SETB 24 ; set D4 bit at 0x23 RAM location SETB 18H ; set D4 bit at 0x23 RAM location 5 END ✌ ✆ The bit of RAM Address 0×20 and 0×23 will be set as shown in the below table.
  • 33. 1.2. RAM 33 RAM Address D7 D6 D5 D4 D3 D2 D1 D0 Value 20H 0 0 0 0 1 0 0 0 08H 21H 0 0 0 0 0 0 0 0 00H 22H 0 0 0 0 0 0 0 0 00H 23H 0 0 0 0 0 0 0 1 01H 24H 0 0 0 0 0 0 0 0 00H The bit indicex from 128 to 255 in decimal count are assigned to SFRs. Note that these bit indices are also used as hexadecimal values. For example 7th bit index is equal to 0×07 and 128th bit index is equal to 0×80 from the RAM Address 0×20. When an operand is used with BIT ADDRESSABLE INSTRUCTIONS then it always represents to the indices of bit rather than RAM address. When operand of the bit addressing instruction is [128, 255], it access to bit of Special Function Registers (SFR), i.e. ROM memory. D7 D6 D5 D4 D3 D2 D1 D0 P0 135 134 133 132 131 130 129 128 TCON 143 142 141 140 139 138 137 136 P1 151 150 149 148 147 146 145 144 SCON 159 158 157 156 155 154 153 152 P2 167 166 165 164 163 162 161 160 IE 175 174 173 172 171 170 169 168 P3 183 182 181 180 179 178 177 176 IP 188 187 186 185 184 PSW 215 214 213 212 211 210 209 208 ACC 231 230 229 228 227 256 225 224 B 247 246 245 244 243 242 241 240
  • 34. 34 Introduction The corresponding indices in hexa-decimal form are shown in the below table. D7 D6 D5 D4 D3 D2 D1 D0 P0 87 86 85 84 83 82 81 80 TCON 8F 8E 8D 8C 8B 8A 89 88 P1 97 96 95 94 93 92 91 90 SCON 9F 9E 9D 9C 9B 9A 99 98 P2 A7 A6 A5 A4 A3 A2 A1 A0 IE AF AE AD AC AB AA A9 A8 P3 B7 B6 B5 B4 B3 B2 B1 B0 IP BC BB BA B9 B8 PSW D7 D6 D5 D4 D3 D2 D1 D0 ACC E7 E6 E5 E4 E3 E2 E1 E0 B F7 F6 F5 F4 F3 F2 F1 F0 Remember that, registers A, B, PSW, IP, IE, ACC, SCON, TCON, all input/output ports are bit addressable as well as SFR. ROM which holds program code for execution and PCON are not bit addressable. 1.2.3 Scratch Pad RAM location from 30H to 7FH are used as scratch pad for data. x0 x1 x2 x3 x4 x5 x6 x7 30 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 7F 00 00 00 00 00 00 00 00 Table 1.13: Scratch Pad.
  • 35. 1.2. RAM 35 1.2.4 Stack Pointer Stack is a section of RAM used to store information. This information may be data or address. The register used to access the stack is called the stack pointer (SP) register. It is 8 bit long register and can store values ranging from 00H to FFH. On power on to 8051, SP contains the value 07H. Bank Range Registers Register Bank 0 00H-07H R0 R1 R2 R3 R4 R5 R6 R7 SP & Register Bank 1 08H-0FH R0 R1 R2 R3 R4 R5 R6 R7 Register Bank 2 10H-17H R0 R1 R2 R3 R4 R5 R6 R7 Register Bank 3 18H-1FH R0 R1 R2 R3 R4 R5 R6 R7 08H location of the RAM is first location which is used by the SP (from Register Bank 1). PUSH instruction is used to add value in the stack and POP is used to remove the value from the stack. SP is last location of stack, this is why, SP is incremented by one on each PUSH and decremented by one on each POP instruction. CPU uses stack to store the last address just after the CALL instruction before handing over the control to the sub routine. On RET instruction, control is transfer back to the caller. Caller resumes the operation from the last address which was stored in stack by CALL. On power up, register bank 1 and SP shares same memory location in the 8051 RAM. This is why, to use register bank 1, we first change the stack pointer location to other RAM location by using MOV instruction as ✞ 1 MOV SP ,<new RAM location > ✌ ✆ Remember that, on each PUSH to stack, stack pointer register is incremented by 1 and on each POP from the stack, stack pointer register is decremented by 1. There is no decrements protocol for stack pointer. Because, in decrements protocol, SP may reach to the memory location of default register bank 0 and may violate the memory sharing protocol. 1.2.5 Program Counter 8051 has a program counter register. It is 8 bit long register. It is because, 8051 has 8 bit address line. It holds the address of opcode and operands
  • 36. 36 Introduction which are to be executed (address of next opcode that would be fetched). Program counter incremented by one when 8051 executed opcode at the current address. 1.3 Subroutines CALL instruction is used to call subroutine. Subroutines are used in seg- mentation of large codes. Subroutines are group of instructions which are frequently used in a program. It makes a program more structured and saves the memory space. When subroutine is called, control is transferred to that subroutine. After finishing of execution, RET return the control back to the caller. See below codes ✞ 1 ORG 0 ;---------Start of Main ----------------- 3 MAIN: MOV A ,#11H 5 LCALL SUBROUT_1 LCALL SUBROUT_2 7 SJMP MAIN ;---------End of Main ------------------- 9 ; ;---------Start of SUBROUT_1 ------------ 11 SUBROUT_1: MOV B ,#12H 13 RET ;Return to caller of SUBROUT_1 ;---------End of SUBROUT_1 ------------- 15 ; ;---------Start of SUBROUT_2 ------------ 17 SUBROUT_2: MOV R0 ,#13H 19 RET ;Return to caller of SUBROUT_2 ;---------End of SUBROUT_2 ------------- 21 END ;End of the program. ✌ ✆
  • 37. 1.3. SUBROUTINES 37 1.3.1 Delay 8051 has two clocks. CPU takes certain number of clock cycle to execute an instruction. It is called machine cycles. Length of machine cycle is depends on the frequency of the crystal cycle. In 8051, one machine cycles lasts 12 oscillator period. If a CPU has a crystal frequency of 11.0592MHz then its machine cycles frequency is computed as MCF = 11.0592 12 = 921.6KHz The machine cycle period is T = 1 MCF = 1.085µs It means to read or write one byte, machine takes 1.085µs. Delay is a method to lapse of time in MCU. Study following program: ✞ 1 ;Start of DELAY subroutine DELAY: 3 ;MOV is one byte instruction here. CPU takes ;one machine cycle to execute this instruction. 5 ;R3 register is stored value 200 immediately. MOV R3 ,#200 7 ;Start HERE loop HERE: 9 ;DJNZ is 2 byte instruction. Here DJNZ decrease ;R3 by one and checks whether it becomes zero 11 ;or not. If R3 is not zero , it again reach to ;HERE. By this way it performs 200 times loop 13 ;It means CPU takes 200x2 machine cycles to ;perform this line of instruction code. 15 DJNZ R3,HERE ;RET is two byte instruction. Here it takes two 17 ;machine cycles to execute it. RET 19 ;By this way total machine cycles used to execute ;DELAY subroutines are 1+400+2 = 403 machine cycles. ✌ ✆
  • 39. 2.1. INSTRUCTION SETS 39 2Instructions 2.1 Instruction Sets Instruction sets guide to MCU for execution of operands following to the instruction as per instruction’s guideline. Some of the instructions like JBC, CPL, INC and DEC are read-modify-write type of instruction, because they read value from the port-bit, register or memory location then modify it by performing an operation on them and again store it again into the same port-bit, register or memory location. 2.1.1 ACALL It is two byte long instruction. ACALL unconditionally calls a subroutine at the indicated code address. ACALL pushes the address of the instruc- tion that follows ACALL onto the stack, least-significant-byte first, most- significant-byte second. The Program Counter is then updated so that pro- gram execution continues at the indicated address. Since only 11 bit of the Program Counter are affected by ACALL, calls may only be made to sub routines located within the same 2K block where the first byte that follows ACALL is located. The syntax of the instruction is ✞ ACALL <accumulator >, <data or address > ✌ ✆ ✞ 1 HERE: ; HERE subroutine MOV A,#1AH ; Store value 1A Hex to Accumulator 3 AGAIN1: ; AGAIN1 subroutine DEC A ; Decrease accumulator by 1 5 JNZ AGAIN1 ; Jump to AGAIN1 subroutine ACALL THERE ; Call the DELAY subroutine 7 SJMP HERE ; Do it again and again THERE: ; THERE subroutine
  • 40. 40 Instructions 9 MOV A,#1AH ; Store value 1A Hex to Accumulator AGAIN1: ; AGAIN subroutine 11 DEC A ; Decrease accumulator by 1 JNZ AGAIN1 ; Jump to AGAIN subroutine 13 RET ; Return control to caller END ; End of program ✌ ✆ 2.1.2 ADD It is acronym of ADD WITH ACCUMULATOR. If ADD uses registers or reg- ister address as second operand then it is one byte long instruction, otherwise it is two byte long instruction. This functions adds the operand value with accumulator and stores result in accumulator. The operand value remains unaffected on execution of this instruction. The syntax of the instruction is ✞ ADD <accumulator >,<data or address > ✌ ✆ This instruction is used in following ways ✞ 1 ADD A,# data ADD A,<address > 3 ADD A,@R0 ADD A,@R1 5 ADD A,Rn ✌ ✆ The carry flags are set accordingly to reflect the result. The Overflow (OV) bit is set if there is a carry-out of bit 6 or out of bit 7, but not both. Illustrated Example To add two hexadecimal numbers 10H and 62H are given below. ✞ 1 10H = 00001010 +62H = 01100010 3 --------------------- 72H = 01101100 ✌ ✆ The code for 8051 MCU is ✞ CLR C ;Clear carry 2 MOV A ,#10H ;Store 10H in accumulator
  • 41. 2.1. INSTRUCTION SETS 41 ADD A ,#62H ;Add hex 62 with accumulator 4 MOV R1 ,A ;Copy contents of accumulator to R1 END ;End the program ✌ ✆ 2.1.3 ADDC It is acronym of ADD TO ACCUMULATOR WITH CARRY. If ADDC uses registers or register address as second operand then it is one byte long instruction, otherwise it is two byte long instruction. This functions adds the operand value and carry with accumulator and stores the result value in accumulator. The operand value remains unaffected. The syntax of the instruction is ✞ 1 ADDC <accumulator >,<data or address > ✌ ✆ This instruction is used in following ways ✞ 1 ADDC A,# data ADDC A,<address > 3 ADDC A,@R0 ADDC A,@R1 5 ADDC A,Rn ✌ ✆ ADDC reset the carry flag to zero. The Overflow (OV) bit is set if there is a carry-out of bit 6 or out of bit 7, but not both. ✞ 1 MOV A ,#20H ; A = 00100000 CLR C ; CY = 0 3 ADDC A ,#20H; A+CY +20H = 01000000 MOV R0 ,A ; R0 = 01000000 5 SETB C ; CY = 1 ADDC A ,#20H; A+CY +20H = 01100001 7 MOV R1 ,A ; R1 = 01100001 END ; End the program ✌ ✆ 2.1.4 AJMP It is short name of ABSOLUTE JUMP. It is two bytes long instruction. The instruction let the jump to compiler within 2K block unconditionally indi-
  • 42. 42 Instructions cated by operand address. The new value for the Program Counter is cal- culated by replacing the least-significant-byte of the Program Counter with the second byte of the AJMP instruction, and replacing bit 0-2 of the most- significant-byte of the Program Counter with 3 bit that indicate the page of the byte following the AJMP instruction. bit 3-7 of the most-significant-byte of the Program Counter remain unchanged. The syntax of the instruction is ✞ AJMP page <number from 0 to 7> ✌ ✆ 2.1.5 ANL It is short name of BITWISE AND. If ANL uses registers or register address as second operand then it is one byte long instruction, otherwise it is two byte long instruction. The syntax of the instruction is ✞ 1 ANL <operand a>,<operand b> ✌ ✆ This instruction is used in following ways ✞ 1 ANL <Internal RAM Address >,A ANL <Internal RAM Address >,#datae 3 ANL A,# data ANL A,<Internal RAM Address > 5 ANL A,@R0 ANL A,@R1 7 ANL A,Rn ANL C,<bit Address > 9 ANL C,/<bit Address > ✌ ✆ This instruction performs a bitwise “AND” operation between two operands. The result is saved in accumulator or Carry flag. The second operand remains unaffected. Logical “AND” gives result if both of the comparing bit are high, otherwise bit is cleared. Illustrated Example Bitwise AND is performed between bit of two num- bers rather than number itself as a whole. The hexadecimal numbers F2H and 20H are equals to their binary values 11110010 and 00100000. The Bit- wise AND is performed bit-by-bit for all the corresponding bit of these two binary numbers as shown below:
  • 43. 2.1. INSTRUCTION SETS 43 ✞ 1 F2H = 11110010 20H = 00100000 3 ------------------- AND 20H = 00100000 5 -------------- ✌ ✆ In above binary AND operation, the output is binary 1 if both operands are binary 1 otherwise output is binary 0. The 8051MCU code for above AND operation is given in following example. ✞ 1 MOV A ,#0F2H ; A = 11110010 ANL A ,#20H ; A = 00100000 3 END ; End of program ✌ ✆ 2.1.6 CJNE It is acronym of COMPARE AND JUMP IF NOT EQUAL. It is three byte long instruction. The syntax of the instruction is ✞ 1 CJNE <accumulator >,<data or address >,<relative address > ✌ ✆ This instruction is used in following ways: ✞ 1 CJNE A,#data ,<location > CJNE A,<Internal RAM Address >,<location > 3 CJNE A,@R0 ,<location > CJNE A,@R1 ,<location > 5 CJNE A,Rn ,<location > ✌ ✆ CJNE compares the value of ‘accumulator’ with ‘data’ or value at ‘address’ and branches to the indicated ‘relative address’ if ‘accumulator’ and ‘data’ or value at ‘address’ are not equal otherwise program moves to next instruc- tion. The Carry bit (C) is set if ‘accumulator’ is less than ‘data’ or value at ‘address’, otherwise it is cleared. CY = 1 if A < Data CY = 0 if A ≥ Data CJNE either sets or clears the Carry bit so we can use JC, JNC, JB, JNB and JBC etc bit addressable instructions. For this purpose, CJNE is used as
  • 44. 44 Instructions ✞ 1 CJNE A,#data ,THERE THERE: 3 JC HERE HERE: 5 END ✌ ✆ Illustrated Example Go through the following example: ✞ 1 MOV R1 ,#12H MOV R2 ,#25H 3 MOV R4 ,#10H MOV A,R1 5 CJNE A ,#12H,HERE MOV B,R2 7 CJNE A ,#11H,HERE MOV B,R4 9 HERE: END ✌ ✆ First, registers R1, R2 and R4 are stored by hex values 12H, 25H and 10H respectively. Now the value of register R1 is copied into accumulator A. Ac- cumulator value is compared with 12H. If both values are equal then there is no conditional jump to the address of HERE label and next instruction MOV B,R2 is executed. In the next instruction accumulator value is com- pared with 11H. Here both values are not equal hence, here a conditional jump occurs to the address of label HERE. The instruction ✞ MOV B,R4 ✌ ✆ is skipped here. 2.1.7 CLR It is acronym of CLEAR. It is two bytes long if it is used in bit addressable mode, otherwise it is one byte long. CLR clears (sets to 0) all the bit(s) of the indicated register. If the operand is a bit (including the carry bit), only that specified bit is affected. Clearing the Accumulator sets the Accumulator’s value to 0.
  • 45. 2.1. INSTRUCTION SETS 45 ✞ 1 CLR bit ;Bit CLR C ;Carry flat 3 CLR A ; Accumulator ✌ ✆ Illustrated Example In the following example, Accumulator, Carry flag and LSB of port P0 are cleared (set to zero) by using CLR instruction. ✞ 1 MOV A ,#0FFH ; A = 11111111 CLR A ; A = 00000000 3 SETB C ; Set carry flag high CLR C ; C = 1 5 CLR P0.0 ; P0= 11111110 END ; End of program ✌ ✆ 2.1.8 CPL It is acronym of COMPLEMENT. It complements the current value of bit or register, i.e. it converts binary 0 to binary 1 and vice-versa. It is two bytes long instruction if it is used as bit addressable mode, otherwise it is one byte long instruction. ✞ CPL bit ;Bit 2 CPL C ;Carry flat CPL A ; Accumulator ✌ ✆ Illustrated Example CPL function toggles a bit or a register value. In the following example, accumulator, Carry flag and LSB of port P0 are comple- mented by instruction CPL. ✞ 1 MOV A ,#0FFH ; A = 11111111 CPL A ; A = 00000000 3 CPL C ; C = 1 CPL P0.0 ; PO= 11111110 5 END ; End of program ✌ ✆
  • 46. 46 Instructions 2.1.9 DA It is short form of DECIMAL ADJUST ACCUMULATOR. It is one byte long instruction. It is just called as DA. This instruction works only with Accumulator (A). 1. The contents of the accumulator are changed from a binary value to two 4-bit binary coded decimal (BCD) digits. This is the only instruction that uses the auxiliary flag to perform the binary to BCD conversion, and the conversion procedure is described below. 2. If the value of the low-order 4-bits in the accumulator is greater than 9 or if Carry flag is set, the instruction adds 6 to the low-order four bits. 3. If the value of the high-order 4-bits in the accumulator is greater than 9 or if the Carry flag is set, the instruction adds 6 to the high-order four bits. The Carry bit (C) is set if the resulting value is greater than 0×99, otherwise it is cleared. In hexadecimal number system, unit and tenth place digit ranges from 0 to F but in decimal form of number system, unit and tenth place digit ranges from 0 to 9. If unit place number is larger than 9 then 6 is added to it and carry is transferred to the tenth place digit. When we convert the hexadecimal number by using DA instruction into BCD form of number, the unit and tenth place digits are changes and they represent the equivalent decimal number as 10 × t + d. Here, t is tenth place digit and d is unit place digit. In hex form of number representation, the maximum possible unit and tenth place digits shall be F. ✞ 1 +---------------+---------------+ | Tenth place | Unit place | 3 +---------------+---------------+ | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 5 +---------------+---------------+ | F | F | 7 +---------------+---------------+ ✌ ✆ But in BCD form of decimal numbers, the maximum binary arrangement shall be as shown below. ✞ 1 +---------------+---------------+
  • 47. 2.1. INSTRUCTION SETS 47 | Tenth place | Unit place | 3 +---------------+---------------+ | 1 | 0 | 0 | 1 | 1 | 0 | 0 | 1 | 5 +---------------+---------------+ | 9 | 9 | 7 +---------------+---------------+ ✌ ✆ This is why, when unit place digit becomes larger than 9 then 6 (binary 0110) is added. Addition of 6 in the LSB bit in hex larger than 9 returns the remainder as per mathematics d = (LSB + 06)%16 Carry is transferred to the left place digit. Consider a decimal number 90 (hex 5A) represented as ✞ 1 +---------------+---------------+ | Tenth place | Unit place | 3 +---------------+---------------+ | 0 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 5 +---------------+---------------+ | 5 | A | 7 +---------------+---------------+ ✌ ✆ The four LSB bit represents to equivalent decimal 10 or hex A (binary 1010). To convert it in BCD form, decimal 6 or hex 06 (binary 0110) is added to it. Now the unit place digit becomes ✞ 1 1010 + 0110 3 ------- (1) 0000 5 ------- ✌ ✆ This carry (1) is added to four MSB bit as ✞ 1 0101 + 0001 3 ------- (0) 0110 5 ------- ✌ ✆
  • 48. 48 Instructions Now the final number after DA instruction is ‘0110 0000’ which is decimal equivalent to 60. 2.1.10 DEC It is short form of DECREMENTS. It is two bytes long instruction if it is used with internal RAM Address, otherwise it is one byte long instruction. It decrements the value of operand by 1. If initial value is 0 then DEC cause to reset the operand to FF in hex, i.e. operand overflows. It uses one Machine Cycle (MC). This instruction is used in either of the following forms. ✞ 1 DEC A DEC <Internal RAM Address > 3 DEC @R0 DEC @R1 5 DEC Rn ✌ ✆ Illustrated Example Accumulator has stored the value FFH and it is subsequently decreases three times by one. The final value on accumulator is FCH. ✞ 1 MOV A,#0 FFH ; A = 11111111 DEC A ; A = 11111110 3 DEC A ; A = 11111101 DEC A ; A = 11111100 5 END ; End of program ✌ ✆ 2.1.11 DIV It is short form of DIVISION. One byte long instruction. It divides primary accumulator A by secondary accumulator B. It affects Carry and Over flow flags to reflect the result. It is used as ✞ 1 DIV AB ✌ ✆ The quotient is placed in accumulator A and remainder is placed in the sec- ondary accumulator B. This instruction always clears the Carry flag. Over- flow flag is set if division is attempted by 0 otherwise it is cleared. Illustrated Example
  • 49. 2.1. INSTRUCTION SETS 49 ✞ 1 MOV A ,#0FFH ; A = 11111111 MOV B ,#10H ; B = 00001010 3 DIV AB ; A = 00001111 & B = 00001111 END ; End of program ✌ ✆ 2.1.12 DJNZ It is short name of DECREMENT AND JUMP IF NOT ZERO. Three bytes long instruction if uses internal RAM Address otherwise it is two bytes long instruction. Machine Cycle (MC) used by this instruction is 2. It is used as either of the any forms given below. ✞ DJNZ <Int RAM Addr >,<relative address > 2 DJNZ Rn,<relative address > ✌ ✆ DJNZ decrements the value of register by 1. If the initial value of register is 0, decrementing register value by one resets this register to 255 (0×FF). If the new value of register is not 0 the program will branch to the address indicated by ‘relative address’. If the new value of register is 0 program flow continues with the instruction following the DJNZ instruction. Illustrated Example In the following example, port P1 is assign a hex value 55H. RAM memory location identified by 32H address is storing decimal value 200. A loop is created to decrements the value stored in this address and jump to LOOP1 is value at this memory address is not zero. Thus a total 200 loops are performed by the following example. ✞ MOV P1 ,55H 2 MOV 32H ,#200 LOOP1: 4 CPL P1.1 DJNZ 32H,LOOP1 6 END ✌ ✆ Another DJNZ example: ✞ MOV A ,#12H ; A = 00010010 2 MOV R0 ,#00H ; R0= 00000000 MOV R2 ,#05H ; R2= 00000101 Counter 4 LCALL DELAY ; Long Call to DELAY
  • 50. 50 Instructions INC R0 ; Increment R0 6 DJNZ A, HERE ; If A not zero , Jump to HERE HERE: ; HERE sub routine 8 MOV R1,A ; Copy value of A in R1 JMP THEEND ; JUMP to THEEND 10 DELAY: ; DELAY sub routine MOV R3 ,#00H ; Set R3 to 0 12 MOV P0,R3 ; Change full port P0 as output port RLC A ; A = 00100100, if first loop. In - 14 ; corresponding loops A has values - ; according to RLC A 16 MOV P0.1 ,C ; Copy carry flag to outport port P0.1 DJNZ R2 , DELAY ; If R2 is not zero flow the loop DELAY 18 RET ; Return the control to LCALL caller THEEND: ; The end sub routine 20 END ; End of the program ✌ ✆ 2.1.13 INC It increments the value of register by 1. It is two bytes long instruction if uses Internal RAM Address otherwise it is one byte long instruction. It is used in any of the following methods. ✞ INC A 2 INC <Internal RAM Address > INC @R0 4 INC @R1 INC Rn 6 INC DPTR ✌ ✆ If the initial value of register is 255 (0×FF), incrementing the value will cause it to reset to 0. In the case of “INC DPTR”, the value two-bytes unsigned integer value of DPTR is incremented. If the initial value of DPTR is 65535 (0×FFFF), incrementing the value will cause it to reset to 0. Again, the Carry Flag is NOT set when the value of DPTR “rolls over” from 65535 to 0. In the following example, increment of accumulator take place four times. ✞ MOV A ,#12H ; A = 0001 0010
  • 51. 2.1. INSTRUCTION SETS 51 2 INC A ; A = 0001 0011 INC A ; A = 0001 0100 4 INC A ; A = 0001 0101 INC A ; A = 0001 0110 6 END ✌ ✆ 2.1.14 JB JB describes to JUMP IF BIT SET. It is a three bytes long instruction. It is used as ✞ JB <bit address >,<relative address > ✌ ✆ It jumps to the ‘relative address’ if bit is set otherwise program executes con- tinue with the instruction following the JB instruction. This is an instruction which can be used to check the overflow flag via PSW register and jump to a relative address. The overflow (OV) flag is represented by ‘PSW.2’. Syntax for OV flag and JB instruction is ✞ 1 JB PSW.2 ,<relative address > ✌ ✆ Illustrated Example See the example below: ✞ 1 MOV A ,#12H ; Set A to 12H MOV P0 ,#00H ; Clear the port P0 3 JB P0.1 ,IFBIT ; If second bit of port P0 is hight then ; jump to IFBIT label. Here no jump occurs 5 SETB P0.1 ; Set second bit of port P0 high JB P0.1 ,IFBIT ; If second bit of port P0 is hight then 7 ; jump to IFBIT label. Here jump occurs MOV R0 ,#0 FFH ; No execution 9 MOV R1 ,#0FH ; No execution IFBIT: ; IFBIT label 11 END ; End of program ✌ ✆ In above example, Accumulator and port P0 are added the values 12H and 00H. JB is used to check the D1 bit of port P0. If D1 bit of port P0 is high then pointer jumps to IFBIT label address.
  • 52. 52 Instructions 2.1.15 JBC JBC is short form of JUMP IF BIT SET AND CLEAR BIT. It is a three bytes long instruction. It is used as ✞ 1 JBC <bit address >,<relative address > ✌ ✆ It jumps to the ‘relative address’ if bit is high, i.e. is ‘1’, otherwise program executes continue to the instruction following the JBC instruction. It also reset the bit after execution of the instruction. Illustrated Example See the example below: ✞ 1 MOV A ,#12H ; Set A to 12H MOV P0 ,#00H ; Clear the port P0 3 JBC P0.1 ,IFBIT ; If second bit of port P0 is hight then ; jump to IFBIT label. Here no jump occurs 5 SETB P0.1 ; Set second bit of port P0 high JBC P0.1 ,IFBIT; If second bit of port P0 is hight then 7 ; jump to IFBIT label. Here jump occurs ; The target bit is reset to zero 9 MOV R0 ,#0 FFH ; No execution MOV R1 ,#0FH ; No execution 11 IFBIT: ; IFBIT label END ; End of program ✌ ✆ In above example, Accumulator and port P0 are added the values 12H and 00H. JBC is used to check the D1 bit of port P0. If D1 bit of port P0 is high then pointer jumps to IFBIT label address. Before jumping to the relative address, the corresponding conditional bit (here, D1 bit of port P0) is cleared. 2.1.16 JC JC instruction describes to JUMP IF CARRY. It is two bytes long instruc- tion. It checks the status of carry bit when executed, and jumps to the address specified by the operand if carry bit is high. It is used as ✞ JC <address > ✌ ✆ If the Carry Bit is not set program execution continues with the instruction following the JC instruction.
  • 53. 2.1. INSTRUCTION SETS 53 ✞ 1 MOV A ,#12H ; Set A to 12H CLR C ; Clear carry flag 3 JC IFCARRY ; If carry flag is high or set to 1, jump ; to IFCARRY label. Here no jump occurs 5 SETB C ; Set carry flag high JC IFCARRY ; If carry flag is high or set to 1, jump 7 ; to IFCARRY label. Here jump occurs MOV R0 ,#0 FFH ; No execution 9 MOV R1 ,#0FH ; No execution IFCARRY : ; IFCARRY label 11 END ; End of program ✌ ✆ 2.1.17 JMP It describes to JUMP TO DATA POINTER PLUS ACCUMULATOR. It is one byte long instruction. It jumps to the address represented by the sum of the value of DPTR and the value of the accumulator specified by operand. It is used as ✞ 1 JMP @A+DPTR ✌ ✆ Example is given below: ✞ 1 MOV A ,#12H ; Set A to 12H JMP IFNOBIT ; Jump to IFNOBIT label 3 MOV R0 ,#0 FFH ; No execution MOV R1 ,#0FH ; No execution 5 IFNOBIT : ; IFNOBIT label END ; End of program ✌ ✆ 2.1.18 JNB It specified to JUMP IF BIT NOT SET. It is three bytes long bit addressable instruction. It is used like ✞ JNB <bit address >,<relative address > ✌ ✆ This instruction lets the jump to program to specified address if the indicated bit is not set otherwise program executes continuously.
  • 54. 54 Instructions Illustrated Example If bit is low, the instruction let the jump to the program pointer to the label or to the address location. Here, Carry flag is used to for comparing its bit level by JNB. ✞ 1 MOV A ,#12H ; Set A to 12H SETB C ; Set carry flag bit to high 3 JNB C,IFNOBIT ; If bit is low or set to 0 jump ; to IFNOBIT label. Here no jump occurs 5 CLR C ; Clear carry flag bit JNB C,IFNOBIT ; If bit is low or set to 0, jump 7 ; to IFNOBIT label. Here jump occurs MOV R0 ,#0 FFH ; No execution 9 MOV R1 ,#0FH ; No execution IFNOBIT : ; IFNOBIT label 11 END ; End of program ✌ ✆ 2.1.19 JNC It specified to JUMP IF CARRY NOT SET. Two byte long instruction. Used as ✞ 1 JNC <relative address > ✌ ✆ This instruction let the jump to program to specified address if the carry flag is not set otherwise executes continuously. Illustrated Example In this example, JNC compare to carry bit. If carry bit is low, this instruction jumps to the program pointer to the label or to the address location. If it is high, program executed continuously to the next instructions. ✞ 1 MOV A ,#12H ; A = 00010010 CLR C ; CY = 0 3 RLC A ; A = 00100100 & CY = 0 JC ENDHERE ; If CY = 1 5 HERE: RLC A ;1. A = 01001000 & CY = 0 7 ;2. A = 10010000 & CY = 0 ;3. A = 00100000 & CY = 1 9 JNC HERE ; If CY = 0 ENDHERE :
  • 55. 2.1. INSTRUCTION SETS 55 11 MOV R0 ,A ; R0 = 00100000 & CY = 1 END ✌ ✆ 2.1.20 JNZ It is short name to JUMP IF ACCUMULATOR IS NOT ZERO. It is two bytes long instruction and used as ✞ JNZ <relative address > ✌ ✆ This instruction let the jump to program to specified address if the accumu- lator contains any value except zero otherwise executes continuously. Illustrated Example JNZ jumps to a pointer or label when accumulator is not zero. In the following program, accumulator is stored value 12H. Accumulator is decrease by one and JNZ compare its value with zero. If accumulator is not zero then the JNZ instruction jumps to the program pointer to the label HERE. ✞ 1 MOV A ,#12H ; A = 00010010 MOV R0 ,#00H ; R0 = 00000000 3 HERE: DEC A ; Decrease A by one 5 INC R0 ; R0++ JNZ HERE ; If A>0 7 END ; End of program ✌ ✆ 2.1.21 JZ It specified to JUMP IF ACCUMULATOR IS ZERO. It is two bytes long instruction. Its operand is always accumulator. As there is no zero flag in 8051 MCU hence, accumulator’s zero value is used for comparison to zero. Used as ✞ 1 JZ <relative address > ✌ ✆ This instruction let the jump to program to specified address if the accumu- lator contains zero value otherwise executes continuously.
  • 56. 56 Instructions Illustrated Example In this example, accumulator is stored value 02H which is decremented one by one. Each decrements is followed by JZ instruc- tion. When accumulator decremented to zero, JZ instruction is executed and program counter reached to HERE label. ✞ 1 MOV A ,#02H ; A = 0000 0010 JZ HERE ; No jump 3 DEC A ; A = 0000 0001 JZ HERE ; No jump 5 DEC A ; A = 0000 0000 JZ HERE ; Jump to HERE Label 7 DEC A ; No execution HERE: ; HERE label 9 MOV A,#0 FFH; Set A = 1111 1111 END ; End of program ✌ ✆ 2.1.22 LCALL It specified to LONG CALL. It is three bytes long instruction. Used as ✞ LCALL <code address > ✌ ✆ LCALL calls a program subroutine. LCALL increments the program counter by 3 and pushes that value onto the stack. The Program Counter is then set to the 16-bit value which follows the LCALL opcode, causing program execution to continue at that address. The target address for LCALL may be anywhere within the 64K byte address. Illustrated Example ✞ 1 MOV A ,#12H ; A = 00010010 MOV R0 ,#00H ; R0= 00000000 3 MOV R2 ,#05H ; R2= 00000101 Counter LCALL DELAY ; Long Call to DELAY 5 INC R0 ; Increment R0 DJNZ A, HERE ; If A not zero , Jump to HERE 7 HERE: ; HERE sub routine MOV R1,A ; Copy value of A in R1 9 JMP THEEND ; JUMP to THEEND DELAY: ; DELAY sub routine 11 MOV R3 ,#00H ; Set R3 to 0
  • 57. 2.1. INSTRUCTION SETS 57 MOV P0 ,R3 ; Change full port P0 as output port 13 RLC A ; A = 00100100, if first loop. In ; consecutive loops , A has values 15 ; according to RLC A MOV P0.1 ,C ; Copy carry flag to outport port P0.1 17 DJNZ R2, DELAY ; If R2 is not zero flow the loop DELAY RET ; Return the control to LCALL caller 19 THEEND: ; The end sub routine END ; End of the program ✌ ✆ 2.1.23 LJMP It specified to LONG JUMP. It is a three bytes long instruction. First byte is opcode and next two bytes are address of location where jump occurs. The maximum range of jump by this instruction is 0000H to FFFFH. Machine Cycle (MC) used by this instruction is 2. It is used as ✞ LJMP <code address > ✌ ✆ LJMP instructs to the program to jump unconditionally to the specified code address. 2.1.24 MOV It copies the contents into destination register directly, indirectly or imme- diately. It also copies data from source register to destination register with some restrictions. Machine Cycle (MC) used by this instruction is 1. Its syntax is ✞ 1 MOV <destination >,<source > ✌ ✆ The destination may be a count number or hex number. For example ✞ 1 MOV 32 ,#200 ✌ ✆ Here, memory location is specified in counting number. It stores value 200 in the at 32nd register of RAM’s memory addresses. If second operand is a
  • 58. 58 Instructions bit, then MOV instruction uses bit addressing of RAM register method to store the data. For Example, ✞ 1 MOV 05,P1.0 ✌ ✆ In above example, second operand is a bit hence MOV instruction copies the D0 bit state of port 1 into the 05th bit started from the 32nd register of the RAM or from 0×20 address of the RAM (See RAM section). Again in the example given below: ✞ 1 MOV 32H ,#200 ✌ ✆ Here memory location is specified in hex numbers. It stores value 200 in the at 3 × 161 + 2 × 160 = 50th register of RAM’s memory addresses. The MOV instruction uses direct addressing mode and register addressing mode to address a register. The register bank locations are accessed by the register names. For example ✞ 1 MOV A,4 ; Direct addressing mode MOV A,R4; Register addressing mode. 3 ; Both are same ✌ ✆ Other method of use of MOV instructions are given below: ✞ 1 MOV @Rn ,# data ;2 byte long instruction MOV @Rn ,A ;1 byte long instruction 3 MOV @Rn ,<Internal RAM Address > ;2 byte long instruction ✌ ✆ Here only registers R0 and R1 are pointer addressable. Therefore, @Rn meant for @R0 and @R1. ✞ 1 MOV A,# data ;2 byte long instruction MOV A,@Rn ;1 byte long instruction 3 MOV A,Rn ;1 byte long instruction MOV A,<Internal RAM Address > ;2 byte long instruction 5 MOV C,<bit Address > ;2 byte long instruction MOV <bit address >,C ;2 byte long instruction ✌ ✆ In above method of application of MOV instruction, accumulator can be loaded either by direct addressing or by indirect addressing. Note that Carry flag can be loaded with only bit value not with a byte value.
  • 59. 2.1. INSTRUCTION SETS 59 ✞ MOV DPTR ,# data ;3 byte long instruction ✌ ✆ Data pointer can handle two byte long data, hence while copying data to and from DPTR, the source and destination shall of equal size. ✞ 1 MOV Rn ,# data ;2 byte long instruction MOV Rn ,A ;1 byte long instruction 3 MOV Rn ,<Internal RAM Address > ;2 byte long instruction ✌ ✆ All registers can accept data from direct addressing method or they can the stored data stored in the internal RAM location. ✞ 1 MOV <Internal RAM Address >,#data ;3 byte long instruction MOV <Internal RAM Address >,@Rn ;2 byte long instruction 3 MOV <Internal RAM Address >,Rn ;2 byte long instruction MOV <Internal RAM Address >,A ;2 byte long instruction 5 MOV <Int RAM Addr >,<Int RAM Addr > ;2 byte long instruction ✌ ✆ MOV also copies data into internal ram address from registers, accumulators and MOV can also copy data between internal RAM addresses. Source value during the execution of this instruction does not change. No flags are affected unless the instruction is moving the value of a bit into the carry bit in which case the carry bit is affected or unless the instruction is moving a value into the PSW register. Movement of data between registers by this instruction is not allowed. Illustrated Example See the following example, in which MOV instructino is used to copy the data in accumulator and registers. ✞ 1 MOV A ,#20H ; A = 00100000 CLR C ; CY = 0 3 ADDC A ,#20H; A+CY +20H = 01000000 MOV R0 ,A ; R0 = 01000000 5 SETB C ; CY = 1 ADDC A ,#20H; A+CY +20H = 01100001 7 MOV R1 ,A ; R1 = 01100001
  • 60. 60 Instructions END ; End the program ✌ ✆ 2.1.25 MOVC It is short name of MOVE CODE BYTE TO ACCUMULATOR. It is one byte long instructio and can read internal code memory. Its syntax is ✞ MOVC <destination >, <source > ✌ ✆ This instruction is used in the form as given below. ✞ 1 MOVC A,@A+DPTR MOVC A,@A+PC ✌ ✆ MOVC moves a byte from Code Memory into the Accumulator. The Code Memory address from which the byte will be moved is calculated by summing the value of the Accumulator with either DPTR or the Program Counter (PC). In the case of the Program Counter, PC is first incremented by 1 before being summed with the Accumulator. ✞ ORG 0 2 MOV DPTR ,# MYDATA B1: CLR A 4 MOVC A,@A+DPTR MOV P0,A 6 JZ EXIT INC DPTR 8 SJMP B1 EXIT: 10 SJMP $ ORG 30H 12 MYDATA: DB ’This is my world’ ,0 14 END ✌ ✆ 2.1.26 MOVX It is MOVE DATA TO/FROM EXTERNAL MEMORY (XRAM). It is one byte long instruction. Its syntax is
  • 61. 2.1. INSTRUCTION SETS 61 ✞ MOVX <destination >,<source > ✌ ✆ This instruction is used in the form as given below. ✞ 1 MOVX @DPTR ,A MOVX @R0 ,A 3 MOVX @R1 ,A MOVX A,@DPTR 5 MOVX A,@R0 MOVX A,@R1 ✌ ✆ MOVX moves a byte to or from External Memory into or from the Accu- mulator. If destination is @DPTR, the Accumulator is moved to the 16-bit External Memory address indicated by DPTR. This instruction uses both P0 (port 0) and P2 (port 2) to output the 16-bit address and data. If source is DPTR then the byte is moved from External Memory into the Accumulator. If destination is @R0 or @R1, the Accumulator is moved to the 8-bit Ex- ternal Memory address indicated by the specified Register. This instruction uses only P0 (port 0) to output the 8-bit address and data. P2 (port 2) is not affected. If source is @R0 or @R1 then the byte is moved from External Memory into the Accumulator. ✞ XDATA EQU 2000H 2 XBYTES EQU 10 MOV DPTR ,# XDATA 4 MOV R2 ,# XBYTES AGAIN: 6 MOVX A,@DPTR MOV P1 ,A 8 INC DPTR DJNZ R2,AGAIN 10 END ✌ ✆ 2.1.27 MUL It is short form of MULTIPLY. One byte long instruction. It multiplies accumulator with secondary accumulator. Machine Cycle (MC) used by this instruction is 4. Its syntax is used as
  • 62. 62 Instructions ✞ MUL AB ; B source (times) and A destination (number) 2 ; AB = B x A ( mathematically) ; So , A is lower level. ✌ ✆ It multiples the unsigned value of the Accumulator by the unsigned value of the “B” register. The least significant byte of the result is placed in the Accumulator and the most-significant-byte is placed in the “B” register. In this operation, Carry flag is always reset to 0. The Overflow Flag is set if the result is greater than 25, otherwise it is cleared. Illustrated Example Assume two hex numbers 5FH and 10H whose byte by byte product is 5FH × 10H = 5F0H. Accumulator stores lower byte while secondary accumulator stores upper byte. ✞ 1 MOV A,#5FH ; A = 01011111 MOV B ,#10H ; B = 00001010 3 MUL AB ; A = 11110000 & B = 00000101 END ; End of program ✌ ✆ 2.1.28 NOP NOP, is acronym of NO OPERATION. It is one byte long instruction. NOP is generally used only for timing purposes. Absolutely no flags or registers are affected. Machine Cycle (MC) used by this instruction is 1. NOP is an instruction that tells to MCU that there is nothing to execute. This instruction is just skipped by the MCU. This operation is useful in delay algorithm where each NOP delays the time equivalent to one clock cycle. Illustrated Example In the following example, there are three preceding and two succeeding NOP instruction to the MOV instruction. The preceding instructions are skipped by the MCU and it takes three clock cycles to reach MOV instruction. After entertaining to MOV instruction, MCU further waits for two clock cycles before it reached to END instruction and execute it. ✞ NOP 2 NOP NOP 4 MOV R3 ,#15H NOP
  • 63. 2.1. INSTRUCTION SETS 63 6 NOP END ✌ ✆ 2.1.29 ORG ORG instruction is used to relocate the location of start of a program. It sets the location counter to the value specified by the operand expression. It is included in a program as first instruction. If no ORG instruction is included before the first instruction or data type in the program, assembly begins at location zero. Multiple ORG instructions can be included in a single program. ✞ 1 ORG 0000H ;Loading address LJMP MAIN ;Jump to main program 3 ORG 0013H ;Timer 0 interrupt address 5 SETB P2.1 ;Set high to D1 pin of port P2 MOV R3 ,#10H ;Store R3 with hex 10H. 7 BACK: DJNZ R3 ,BACK;Loop upto 16 times CLR P2.1 ;Clear the D2 pin of port P2 9 RETI ;Return from interrupt 11 ORG 0030H ;Main program location MAIN: 13 MOV IE ,#84H ;HW based interrupt , EA high , EX1 HERE:SJMP HERE ;Return to BACK subroutine 15 END ✌ ✆ 2.1.30 ORL ORL is short name of Bitwise OR. The OR output is binary 1 if either of the two operands or both operands are binary 1 and binary 0 otherwise. It is a binary operator and it operates on binary bit of the operands. See the example below, in which two integer values 37 and 82 undergone binary OR operation. First, these two decimal numbers are converted into their binary equivalent and then bit-by-bit OR operation is performed. The binary result is again converted into decimal equivalent. Here, the decimal equivalent of binary result of OR operation on 37 and 82 is 119.
  • 64. 64 Instructions ✞ 1 0010 0101 : X=37 0101 0010 : Y=82 3 ---------------- OR 0111 0111 : 119 ✌ ✆ This operator ORed the value at first register with the value at second register and stores the result in first register. ✞ ORL <Internal RAM Address >,A ;2 byte long instruction. 2 ORL <Internal RAM Address >,#data ;3 byte long instruction. ORL A,# data ;2 byte long instruction. 4 ORL A,<Internal RAM Address > ;2 byte long instruction. ORL A,@R0 ;1 byte long instruction. 6 ORL A,@R1 ;1 byte long instruction. ORL A,Rn ;1 byte long instruction. 8 ORL C,<bit address > ;2 byte long instruction. ✌ ✆ See the example given below: ✞ ;10H = 00010000 2 MOV A ,#10H ;Copy hex 10H into ACC ;11H = 00010001 4 MOV R1 ,#11H ;Copy hex 11H into R1 ;10H = 00010000 6 ;11H = 00010001 ;----------------OR 8 ;11H = 00010001 ORL A,R1 ;Get OR of 10H and 11H ✌ ✆ 2.1.31 POP It is two bytes long instruction. Its syntax is
  • 65. 2.1. INSTRUCTION SETS 65 ✞ 1 POP <Internal RAM Address > ✌ ✆ POP removes the last value placed on a stack and copied it into the internal RAM address specified by its operand. Address of last value at stack pointer is decreased by one after each POP. ✞ 1 ;Begining of stack pointer register in RAM memory MOV SP ,#31H 3 ;Store data into registers MOV R2 ,#25H 5 MOV R1 ,#12H MOV R4 ,#10H 7 ;Push data from register R2 into stack ;pointer addressed at 31H in internal RAM 9 PUSH 2 ; 25H ;Push data from register R1 into address 11 ;of next address of stack pointer PUSH 1 ; 12H 13 ;Push data from register R4 into address ;of next address of stack pointer 15 PUSH 4 ; 10H ;Pop last value from stack pointer 17 ;into the addressed at 09H in RAM ;Here last value on stack is 10H 19 POP 09H ;Pop next last value from stack pointer 21 ;into the addressed at 10H in RAM ;Here next value in stack is 12H 23 POP 10H ;Pop next last value from stack pointer 25 ;into the addressed at 11H in RAM ;Here next value in stack is 25H 27 POP 11H ✌ ✆
  • 66. 66 Instructions x0 x1 x2 x3 x4 x5 x6 x7 00 00 12 25 10 00 00 00 00 08 00 10 00 00 00 00 00 00 10 12 25 00 00 00 00 00 00 18 00 00 00 00 00 00 00 00 20 00 00 01 00 00 00 00 00 28 00 00 00 00 00 00 00 00 30 00 00 25 12 10 00 00 00 ... 00 00 00 00 00 00 00 00 Table 2.1: Memory result. The output can be view in the RAM address started from 32H. Only direct addressing mode is allowed for popping the stack. Note that while using POP instruction, we should have knowledge of number of elements present in stack pointer, otherwise POP starts fetching the value beyond the stake pointer values. ✞ 1 MOV SP ,#31H MOV R2 ,#25H 3 MOV R1 ,#12H MOV R4 ,#10H 5 PUSH 2 ; Value 25H PUSH 1 ; Value 12H 7 PUSH 4 ; Value 10H POP 10H ; Value 10H 9 POP 11H ; Value 12H POP 12H ; Value 25H 11 POP 13H ; Fetches value from RAM address 0x31H POP 14H ; Fetches value from RAM address 0x30H 13 POP 15H ; Fetches value from RAM address 0x2FH END ✌ ✆
  • 67. 2.1. INSTRUCTION SETS 67 2.1.32 PUSH It is two byte long instruction. Its syntax is ✞ PUSH <Internal RAM Address > ✌ ✆ PUSH “pushes” the value of the specified ‘Int. RAM Address’ onto the stack. PUSH first increments the value of the Stack Pointer by 1, then takes the value stored in ‘Int. RAM Address’ and stores it in Internal RAM at the location pointed to by the incremented Stack Pointer. ✞ 1 ;Begining of stack pointer ;register in RAM memory 3 MOV SP ,#31H ;Add data into registers 5 MOV R2 ,#25H MOV R1 ,#12H 7 MOV R4 ,#10H ;Push data from register R2 into stack 9 ;pointer at address 31H in internal RAM PUSH 2 11 ;Push data from register R1 into stack ;pointer at next address of stack pointer 13 PUSH 1 ;Push data from register R4 into stack 15 ;pointer at next address of stack pointer PUSH 4 ✌ ✆ The output can be view in the RAM address started from 32H. Only direct addressing mode is allowed for pushing the stack.
  • 68. 68 Instructions x0 x1 x2 x3 x4 x5 x6 x7 00 00 12 25 10 00 00 00 00 08 00 00 00 00 00 00 00 00 10 00 00 00 00 00 00 00 00 18 00 00 00 00 00 00 00 00 20 00 00 01 00 00 00 00 00 28 00 00 00 00 00 00 00 00 30 00 00 25 12 10 00 00 00 ... 00 00 00 00 00 00 00 00 Table 2.2: Memory result. 2.1.33 RET It is short form of RETURN FROM SUBROUTINE. It is two bytes instruc- tion. RET is used to return from a subroutine previously called by LCALL or ACALL. Program execution continues at the address that is calculated by popping the topmost 2 bytes off the stack. The most-significant-byte is popped off the stack first, followed by the least-significant-byte. Machine Cycle (MC) used by this instruction is 2. It is the last instruction of a sub-routine. It syntax is ✞ RET ✌ ✆ Illustrated Example ✞ 1 MOV A ,#12H ; A = 00010010 MOV R2 ,#05H ; R2= 00000101 Counter 3 LCALL DELAY ; Long Call to DELAY JMP THEEND ; JUMP to THEEND 5 DELAY: ; DELAY sub routine MOV R3 ,#00H ; Set R3 to 0 7 MOV P0,R3 ; Change full port P0 as output port RLC A ; A = 00100100, if first loop. In - 9 ; corresponding loops A has values -
  • 69. 2.1. INSTRUCTION SETS 69 ; according to RLC A 11 MOV P0.1 ,C ; Copy carry flag to outport port P0.1 DJNZ R2, DELAY ; If R2 is not zero flow the loop DELAY 13 RET ; Return the control to LCALL caller THEEND: ; The end sub routine 15 END ; End of the program ✌ ✆ 2.1.34 RETI It is acronym of RETURN FROM INTERRUPT. It is one byte long in- struction. RETI is used to return from an interrupt service routine. RETI first enables interrupts of equal and lower priorities to the interrupt that is terminating. Program execution continues at the address that is calculated by popping the topmost 2 bytes off the stack. The most-significant-byte is popped off the stack first, followed by the least-significant-byte. RETI functions identically to RET if it is executed outside of an interrupt service routine. It s syntax is ✞ 1 RETI ✌ ✆ On execution of RETI, TCON.1 and TCON.3 bit are cleared, indicating that the interrupt is finished and MCU is ready for another interrupt. During the execution of interrupt subroutine, interrupt pins are ideally ignored, no matter how many times and how long it makes high-to-low transitions. Illustrated Example For interrupt of MCU via Interrupt Enable (IE) Spe- cial Function Register (SFR), we use the roll over state of the Timer 0. On roll over, TF0 flag is set to high. If interrupt flag (ET0) for Timer 0 (TF0) is set high then on rollover, MCU assumed that it is interrupted for Timer 0. TF0 reset to low on call of interrupt. The MCU gets the address of ROM location fixed for Timer 0 (TF0) interrupt. It is 0003H. Now MCU executes the subroutine started from the ROM address 0×0003. On execution of RETI instruction, it returns to the original address from where it was interrupted. ✞ 1 ORG 0000H ;Loading address LJMP MAIN ;Jump to main program 3
  • 70. 70 Instructions ORG 000BH ;Timer 0 interrupt address 5 CPL P2.1 ;CPL to D1 pin of port P2 RETI ;Return from interrupt 7 ORG 0030H ;Main program location 9 MAIN: MOV TMOD ,#02H ;Timer 1 mode 2 11 MOV P0 ,#0 FFH ;Make port P0 input port MOV TH0 ,#90H ;Load TH of Timer 0 with 90H 13 ;+----------------------------------------------+ ;| IE | 15 ;+-----+-----+-----+----+-----+-----+-----+-----+ ;| EA | - | ET2 | ES | ET1 | EX1 | ET0 | EX0 | 17 ;+-----+-----+-----+----+-----+-----+-----+-----+ ;| 1 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 19 ;+-----+-----+-----+----+-----+-----+-----+-----+ MOV IE ,#82H ;SW based interrupt , EA high , 21 ;ET0 high enables interrupt ;via TF0 flag. 23 SETB TR0 ;Start Timer 0 25 BACK: MOV A,P0 ;Copy input into ACC from P0 27 MOV P1,A ;Mov data of ACC into P1 SJMP BACK ;Return to BACK subroutine 29 END ✌ ✆ There is another example in which external interrupt is applicable. ✞ 1 ORG 0000H ;Loading address LJMP MAIN ;Jump to main program 3 ORG 0013H ;Timer 0 interrupt address 5 SETB P2.1 ;Set high to D1 pin of port P2 MOV R3 ,#10H ;Store R3 with hex 10H. 7 BACK: DJNZ R3,BACK;Loop upto 16 times CLR P2.1 ;Clear the D2 pin of port P2 9 RETI ;Return from interrupt 11 ORG 0030H ;Main program location ;+----------------------------------------------+
  • 71. 2.1. INSTRUCTION SETS 71 13 ;| IE | ;+-----+-----+-----+----+-----+-----+-----+-----+ 15 ;| EA | - | ET2 | ES | ET1 | EX1 | ET0 | EX0 | ;+-----+-----+-----+----+-----+-----+-----+-----+ 17 ;| 1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | ;+-----+-----+-----+----+-----+-----+-----+-----+ 19 MAIN: MOV IE ,#84H ;HW based interrupt , EA high , 21 ;EX1 high enables interrupt ;via IE1 flag. 23 HERE:SJMP HERE ;Return to BACK subroutine END ✌ ✆ In above example, EA and EX1 flags of IE SRF are set high. EA allows interrupts and EX1 allows the external interrupt via IE1 flag which is D3 bit of TCON SRF. On interrupt via IE1 flag or TCON.3 flag, the MCU gets the address of External Hardware Interrupt 1 (INT1), which is 0013H. On inter- rupt, MCU starts execution from the address of 0×0013 and returns to the location from where it was interrupted when RETI instruction is executed. 2.1.35 RL It is short form of ROTATE ACCUMULATOR LEFT. It is one byte long instruction. Syntax for this instruction is ✞ RL A ✌ ✆ It shifts the bit of accumulator to left. The bit D7 bit is removed from its place and postfixed after the bit D0. Illustrated Example Consider an example as given below: ✞ 1 MOV A ,#25H ; A = 0010 0101 RL A ; A = 0100 1010 3 RL A ; A = 1001 0100 END ; End of program ✌ ✆ In above example, D7 bit is removed from its place and placed just after the bit D0. The new byte thus formed is the result of RL instruction. If RL instruction is performed by four times then four bit of LSB side and four bit of MSB side are swapped (like SWAP instruction).
  • 72. 72 Instructions ✞ MOV A ,#25H ; A = 0010 0101 2 RL A ; A = 0100 1010 RL A ; A = 1001 0100 4 RL A ; A = 0010 1001 RL A ; A = 0101 0010 (swapped ) 6 END ; End of program ✌ ✆ 2.1.36 RLC It is short form of ROTATE ACCUMULATOR LEFT THROUGH CARRY. It is one byte long instruction. Its syntax is ✞ RLC A ✌ ✆ It shifts the bit of the Accumulator to the left. Most significant bit D0 is loaded on Carry flag and original value of Carry flag is post-fixed after bit D0. This function can be used to quickly multiply a byte by 2. This instruction can also be used to get in or out the bytes from I/O ports or port pins through carry bit taking MSB first. Assume initially, a register has values in hex represented by bit D7 to D0 where D7 is MSB. These places are assumed as original places of data bit and Carry. The carry bit is represented by CY . D7 D6 D5 D4 D3 D2 D1 D0 CY I/O Pn After the execution of instruction RLC for the register, all bit shift to leftward. The CY bit acquires the original place of bit D0, D0 acquires the original place of D1 and so on. Bit D7 acquires the place of CY. If we want to put the MSB at output port. Instruction MOV is used as ✞ 1 MOV Pn.X ,C ✌ ✆ Here Pn is port either P0 or P2, X is any port bit of its 8 port bits. By this way at output port, bit D7 is obtained.
  • 73. 2.1. INSTRUCTION SETS 73 D6 D5 D4 D3 D2 D1 D0 CY D7 I/O Pn D7 After the execution of instruction RLC for the register again, all bit shift to leftward. The CY bit acquires the original place of bit D1, D1 acquires the original place of D2, D7 acquires the original place of D0 and so on. Bit D6 acquires the place of CY. If we want to put the MSB at output port. Instruction MOV is used as ✞ 1 MOV Pn.X ,C ✌ ✆ By this way at output port, bit D6 is obtained. Now, two bit D7 and D6 are obtained at output port in which MSB comes first. D5 D4 D3 D2 D1 D0 CY D7 D6 I/O Pn D7 D6 Next step is performed similarly and three consecutive bit are obtained at output port in which MSB comes first. D4 D3 D2 D1 D0 CY D6 D7 D5 I/O Pn D7 D6 D5 Illustrated Example ✞ 1 MOV A ,#25H ; A = 00100101, C=0 RLC A ; A = 01001010, C=0 3 RLC A ; A = 10010100, C=0
  • 74. 74 Instructions RLC A ; A = 00101000, C=1 5 RLC A ; A = 01010001, C=0 RLC A ; A = 10100010, C=0 7 END ; End the program ✌ ✆ 2.1.37 RR It is acronym of ROTATE ACCUMULATOR RIGHT. It is one byte long instruction. Syntax of this instruction is ✞ 1 RR A ✌ ✆ It shifts the bit of the Accumulator to the right. The least significant bit D0 is removed from its place and prefixed before the most significant bit D7. Illustrated Example Consider an example as given below: ✞ 1 MOV A ,#25H ; A = 0010 0101 RR A ; A = 1001 0010 3 RR A ; A = 0100 1001 END ✌ ✆ In above example, D0 bit is removed from its place and placed just before the bit D7. The new byte thus formed is the result of RR instruction. If RR instruction is performed by four times then four bit of LSB side and four bit of MSB side are swapped (like SWAP instruction). ✞ MOV A ,#25H ; A = 0010 0101 2 RR A ; A = 1001 0010 RR A ; A = 0100 1001 4 RR A ; A = 1010 0100 RR A ; A = 0101 0010 (swapped ) 6 END ✌ ✆ 2.1.38 RRC It is short form of ROTATE ACCUMULATOR RIGHT THROUGH CARRY. It is one byte long instruction. Its syntax is ✞ RRC A ✌ ✆
  • 75. 2.1. INSTRUCTION SETS 75 It shifts the bit of the Accumulator to the left. Most significant bit D0 is loaded on Carry flag and original value of Carry flag is prefixed after bit D0. This function can be used to quickly multiply a byte by 2. This instruction can also be used to get in or out the bytes from I/O ports or port pins through carry bit taking LSB first. Assume initially, a register has values in hex represented by bit D7 to D0 where D0 is LSB. These places are assumed as original places of data bit and Carry. The carry bit is represented by CY . D7 D6 D5 D4 D3 D2 D1 D0 CY I/O Pn After the execution of instruction RRC, all bit shift to rightward. D0 acquires the original place of CY , CY acquires the original place of D7 and so on. If we want to put the LSB at output port. Instruction MOV is used as ✞ 1 MOV Pn.X ,C ✌ ✆ Here Pn represents to a port of specific number n ranging from zero to four. For example, P0 is first port, P2 is third port etc. X is any port pin of its 8 port pins. By this way at output port, bit D0 is obtained. D7 D6 D5 D4 D3 D2 D1 D0 I/O Pn CY D0 After the execution of instruction RRC again, all bit shift to rightward. The CY bit acquires the original place of bit D6, D6 acquires the original place of D5, D0 acquires the original place of D7 and so on. Bit D1 acquires the original place of CY . If we want to put the next LSB at output port. Instruction MOV is used as ✞ 1 MOV Pn.X ,C ✌ ✆
  • 76. 76 Instructions By this way at output port, bit D1 is obtained. Now, two bit D0 and D1 are obtained at output port in which LSB comes first. D7 D6 D5 D4 D3 D2 D1 I/O Pn D0 CY D0 D1 By this way, we can get the other bit consecutively. ✞ 1 MOV A ,#25H ; A = 00100101, C=0 RRC A ; A = 00010010, C=1 3 RRC A ; A = 10001001, C=0 RRC A ; A = 01000100, C=1 5 RRC A ; A = 10100010, C=0 RRC A ; A = 01010001, C=0 7 END ; End the program ✌ ✆ 2.1.39 SETB It sets the carry bit or bit address. It is used as ✞ 1 SETB C ;1 byte long instruction. SETB <bit address > ;2 bytes long instruction. ✌ ✆ Illustrated Example Here is an example in which Carry flag is first clear and then it is set to high. The carry bit is sent to the first bit of port P0. ✞ MOV A ,#00H 2 CLR C SETB C 4 MOV P0,A SETB P0.1 6 END ✌ ✆
  • 77. 2.1. INSTRUCTION SETS 77 2.1.40 SJMP It is two bytes long instruction. First byte is opcode and second byte is ad- dress. SJMP jumps unconditionally to the address specified relative address. The maximum range of SHORT JUMP is from 00H to FFH. Relative address must be within -128 (for backward jump) or +127 (for forward jump) bytes of the instruction that follows the SJMP instruction. Machine Cycle (MC) used by this instruction is 2. Syntax of this instruction is ✞ SJMP <relative address > ✌ ✆ Compiler shows error as jump is out of range if ‘relative address’ is beyond the range. The following example is self explanatory. ✞ 1 MOV A ,#25 ; Accumulator HERE: ; HERE subroutine 3 CPL P1.0 ; Toggle the 1st bit of P0 port SJMP HERE ; Do it again and again 5 END ; End of program ✌ ✆ ‘$’ symbol is used for ‘stay here’ to the SJMP instruction, i.e. ‘$’ symbol tells to MCU that the jump is to the current address. Above example is modified to reflect the stay here phenomenon in the 8051MCU. ✞ 1 MOV A ,#25 ; Accumulator HERE: ; HERE subroutine 3 CPL P1.0 ; Toggle the 1st bit of P0 port SJMP $ ; Stay here. No further execution 5 SJMP HERE ; Do it again and again END ; End of program ✌ ✆ 2.1.41 SUBB It is short form of SUBTRACT FROM ACCUMULATOR WITH BORROW. Its syntax is ✞ SUBB <destination >,<source > ✌ ✆ Its destination is always accumulator while source may be data, register or Int. RAM Address. It is used as
  • 78. 78 Instructions ✞ 1 SUBB A,# data ;2 byte long instruction. SUBB A,<Internal RAM Address > ;2 byte long instruction. 3 SUBB A,@R0 ;1 byte long instruction. SUBB A,@Rn ;1 byte long instruction. ✌ ✆ It resets the Carry flag, Auxiliary Carry flag and Overflow flags accordingly to reflect the result. If destination is lesser than the source value, where values are represented in hex, it borrows the carry equal to 100H and sets the carry flag high. In any step of the borrow subtraction, previous carry is also subtracted in the current borrow subtraction. Illustrated Example Assume two one bytes long hexadecimal numbers 62H and 96H, which are to be subtracted from each other. In first case, 96H is being subtracted from 62H. ✞ 62H = 01100010 (smaller ) 2 -96H = 10010110 (larger) - 0H = 0 (Previous Carry) 4 ------------------------------- ✌ ✆ Here 62H is smaller than 96H. So there is borrows a number 100H. Now the subtraction is ✞ 100H = 100000000 2 +62H = 01100010 (smaller ) ------------------------------ 4 162H = 101100010 -96H = 10010110 (larger) 6 - 0H = 0 (Previous Carry) ------------------------------- 8 CCH = 11001100 ✌ ✆ In Borrow subtraction, on state of borrow, carry flag is set. The carry set in first borrow subtraction, it is subtracted in consecutive next subtraction. Again to subtract 2045H from 2221H we use following steps. ✞ 2221H 2 -2045H ✌ ✆ This subtraction is performed for lower and higher bytes respectively. Ini- tially, carry bit is reset to zero. Now subtraction of lower byte is
  • 79. 2.1. INSTRUCTION SETS 79 ✞ 100H = 100000000 2 +21H = 100001 (smaller ) ------------------------------ 4 121H = 100100001 -45H = 1000101 (larger) 6 - 0H = 0 (Previous Carry) ------------------------------- 8 DCH = 11011100 ✌ ✆ Now the carry is set to 1. Now the subtraction of higher byte is ✞ +22H = 00100010 (larger) 2 -20H = 00100000 (smaller ) - 1H = 1 (Previous Carry) 4 ------------------------------- 01H = 00000001 ✌ ✆ Now the subtration result is 01DCH. ✞ 1 2221H -2145H 3 ------- 01DCH ✌ ✆ The equivalent assembly code for 8051 MCU is given below: ✞ CLR C 2 MOV A ,#21H SUBB A ,#45H 4 MOV R7 ,A MOV A ,#22H 6 SUBB A ,#20H MOV R6 ,A 8 END ✌ ✆ 2.1.42 SWAP It is short form of SWAP ACCUMULATOR NIBBLES. It is one byte long instruction. It is used as
  • 80. 80 Instructions ✞ SWAP A ✌ ✆ SWAP swaps bit D0 to D3 of the Accumulator with bit D4 to D7 of the Accumulator. This instruction works only with Accumulator. Illustrated Example ✞ 1 MOV A ,#72H ;A: 0111 0010 SWAP A ;A: 0010 0111 ✌ ✆ This instruction is also equal to four times execution of RR or RL instruction. Example with RL instruction is given below. ✞ MOV A ,#72H ;A: 0111 0010 2 RL A ;A: 1110 0100 RL A ;A: 1100 1001 4 RL A ;A: 1001 0011 RL A ;A: 0010 0111 ✌ ✆ Example with RR instruction is given below. ✞ 1 MOV A ,#72H ;A: 0111 0010 RR A ;A: 0011 1001 3 RR A ;A: 1001 1100 RR A ;A: 0100 1110 5 RR A ;A: 0010 0111 ✌ ✆ 2.1.43 XCH It is acronym of EXCHANGE. It has syntax like ✞ 1 XCH <destination >,<source > ✌ ✆ It is used as ✞ 1 XCH A,@R0 ;1 byte long instruction. XCH A,@R1 ;1 byte long instruction. 3 XCH A,Rn ;1 byte long instruction. XCH A,<Internal RAM Address > ;2 byte long instruction. ✌ ✆
  • 81. 2.1. INSTRUCTION SETS 81 It exchanges the values between the accumulator and registers. This instruc- tion is also used to exchange the values between the accumulator and the RAM address indicated by the value stored in register R0 or R1. To use register R0 and R1 as RAM addresses, they are prefixed by ‘@’ symbol as @R0 and @R1. Illustrated Example Assume that accumulator has value 25H and register R0 has value 52H. On call of XCH, accumulator and register R0 exchange their values mutually. ✞ MOV A ,#25H ; A = 0010 0101 2 MOV R0 ,#52H ; R0= 0101 0010 XCH A,R0 ; A = 0101 0010 4 ; R0= 0010 0101 END ; End of program ✌ ✆ Another example is ✞ 1 MOV A ,#24H ; A=24H = 0010 0100 MOV R0 ,#65H ; Store address value to R0 3 ; Initially value at RAM address ; 65H is decimal zero (1000 1110) 5 XCH A,@R0 ; Exchange four bit from LSB side. ; Now , A = 1000 1110 and value at 7 ; RAM Address 65H = 0010 0100 END ; End the program ✌ ✆ In above example, hex value 24H is stored in accumulator. A auxiliary RAM address is stored in register R0. The value at RAM address 0×65 (equal to the value of R0) is 8E. On calling of instruction XCH, the values of accumulator and RAM address 0×65 are exchanged mutually. Now the new values of accumulator and RAM address 0×65 are 8EH and 24H respectively. @R0 in XCH represents that we have first retrieved the value from RAM address equivalent to value of R0. 2.1.44 XCHD It is short name of EXCHANGE BITS. It is one byte long instruction. Its syntax is ✞ XCHD A,[@R0/@R1] ✌ ✆
  • 82. 82 Instructions And it is used as ✞ 1 XCHD A,@R0 XCHD A,@R1 ✌ ✆ This instruction exchanges bit D0 to D3 of the Accumulator with bit D0 to D3 of the Internal RAM Address pointed to indirectly by R0 or R1. Bits D4 to D7 of each register are unaffected. ✞ MOV A ,#24H ; A=24H = 0010 0100 2 MOV R0 ,#65H ; Assign RAM address value to R0 ; Initially value at RAM Address 4 ; 0x65 is decimal zero (0000 0000) XCHD A,@R0 ; Exchange four bit from LSB side. 6 ; Now , A = 0010 0000 and value at ; RAM Address 65H = 0000 0100 8 END ; End the program ✌ ✆ In above example, hex value 24H is stored in accumulator. An auxiliary RAM address (0×65) is stored in register R0. Value at this RAM address is 00H. Now, XCHD instruction is used to exchange the 4 bit from LSB side in the values of accumulator and RAM address 0×65 (equal to the value stored in register R0). Now the new values of accumulator and RAM address 0×65 are 20H and 04H respectively. @R0 in XCHD represents that we have first retrieved the value from RAM address equivalent to value of R0. 2.1.45 XRL It describes to BITWISE EXCLUSIVE OR. It gives the exclusive OR of two given values. It is used as ✞ XRL <Internal RAM Address >,A ;2 byte long instruction. 2 XRL <Internal RAM Address >,#data ;3 byte long instruction. XRL A,# data ;2 byte long instruction. 4 XRL A,<Internal RAM Address > ;2 byte long instruction. XRL A,@R0 ;1 byte long instruction. 6 XRL A,@R1 ;1 byte long instruction. XRL A,Rn ;1 byte long instruction. ✌ ✆ The result is stored in the accumulator.
  • 83. 2.1. INSTRUCTION SETS 83 ✞ 1 MOV A ,#25H ; A = 0010 0101 MOV R0 ,#52H ; R0= 0101 0010 3 XRL A,R0 ; A = 0111 0111 END ; End of program ✌ ✆
  • 84. 84 µC Structure Instruction M Cycle Remarks Instruction M Cycle Remarks ACALL 2 MOV 1 ADD 1 MOV 2 ADDC 1 MOVC 2 AJMP 2 MOVX 2 ANL 1 MUL 4 CJNE 2 NOP 1 CJNE 2 ORL 1 CLR 1 ORL 2 CPL 1 POP 2 DA 1 PUSH 2 DEC 1 RET 2 DIV 4 RETI 2 DJNZ 2 RL 1 INC 1 RLC 1 JB 2 RR 1 JBC 2 RRC 1 JC 2 SETB 1 JMP 2 SJMP 2 JNB 2 SUBB 1 JNC 2 SWAP 1 JNZ 2 XCH 1 JZ 2 XCHD 1 LCALL 2 XRL 1 LJMP 2 XRL 2 Table 2.3: Table of Instruction Cycles
  • 85. 3.1. TIMER 85 3µC Structure 3.1 Timer The 8051 MCU has two timers known as Timer 0 and Timer 1. Each of these two timers is 16 bit long. The lower 8 bit of Timer 0 are called as TL0 and higher 8 bit of Timer 0 are called TH0. Similarly, lower and higher 8 bit of Timer 1 are called TL1 and TH1 respectively. In Timer 0, bit are represented as D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 TH0 TL0 Similarly, in Timer 1, bit are represented as D15 D14 D13 D12 D11 D10 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 TH1 TL1 Port 3 can be used as input or output port. As input, Port 3 is used to handle serial communication, timer/counter and read write signal. The specifications of Port 3 are given below:
  • 86. 86 µC Structure P3 Function Pin No Description P3.0 RxD 10 Receive data from serial communication. P3.1 TxD 11 Transmit data to serial communication. P3.2 INT0 12 External Interrupt for timer 0. P3.3 INT1 13 External Interrupt for timer 1. P3.4 T0 14 Externally pulse feed to timer 0. P3.5 T1 15 Externally pulse feed to timer 1. P3.6 WR 16 Write to external data memory. P3.7 RD 17 Read from external data memory. 3.1.1 TMOD Both the timer 0 and the timer 1 use the same register, called TMOD (Timer Mode). TMOD is 8 bit register of which lower 4 bit are meant for Timer 0 and upper 4 bit are meant for Timer 1. TMOD bit are shown in figure below. GATE C/T M1 M0 Timer 1 MSB GATE C/T M1 M0 Timer 0 LSB Of these lower and upper 4 bit of TMOD, lower 2 bit are used to set the timer mode and upper 2 bit are used for specification of the operation.
  • 87. 3.1. TIMER 87 M1 M0 Mode Operating Mode 0 0 0 13 bit timer mode operation. 8 bit for TH and 5 bit prescalar for TL. 0 1 1 16 bit timer mode operation. TH and TL are cascaded. No prescalar. 1 0 2 8 bit auto reload operation. TH holds a value which reloaded into TL each time when TL overflows from FFH to 00H. 1 1 3 Split time mode Mode 2 is auto reload mode. It means when timer is roll over or inter- rupted, TLn is automatically reloaded by the initial value of THn. In other modes, timer is not reloaded automatically but reset to 00H. To load the timer with initial values of THn and TLn, they are required to readdressed by their initial values on interrupt or on roll over of timer. The following instruction sets the Timer 1 in operation mode 2 while Timer 0 in operation mode 0. TMOD Timer 1 Timer 0 GATE C/T M1 M0 GATE C/T M1 M0 0 0 1 0 0 0 0 0 In above example, the Timer 1, in which M1 is set high and M0 is set low. Which is operation mode 2 of the Timer 1. Similarly, following instruction on TMOD register TMOD Timer 1 Timer 0 GATE C/T M1 M0 GATE C/T M1 M0 0 0 1 0 0 0 0 1 ✞ MOV TMOD ,#21H ; 21H = 0010 0001 ✌ ✆
  • 88. 88 µC Structure selects the Timer 1 in operation mode 2 and Timer 0 in operation mode 1. GATE is used to start or stop timer either by hardware mode or by software mode. If GATE is set to 1, timer can be start or stopped by external source. At the state of GATE set to 1, timer/counter is enabled only when the INT pin is high and the TR control pin is set (TR). If GATE is set to 0, TR instruction is used to start or stop the timer. To start Timer 0 or Timer 1, TR instruction is used as TR0 or TR1 respectively. If TR is set to high, timer is started. If TR is cleared, timer is stopped. ✞ 1 SETB TRx ;Start a Timer x CLR TRx ;Stop the Timer x ✌ ✆ Each timer has its own flag, known as TF. TF0 and TF1 for Timer 0 and Timer 1 respectively. These flags are set to high when 16bit Timer are overflows. Timers are used for time delay. C/T bit in TMOD used to get the pulses from the outside of 8051 when C/T is set high. If C/T is zero, then timer is used as timer and if C/T is one, then timer is used as counter. Port pins P3.4 and P3.5 are used by Timer 0 and Timer 1 to get the pulses from the external source clock. When GATE is high then we can interrupt the timer externally. To start or stop the Timer 0 and Timer 1, pin P3.2 and P3.3 are used respectively. Mode 1 Example ✞ MOV TMOD ,#01H ; Timer 0 in mode 1 (16-bit) 2 MOV TL0 ,#0F2H ; TL0 = F2 Hex MOV TH0 ,#0FFH ; TH0 = FF Hex 4 SETB TR0 ; Start the timer 0 AGAIN: ; AGAIN subroutine 6 JNB TF0 ,AGAIN ; Monitor timer flag 0 ; until it rolls over 8 CPL P1.0 ; Toggle the 1st bit of P0 port CLR TR0 ; Stop the timer 0 10 CLR TF0 ; Clear the timer 0 flag END ; End of program ✌ ✆ Mode 2 Example It is 8-bit timer. It allows only values from 00H to FFH in the THx register. After the timer started, it started counting up by incrementing the TLx register. When TLx register is rolls over FFH to 00H, the timer flag is set to high. TLx is automatically reloaded with original value
  • 89. 3.1. TIMER 89 kept by the THx register. Observe this roll over and execute the subroutine. ✞ 1 MOV TMOD ,#02H ; Timer 0 in mode 2 ; (8-bit/auto reload) 3 MOV TH0 ,#0 FFH ; TH0 = FF Hex SETB TR0 ; Start the timer 0 5 AGAIN: ; AGAIN subroutine JNB TF0 ,AGAIN ; Monitor timer flag 0 7 ; until it rolls over CPL P1.0 ; Toggle the 1st bit of P0 port 9 CLR TR0 ; Stop the timer 0 CLR TF0 ; Clear the timer 0 flag 11 END ; End of program ✌ ✆ Timer as Delay Timers are used to generate time delay. In 8051 MCU, XTAL is used to generate oscillations (clock frequency). 12 oscillation called one machine cycle. The frequency of XTAL is 11.0592 MHz. The time period of each machine cycle is 1.085µs. Number of counts are measured in half of the pulse hence entire period of time delay is measured by doubling the number of counts. Illustrated Example To understand the amount of time delay in the DE- LAY subroutine generated by the timer, following example is given. ✞ 1 MOV TMOD ,#01H ; Timer 0 in mode 1 (16-bit) HERE: ; HERE subroutine 3 MOV TL0 ,#0 F2H ; TL0 = F2 Hex MOV TH0 ,#0 FFH ; TH0 = FF Hex 5 CPL P1.0 ; Toggle the D0 bit of P0 port ACALL DELAY ; Call the DELAY subroutine 7 SJMP HERE ; Do it again and again DELAY: ; Delay subroutine 9 SETB TR0 ; Start the Timer 0 ;1 Machine Cycle AGAIN: ; AGAIN subroutine 11 JNB TF0 ,AGAIN ; Monitor timer flag 0 ;14 Machine Cycles ; until it rolls over
  • 90. 90 µC Structure 13 CLR TR0 ; Stop the timer 0 ;1 Machine Cycles CLR TF0 ; Clear the timer 0 flag ;1 Machine Cycles 15 RET ; Return to subroutine caller ;2 Machine Cycles END ; End of program ✌ ✆ In above example a time delay is generated by using DELAY subroutine. In a subroutine execution, total machine cycles are 19. So, time delay is of 19 × 1.085 = 20.615s. Illustrated Example Time delay is also used to generate a square wave of required time period. In one complete time period cycle of a square wave, wave remains low during the first half cycle and remains high during the second half cycle or vice-versa. This is why during the generation of square wave, we always computes the number of cycle required for either high state or low state of the square wave. In this explanation, we have to compute the time period of the square wave when initial value of Timer 1 is FFF2H. For MCU 8051, the XTAL frequency is 11.0592 MHz and clock frequency of timer is one twelfth of it. So, timer frequency is 11.0592MHz 12 = 921.6kHz The clock period is Tc = 1 921.6kHz = 1.085µs Now the number of count are FFFFH − FFF2H = 0DH. It is equal to 13 in decimal. One more count required for roll over from FFFFH to 0000H to raised the TF flag. Hence total counts are 13 + 1 = 14. This gives the time period for half pulse equal to 14 × 1.085µs = 15.19µs For the entire period it is T = 2 × 15.19µs = 30.38µs as the time delay generated by the timer. See the below example which uses Timer 1 for square wave generation.
  • 91. 3.1. TIMER 91 ✞ MOV TMOD ,#10H ; Timer 1 in mode 1 (16-bit) 2 HERE: ; HERE subroutine MOV TL1 ,#0 F2H ; TL1 = F2 Hex 4 MOV TH1 ,#0 FFH ; TH1 = FF Hex CPL P1.0 ; Toggle the D0 bit of P0 port 6 SETB TR1 ; Start the Timer 1 AGAIN: ; AGAIN subroutine 8 JNB TF1 ,AGAIN ; Monitor timer flag 0 ;14 Machine Cycles ; until it rolls over 10 CLR TR1 ; Stop the timer 0 CLR TF1 ; Clear the timer 0 flag 12 SJMP HERE ; Jump to HERE END ; End of program ✌ ✆ Illustrated Example Similarly, other operation mode of Timers can be used to generate the time delay or square wave. But the time period or time delay is restricts to the values of THn and TLn in the corresponding operation mode. In operation mode 2 of a timer, the initial count value is stored in TH and it is automatically copied into TL when timer is started. Timer counting started next to the value current value of TL. When timer is roll over, TL is reset to the value of TH. See the example below: ✞ 1 MOV TMOD ,#20H ; Timer 1 in mode 2 (16-bit) MOV TH1 ,#05H ; TH1 = 05 Hex. It auto reload 3 ; to TL1 when timer roll over HERE: ; HERE subroutine 5 CPL P1.0 ; Toggle the 1st bit of P0 port SETB TR1 ; Start the timer 1. 7 ; with value of TH1 AGAIN: ; AGAIN subroutine 9 JNB TF1 ,AGAIN ; Monitor timer flag 0 ; until it rolls over 11 CLR TR1 ; Stop the timer 0 CLR TF1 ; Clear the timer 0 flag 13 SJMP HERE ; Jump to HERE , Timer Mode 2 ; automatically reloaded. 15 END ; End of program ✌ ✆
  • 92. 92 µC Structure Illustrated Example Now a question rises that how we set the Timer values initially to get the specific time delay. For example we want a time delay of 15.19µs, then what should be the initial values of the Timer 1 in operation mode 1. To do this first we get the number of cycles to be performed by the machine for time delay of 15.19µs. n = 15.19 1.085 = 14 In operation mode 1, the final value of Timer 1 is FFFFH. So first we reduce the number of cycles from the value of FFFFH in decimal form, which is 65535. So, 65535 − 14 = 65521. To roll over of the timer, we need one more cycle so the final value is 65521 + 1 = 65522. 65522 is equivalent to FFF2H. Now the initial timer values should be TLn=F2H and THn=FFH. The timer delay example shall be ✞ 1 MOV TMOD ,#10H ; Timer 1 in mode 1 (16-bit) HERE: ; HERE subroutine 3 MOV TL1 ,#0 F2H ; TL1 = F2 Hex MOV TH1 ,#0 FFH ; TH1 = FF Hex 5 CPL P1.0 ; Toggle the 1st bit of P0 port SETB TR1 ; Start the timer 1 7 AGAIN: ; AGAIN subroutine JNB TF1 ,AGAIN ; Monitor timer flag 0 9 ; until it rolls over CLR TR1 ; Stop the timer 0 11 CLR TF1 ; Clear the timer 0 flag SJMP HERE ; Jump to HERE 13 END ; End of program ✌ ✆ Illustrated Example For time delay we use loops for delaying the execution of instruction out of the loop. Delay of time is counted between start of timer and stop of timer. In MCU instruction programs, there are some additional instructions other than loops and start and stop of timer. These additional instruction causes the extra time delay counted by the MCU. This is why a proper care is required in time delay computation. This extra time delay is called overhead time delay. We want to make a time delay of 10.85µs between the loop and stopping of timer. See example below: ✞ 1 MOV TMOD ,#01H ;2 Machine Cycle = 2.17 micro second
  • 93. 3.1. TIMER 93 MOV TH0 ,#0 FFH ;2 Machine Cycle = 2.17 micro second 3 MOV TL0 ,#0 F6H ;2 Machine Cycle = 2.17 micro second SETB TR0 ;1 Machine Cycle = 1.085 micro second 5 BACK: JNB TF0 ,BACK ;1 Machine Cycle = 1.085 micro second 7 ;total 10 Cycles =10 .85 micro second CLR TR0 ;1 Machine Cycle = 1.085 micro second 9 END ✌ ✆ If an extra instruction is added as shown below ✞ 1 MOV TMOD ,#01H ;2 Machine Cycle = 2.17 micro second MOV TH0 ,#0 FFH ;2 Machine Cycle = 2.17 micro second 3 MOV TL0 ,#0 F6H ;2 Machine Cycle = 2.17 micro second SETB TR0 ;1 Machine Cycle = 1.085 micro second 5 BACK: JNB TF0 ,BACK ;1 Machine Cycle = 1.085 micro second 7 ;total 10 Cycles =10 .85 micro second MOV A,P0 ;1 Machine Cycle = 1.085 micro second 9 CLR TR0 ;1 Machine Cycle = 1.085 micro second END ; ✌ ✆ The time delay between the loop and stopping of timer is excess by one machine cycle. This is called overhead time delay. In the instruction lines ✞ BACK: 2 JNB TF0 ,BACK ;1 Machine Cycle = 1.085 micro second ;total 10 Cycles = 10 .85 micro second 4 MOV A,P0 ;1 Machine Cycle = 1.085 micro second CLR TR0 ;1 Machine Cycle = 1.085 micro second ✌ ✆ One extra cycle is consumed by MOV instruction which is added in the time delay before the timer is stopped. This extra time is called overhead time delay. To compensate the overhead time delay to the loop time delay we have to set initial values of timer such a way that excess overhead time delay is compensated. Above example is rewritten as ✞ 1 MOV TMOD ,#01H ;2 Machine Cycle = 2.17 micro second MOV TH0 ,#0 FFH ;2 Machine Cycle = 2.17 micro second 3 ;-----------Reset the TL0 of Timer 0--------------; MOV TL0 ,#0 F7H ;2 Machine Cycle = 2.17 micro second
  • 94. 94 µC Structure 5 SETB TR0 ;1 Machine Cycle = 1.085 micro second BACK: 7 JNB TF0 ,BACK ;1 Machine Cycle = 1.085 micro second ;Total 10 Cycles = 10.85 micro second 9 MOV A,P0 ;1 Machine Cycle = 1.085 micro second CLR TR0 ;1 Machine Cycle = 1.085 micro second 11 END ✌ ✆ Timer as Counter Timer can also be used as counters. It counts the events happening outside the 8051. When it is used as counter, outside pulse increments the TH and TL registers. The C/T bit in the TMOD register decides the source of the clock. If C/T is 0 then the source of the clock is internal XTAL. If C/T is 1 then external source of clock is selected. The external source of pulse is connected with P3.4 and P3.5 port pins. P3.4 pin is used for timer/counter 0 and P3.5 pin is used for timer/counter 1. Solved Problem 3.1 A programmer wants to use Timer 1 in auto reload mode. What should be the value for TMOD register? Solution Upper 4 bit are used to control the Timer 1. M1 and M0 are used to select the timer mode. Mode 2 of Timer 1 is used as auto reload operation. So M1 of Timer 1 should be high and M0 of Timer 1 should be low. So, the value of TMOD should be 20H. TMOD Timer 1 Timer 0 GATE C/T M1 M0 GATE C/T M1 M0 0 0 1 0 0 0 0 0 ✞ 1 MOV TMOD ,#20H ; 20H = 0010 0000 ✌ ✆ Solved Problem 3.2 A programmer wants to use Timer 0 in 16 bit operation. What should be the value for TMOD register? If timer gets roll over in 10 machine cycles then find the initial values for TH and TL register of the timer.
  • 95. 3.1. TIMER 95 Solution Lower 4 bit are used to control the Timer 0. M1 and M0 are used to select the timer mode. Mode 1 of Timer 0 is used for 16 bit operation. So M1 of Timer 0 should be low and M0 of Timer 0 should be high. So, the value of TMOD should be 01H. TMOD Timer 1 Timer 0 GATE C/T M1 M0 GATE C/T M1 M0 0 0 0 0 0 0 0 1 ✞ 1 MOV TMOD ,#01H ; 20H = 0000 0001 ✌ ✆ To get rollover in 10 machine cycles, the TH0 and TL0 values should be FFH and 06H respectively. 3.1.2 TCON Timer Control (TCON) register is a 8 bits, bit-addressable register. Its upper 4 bit controls the TF (Timer Flag) and TR (Timer Run) bit of both timers while lower 4 bit are used for controlling interrupts externally and interrupt timers. TF1 TR1 TF0 TR0 MSB IE1 IT1 IE0 IT0 LSB If GATE is 1, then start and stop of timers are done externally through pins P3.2 and P3.3 for Timer 0 and Timer 1 respectively. TF1 and TF0 flags are used to check whether a timer is rolled over or not. A timer is 16 bit register having two 8 bit groups TL and TH. The range of timer (Timer 0 or Timer 1) is 0000H to FFFFH. When timer registers is at FFFFH state then at next counting it becomes 0000H. This is called roll over. If a timer is roll over then it sets Timer Flag (TF0 for Timer 0 and TF1 for Timer 1) to high. TR1 or TR0 is set high when Timer 1 or Timer 0 is started respectively. IE0 or IE1 is set to high when Timer 0 or Timer 1 is interrupted via hardware (externally from P3.2 for Timer 0 and P3.3 for Timer 1).
  • 96. 96 µC Structure 3.2 Interrupt Interrupt is an action taken by internally or externally where current pro- cessing is stopped immediately and new process is started. An interrupt is an external or internal event that interrupts the micro-controller to inform that device needs its service. The interrupts must be enabled by software in order for micro-controller to respond to them. IE EA - ET2 ES ET1 EX1 ET0 EX0 1 0 0 0 0 0 1 0 The instruction IE register is used for this purpose. IE register is bit addressable. IE register has 8 bit with following meanings. Bit Name IE Bit Description EA IE.7 Enable/Disable all interrupts. – IE.6 Reserved. ET2 IE.5 Enables or disables timer 2 overflow interrupt. ES IE.4 Enables or disables serial port interrupt. ET1 IE.3 Enables or disables timer 1 overflow interrupt. EX1 IE.2 Enables or disables external interrupt for T1. ET0 IE.1 Enables or disables timer 0 overflow interrupt. EX0 IE.0 Enables or disables external interrupt for T0. In 8051, two pins P3.2 and P3.3 are designated as INT0 and INT1 and used as external hardware interrupts of the Timer 0 and Timer 1 respectively. 3.2.1 External Interrupt For each interrupt, there must be an Interrupt Service Routine (ISR) or interrupt handler. On invoke of an interrupt, the micro-controller runs the interrupt service routine. For every interrupt, there is a fixed location in
  • 97. 3.2. INTERRUPT 97 memory that holds the address of ISR. These memory location is called Interrupt Vector Table. On activation of an interrupt, MCU jumps to a fixed location in memory and upon execution of RETI instruction, it returns to the place where it was interrupted. Go through the following example in which external interrupt is used. ✞ 1 ORG 0000H ;Loading address LJMP MAIN ;Jump to main program 3 ORG 0013H ;Timer 0 interrupt address 5 SETB P2.1 ;Set high to D1 pin of port P2 MOV R3 ,#10H ;Store R3 with hex 10H. 7 BACK: DJNZ R3 ,BACK;Loop upto 16 times CLR P2.1 ;Clear the D2 pin of port P2 9 RETI ;Return from interrupt 11 ORG 0030H ;Main program location MAIN: 13 MOV IE ,#84H ;HW based interrupt , EA high , EX1 HERE:SJMP HERE ;Return to BACK subroutine 15 END ✌ ✆ 3.2.2 Interrupt Priority Register By default the highest to lowest priorities of interrupt or internal polling sequence of MCU is given in the table below. Interrupt Priority Symbol External Interrupt 0 INT0 Timer Interrupt 0 TF0 External Interrupt 1 INT1 Timer Interrupt 1 TF1 Serial Communication (RI + TI) These default priorities are poled at the time of power on of 8051 MCU. We can change the sequence of interrupt priority by assigning the higher
  • 98. 98 µC Structure priority to any one of the interrupts by programming the Interrupt Priority Register. To give a higher priority to an interrupt, its corresponding pin set to high. The interrupt priority register has following bits. Bit Name IP Bit Explanation – IP.7 Reserved. – IP.6 Reserved. PT2 IP.5 Timer 2 interrupt priority. PS IP.4 Serial port interrupt priority. PT1 IP.3 Tmer 1 interrupt priority. PX1 IP.2 External interrupt 1 priority. PT0 IP.1 Timer 0 interrupt priority. PX0 IP.0 External interrupt 0 priority. When two or more interrupts are activated at a time, then they are ser- viced according to their default priority. 3.2.3 Interrupt Vector Table There are six interrupts, (i) one interrupt for reset, (ii) two interrupts for timer, (iii) two external interrupts and (iv) one serial communication inter- rupts. On interrupts, MCU jumps to the fixed memory locations of ROM. The group of all memory locations fixed for interrupts are called Interrupt Vector Table. These memory locations are also called Interrupt Service Rou- tine (ISR). The interrupt vector table is given below:
  • 99. 3.2. INTERRUPT 99 Interrupt ROM Memory (Hex) Pin Reset 0000H Pin 9 External HW (INT0) 0003H P3.2 Timer 0 (TF0) 000BH External HW (INT1) 0013H P3.3 Timer 1 (TF1) 001BH Serial COM (RI & TI) 0023H On reset MCU jumps to the fixed memory location specified for the in- terrupt. It starts execution of the Interrupt Service Subroutine until it is not instructed to return from interrupt by instruction RETI. After return- ing from the interrupt, MCU starts execution from the place where it was interrupted. ✞ 1 ORG 0000H ;Loading address LJMP MAIN ;Jump to main program 3 ORG 000BH ;Timer 0 interrupt address 5 CPL P2.1 ;CPL to D1 pin of port P2 RETI ;Return from interrupt 7 ORG 0030H ;Main program location 9 MAIN: MOV TMOD ,#02H ;Timer 1 mode 2 11 MOV P0 ,#0 FFH ;Make port P0 input port MOV TH0 ,#90H ;Load TH of Timer 0 with 90H 13 MOV IE ,#82H ;SW based interrupt , EA high SETB TR0 ;Start Timer 0 15 BACK: 17 MOV A,P0 ;Copy input into ACC from P0 MOV P1 ,A ;Mov data of ACC into P1 19 SJMP BACK ;Return to BACK subroutine END ✌ ✆ On reset interrupt or power on of the MCU, all the values in the RAM and
  • 100. 100 µC Structure register are lost. On power up, the default values of MCU registers are as given below: Register Value PC 0000 DPTR 0000 ACC 00 PSW 00 SP 07 B 00 P0-P3 FF To activate a reset input to be effective, it must be pushed at least two duration of machine cycles. 3.3 Communication To communicate between PC & Micro controller, DB-9 version of serial I/O standards are used. It has nine pins.
  • 101. 3.3. COMMUNICATION 101 Pin Description 1 Data Carrier Detect (DCD) 2 Received Data (RxD) 3 Transmitted Data (TxD) 4 Data Terminal Ready (DTR) 5 Signal Ground (GND) 6 Data Set Ready (DSR) 7 Request To Sent (RTS) 8 Clear To SenT (CTS) 9 Ring Indicator (RI) We required minimum 3 pins. TxD, RxD and ground. Description of other pins are: DTR When terminal is turned on, it sends out signal DTR to indicate that it is ready for communication. DSR When DCE is turned on and has gone through the self-test, it assert DSR to indicate that it is ready to communicate RTS (request to send) RTS When the DTE device has byte to transmit, it assert RTS to signal the modem that it has a byte of data to transmit CTS (clear to send) CTS When the modem has room for storing the data it is to receive, it sends out signal CTS to DTE to indicate that it can receive the data now DCD The modem asserts signal DCD to inform the DTE that a valid carrier has been detected and that contact between it and the other modem is established RI (ring indicator). RI An output from the modem and an input to a PC indicates that the telephone is ringing. It goes on and off in synchronous with the ringing sound
  • 102. 102 µC Structure 3.3.1 Baud Rate Baud rate is bits per second transmitted during communication. 8051 MCU UART is 32. The machine cycle frequency of MCU is 921.6 kHz. If TMOD is set to low, timer frequency is given by 921.6kHz ÷ 32 = 28,800. If TMOD is set to high, timer frequency is given by 921.6kHz ÷ 16 = 57,600. By default TMOD is low. To set the baud rate of MCU communication, 28,800 or 57,600 is divided by an integer. If THn is set to 3 then baud rate shall be 28,800 ÷ 3 = 9600 bits per second and so on. Baud rate is required to construct a delay sub routine as a bit in serial communication requires a specific time for its transmission. For example, if baud rate is 100K, i.e. 100K bits per second then one bit will last in 1/100K second or in 10µs (bit time). The delay loop of 10µs is setup and transmission line is setup for the different bits for this duration of delay. 3.3.2 SBUF SBUF is acronym of Serial Data Buffer. It is an 8-bit register, solely used for serial communication. To transfer a byte data via the TxD line, it must be placed in the SBUF register. When the data is placed in the SBUF register, it is framed with start and stop bit and transferred serially via TxD line. SBUF also receives the one byte data via RxD line. At the moment of data received via RxD, data is deframed by eliminating stop and start bit and is placed in SBUF register. By using MOV instruction, data can be directly transfer into the SBUF or data can transfer mutually between accumulator and SBUF. ✞ MOV SBUF ,#’D’ 2 MOV A,SBUF MOV SBUF ,A ✌ ✆ Illustrated Example In following example one byte data equivalent to letter ‘A’ is transmitted serially via TxD line. The timer mode is set for mode 2 operation. Timer is started. Serial Communication register is set for 8 bit, 1 stop and REN enabled transfer mode. In serial control (SCON), the data moves in or moves out through only SBUF register. This is why in the loop sub-routine data ‘A’ is first copied into the SBUF register and then it is transferred serially. Wait until data is not completely transferred serially and TI is not set to high. When TI is set to high data transmission
  • 103. 3.3. COMMUNICATION 103 completed. Clear TI so that TI flag is ready for next data transmission loop. Reach again at the beginning of loop for transferring of the data serially. ✞ 1 MOV TMOD ,#20H ; Timer 0 in mode 2 ; (8-bit/auto reload) 3 MOV TH1 ,#6 ; TH0 = FF Hex MOV SCON ,#50 ; 8-bit , 1 stop , REN enable 5 SETB TR1 ; Start the timer AGAIN: ; AGAIN subroutine 7 MOV SBUF ,#’A’ ; Transmit letter A HERE: 9 JNB TI ,HERE ; Monitor for transfer interrupt CLR TI ; Clear the transfer interrupt 11 SJMP AGAIN ; Do it again ✌ ✆ In following example a byte data equivalent to letter ‘FFH’ is received serially via RxD line. ✞ 1 MOV TMOD ,#20H ; Timer 0 in mode 2 ; (8-bit/auto reload) 3 MOV TH1 ,#6 ; TH0 = FF Hex MOV SCON ,#50 ; 8-bit , 1 stop , REN enable 5 SETB TR1 ; Start the timer HERE: 7 JNB RI ,HERE ; Monitor for receive interrupt MOV A,SBUF ; Copy SBUF data to accumulator 9 MOV P1 ,A ; Send accumulator data to port P1 CLR RI ; Clear the receive interrupt 11 SJMP HERE ; Do it again ✌ ✆ 3.3.3 SCON SCON is acronym of Serial Control. It is 8-bit register. It is used in data framing that consists the start bit, stop bit and data bits. SM0 SM1 SM2 REN TB8 RB8 TI RI MSB LSB
  • 104. 104 µC Structure The pin description of SCON register are given in the following table. Pin Description SM0 or SCON.7 Serial port mode specifier. SM1 or SCON.6 Serial port mode specifier. SM2 or SCON.5 Used for multiprocessor communication. REN or SCON.4 Set/cleared by software to enable/disable reception. TB8 or SCON.3 Not used. RB8 or SCON.2 Not used. TI or SCON.1 Transmit interrupt flag. RI or SCON.0 Receive interrupt flag. SM0, SM1 These bit selects the framing/encapsulation of data by spec- ifying the number of bit per character and the start and the stop bit if required. There are four types of data framing which are identified by Mode 0 to Mode 4 operations. SM0 SM1 Mode 0 0 Serial Mode 0 0 1 Serial mode 1, 8-bit data, 1 stop bit, 1 start bit 1 0 Serial mode 2 1 1 Serial mode 3 SM2 It enables the multiprocessing capabilities of 8051. REN SCON is a bit addressable register. Therefore, the REN pin can be assessed by SCON.4 address. When it is high, it allows to receive data on RxD pin.
  • 105. 3.3. COMMUNICATION 105 TI When 8051 finishes the transfer of 8-bit character, TI becomes high, it indicates that it is ready to transfer another byte. TI bit is raised at the beginning of the stop bit. By monitoring TI flag, we can make sure that SBUF is not overloading. If we write another byte into SBUF before TI raised to high, the un-transmitted portion of the previous byte will be lost. Before loading SBUF with new data byte, TI must be reset to 0. RI When 8051 receives data serially via RxD, it strips off start and stop bit and places the byte in SBUF register. It raised the RI flag bit to high and indicates that a byte has been received and should be picked up before it is lost. RI is raised halfway through the stop bit. Illustrated Example The minimum structure for serial transmission is re- quired the following lines of coding. We need proper initialization of TMOD, SCON registers. SBUF register is used to load input as it is the register through which data is either transmitted or received. ✞ 1 MOV TMOD ,#20H ; Timer 1, mode 2 (auto reload) MOV TH1 ,#3 ; 9600 Baud rate 3 MOV SCON ,#50H ; 8-bit , 1 stop , REN enabled SETB TR1 ; Start Timer 1 5 AGAIN: MOV SBUF ,#’Y’ ; Load SBUF with letter Y 7 HERE: JNB TI ,HERE ; Wait for last byte 9 CLR TI ; Clear Transfer Interrupt SJMP AGAIN ; Do again 11 END ; End program ✌ ✆ Following is a program which transmits the word NOT serially. This program is slightly modified form of above program. TMOD and SCON registers are initialized as shown in above example. SBUF register is used to transfer the bytes from Accumulator to output port. ✞ 1 MOV TMOD ,#20H ; Timer 1, mode 2 (auto reload) MOV TH1 ,#3 ; 9600 Baud rate 3 ;+-----------------------------------------------+ ;| SCON | 5 ;+-----+-----+-----+-----+-----+-----+-----+-----+ ;| SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI | 7 ;+-----+-----+-----+-----+-----+-----+-----+-----+
  • 106. 106 µC Structure ;| 0 | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 9 ;+-----+-----+-----+-----+-----+-----+-----+-----+ MOV SCON ,#50H ; 8-bit , 1 stop , REN enabled 11 SETB TR1 ; start timer 1 AGAIN: 13 MOV A,#’N’ ; Transfer N ACALL TRANS 15 MOV A,#’O’ ; Transfer O ACALL TRANS 17 MOV A,#’T’ ; Transfer T ACALL TRANS 19 SJMP AGAIN TRANS: 21 MOV SBUF ,A ; Load SBUF HERE: 23 JNB TI ,HERE ; Wait for last byte CLR TI ; Get ready for next byte 25 RET END ✌ ✆ Here, SBUF data is reloaded into Accumulator. After complete receiving of data from SBUF register, Receive Interrupt (RI) is cleared and the program is ready for receiving of new byte. ✞ MOV TMOD ,#20H ; Timer 1, mode 2 (auto reload) 2 MOV TH1 ,#6 ; 9600 Baud rate MOV SCON ,#51H ; 8-bit , 1 stop , REN enabled 4 SETB TR1 ; Start Timer 1 AGAIN: 6 JNB RI,AGAIN; Wait untill whole byte ; not received. 8 MOV A,SBUF ; Load Accumulator with SBUF value CLR RI ; Clear Receive Interrupt 10 SJMP AGAIN ; Do again END ; End program ✌ ✆ 3.3.4 PCON It is acronym of Power Control. It is used to change the baud rate of the MCU. It is a 8-bit register. When 8051 is powered up, SMOD is zero. We can
  • 107. 3.3. COMMUNICATION 107 set it to high by software and thereby double the baud rate. SMOD1 uses 57600Hz and SMOD0 uses 28800Hz. The PCON register is shown below. SMOD1SMOD0 – POF GF1 GF0 PD IDL When PCON.0 sets high, MCU goes under idle condition. PCON is not a bit addressable register, hence a manipulation is required for it. First we copied the PCON into A (Accumulator) and then its bit values are altered by using SETB instruction as ✞ 1 SETB ACC.7 ✌ ✆ After that, the accumulator value is copied into PCON register. The com- plete routine is ✞ 1 MOV A,PCON SETB ACC.7 3 MOV PCON ,A ✌ ✆ By this way the baud rate is doubled. Following is an example for transmit- ting NOT serially in double baud rate. ✞ 1 MOV A,PCON SETB ACC.7 3 MOV PCON ,A MOV TMOD ,#20H ; Timer 1, mode 2 (auto reload) 5 MOV TH1 ,#6 ; 9600 Baud rate MOV SCON ,#51H ; 8-bit , 1 stop , REN enabled 7 SETB TR1 ; Start Timer 1 AGAIN: 9 JNB RI ,AGAIN; Wait untill whole byte ; not received. 11 MOV A,SBUF ; Load Accumulator with SBUF value MOV P1 ,A ; Send accumulator into P1 13 CLR RI ; Clear RI SJMP AGAIN ; Do again 15 END ; End program ✌ ✆ In idle mode, The ALE and PSEN output are held high. Last instruction executed before going into the Idle mode. The internal CPU clock is gated
  • 108. 108 µC Structure off. Interrupt, Timer, and Serial Port functions act normally. All of registers, ports and internal RAM maintain their data during Idle. An instruction that sets PCON.1 causes power down mode. Last instruction executed before going into the power down mode. The on-chip oscillator is stopped. All functions are stopped, the contents of the on-chip RAM and Special Function Registers are maintained. The ALE and PSEN output are held low. The reset that terminates the Power Down. 3.4 Assembly Directives Directives in assembly language provide the way to assign a number or string to a variable for further use. Classifications and their use is explained in the following sections. 3.4.1 Symbol Definition The assembler automatically assigns values to variables that appear as in- struction labels. This value is the current setting of the location counter when the instruction is assembled. Symbols and assigning a value to them by using EQU directive. ✞ 1 <symbol name > EQU <value or expression > ✌ ✆ The example of EQU definition is given below: ✞ 1 AGE EQU 10 MOV P0 ,# AGE 3 MOV P1 ,# AGE END ✌ ✆ 3.4.2 Data Definition The DB (Define Byte) and DW (Define Word) directive enable you to define data to be stored in your program. Data can be in the form of 8-bit or 16-bit values or in a string of text characters. The synopses of data definition is ✞ <symbol name > DB <string expression > ✌ ✆
  • 109. 3.5. ASSEMBLY IN C 109 DB directive should be at different location to avoid the shortage of registers for program’s internal process. See the example below: ✞ 1 ORG 0000H <program codes > 3 ORG 200H ;Different location of data MYDATA: DB "This is my name" ,0 ✌ ✆ Here, number 0 at the end of data definition ‘MYDATA’ represents the end of string. Through DPTR pointer, number 0 is copied into accumulator and by JZ instruction we exit from the loop which is reading data bytes of ‘MYDATA’. In case of numerical data definition, this method of termination of data is not suitable if the data bytes are numbers as 0 itself is a number. In that case datasize is computed by the following method. ✞ ORG 0 2 MOV DPTR ,# MYDATA ; Get Address of MYDATA MOV R6 , DPL ; Copy address to R6 4 MOV DPTR ,# MYDATAEND ; Get Address of MYDATAEND MOV A,DPL ; Copy address to Accumulator 6 SUBB A,R6 ; Get size of data MOV R7 ,A ; Copy data size into Register R7 8 ; Value is 06 in this example MYDATA: ; ROM data 10 DB 10,5,0,8,5,8 MYDATAEND: ; End mark of ROM data 12 END ✌ ✆ Note that here, data is one byte long. Hence data size of user data is 06. If it is two bytes long, then the number of times data being read is half of the total data size and in each iteration, two bytes shall be read cumulatively. 3.5 Assembly in C Here we use Small Devices C Compiler (SDCC) for compilation of the C assembly program for 8051. The Internal registers instruction is included from the header file <8051.h>. A simple program that writes the character codes ranging from 0 to 255 is given below. ✞ #include <8051.h>
  • 110. 110 µC Structure 2 void main(void) { 4 unsigned char z; /* Declaration of character variable .*/ P1 = 0x00; /* Reset the port P1 to low */ 6 for (z = 0; z <= 255; z++) /* For loop */ P1 = z; /* Put the char code in port P1.*/ 8 } ✌ ✆ During the further discussion, it is assumed that the reader are well aware of C programming. The data type in SDCC are int and char either signed or unsigned. By default, all the data types are signed. ✞ #include <8051.h> 2 void main(void) { 4 unsigned char z[]="0123456789abcdefghij"; unsigned int i; 6 for(i=0;i <20;i++){ P1=z[i]; 8 } } ✌ ✆ Data Type Size Range unsigned char 8-bit 0 to 255 signed char 8-bit -128 to +127 unsigned int 16-bit 0 to 65535 signed int 16-bit -32768 to 32767 sbit 1-bit SFR bit addressable only bit 1-bit RAM bit addressable only sfr 8-bit RAM address 80H to FFH only Table 3.1: Data type in SDCC for 8051 MCU.
  • 111. 3.5. ASSEMBLY IN C 111 We only discuss that how the internal architecture of the 8051 is to be ac- cessed via a C program. The following program is for infinite loop. ✞ 1 #include <8051.h> 3 void main(void) { P1 = 0x00; 5 for (;;) { /* Infinite loop. */ P1 = 0xFF; 7 P1 = 0x00; } 9 } ✌ ✆ Arrays can be used as shown in the example below. ✞ 1 #include <8051.h> 3 void main(void) { unsigned char myArr[] = "abcd"; 5 unsigned char i; for (i = 0; i < 4; i++) { 7 P1 = myArr[i]; } 9 } ✌ ✆ Following is an example for integer datatype. ✞ 1 #include <8051.h> 3 void main(void) { unsigned int msg[] = {0x62 ,0 x5b}; 5 unsigned char z; while (1){ 7 for (z = 0; z < 2; z++) { P0 = msg[z]; 9 P1 = z; } 11 } } ✌ ✆ In SDCC, bytes register and bit registers are directly accessed by their name. Byte registers are assigned by their true name. Bit registers are assigned by
  • 112. 112 µC Structure their byte name and bit position joined with ‘ ’ symbol (underscore). For example all bit of port P0 is accessed as ✞ P0 = 0xAF; /* Assigned 0xAF to port P0.*/ ✌ ✆ ✞ 1 #include <8051.h> 3 void main(void) { for (;;) { 5 P0 = 0xAF; P0 = 0x00; 7 } } ✌ ✆ While a single bit of port pin is assessed as ✞ P0_1 /* For port bit 1 of port 0.*/ 2 P0_2 /* For port bit 2 of port 0.*/ P1_1 /* For port bit 1 of port 1.*/ ✌ ✆ To access the first port bit of port P0, following method is used. ✞ 1 #include <8051.h> 3 void main(void) { unsigned char z; 5 for (z = 0; z < 255; z++) { P2_0 = 1; 7 P2_0 = 0; } 9 } ✌ ✆ Similarly, other bit registers which are given specific name are accessed by their name rather that bit value. In following example, Carry flags is accessed by its name CY rather than its bit value “PSW.7”. ✞ 1 #include <8051.h> 3 void main(void) { for (;;) { 5 CY = 1; CY = 0;
  • 113. 3.5. ASSEMBLY IN C 113 7 } } ✌ ✆ The byte register name and bit register name are listed in the header file <8051.h>. Nearly, all the C like operations, i.e. if, if-else or if-else-if, switch- case, for loop, while loop, increments & decrements, array, arithmetic oper- ations, binary operations and bit shifts etc can be performed in 8051 with SDCC. ✞ #include <8051.h> 2 void main(void) { 4 unsigned int i; while (1) { 6 if (i > 3) { P0 = 0x50; 8 break; } else { 10 P0 = 0x01; } 12 i++; } 14 } ✌ ✆ Example of bit shift ✞ #include <8051.h> 2 void main(void) { 4 unsigned int i; P1=0x01; 6 while (1) { if (i > 3) { 8 P0 = 0x50; break; 10 } else { P1 = P1 << 1; 12 } i++; 14 } } ✌ ✆
  • 114. 114 µC Structure In following example, port P1 is reset to store given data A1H. The bit at port P1 are shifted leftward. The MSB bit of data at Port P1 is serially accepted and sent to the LSB bit of port P0 at first. ✞ 1 #include <8051.h> 3 void main(void) { unsigned int i; 5 P0 = 0x00; P1 = 0xA1; 7 for (i = 0; i < 8; i++) { P0_0 = P1_7; 9 P1 = P1 << 1; } 11 } ✌ ✆ In following example, array arithmetic is executed to get the sum of three numbers. ✞ 1 #include <8051.h> 3 void main(void) { unsigned int i; 5 unsigned char sum=0; unsigned char data[]={0x10 , 0x20 , 0x30}; 7 for (i = 0; i < 3; i++) { sum = sum+data[i]; 9 } P0=sum; 11 } ✌ ✆ After execution of program, we get the 60H value as result at port P0. Re- member that in 8051 C assembly, in the divisions of decimal numbers, quo- tient is always an integer. i.e. division does not takes place upto decimal place. This property is used in the conversion of numbers from one form to another form. ✞ 1 #include <8051.h> 3 void main(void) { unsigned char x,by ,d1 ,d2,d3;
  • 115. 3.5. ASSEMBLY IN C 115 5 by=0xAD; /* 0xAD = 173d*/ x=by /10; /* 173/10 = 17d*/ 7 d1=by %10;/* 137%10 = 3d*/ d2=x%10; /* 17%10 = 7d*/ 9 d3=x/10; /* 17/10 = 1d*/ P0=d1; /* 3d in unit place*/ 11 P1=d2; /* 7d in tenth place*/ P2=d3; /* 1d in hundredth place*/ 13 } ✌ ✆ If we want to catch the interrupt raised by timer, then we should use the following syntax: ✞ 1 void Timer <n>_ISR(void) __interrupt (<IE bit number for Tn >){ <statements > 3 } ✌ ✆ For example, for Time 0 interrupt, above syntax becomes ✞ 1 void Timer0_ISR(void) __interrupt (1){ <statements > 3 } ✌ ✆ Here, ‘1’ inside parentheses is bit number of IE register. To catch the in- terrupt arises from Timer 0 flag, we use ‘1’. To catch the interrupt arises from Timer 1 flag, we use ‘3’. Similarly, we can catch other interrupts. This function is used outside the main part of the C program. The syntax for serial interrupt is given below: ✞ 1 void Serial0_ISR(void) __interrupt (4){ <statements > 3 } ✌ ✆ An example using ISR is given below: ✞ 1 #include <8051.h> 3 void Timer0_ISR(void) __interrupt (1){ if(P1_1==0) 5 P1_1=1; else
  • 116. 116 µC Structure 7 P1_1=0; } 9 void main(void) { 11 TMOD=0x02; TH0=0XA4; 13 IE=0 x82; TR0 =1; 15 while(1){ P0 = P1; 17 } } ✌ ✆ 8051 C Compiler allocates RAM location for Register Bank 0 from address of 0 to 7. Individual variables are stored from address 08 and beyond. Array element are stored right after the variables’ address. Stack addresses are right after array elements. In the following example, we shall try to get the code space locations stored in the RAM. ✞ #include <8051.h> 2 void main(void) { 4 unsigned char mydata [100]; unsigned char x, z = 0; 6 for (x = 0; x < 100; x++) { z--; 8 mydata[x] = z; P1 = z; 10 } } ✌ ✆ Simulate the above program and see the result. See another example ✞ 1 #include <8051.h> 3 void main(void) { unsigned char mydata[] = "ABCD"; 5 unsigned char x, z = 0; for (x = 0; x < 4; x++) { 7 P1 = mydata[x]; }
  • 117. 3.5. ASSEMBLY IN C 117 9 } ✌ ✆ After successfull execution of simulation, the RAM data is looked like x0 x1 x2 x3 x4 x5 x6 x7 00 00 0B 00 00 00 00 00 04 08 41 42 43 44 00 00 00 00 10 00 00 00 00 00 00 00 00 ... 00 00 00 00 00 00 00 00 Table 3.2: RAM memory result. 3.5.1 Byte Register Some registers in 8051 are byte addressable via SDCC. The names of byte addressable registers are P0, P1, P2, P3, SP, DPL, DPH, PCON, TCON, TMOD, TL0, TL1, TH0, TH1, SCON, SBUF, IE, IP, Process State Word (PSW), ACC and B. 3.5.2 Bit Register Some registers in 8051 are bit addressable via SDCC. These bit are P0 0, P0 1, P0 2, P0 3, P0 4, P0 5, P0 6, P0 7, P1 0, P1 1, P1 2, P1 3, P1 4, P1 5, P1 6, P1 7, P2 0, P2 1, P2 2, P2 3, P2 4, P2 5, P2 6, P2 7, P3 0, P3 1, P3 2, P3 3, P3 4, P3 5, P3 6, P3 7, ACC, IT0, IE0, IT1, IE1, TR0, TF0, TR1, TF1, RI, TI, RB8, TB8, REN, SM2, SM1, SM0, EX0, ET0, EX1, ET1, ES, EA, RXD, TXD, INT0, INT1, T0, T1, WR, RD, PX0, PT0, PX1, PT1, PS, P, F1, OV, RS0, RS1, F0, AC and CY. In SDCC, we can access a register bit-by-bit using sbit operator. This operator is used as ✞ 1 __sbit __at (<bit address >) <bit name >; ✌ ✆ For example, to access the ACC.0 address bit we use ✞ 1 __sbit __at (0xE0) ACC_0; ✌ ✆ Now ACC 0 can modify to D0 bit of the accumulator. See example below:
  • 118. 118 µC Structure ✞ 1 #include <8051.h> __sbit __at(0xE0) ACC_0; 3 void main(void) { 5 int i; while (1) { 7 for (i = 0; i < 1; i++); ACC_0 = 1; 9 for (i = 0; i < 1; i++); ACC_0 = 0; 11 } } ✌ ✆ Solved Problem 3.3 Write a C program for 8051 to transfer the letter “Z serially at 4800 baud continuously. Use 8-bit data and 1 stop bit. Solution This problem is solved by setting the timer, baud rate and creating a loop for continuous transmission of symbol ‘Z’. The C program is given below: ✞ #include <8051.h> 2 void main(void) { 4 //+---------------------+ +-----------------------+ //| Timer 1 | | Timer 0 | 6 //+-----+-----+----+----+ +------+-----+-----+----+ //|TMOD | C/T | M1 | M0 | | TMOD | C/T | M1 | M0 | 8 //+-----+-----+----+----+ +------+-----+-----+----+ //| 0 | 0 | 1 | 0 | | 0 | 0 | 0 | 0 | 10 //+-----+-----+----+----+ +------+-----+-----+----+ TMOD = 0x20; 12 TH1 = 0xFA; 14 //+---------------------------------------------+ //| SCON | 16 //+-----+-----+-----+-----+-----+-----+----+----+ //| SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI | 18 //+-----+-----+-----+-----+-----+-----+----+----+ //| 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 |
  • 119. 3.5. ASSEMBLY IN C 119 20 //+-----+-----+-----+-----+-----+-----+----+----+ SCON = 0x50; 22 TR1 = 1; while (1) { 24 SBUF = ’Z’; while (TI == 0); 26 TI = 0; } 28 } ✌ ✆ Solved Problem 3.4 Write an 8051 C program to transfer the message “NO” serially at 9600 baud, 8-bit data, 1 stop bit. Do this continuously. Solution Here we use the Timer 1 for time count. “No” is made of two characters “N” and “O”. We use while loop to transmit these two characters continuously. A character is transmit serially in binary form. First it is stored in SBUF register and then transmits. Initially Transfer Interrupt (TI) flag is set to low. In each transmit of the character we wait until the whole byte is not transmitted. When a character is transmitted completely, i.e. at the end of 8th bit, TI flag is set to high. ✞ #include <8051.h> 2 void SerTx(unsigned char); 4 void main(void) { 6 //+---------------------+ +-----------------------+ //| Timer 1 | | Timer 0 | 8 //+-----+-----+----+----+ +------+-----+-----+----+ //|TMOD | C/T | M1 | M0 | | TMOD | C/T | M1 | M0 | 10 //+-----+-----+----+----+ +------+-----+-----+----+ //| 0 | 0 | 1 | 0 | | 0 | 0 | 0 | 0 | 12 //+-----+-----+----+----+ +------+-----+-----+----+ 14 TMOD = 0x20; TH1 = 0xFD; 16 //+---------------------------------------------+ //| SCON | 18 //+-----+-----+-----+-----+-----+-----+----+----+ //| SM0 | SM1 | SM2 | REN | TB8 | RB8 | TI | RI |
  • 120. 120 µC Structure 20 //+-----+-----+-----+-----+-----+-----+----+----+ //| 0 | 1 | 0 | 1 | 0 | 0 | 0 | 0 | 22 //+-----+-----+-----+-----+-----+-----+----+----+ 24 SCON = 0x50; TR1 = 1; 26 while (1) { SerTx(’N’); 28 SerTx(’O’); } 30 } 32 void SerTx(unsigned char x) { SBUF = x; 34 while (TI == 0); TI = 0; 36 } ✌ ✆ Solved Problem 3.5 Write a C program for 8051 MCU to toggle D1 bit of port P2 after each 250µs. Use Mode 1 of Timer 1 for this purpose. Solution The number of cycles required to be wait for time delay of 250µs is N = 250µs 1.085µs = 230 The timer should move 230 cycles to becomes FFFFH (65535 in decimal). The timer require one more cycle to get roll over, i.e. time value should have 65536 in decimal. Now, the initial values of Timer 1 should be 65536-230 = 65306 in decimal or FF1AH. Now the TH1 is FFH and TL1 is 1AH. When timer is roll over, it sets TF1 high. We read the TF1 flag to check the time delay of 250µs. When the bit is roll over, toggle the D1 bit of port P2. ✞ #include <8051.h> 2 void main(void) { 4 //+---------------------+ +-----------------------+ //| Timer 1 | | Timer 0 | 6 //+-----+-----+----+----+ +------+-----+----+-----+ //|TMOD | C/T | M1 | M0 | | TMOD | C/T | M1 | M0 | 8 //+-----+-----+----+----+ +------+-----+----+-----+
  • 121. 3.5. ASSEMBLY IN C 121 //| 0 | 0 | 1 | 0 | | 0 | 0 | 0 | 0 | 10 //+-----+-----+----+----+ +------+-----+----+-----+ TMOD = 0x20; // Timer 1 Mode 1 12 //+---------------------+ +----------------------+ 14 //| TH1 | | TH1 | //+---------------------+ +----------------------+ 16 //| 1 | 1 | 1 | 1 | | 1 | 1 | 1 | 1 | //+---------------------+ +----------------------+ 18 TH1 = 0xFF; // Timer 1, TH1 values 20 //+---------------------+ +----------------------+ //| TL1 | | TL1 | 22 //+---------------------+ +----------------------+ //| 0 | 0 | 0 | 1 | | 1 | 0 | 1 | 0 | 24 //+---------------------+ +----------------------+ TL1 = 0x1A; // Timer 1, TL1 values 26 // start the timer TR1 = 1; 28 while (1) { // infinite loop if (TF1 == 1) { // catch the roll over 30 if (P2_1 == 1) { //if P0_1 is high P2_1 = 0; // set it low 32 } else { // otherwise P2_1 = 1; // set it high 34 } TR1 = 0; // stop the timer 36 TF1 = 0; // clear rollover flag TH1 = 0xFF; // set initial TH1 38 TL1 = 0x1A; // set initial TL1 TR1 = 1; // start the timer 40 } } 42 } ✌ ✆ Above programm shall toggle D1 bit of port P2 after each 250µs.
  • 122. 122 Numerical Problems 3.6 Appendix 3.6.1 Hex File Hex file is the file which is loaded on MCU by the loader. Hex file provides the following information ✞ :CC AAAA TT DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD SS ✌ ✆ In hex file, each line is started with colon (:). It is followed by CC which tells how many data bytes to be loaded. Next, AAAA are the address of location where data of the current line is to be loaded by loaders. TT tells that whether there is a next line following to the current line or not. If TT is 00 then there is next line to be loaded and if TT is 01 then the current line is last one. Next 16 bytes are data bytes in hex form. SS is check-sum byte of whatever is there in the current line. 3.7 Glossary Masking If an interrupt or device is disabled either by software or by hardware, then it is said masking. A masked device or masked interrupt shall never be entertained by the micro controller.
  • 123. 3.7. GLOSSARY 123 4Numerical Problems Solved Problem 4.1 During execution of program by 8051MCU, its carry and parity flags are set to high. What is the value of Process Status Word? Solution PSW is a bit addressable register. The Process Status Word (PSW) has 6 conditional flags. The position of these conditional flags and their corresponding bit positions are shown in the following table. D7 D6 D5 D4 D3 D2 D1 D0 CY AC RS1 RS0 OV P When carry and parity flags are set high the binary value of PSW register becomes 10000001. CY AC RS1 RS0 OV P D7 D6 D5 D4 D3 D2 D1 D0 1 0 0 0 0 0 0 1 This binary is equivalent to decimal 129 and also equivalent to hexa- decimal 81H. Thus the value of PSW is 81H. Solved Problem 4.2 Set the value of timer register (TMOD) so that T1 timer works in Mode 0 operation. Solution Here the programming problem is to set the timer register so that its T1 timer works in mode 0 operation. The TMOD register bit are represented like GATE C/T M1 M0 Timer 1 MSB GATE C/T M1 M0 Timer 0 LSB
  • 124. 124 Numerical Problems A Mode 0 operation is a 13 bit timer mode in which 8 bit are reserved for TH and 5 bit are prescalar for TL. For Mode 0 operation of timer T1, its M1 and M0 bit of should be binary zeros. Now, the value of TMOD is TMOD Timer 1 Timer 0 GATE C/T M1 M0 GATE C/T M1 M0 0 0 0 0 0 0 0 0 Solved Problem 4.3 Set the value of timer register (TMOD) so that timer T1 works in 16 bit operation. Solution Here the programming problem is to set the timer register so that its T1 timer works in 16 bit operations. The TMOD register bit are represented like GATE C/T M1 M0 Timer 1 MSB GATE C/T M1 M0 Timer 0 LSB A Mode 1 operation is a 16 bit timer operation in which TH and TL are cascaded without prescalar. For Mode 1 operation of timer T1, its M1 bit should be binary zero and M0 bit should be binary 1. Now, the value of TMOD is TMOD Timer 1 Timer 0 GATE C/T M1 M0 GATE C/T M1 M0 0 0 0 1 0 0 0 0 The TMOD should be assigned value ✞ 1 MOV TMOD ,#10H ; 10H = 0001 0000B ✌ ✆ Solved Problem 4.4 How will you configure SCON register if you want to operate Mode 1 communication and Transfer Interrupts?
  • 125. 3.7. GLOSSARY 125 Solution The SCON register has following bit which are independently addressable. SM0 SM1 SM2 REN TB8 RB8 TI RI MSB LSB SM0 and SM1 bit are used to determine the data framing, i.e. the data communication mode. For Mode 1 communication, SM0 and SM1 bit should be 0 and 1 respectively. The transfer interrupt is enabled by setting TI bit high. Now the actual binary value of SCON register is 01000010, i.e. 42H or 66 in decimal. ✞ 1 MOV SCON ,#42H ; 8-bit , 1 stop , REN enabled END ; End program ✌ ✆ Solved Problem 4.5 Set the value of timer register (TMOD) so that timer T0 works in 8 bit auto reload operation mode. Solution Here the programming problem is to set the timer register so that its T0 timer works in mode 2 operation, i.e. 8 bit auto reload operations. The TMOD register bit are represented like GATE C/T M1 M0 Timer 1 MSB GATE C/T M1 M0 Timer 0 LSB A Mode 2 operation is a 8 bit auto reload operation. It means, the TH of the timer T0 hold an initial value and on timer run, TL of the timer T0 is counted upward. When TL is overflown or rolls-over, initial value of TH is copied into the TL as its initial value just after rollover. TMOD Timer 1 Timer 0 GATE C/T M1 M0 GATE C/T M1 M0 0 0 0 0 0 0 1 0
  • 126. 126 Numerical Problems ✞ MOV TMOD ,#02H ; 10H = 0000 0010B ✌ ✆ Solved Problem 4.6 Retrieve the numbers stored in ROM and add them cumulatively. Also show the result at Register R1. Solution In this problem, we first store the data in ROM. A size module is written to get the actual size of data. Size of data is obtained by subtracting the offsets addresses of start and end of data. Now a loop B1 is declared. This loop iterates the same number of times as the data size is. In each iteration, data is retrieved from ROM and copied into accumulator. This retrieved data is added to the previously stored data at register R1 and then again sum result is copied into R1. When all data is cumulatively summed, the iteration loop is exits. ✞ 1 ORG 0 ;----Size module begin ---------- 3 MOV DPTR ,# MYDATA ;Get Address of MYDATA MOV R6, DPL ;Copy address to R6 5 MOV DPTR ,# MYDATAEND ;Get Address of MYDATAEND MOV A,DPL ;Copy address to Accumulator 7 SUBB A,R6 ;Get size of data MOV R7,A ;Copy data size into Register R7 9 ;It is size of data and acts as counter ;----Size module end ---------- 11 MOV DPTR ,# MYDATA ;Get Address of MYDATA MOV R1 ,0 ;Reset the value of R1 to 0 13 B1: CLR A ;Clear accumulator 15 MOVC A,@A+DPTR ;Get ROM data to Accumulator ADD A,R1 ;Add R1 value to accumulator 17 MOV R1,A ;Copy accumulator value to R1 INC DPTR ;Increase data pointer 19 DJNZ R7 ,B1 ;Jump to B1 if R7 is not zero ;after decrements by one 21 ORG 30H ;New location of RAM MYDATA: ;ROM data 23 DB 10,5,12,8,5,8 MYDATAEND:
  • 127. 3.7. GLOSSARY 127 25 EXIT: END ✌ ✆ Solved Problem 4.7 Write a program that may able to transmit 8 bit data at output port P0 and transmit it with LSB comes first at this output port. Solution To solve this problem, we have stored an specific value in accu- mulator, i.e. register A. The bit of accumulator data are to be transmitted serially at output port P0. The loop times is controlled by value stored in register R1. In each loop, value of accumulator is rotated rightward through carry (RRC). The value at carry is moved (MOV) to D1 bit of port P0. The value of R1 is decremented and then jumped (DJNZ) to next iteration or loop until R1 is not become zero. ✞ MOV A ,#00H 2 MOV P0 ,A ;Reset port MOV A ,#72H ;Data to be transmitted serially 4 MOV R1 ,#8 HERE: 6 RLC A ;Rotate accumulator right through carry MOV P0.1 ,C ;Transmit data to D1 bit of P0 port 8 DJNZ R1,HERE;Jump to HERE if R1 is not zero after ;decrements by one 10 END ✌ ✆ Solved Problem 4.8 Write a program that may able to transmit 8 bit data at output port P0 and transmit it with MSB comes first at this output port. Solution To solve this problem, we have stored an specific value in accu- mulator, i.e. register A. The bit of accumulator data are to be transmitted serially at output port P0. The loop times is controlled by value stored in register R1. In each loop, value of accumulator is rotated leftward through carry (RLC). The value at carry is moved (MOV) to D1 bit of port P0. The value of R1 is decremented and then jumped (DJNZ) to next iteration or loop until R1 is not become zero. ✞ MOV A ,#00H 2 MOV P0 ,A ;Reset port MOV A ,#72H ;Data to be transmitted serially
  • 128. 128 Numerical Problems 4 MOV R1 ,#8 HERE: 6 RLC A ;Rotate accumulator left through carry MOV P0.1 ,C ;Transmit data to D1 bit of P0 port 8 DJNZ R1 ,HERE;Jump to HERE if R1 is not zero after ;decrements by one 10 END ✌ ✆ Solved Problem 4.9 Write a program that may able to receive 8 bit input data at port P2 and transmit it with LSB comes first at any of the output port. Solution To solve this problem, we have accepts an specific value from port P0 and stored it in accumulator. The bit of accumulator data are to be transmitted serially at output port P2. The loop times is controlled by value stored in register R1. In each loop, value of accumulator is rotated rightward through carry (RRC). The value at carry is moved (MOV) to D1 bit of port P2. The value of R1 is decremented and then jumped (DJNZ) to next iteration or loop until R1 is not become zero. ✞ MOV A ,#00H 2 MOV P2,A ;Reset port MOV A,P0 ;Acquire data from port P0 4 MOV R1 ,#8 HERE: 6 RRC A ;Rotate accumulator right through carry MOV P2.1 ,C ;Transmit data to D1 bit of P2 port 8 DJNZ R1 ,HERE;Jump to HERE if R1 is not zero after ;decrements by one 10 END ✌ ✆ Solved Problem 4.10 Write a program that may able to receive 8 bit input data at port P2 and transmit it with MSB comes first at any of the output port. Solution To solve this problem, we have accepts an specific value from port P0 and stored it in accumulator. The bits of accumulator data are to be transmitted serially at output port P2. The loop times is controlled by value stored in register R1. In each loop, value of accumulator is rotated
  • 129. 3.7. GLOSSARY 129 leftward through carry (RRC). The value at carry is moved (MOV) to D1 bit of port P2. The value of R1 is decremented and then jumped (DJNZ) to next iteration or loop until R1 is not become zero. ✞ MOV A ,#00H 2 MOV P2 ,A ;Reset port MOV A,P0 ;Acquire data from port P0 4 MOV R1 ,#8 HERE: 6 RLC A ;Rotate accumulator left through carry MOV P2.1 ,C ;Transmit data to D1 bit of P2 port 8 DJNZ R1,HERE;Jump to HERE if R1 is not zero after ;decrements by one 10 END ✌ ✆ Solved Problem 4.11 We want the output ‘47H’ (in BCD) in the accumulator by pressing two keys ‘4’ and ‘7’ from keyboard. Write the program for the same. Solution The hex number 47H is made of two digits ‘4’ and ‘7’. The numeric number ‘4’ pressed from key board of the computer is represented by char code 52 (decimal) or 34 (hex) in computer language. Similarly, numeric number ‘7’ is represented by char code 55 (decimal) or 37 (hex). First store the key data in accumulator and register. ✞ MOV A,#’4’ ;key 4 = ASCII 52 = 34H = 0011 0100 2 MOV R0 ,#’7’ ;key 7 = ASCII 55 = 37H = 0011 0111 ✌ ✆ We need only lower nibbles of the two numbers to make 47H. So we have to mask the upper nibbles. ✞ ANL A ,#0FH ; 0011 0100 AND 0000 1111 = 0000 0100 2 ANL R0 ,#0FH ; 0011 0111 AND 0000 1111 = 0000 0111 ✌ ✆ Remember that ANL can not be performed with registers other than accu- mulator or carry. So we have to add additional steps to move data from accumulator to register and vice versa. Here, only indicative steps are used. See complete code given at the last of the solution. Now the binary 0000 0100 should make the upper nibble of 47H. So we swap the lower and upper nibbles with each other.
  • 130. 130 Numerical Problems ✞ SWAP A ; 0000 0100 = 0100 0000 ✌ ✆ Now add the accumulator with R0 to store 47H in accumulator. ✞ 1 ORL A,R0 ; 0100 0000 OR 0000 0111 = 0100 0111 ✌ ✆ The whole program is written as ✞ 1 MOV A,#’4’ MOV R0 ,#’7’ 3 ANL A,#0FH MOV R1,A 5 MOV A,R0 ANL A,#0FH 7 MOV R0,A MOV A,R1 9 SWAP A ORL A,R0 11 END ✌ ✆ Solved Problem 4.12 Write a C program for 8051 MCU that may able to send a message to LCD through port pin P1. Solution Solution of this problem is explained in below C program. Here, a string i.e. a message is declared as an array. Read the message byte by byte and transfer it into the port P1. ✞ 1 #include <8051.h > 3 void main(void) { unsigned char msg[] = "The Earth"; 5 unsigned char z; for (z = 0; z < 9; z++) { 7 P1 = msg[z]; } 9 } ✌ ✆ Solved Problem 4.13 Write a C program for 8051 MCU to toggle D1 digit of the port P0 upto any specific times of loop.
  • 131. 3.7. GLOSSARY 131 Solution In this program, a for loop is used to toggle the D1 digit of the port P0 upto specific times. As the pin is toggling between binary 0 to binary 1, therefore we have used a condition inside the for loop to check the current value of D1 digit of the port P0 and toggle it to other level. ✞ 1 #include <8051.h> 3 void main(void) { unsigned int j; 5 for(j=0;j <1085;j++){ // Toggle 1085 times if(P0_1==1) //if P0_1 pin is high 7 P0_1=0x00; // set pin to low else // otherwise 9 P0_1=0x01; // set pin to high } 11 } ✌ ✆ Solved Problem 4.14 Write a C program for 8051 MCU to toggle D1 digit of the port P2 high when there is an odd number and low when there is an even number. Solution In this program, a for loop is used to toggle the D1 digit of the port P2 high when there is an odd number and toggle to low when there is an even number. The remainder rule is used to check the number whether it is odd or whether it is even and according to the type of number, we toggle the D1 digit of port P2. ✞ 1 #include <8051.h> 3 void main(void) { int i = 0; 5 while (i < 100) { if (i % 2 == 0) { 7 P2_1 = 0; } else { 9 P2_1 = 1; } 11 i++; } 13 } ✌ ✆
  • 132. 132 Numerical Problems Solved Problem 4.15 Write a C program for 8051 MCU to toggle D1 digit of the port P2 low when there is a prime number and high when there is non prime number. Also show the number at port P1. Solution The complete C program for this problem is given below. This program is self explanatory. A number is a prime number if its factors are either itself or one only. For first 100 numbers, we first checked whether the number is a prime number or not. After that, if number is prime number then D1 digit of port P2 is toggled low. Similarly, if number is not a prime number then D1 digit of port P2 is toggled to high. The number is also transferred to port P1 for its display. ✞ 1 #include <8051.h> int isprime (int); 3 void main(void) { int i = 3; 5 while (i < 100) { if (isprime (i)) { 7 P2_1 = 0; } else { 9 P2_1 = 1; } 11 P1 = i; i++; 13 } } 15 int isprime (int i) { int j = 0; 17 int k = 0; 19 if (i < 3) { return 0; 21 } if (i == 3) { 23 return 1; } 25 if (i % 2 == 0) { return 0; 27 } else { int l = (i / 2) + 1;
  • 133. 3.7. GLOSSARY 133 29 for (j = 3; j <= l; j += 2) { if (i % j == 0) { 31 return 0; } else { 33 k = 1; } 35 } } 37 return k; } ✌ ✆