8051 Microcontroller: Branching, Looping, and Bit Manipulation Instructions
Overview of Module 5 and 8051 Memory Organization
Module 5 focuses on LOOP and CALL instructions, Assembly Language Programming (ALP), and interfacing techniques for the 8051 microcontroller. Covered topics include the generation of Square, Sine, and Triangular waveforms using Digital-to-Analog Converter (DAC) interfacing, as well as Stepper motor interfacing. Key references include Text 3, sections 3.1, 3.2, 13.2, and 17.2.
8051 Memory Specifications
The 8051 microcontroller features distinct memory spaces with specific sizes and address ranges:
- Internal RAM: bytes, residing in the address range to .
- Internal ROM: , residing in the address range to .
- External RAM: Up to , spanning the address range to .
- External ROM: Up to , spanning the address range to .
Memory Paging and Program Memory
The total program memory of the 8051 is , covering addresses from to . This memory is architecturally divided into pages to facilitate specific jump and call instructions. Each page is exactly ( bytes) in size.
- Page 0: to
- Page 1: to
- Page 2: to
- … and continuing through the address space.
Unconditional Jump Instructions
Unconditional jumps are branching instructions used to control program flow without checking any specific flags or conditions.
SJMP (Short Jump)
- Description: Performs a jump within a restricted short range.
- Range: to bytes from the current program counter.
- Syntax:
SJMP label - Example Code:
assembly MOV A, #00H SJMP SKIP MOV A, #FFH SKIP: NOP ; Accumulator A remains 00H
LJMP (Long Jump)
- Description: Allows the program to jump to any location within the entire program memory space.
- Syntax:
LJMP address - Example:
LJMP 2000H(Jumps to memory address ).\n
AJMP (Absolute Jump)
- Description: Jumps to an address within the same memory page currently being accessed by the program counter.
- Syntax:
AJMP address - Example:
AJMP 0100H
Conditional Jump Instructions
These instructions execute a jump only if a specific condition (determined by flags or bit states) is met.
Accumulator-Based Jumps
- JZ (Jump if Zero): Jumps to the specified label if the Accumulator () is exactly zero.
- Syntax:
JZ label - Example:
assembly MOV A, #00H JZ ZERO_LABEL ; Jump occurs because A = 0
- Syntax:
- JNZ (Jump if Not Zero): Jumps to the specified label if the Accumulator () is not zero.
- Syntax:
JNZ label - Example:
assembly MOV A, #05H JNZ NOT_ZERO ; Jump occurs because A = 5
- Syntax:
Carry Flag-Based Jumps
- JC (Jump if Carry): Jumps if the Carry Flag () is set to .
- Syntax:
JC label - Example:
assembly SETB C JC CARRY_LABEL ; Jump occurs
- Syntax:
- JNC (Jump if No Carry): Jumps if the Carry Flag () is cleared to .
- Syntax:
JNC label - Example:
assembly CLR C JNC NO_CARRY ; Jump occurs
- Syntax:
Bit-Based Jumps
- JB (Jump if Bit Set): Jumps to the label if the specified bit is .
- Syntax:
JB bit, label - Example:
assembly SETB P1.0 JB P1.0, BIT_SET ; Jump occurs
- Syntax:
- JNB (Jump if Bit Not Set): Jumps to the label if the specified bit is .
- Syntax:
JNB bit, label - Example:
assembly CLR P1.0 JNB P1.0, BIT_CLEAR ; Jump occurs
- Syntax:
- JBC (Jump if Bit Set and Clear): Jumps if the bit is , then automatically clears that bit to after the jump.
- Syntax:
JBC bit, label - Example:
assembly SETB P1.0 JBC P1.0, LABEL ; Jumps and then sets P1.0 to 0
- Syntax:
Loop Control Instructions
DJNZ (Decrement and Jump if Not Zero)
- Description: This is the primary instruction for creating loops. It decrements the specified register or memory location by and performs a jump if the resulting value is not zero. If the value becomes zero, the program continues with the next instruction in sequence.
- Syntax:
DJNZ reg, label - Example (Looping 5 times):
assembly MOV R0, #05H LOOP: DJNZ R0, LOOP ; Decrements R0; if R0 ≠ 0, jump to LOOP
Subroutine Instructions
Subroutines are blocks of code that can be called from different parts of a program to perform specific tasks.
- ACALL (Absolute Call): Calls a subroutine within the same memory range as the current program counter.
- Syntax:
ACALL address - Example:
ACALL SUB1
- Syntax:
- LCALL (Long Call): Calls a subroutine located anywhere in the entire program memory space.
- Syntax:
LCALL address - Example:
LCALL 3000H
- Syntax:
- RET (Return): Used at the end of a subroutine to return the program flow to the instruction following the original CALL.
- Example:
assembly SUB1: MOV A, #55H RET
- Example:
- RETI (Return from Interrupt): A specialized return instruction used at the conclusion of an Interrupt Service Routine (ISR) to restore the program flow and signal that the interrupt processing is complete.
Bit Manipulation Instructions
These instructions allow for the manipulation of individual bits in the bit-addressable RAM and I/O ports.
Set, Clear, and Complement
- SETB (Set Bit): Sets the specified bit or the Carry Flag to .
- Syntax:
SETB bit/SETB C - Examples:
SETB P1.0(Port 1 bit 0 = );SETB C(Carry = ).
- Syntax:
- CLR (Clear Bit): Sets the specified bit or the Carry Flag to .
- Syntax:
CLR bit/CLR C - Examples:
CLR P1.0(Port 1 bit 0 = );CLR C(Carry = ).
- Syntax:
- CPL (Complement Bit/Carry): Toggles the state of the bit ( or ).
- Syntax:
CPL bit/CPL C - Example (Toggling P1.0):
assembly SETB P1.0 CPL P1.0 ; P1.0 changes from 1 to 0
- Syntax:
Bit Movement
- MOV C, bit: Copies the state of the specified bit into the Carry Flag.
- Syntax:
MOV C, bit - Example:
MOV C, P1.0(Carry flag takes state of Port 1 bit 0).
- Syntax:
- MOV bit, C: Copies the context of the Carry Flag into the specified bit.
- Syntax:
MOV bit, C - Example:
MOV P1.1, C(Sets bit P1.1 to match Carry).
- Syntax:
Bit Logic Operations
- ANL C, bit: Performs a logical AND between the Carry Flag and the specified bit; the result is stored in the Carry Flag.
- Syntax:
ANL C, bit - Example:
assembly MOV C, P1.0 ANL C, P1.1 ; Carry = P1.0 AND P1.1
- Syntax:
- ORL C, bit: Performs a logical OR between the Carry Flag and the specified bit; the result is stored in the Carry Flag.
- Syntax:
ORL C, bit - Example:
assembly MOV C, P1.0 ORL C, P1.1 ; Carry = P1.0 OR P1.1
- Syntax: