1. Blink an LED Connected to P1.
Program:
START: SETB P1.0 ; Set P1.0 (turn on the LED)
ACALL DELAY ; Call the delay subroutine
CLR P1.0 ; Clear P1.0 (turn off the LED)
ACALL DELAY ; Call the delay subroutine
SJMP START ; Repeat the process indefinitely
DELAY: MOV R7, #0FFH ; Load R7 with a delay value
DELAY_LOOP: DJNZ R7, DELAY_LOOP ; Decrement R7 until it becomes 0
RET ; Return from the subroutine
Explanation:
SETB P1.0: Turns on the LED connected to P1.0.
ACALL DELAY: Calls the delay subroutine.
CLR P1.0: Turns off the LED connected to P1.0.
SJMP START: Creates an infinite loop to keep blinking the LED.
DELAY subroutine: Introduces a simple delay using a loop.
Output:
The LED connected to P1.0 will blink on and off continuously.
2. Toggle P2.3 Continuously
Program:
MAIN: CPL P2.3 ; Complement P2.3 (toggle the bit)
ACALL DELAY ; Call the delay subroutine
SJMP MAIN ; Repeat the process indefinitely
DELAY: MOV R7, #0FFH ; Load R7 with a delay value
DELAY_LOOP: DJNZ R7, DELAY_LOOP ; Decrement R7 until it becomes 0
RET ; Return from the subroutine
Explanation:
CPL P2.3: Toggles the state of P2.3.
ACALL DELAY: Calls the delay subroutine to slow down the toggling.
SJMP MAIN: Creates an infinite loop to keep toggling P2.3.
Output:
The pin P2.3 will toggle between high and low states continuously.
3. Send a Byte to Port 1
Program:
MOV P1, #0xA5 ; Move the value 0xA5 to Port 1
Explanation:
MOV P1, #0xA5: Sends the byte 0xA5 (10100101 in binary) to all pins of Port 1.
Output:
The value 0xA5 will be output on Port 1.
4. Increment a Value in Internal RAM
Program:
MOV 20H, #0x10 ; Move the initial value 0x10 to RAM location 20H
INC 20H ; Increment the value at RAM location 20H
Explanation:
MOV 20H, #0x10: Initializes the RAM location 20H with the value 0x10.
INC 20H: Increments the value stored in RAM location 20H.
Output:
The RAM location 20H will contain the value 0x11.
5. Add Two Numbers and Store the Result
Program:
MOV A, #0x25 ; Load the first number into Accumulator
ADD A, #0x17 ; Add the second number to Accumulator
MOV 30H, A ; Store the result in RAM location 30H
Explanation:
MOV A, #0x25: Loads the number 0x25 into the Accumulator.
ADD A, #0x17: Adds 0x17 to the Accumulator.
MOV 30H, A: Stores the result (0x3C) in RAM location 30H.
Output:
The RAM location 30H will contain the value 0x3C.
6. Copy Data from Port 0 to Port 1
Program:
MOV A, P0 ; Copy the value from Port 0 to the Accumulator
MOV P1, A ; Transfer the value from Accumulator to Port 1
Explanation:
MOV A, P0: Reads the data from Port 0 into the Accumulator.
MOV P1, A: Writes the data from the Accumulator to Port 1.
Output:
The value on Port 0 is copied to Port 1.
7. Check if P3.2 is Set and Set P1.0
Program:
JB P3.2, SET_LED ; Jump to SET_LED if P3.2 is set (1)
SJMP END ; Skip setting the LED if P3.2 is not set
SET_LED: SETB P1.0 ; Set P1.0 (turn on the LED)
END: SJMP END ; Infinite loop to end the program
Explanation:
JB P3.2, SET_LED: Checks if P3.2 is set (1) and jumps to SET_LED if true.
SETB P1.0: Turns on the LED connected to P1.0 if P3.2 is set.
Output:
If P3.2 is high, the LED connected to P1.0 will turn on.
8. Send a Series of Numbers to Port 2
Program:
MOV R0, #0x00 ; Initialize the counter in R0
LOOP: MOV P2, R0 ; Send the counter value to Port 2
INC R0 ; Increment the counter
ACALL DELAY; Call the delay subroutine
SJMP LOOP ; Repeat the process indefinitely
DELAY: MOV R7, #0FFH ; Simple delay
DELAY_LOOP: DJNZ R7, DELAY_LOOP
RET
Explanation:
MOV R0, #0x00: Initializes the counter with 0.
MOV P2, R0: Sends the counter value to Port 2.
INC R0: Increments the counter.
SJMP LOOP: Creates an infinite loop to keep sending values.
Output:
Port 2 will output a sequence of increasing numbers, 0x00, 0x01, 0x02, etc.
9. Generate a Square Wave on P1.7
Program:
MAIN: CPL P1.7 ; Toggle P1.7
ACALL DELAY ; Delay to control frequency
SJMP MAIN ; Repeat the process
DELAY: MOV R7, #0FFH ; Simple delay
DELAY_LOOP: DJNZ R7, DELAY_LOOP
RET
Explanation:
CPL P1.7: Toggles P1.7, creating a square wave.
ACALL DELAY: Delays to control the frequency of the wave.
SJMP MAIN: Creates an infinite loop for continuous wave generation.
Output:
P1.7 will produce a square wave signal.
10. Move Block of Data in Internal RAM
Program:
MOV R0, #20H ; Source address
MOV R1, #30H ; Destination address
MOV R2, #0AH ; Number of bytes to move
COPY: MOV A, @R0 ; Load the value from source
MOV @R1, A ; Store the value to destination
INC R0 ; Increment source address
INC R1 ; Increment destination address
DJNZ R2, COPY ; Repeat for the number of bytes
Explanation:
MOV R0, #20H: Points to the source address.
MOV R1, #30H: Points to the destination address.
MOV R2, #0AH: Sets the counter to 10 bytes.
MOV A, @R0: Loads the value from the source address.
MOV @R1, A: Stores the value to the destination address.
DJNZ R2, COPY: Decrements the counter and repeats the process.
Output:
10 bytes of data are moved from address 20H to 30H in internal RAM.