3) Write an ALP to read the 8 switches connected through Port 2 of 8051
and output the state of the switches through Port 1. Demonstrate your
output in Edsim
Algorithm:
1. Begin.
2. Initialize Port 1 as output for LED display.
3. Continuously:
a. Read the state of Port 2 (where the switches are connected) and store it
in a variable. b. Output the state of the switches (stored in the variable)
through Port 1. c. Add a short delay to avoid rapid flickering.
c. Repeat step 3 indefinitely.
4. End.
Code:
MOV P1, #0FFH ; Initialize Port 1 as output
LOOP:
MOV A, P2 ; Move the contents of Port 2 to Accumulator
MOV P1, A ; Output the contents of Accumulator to Port 1
ACALL DELAY ; Call delay subroutine
SJMP LOOP ; Jump back to LOOP
DELAY:
MOV R1, #20H ; Load R1 with 20H (can be adjusted for desired delay)
DELAY_LOOP:
DJNZ R1, DELAY_LOOP ; Decrement R1 and jump if not zero
RET ; Return from subroutine
Output:
2) Write an ALP to generate three different PWM signals with 25%, 50%
and 75% duty cycle. Present the output waveforms in the Edsim
platform.
Algorithm:
1. Begin.
2. Initialize the required parameters: a. Set the frequency of the PWM
signals. b. Calculate the on-time (high state duration) for each PWM
signal based on the desired duty cycle: - For 25% duty cycle: on-
time = 25% of the PWM period - For 50% duty cycle: on-time = 50%
of the PWM period - For 75% duty cycle: on-time = 75% of the PWM
period
3. Configure the microcontroller's timer/counter module to generate
PWM signals: a. Set the timer/counter mode to PWM mode. b. Set
the PWM period based on the desired frequency.
4. Set the PWM duty cycle for each signal: a. For the first PWM signal
(25% duty cycle): - Set the duty cycle to the calculated on-time. b.
For the second PWM signal (50% duty cycle): - Set the duty cycle to
the calculated on-time. c. For the third PWM signal (75% duty cycle):
- Set the duty cycle to the calculated on-time.
5. Enable the PWM output pins.
6. Continuously repeat the following: a. Update the duty cycle for each
PWM signal if required (for dynamic PWM generation).
7. End.
Code:
MOV P0, #0C3H ; Set P0 to 11000011 for 25% duty cycle
ACALL DELAY ; Call the delay subroutine
MOV P0, #0F9H ; Set P0 to 11111001 for 50% duty cycle
ACALL DELAY ; Call the delay subroutine
MOV P0, #0FFH ; Set P0 to 11111111 for 75% duty cycle
ACALL DELAY ; Call the delay subroutine
SJMP $ ; Endless loop
DELAY:
MOV R1, #0FFH ; Initialize R1 with FFH
AGAIN:
DJNZ R1, AGAIN ; Decrement R1 and jump if not zero
RET ; Return from subroutine
1) Write an ALP to realize the following logic expression using 8051
RESULT = (AL xor BL xor CL) + (Num1 * Num2 * Num3).
Demonstrate your output in Edsim
Algorithm:
1. Begin.
2. Initialize the required variables: a. Define variables AL, BL, and CL to
represent the input values for the XOR operation. b. Define variables
Num1, Num2, and Num3 to represent the input numbers for
multiplication. c. Define a variable RESULT to store the final result.
3. Perform the XOR operation: a. Load the value of AL into the
Accumulator. b. Exclusive OR (XOR) the Accumulator with the value
of BL. c. Exclusive OR (XOR) the Accumulator with the value of CL. d.
Store the result in RESULT.
4. Perform multiplication: a. Multiply Num1 by Num2. b. Multiply the
result by Num3. c. Add the result to the value of RESULT.
5. Store the final result in RESULT.
6. End.
Code:
MOV A, AL ; Load AL into Accumulator
XRL A, BL ; XOR BL with Accumulator
XRL A, CL ; XOR CL with Accumulator
MOV RESULT, A ; Store the result in RESULT
MOV A, Num1 ; Load Num1 into Accumulator
MUL AB, Num2 ; Multiply Accumulator with Num2
MOV B, Num3 ; Load Num3 into B
MUL AB, B ; Multiply Accumulator with B
ADD A, RESULT ; Add the result to RESULT
MOV RESULT, A ; Store the final result in RESULT