MP MC PT-2 Ans
MP MC PT-2 Ans
2. An array of 20 numbers is stored in the internal data RAM starting from the location 30H.
Write a program to find the maximum and minimum numbers in the array.
✅ Program Explanation:
NEXT:
INC R0 ; Move to next array element
MOV A, @R0 ; Load array element into accumulator
1
CHECK_MAX:
JC SKIP_MAX ; If A < MAX, skip updating
MOV 40H, A ; Else, update MAX
SKIP_MAX:
END
Register/Memory Purpose
R0 Pointer to traverse array (30H onward)
A Accumulator to load array elements
B Temporary register for comparison
40H Memory location to store MAX
41H Memory location to store MIN
R2 Loop counter for 19 iterations
2
✅ Working:
✅ Result:
1. Write a program to generate a square wave of frequency 2 kHz on any one port
pins, using timer 0, Assuming that clock frequency of the 8051 system is 12 MHz.
1. Frequency Analysis:
• Square wave frequency f=2 kHzf = 2\,kHzf=2kHz → Period T=12000=500 μsT = \frac{1}{2000}
= 500\,\mu sT=20001=500μs
• Since it's a square wave, the high and low times are equal: 500 μs2=250 μs\frac{500\,\mu
s}{2} = 250\,\mu s2500μs=250μs each.
• Clock = 12 MHz
• Machine cycle = 12 MHz12=1 MHz\frac{12\,MHz}{12} = 1\,MHz1212MHz=1MHz
• So, 1 machine cycle = 1 µs
3
✅ Final Program in Assembly (8051):
MAIN:
MOV P1, #00H ; Make P1 initially 0 (Square wave output pin P1.0)
TOGGLE:
MOV TMOD, #01H ; Timer 0, Mode 1 (16-bit timer)
WAIT:
JNB TF0, WAIT ; Wait until Timer 0 overflows (TF0=1)
END
✅ Explanation:
✅ Output:
4
6. Interface 8 LEDs with 8051 port 0 and write assembly language programs to a) Glow all the
LEDs continuously. b)Flash all the LEDs on and off for 1 second each.
✅ Assumptions:
✅ Program:
MOV P0, #0FFH ; Send all 1's to Port 0 to turn ON all LEDs
END
✅ Explanation:
🌐 Part (b): Flash all the LEDs ON and OFF for 1 second each
✅ Program:
ORG 0000H ; Program start address
MAIN:
MOV P0, #0FFH ; Turn ON all LEDs
ACALL DELAY ; 1-second delay
5
DJNZ R1, LOOP2 ; Middle loop countdown
DJNZ R2, LOOP1 ; Outer loop countdown
RET ; Return from delay
END
✅ Explanation:
Instruction Description
MOV P0, #0FFH Turn ON all LEDs
ACALL DELAY Call delay subroutine (approx 1 second)
MOV P0, #00H Turn OFF all LEDs
ACALL DELAY Call delay subroutine again (1 second OFF)
SJMP MAIN Repeat the ON-OFF cycle forever
✅ Delay Subroutine:
6
Architectural Representation – Diagram Explanation:
🔹 Status & Control Unit – Monitors execution status, handling flags and interrupts.
🔹 32 × 8 General Purpose Registers – Small, high-speed storage locations used for quick
computations.
🔹 Arithmetic Logic Unit (ALU) – Performs arithmetic (addition, subtraction) and logical
(AND, OR, XOR) operations essential for data processing.
C. Memory Organization
🔹 Data SRAM (Static RAM) – Temporarily holds variable data during program execution.
🔹 EEPROM (Electrically Erasable Programmable ROM) – Retains non-volatile user
7
data, such as configurations.
🔹 I/O Lines – Connects input devices (e.g., sensors) and output devices (e.g., LEDs,
motors) for interaction.
🔹 Interrupt Unit – Handles external and internal events, pausing execution to process
higher-priority tasks.
🔹 SPI (Serial Peripheral Interface) Unit – Manages high-speed data exchange with
external devices like sensors.
🔹 Watchdog Timer – Resets the system if a malfunction occurs, preventing failures.
🛠 Technologies Integrated
9. Interface LCD with arduino microcontroller and develop code for the same.
1. Definition
8
2. Interfacing Diagram:
This code displays a simple message "Hello, World!" on a 16x2 LCD screen.
void setup() {
9
void loop() {
• The LCD is initialized using digital pins 7, 8, 9, 10, 11, and 12 for control and data
communication.
10
🔹 Step 5: Loop Execution
✅ Output:
• LCD will alternate between displaying "Hello, World!" and "LCD Interface!", then "Arduino
LCD" and "Example", each displayed for a few seconds.
✅ 1. Arithmetic Operations
int a = 10, b = 3;
void setup() {
sum = a + b;
11
diff = a - b;
prod = a * b;
quot = a / b;
rem = a % b;
Serial.println(sum); // Output: 13
Serial.println(diff); // Output: 7
Serial.println(prod); // Output: 30
Serial.println(quot); // Output: 3
Serial.println(rem); // Output: 1
✅ 2. Logical Operations
Logical operations are used for decision-making based on conditions. They return boolean
results: true (1) or false (0).
Logical AND && (a > 5) && (b < 10) true if both conditions are true
Logical OR ` `
12
✅ Example Code for Logical Operations:
int a = 10, b = 5;
void setup() {
if (!(a == b)) {
13
✅ Conclusion:
12. Develop 8051 program to move a block of data from external program
memory to external data memory
📌 Problem Statement:
• Move a block of data (e.g., 10 bytes) from external program memory (code
memory) starting at address 2000H to external data memory starting at address
4000H.
• This operation involves reading data from ROM and writing it to RAM externally.
MOV R1, #00H ; R1 used as offset for destination address (data memory)
END
✅ Explanation of Instructions:
Instruction Purpose
MOV R7, #0AH Load counter to move 10 bytes
MOV DPTR, #2000H Set DPTR to source program memory address
MOV DPTR, #4000H Set DPTR to destination data memory address
MOVC A, @A+DPTR Read data from program (code) memory
MOVX @DPTR, A Write data to external data memory
INC R0, INC R1 Increment offsets for next byte
DJNZ R7, LOOP Loop until all bytes are moved
✅ Final Result:
• Data block from 2000H to 2009H (program memory) is copied to 4000H to 4009H
(data memory).
• Useful for applications like bootloading, initial data setup, or updating runtime
variables from stored ROM constants.
15
13. With neat circuit diagram explain how 4x4 keyboards is interfaced with 8051
microcontroller.
📌 Introduction:
A 4x4 matrix keyboard contains 16 keys arranged in 4 rows and 4 columns. It is widely
used as an input device to give commands or data to the microcontroller. It reduces the
number of I/O pins required for interfacing.
⭐ Circuit Diagram:
16
✅ Explanation of Circuit Diagram:
1. Rows (R1 to R4) of the keypad are connected to P1.0 to P1.3 of Port 1 of 8051.
2. Columns (C1 to C4) of the keypad are connected to P1.4 to P1.7 of Port 1 of 8051.
3. Each key is located at the intersection of a row and a column.
4. Pull-up resistors are generally connected to Port 1 (if needed, internal pull-ups can
also be enabled).
✅ Working Principle:
1. Set all column lines as input (high) and set one row line low at a time.
2. Scan the column lines:
o If a key is pressed in the active row, corresponding column will show low.
3. Identify the pressed key by checking which row and column are connected.
4. Debounce the keypress (software delay).
5. Return/Process the key value.
MOV P1, #0FFH ; Set all pins of Port 1 as high (input mode)
SCAN_ROW:
MOV P1, #0FEH ; Set Row 1 low, others high
JB P1.4, NEXT1 ; Check Column 1
; If P1.4 is low, key at R1C1 is pressed
SJMP KEY_FOUND
NEXT1:
JB P1.5, NEXT2 ; Check Column 2
; If P1.5 is low, key at R1C2 is pressed
17
SJMP KEY_FOUND
NEXT2:
; Continue for other columns and rows
MOV P1, #0FDH ; Set Row 2 low, others high
; Repeat similar checks...
KEY_FOUND:
; Handle keypress
SJMP $
✅ Applications:
✅ Conclusion:
• The 4x4 matrix keypad interface with 8051 allows effective human-machine
interaction.
• Proper scanning and debounce handling ensure reliable input detection.
• Easy to implement with minimal hardware complexity.
18
19. Explain about the various addressing modes of aurdino microcontroller
• Definition: The operand is directly embedded within the instruction. The CPU does not fetch
it from memory.
• Usage: Used for initializing registers with constants.
• Example (Assembly):
• LDI R16, 0x25 ; Load immediate value 0x25 into register R16
• Definition: The instruction specifies the exact memory address where the operand is stored.
• Usage: Useful when accessing specific memory locations.
• Example (Assembly):
• LDS R18, 0x100 ; Load value from memory address 0x100 into R18
• Definition: A register holds the address of the operand, and the data is fetched from that
address.
• Usage: Commonly used for arrays and pointer-based operations.
• Example (Assembly):
• LD R19, X ; Load value from memory address stored in register X into
R19
19
• Example (Assembly):
• LD R21, Y+2 ; Load value from memory at (Y + 2) into R21
• Definition: The address is relative to the program counter (PC). Used for branching
instructions.
• Usage: Applied in loops and conditional jumps.
• Example (Assembly):
• RJMP NEXT ; Jump to label "NEXT" relative to current PC
Conclusion
These addressing modes help in efficient memory access, data handling, and instruction
execution in Arduino microcontrollers (ATmega328P). 🚀
Three Marks:
MOV B, R1
MUL AB ; A = A * B (Cube of 5)
MOV R2, A ; Store Cube result in R2
Explanation:
20
2. 8051 ALP to Generate Square Wave on Port 1 Using DAC
MOV P1, #00H ; Output LOW
ACALL DELAY
MOV P1, #FFH ; Output HIGH
ACALL DELAY
SJMP HERE ; Repeat loop
Explanation:
• Definition: The 8051 microcontroller can be interfaced with sensors to monitor and control
physical parameters.
• Example: Temperature sensor-based automatic fan control.
• Uses:
o Temperature monitoring
o Smart security systems
o Industrial automation
Explanation:
• The instruction SWAPF swaps the nibbles of the BCD value, and ANDLW 0x0F extracts the
lower nibble to get the HEX equivalent.
21
6. Program to Find Square and Cube of a Given Number (Same as Q1)
Explanation:
22
11. Calculate Time Duration for One State and Machine Cycle (8051, 6 MHz)
Explanation:
14. C18 Program to Set Bit RB0 and Send It to RC7 After Inverting
LATBbits.LATB0 = 1; // Set RB0 HIGH
LATCbits.LATC7 = ~LATBbits.LATB0; // Invert RB0 and send to RC7
Explanation:
23