8051 Notes 3rd Unit PDF
8051 Notes 3rd Unit PDF
refreshnotes.com/2016/02/8051-data-transfer-instructions.html
A, Acc: Accumulator
B: B special function register
C: Carry flag in PSW
Rn: Register R7-R0 of the currently selected Register Bank.
dir: 8-bit internal data location’s address. This could be an Internal Data RAM
location (0-127) or a SFR [i.e., I/O port, control register, status register, etc. (128-
255)]. Direct Addressing Mode.
@Ri : 8-bit internal data RAM location (0-255) addressed indirectly through
register R1or R0. Indirect Addressing Mode.
#data: 8-bit constant included in instruction. Immediate Addressing Mode.
#data 16: 16-bit constant included in instruction. Immediate Addressing Mode.
addr 16: 16-bit destination address. Used by LCALL and LJMP. A branch can be
anywhere within the 64K byte Program Memory address space. Long Addressing
Mode.
addr 11: 11-bit destination address. Used by ACALL and AJMP. The branch will be
within the same 2K byte page of program memory as the first byte of the following
instruction. Absolute Addressing Mode.
rel: Signed (two’s complement) 8-bit offset byte. Used by SJMP and all conditional
jumps. Range is -128 to +127 bytes relative to first byte of the following instruction.
Relative Addressing Mode.
bit: Direct Addressed bit in Internal Data RAM or Special Function Register
1B: 1 byte
2B: 2 byte
3B: 3 byte
1C: 1 cycle
2C: 2 cycles
4C: 4 cycles
P: oscillator Period
Mnemonic Description
1/4
MOV A,#data Move immediate data to Accumulator
[2B,1C,12P]
2/4
PUSH direct Push direct byte onto Stack
[2B,2C,24P]
Move
MOV instruction is concerned with moving data internally
MOV R2, #80h ; Move immediate data value 80h to register R2
MOV R4, A ; Copy data from accumulator to register R4
MOV DPTR, #0F22Ch ; Move immediate value F22Ch to the DPTR register
MOV R2, 80h ; Copy data from 80h (Port 0 SFR) to R2
MOV 52h, #52h ; Copy immediate data value 52h to RAM location 52h
MOV 52h, 53h ; Copy data from RAM location 53h to RAM 52h
MOV A, @R0 ; Copy contents of location addressed in R0 to A
MOVX instruction is used to access the external memory (X indicates eXternal memory
access). All external moves must work through the A register (accumulator).
MOVX @DPTR, A ; Copy data from A to the address specified in DPTR
MOVX A, @DPTR ; Copy data from address specified in DPTR to A
MOVC instruction is used to read data from the external code memory (ROM).
MOV DPTR, # 2000h ; Copy the data value 2000h to the DPTR register
MOV A, #80h ; Copy the data value 80h to register A
MOVC A, @A+DPTR ; Copy the contents of the address 2080h (2000h + 80h) to register
A
Exchange
XCH (eXCHange) instruction is used to swap the data between source and destination,
effectively changing the source data. XCH instructions must use register A. XCHD is a
special case of the exchange instruction where just the lower nibbles are exchanged.
3/4
XCH A, R3 ; Exchange bytes between A and R3
XCH A, @R0 ; Exchange bytes between A and RAM location whose address is in R0
XCH A, A0h ; Exchange bytes between A and RAM location A0h (SFR port 2)
4/4
Arithmetic group in 8051
tutorialspoint.com/arithmetic-group-in-8051
In 8051 Microcontroller there are 24 different instructions under the Arithmetic Group.
In total there are 64 opcodes. The Carry Flag (CY), Auxiliary Carry (AC)and Overflow flag
(OV) are affected based on the result of ADD, ADDC, SUBB etc. instructions. The
multiple and divide instructions clear the Carry flag, and also does not affect the AC flag.
After execution of multiplication, the OV flag will be 1 when the result is greater than
FFH. Otherwise, it is 0. Similarly, after division OV flag is 1 when the content of B is 00H
before division, otherwise it is 0. The DA A (Decimal Adjust Accumulator) affects the
Carry Flag only.
In the following table, we will see the Mnemonics, Lengths, Execution Time in terms of
the machine cycle, Number of Opcodes etc.
ADD A, Rn 8
ADD A, a8 1
ADD A, @Ri 2
ADD A, #d8 1
ADDC A, Rn 8
ADDC A, a8 1
ADDC A, @Ri 2
ADDC A, #d8 1
SUBB A, Rn 8
SUBB A, a8 1
SUBB A, @Ri 2
SUBB A, #d8 1
INC A 1
INC Rn 8
INC a8 1
INC @Ri 2
DEC A 1
1/3
Mnemonics Byte Count execution time OpcodeCount
DEC Rn 8
DEC a8 1
DEC @Ri 2
INC DPTR 1
MUL AB 1
DIV AB 1
DA A 1
Examples
1 ADD A, 32H
This is an instruction of type ADD A, #d8. The immediate data 32H is added to
register A. The result is also stored in A.
2 ADDC A, @R1
This is an instruction of type ADDC A, @Ri. It means the content on internal
RAM location which is pointed by register R1 is added to A.
3 SUBB A, R5
This is SUBB A, Rn type instruction. The SUBBstands for Subtract with
borrow. So the content of R5 will be subtracted from A.
4 INC 40H
This is a type of INC a8. Here the content in local RAM whose address is 40H,
it is increased by 1.
5 INC DPTR
It can increase the content of DPTR by 1. This instruction takes two machine
cycle to execute.
6 MUL AB
This instruction is used to multiply the content of register A and B. The 16-bit
address will be stored at B and A registers. The B will hold the MS byte, and A
will hold the LSByte.
7 DIV AB
This instruction is used to divide the content of A register by B register. The 8-
bit quotient is stored into the register A, and the 8-bit remainder is stored into
the register B.
2/3
3/3
8051 Logical Operation Instructions
refreshnotes.com/2016/02/8051-logical-operation-instructions.html
Logical Operations
A, Acc: Accumulator
B: B special function register
C: Carry flag in PSW
Rn: Register R7-R0 of the currently selected Register Bank.
dir: 8-bit internal data location’s address. This could be an Internal Data RAM
location (0-127) or a SFR [i.e., I/O port, control register, status register, etc. (128-
255)]. Direct Addressing Mode.
@Ri : 8-bit internal data RAM location (0-255) addressed indirectly through
register R1or R0. Indirect Addressing Mode.
#data: 8-bit constant included in instruction. Immediate Addressing Mode.
#data 16: 16-bit constant included in instruction. Immediate Addressing Mode.
addr 16: 16-bit destination address. Used by LCALL and LJMP. A branch can be
anywhere within the 64K byte Program Memory address space. Long Addressing
Mode.
addr 11: 11-bit destination address. Used by ACALL and AJMP. The branch will be
within the same 2K byte page of program memory as the first byte of the following
instruction. Absolute Addressing Mode.
rel: Signed (two’s complement) 8-bit offset byte. Used by SJMP and all conditional
jumps. Range is -128 to +127 bytes relative to first byte of the following instruction.
Relative Addressing Mode.
bit: Direct Addressed bit in Internal Data RAM or Special Function Register
1B: 1 byte
2B: 2 byte
3B: 3 byte
1C: 1 cycle
2C: 2 cycles
4C: 4 cycles
P: oscillator Period
Mnemonic Description
1/3
ANL A,#data AND immediate data to Accumulator
[2B,1C,12P]
2/3
RR A Rotate Accumulator Right
[1B,1C,12P]
Boolean operations
ANL Logical AND
ORL Logical OR
CPL Complement (logical NOT)
XRL Logical XOR (exclusive OR)
ANL A, #55h ; AND each bit in A with corresponding bit in number 55h, leaving the result
in A.
ANL 42h, R4 ; AND each bit in RAM location 42h with corresponding bit in R4, leaving
the result in RAM location 42h.
ORL A,@R1 ; OR each bit in A with corresponding bit in the number whose address is
contained in R1 leaving the result in A.
XRL R4, 80h ; XOR each bit in R4 with corresponding bit in RAM location 80h (port 0),
leaving result in A.
CPL R0 ; Complement each bit in R0
Rotate
The ability to rotate the A register (accumulator) data is useful to allow examination of
individual bits. The options for such rotation are as follows:
RL A ; Rotate A one bit to the left. Bit 7 rotates to the bit 0 position
RLC A ; The Carry flag is used as a ninth bit in the rotation loop
RR A ; Rotates A to the right (clockwise)
RRC A ; Rotates to the right and includes the carry bit as the 9th bit.
Swap
The Swap instruction swaps the accumulator’s high order nibble with the low-order
nibble using the instruction.
SWAP A
3/3
8051 Program Branching Instructions
refreshnotes.com/2016/02/8051-program-branching-instructions.html
A, Acc: Accumulator
B: B special function register
C: Carry flag in PSW
Rn: Register R7-R0 of the currently selected Register Bank.
dir: 8-bit internal data location’s address. This could be an Internal Data RAM
location (0-127) or a SFR [i.e., I/O port, control register, status register, etc. (128-
255)]. Direct Addressing Mode.
@Ri : 8-bit internal data RAM location (0-255) addressed indirectly through
register R1or R0. Indirect Addressing Mode.
#data: 8-bit constant included in instruction. Immediate Addressing Mode.
#data 16: 16-bit constant included in instruction. Immediate Addressing Mode.
addr 16: 16-bit destination address. Used by LCALL and LJMP. A branch can be
anywhere within the 64K byte Program Memory address space. Long Addressing
Mode.
addr 11: 11-bit destination address. Used by ACALL and AJMP. The branch will be
within the same 2K byte page of program memory as the first byte of the following
instruction. Absolute Addressing Mode.
rel: Signed (two’s complement) 8-bit offset byte. Used by SJMP and all conditional
jumps. Range is -128 to +127 bytes relative to first byte of the following instruction.
Relative Addressing Mode.
bit: Direct Addressed bit in Internal Data RAM or Special Function Register
1B: 1 byte
2B: 2 byte
3B: 3 byte
1C: 1 cycle
2C: 2 cycles
4C: 4 cycles
P: oscillator Period
Mnemonic Description
1/3
RETI Return from interrupt
[1B,2C,24P]
CJNE A,direct,rel Compare direct byte to Acc and Jump if Not Equal
[3B,2C,24P]
NOP No operation
[1B,1C,12P]
Jump
LJMP (long jump) causes the program to branch to a destination address defined by the
16-bit operand in the jump instruction. Because a 16-bit address is used the instruction
can cause a jump to any location within the 64KByte program space (216 = 64K).
LJMP LABEL_X ; Jump to the specified label
LJMP 0F200h ; Jump to address 0F200h
LJMP @A+DPTR ; Jump to address which is the sum of DPTR and Reg. A
2/3
SJMP (short jump) uses a singe byte address. This address is a signed 8-bit number and
allows the program to branch to a distance –128 bytes back from the current PC address
or +127 bytes forward from the current PC address.
Most 8051 jump instructions use an 8-bit destination address, based on relative
addressing, i.e. addressing within the range –128 to +127 bytes.
JZ LABEL_1 ; Jump to LABEL_1 if accumulator is equal to zero
JNZ LABEL_X ; Jump to LABEL_X if accumulator is not equal to zero
JNC LABEL_Y ; Jump to LABEL_Y if the carry flag is not set
DJNZ R2, LABEL ; Decrement R2 and jump to LABEL if the resulting value of R2 is not
zero.
CJNE R1, #55h , LABEL_2 ; Compare the magnitude of R1 and the number 55h and jump
to LABEL_2 if the magnitudes are not equal.
Call
LCALL instruction is used to call a subroutine at a specified address. The address is 16
bits long so the call can be made to any location within the 64KByte memory space.
ACALL instruction is logically similar to the LCALL but has a limited address range
similar to the AJMP instruction.
Return
The return from subroutine is achieved using the RET instruction.
3/3
8051 IO Port Programming
refreshnotes.com/2016/03/8051-io-port-programming.html
In the 8051 there are a total of four ports for I/O operations. A total of 32 pins are set
aside for the four ports PO, P1 P2, and P3, where each port takes 8 pins.
The four ports PO, P1, P2, and P3 each use 8 pins, making them 8-bit ports. All the ports
upon RESET are configured as inputs, ready to be used as input ports. When the first 0 is
written to a port, it becomes an output. To reconfigure it as an input, a 1 must be sent to
the port.
ORG 0H
ACALL DELAY
ACALL DELAY
SJMP BACK
; Delay Subroutine
1/5
DELAY MOV R3, ; initialize the counter
: #0FFH
END
ORG 0H
BACK : MOV A, P0
MOV P1, A
SJMP BACK
END
Bit Addressability:
Sometimes we need to access only 1 or 2 bits of the port instead of the entire 8 bits. When
accessing a port in single-bit manner, we use the syntax “SETB X. Y” where X is the port
number 0, 1,2. or 3, and Y is the desired bit number from 0 to 7.
Toggle P0.1:
ORG 0H
ACALL DELAY
CLR P0.1
ACALL DELAY
SJMP AGAIN
2/5
; Delay Subroutine
END
Instruction Function
Monitoring an IO pin:
ORG 0H
END
3/5
Mnemonic Example Description
MOV C, PX.Y MOV C, P0.1 Copy the status of P0.1 to Carry flag
Mnemonic Example
INC PX INC P0
DEC PX DEC P0
The instructions that read the port latch normally read a value, perform an operation (and
possibly change it), then rewrite it back to the port latch. This is often called “Read-
Modify-Write”. A single instruction can be used to read the port, modify its value and
write the result back to port.
ORG 0H
4/5
AGAIN: XRL P0, ; read P0, XOR P0 with FFH and write the result back to
#0FFH P0
ACALL DELAY
SJMP AGAIN
; Delay Subroutine
END
5/5
8051 Timer Programming
refreshnotes.com/2016/03/8051-timer-programming.html
The 8051 has two timers/counters. They can be used either as timers to generate a time
delay or as counters to count events happening outside the microcontroller.
The two timers, Timer 0 and Timer 1, are 16-bit wide. Both timers 0 and 1 use the same
register, called TMOD, to set the various timer operation modes. There are three modes:
0, 1, 2 and 3. Mode 0 is a 13-bit timer, mode 1 is a 16-bit timer, mode 2 is an 8-bit timer
and mode 3 is split timer mode.
C/T bit in the TMOD register is used to decide whether the timer is used as a delay
generator or an event counter. If C/T = 0, it is used as a timer for time delay generation.
The clock source for the time delay is the crystal frequency of the 8051.
The timers in the 8051 can be controlled by both software and hardware. The start and
stop of the timer are controlled by way of software by the TR (timer start) bits TRO and
TR1. Software instructions start and stop the timers as long as GATE = 0 in the TMOD
register. The hardware way of starting and stopping the timer by an external source is
achieved by making GATE = 1 in the TMOD register.
Mode 1:
ORG 0H
1/5
CPL P1.5 ; toggle P1.5
ACALL DELAY
SJMP START
ORG 100H
LOOP : JNB TF0, LOOP ; monitor timer flag 0 until it rolls over
RET
END
Delay calculation for XTAL = 11.0592 MHz is Delay = (FFFF – YYXX + 1) * 1.085 µs
where YYXX are TH, TL initial values respectively. Notice that values YYXX are in hex.
To get the largest delay we make TL and TH both 0. This will count up from 0000 to
FFFFH and then roll over to zero.
Largest delay = (65536 – 0) * 1.085 µs = 71106.56 µs = 71.1065 ms.
For known delay, to find the values needed for the TH, TL registers,
ORG 0H
2/5
START : CPL P1.5 ; toggle P1.5
ACALL DELAY
SJMP START
ORG 100H
LOOP2 : JNB TF0, LOOP2 ; monitor timer flag 0 until it rolls over
RET
END
Mode 0:
Mode 0 is exactly like mode 1 except that it is a 13-bit timer instead of 16-bit. The 13-bit
counter can hold values between 0000 to 1FFFH in TH – TL. Therefore, when the timer
reaches its maximum of 1FFH, it rolls over to 0000, and TF is raised.
Mode 2:
3/5
When the TL register rolls from FFH to 0 and TF is set to 1, TL is reloaded
automatically with the original value kept by the TH register. To repeat the process,
we must simply clear TF and let it go without any need by the programmer to reload
the original value. This makes mode 2 an auto-reload, in contrast with mode 1 in
which the programmer has to reload TH and TL.
In auto-reload, TH is loaded with the initial count and a copy of it is given to TL. This
reloading leaves TH unchanged, still holding a copy of the original value.
ORG 0H
SJMP START
ORG 100H
LOOP1 : JNB TF0, LOOP1 ; monitor timer flag 0 until it rolls over
RET
END
Delay calculation for XTAL = 11.0592 MHz is Delay = (FF – XX) * 1.085 µs where XX is
TH initial value. Notice that value XX is in hex.
4/5
5/5
8051 Counter
refreshnotes.com/2016/03/8051-counter.html
8051 timers can also be used as counters to count the external events. When the
timer/counter is used as a timer, the 8051′s crystal is used as the source of the frequency.
When it is used as a counter, however, it is a pulse outside the 8051 that increments the
TH, TL registers.
The C/T bit in the TMOD register decides the source of the clock for the timer. If C/T = 0,
the timer gets pulses from the crystal. In contrast, when C/T = 1, the timer is used as a
counter and gets its pulses from outside the 8051. Therefore, when C/T = 1, the counter
counts up as pulses are fed from pins 14 (Timer 0 input) and 15 (Timer 1 input). In the
case of Timer 0, when C/T = 1, pin P3.4 provides the clock pulse and the counter counts
up for each clock pulse coming from that pin. Similarly, for Timer 1, when C/T = 1 each
clock pulse coming in from pin P3.5 makes the counter count up.
ORG 0H
SJMP START
END
1/1
8051 Serial Port
refreshnotes.com/2016/03/8051-serial-port.html
Registers:
SCON
5 SM2 Mutliprocessor Communications Enable. When set the "RI" flag will only
be triggered if the 9th bit received was a "1"
4 REN Receiver Enable. This bit must be set in order to receive characters
1/2
Mode 0: Oscillator / 12 = 11.059 MHz/12 = 921,583 baud
Mode 2: Oscillator / 64 = 11.059 MHz/64 = 172,797 baud
Mode 1 & 3: Baud rate is determined by how frequently timer 1 overflows. The most
common method is to configure timer 1 in 8-bit auto reload mode and set a reload
value (TH1) that causes Timer 1 to overflow at a frequency appropriate to generate a
baud rate.
Transmit Data:
To send a byte to the serial port one must simply write the value to the SBUF (99h)
SFR
Wait until TI bit is set
Receive Data:
Wait for the 8051 to set the RI flag
Read the value stored in the SBUF (99h) SFR
8051
2/2
8051 Programming Serial Interrupt
refreshnotes.com/2016/03/8051-programming-serial-interrupt.html
TI (transfer interrupt) is raised when the last bit of the framed data, the stop bit, is
transferred, indicating that the SBUF register is ready to transfer the next byte. RI
(received interrupt), is raised when the entire frame of data, including the stop bit, is
received. In other words, when the SBUF register has a byte, RI is raised to indicate that
the received byte needs to be picked up before it is lost (overrun) by new incoming serial
data. In the 8051 only one interrupt is set aside for serial communication. This interrupt
is used to both send and receive data. If the interrupt bit in the IE register (IE.4) is
enabled, when RI or TI is raised the 8051 gets interrupted and jumps to memory address
location 0023H to execute the ISR. In that ISR we must examine the TI and RI flags to see
which one caused the interrupt and respond accordingly.
; wake up and go to main, avoid using memory space allocated for ISR
ORG 0H
ORG 0023H
LJMP ISR_SERIAL
ORG 30H
HERE : MOV A, R2
JZ HERE
1/2
DEC R2
DEC R1
ORG 100H
CLR RI
TRANSMIT : CLR TI
END
2/2
8051 Interrupts
refreshnotes.com/2016/03/8051-interrupts.html
1/4
External event 1 0013h P3.3 Auto
(13)
A limited number of bytes is set aside for each interrupt. A total of 8 bytes from location
0003 to 000A is set aside for INT0, external hardware interrupt 0. If the service routine
for a given interrupt is short enough to fit in the memory space allocated to it, it is placed
in the vector table; otherwise, an LJMP instruction is placed in the vector table to point to
the address of the ISR. In that case, the rest of the bytes allocated to that interrupt are
unused.
Setting Interrupts:
By default at power up, all interrupts are disabled
Program may enable and disable interrupts by modifying the IE SFR (A8h).
EA bit must be set to trigger interrupt(s)
6 -- Undefined
5 -- Undefined
Interrupt Priority:
2/4
By default, 8051 checks interrupt conditions in the following order
External 0 Interrupt
Timer 0 Interrupt
External 1 Interrupt
Timer 1 Interrupt
Serial Interrupt
Program can set priority to interrupt. There are two levels of interrupt priority: high and
low. Interrupt priorities are controlled by IP SFR (B8h).
7 -- Undefined
6 -- Undefined
5 -- Undefined
3/4
Two bytes are popped off the stack into the Program Counter to restore normal
program execution.
Interrupt status is restored to its pre-interrupt status.
Important Considerations:
Microcontroller protects Program Counter only. It does not protect other registers like,
PSW
DPTR (DPH/DPL)
SP
ACC
B
Registers R0-R7
In general, interrupt routine must protect the above registers. Interrupt routine must
pushes the register values onto the stack using the PUSH instruction before executing
event handling related code. Once the execution is done, it pops the original values back
into the registers. It is generally a good idea to always protect registers by pushing and
popping it off the stack at the beginning and end of your interrupts.
4/4
8051 Assembly Programming
refreshnotes.com/2016/03/8051-assembly-programming.html
ORG
END
ORG – Indicates the start of the program. It tells the assembler to place the opcode
at memory location 0
END – Indicates to the assembler the end of program or source code
Label – Allows the program to refer a line of code by name. Any label referring to an
instruction must be followed by a colon symbol (:). It is an optional field. Each label
name must be unique. Label consists of alphabets A to Z or a to z, numbers 0 – 9,
special characters question mark (?), period (.), at (@), underline (_) and dollar ($).
First character of label must be alphabetic. Mnemonics and reserved words must
not be used as label.
Mnemonic is the command to CPU
Operand(s) is the data to command. It is an optional field. Few commands do not
need data.
Comment – Comments may be at the end of a line or on a line by themselves.
Comment must begin with semicolon (;). It is an optional field.
Sample Program:
1/2
START : MOV R1, #23H ; load 23H into R1
2/2
8051 Assembling a Program
refreshnotes.com/2016/03/8051-assembling-program.html
Editor: It is to create source (myfile.asm) file with extension asm or src. The asm
extension for the source file is used by an assembler.
Assembler: It converts the assembly language instructions into machine code. The
assembler will produce an object file (.obj) and a list file (.lst). List file lists all the
opcodes, addresses and errors, if detected, in program
Linker: It takes one or more object files and produces an absolute object file with the
extension abs
OH: It is an object to hex converter. It creates a file with extension hex that is ready to
burn into ROM
List File:
It lists all the opcodes and addresses as well as
errors that the assembler detected. The
programmer uses the list file to find syntax errors.
It is only after fixing all the errors indicated in the
lst file that the obj file is ready to be input to the
linker program.
Machine Assembly
Address Language Language
0000 ORG 0H
0006 2D ADD A, R5
0007 2F ADD A, R7
000A END
1/2
2/2