Solutions for Microcontroller and Embedded Systems Test
Instructions:
1. For MCQs, the correct answer is provided.
2. For programming tasks, the assembly code solution is provided with explanations.
3. Ensure you understand the logic behind each solution.
Section 1: Multiple Choice Questions - Solutions
What is the primary purpose of a microcontroller?
Answer: b) Control external devices and systems
Which microcontroller family does the ATmega32 belong to?
Answer: c) AVR
Which addressing mode is used to load an immediate value directly into a register?
Answer: b) Immediate addressing
Which instruction performs a logical AND operation on two registers?
Answer: b) AND
What is the maximum size of EEPROM in ATmega16?
Answer: c) 1024 bytes
Which flag in the status register is set when the result of an operation is zero?
Answer: a) Z flag
Which of the following is a shift instruction in AVR assembly?
Answer: b) LSR
What is the primary purpose of the ADC in a microcontroller?
Answer: a) To convert analog signals to digital
Which register is used to control the baud rate in USART communication?
Answer: a) UBRR
What is the maximum number of I/O pins in ATmega32?
Answer: a) 32
Which of the following is a feature of RISC architecture?
Answer: b) Single-clock cycle instructions
What is the primary function of the interrupt system in a microcontroller?
Answer: b) Temporarily halt program execution
Which of the following protocols is used for serial communication?
Answer: d) All of the above
Which memory type is typically used for temporary data storage in microcontrollers?
Answer: c) SRAM
Which instruction is used to store a value into a register?
Answer: c) STS
What is the bit width of the registers in AVR microcontrollers?
Answer: a) 8 bits
What does the `NOP` instruction do in AVR assembly?
Answer: a) No operation
Which peripheral is controlled by the Timer/Counter module in AVR?
Answer: b) PWM
Which of the following instructions is used to rotate the contents of a register left through
the carry?
Answer: b) ROL
In a microcontroller, what does the ADC stand for?
Answer: b) Analog-to-Digital Converter
Section 2: Assembly Program Solutions
Task 1: Add Two 8-Bit Numbers
Solution:
; Add two 8-bit numbers and store the result in a register
LDI R20, 0x25 ; Load the first number into R20
LDI R21, 0x30 ; Load the second number into R21
ADD R22, R20 ; Add R20 to R22 (R22 = R22 + R20)
ADD R22, R21 ; Add R21 to R22 (R22 = R22 + R21)
Task 2: Subtract Two 8-Bit Numbers
Solution:
; Subtract two 8-bit numbers and store the result in a register
LDI R20, 0x60 ; Load the first number into R20
LDI R21, 0x30 ; Load the second number into R21
SUB R22, R20 ; Subtract R21 from R20 (R22 = R20 - R21)
Task 3: Rotate Left Using ROL Instruction
Solution:
; Rotate the contents of R20 left through the carry flag
LDI R20, 0x45 ; Load the value 0x45 into R20
ROL R20 ; Rotate the contents of R20 left through the carry
Task 4: Check If a Number Is Even or Odd Using AND
Solution:
; Check if a number is even or odd using AND
LDI R20, 0x56 ; Load the number into R20
ANDI R20, 0x01 ; AND R20 with 0x01
BRNE ODD ; If result is non-zero, the number is odd
; EVEN Case
; Code for even number
BR Exit
ODD:
; Code for odd number
Exit:
Task 5: Perform a Logical OR Between Two Registers
Solution:
; Perform a logical OR operation between R20 and R21
LDI R20, 0x55 ; Load value 0x55 into R20
LDI R21, 0xAA ; Load value 0xAA into R21
OR R20, R21 ; OR the contents of R21 with R20
Task 6: 16-Bit Addition
Solution:
; Perform a 16-bit addition and store the result in R20 and R21
LDI R20, 0x10 ; Load low byte of the first number
LDI R21, 0x20 ; Load high byte of the first number
LDI R22, 0x30 ; Load low byte of the second number
LDI R23, 0x40 ; Load high byte of the second number
ADD R20, R22 ; Add low bytes
ADC R21, R23 ; Add high bytes with carry
Task 7: Compare Two Registers and Set a Flag
Solution:
; Compare two registers and set the Zero flag if they are equal
LDI R20, 0x30 ; Load value into R20
LDI R21, 0x30 ; Load value into R21
CP R20, R21 ; Compare R20 with R21
BRNE NOT_EQUAL ; If registers are not equal, branch
; EQUAL case
; Code for equal case
BR Exit
NOT_EQUAL:
; Code for not equal case
Exit:
Task 8: Set a Particular Bit in a Register
Solution:
; Set the third bit in R20
LDI R20, 0x00 ; Load value 0x00 into R20
SBI R20, 2 ; Set bit 2 in R20 (binary 00000100)
Task 9: Use CMP Instruction to Compare Two Registers
Solution:
; Compare R20 and R21, branch if equal
LDI R20, 0x55 ; Load value 0x55 into R20
LDI R21, 0x55 ; Load value 0x55 into R21
CMP R20, R21 ; Compare R20 with R21
BREQ EQUAL ; Branch if equal
Task 10: Implement a Simple Delay Using Loop
Solution:
; Simple delay loop
LDI R20, 0xFF ; Load value into R20 for loop count
DELAY:
DEC R20 ; Decrement R20
BRNE DELAY ; Repeat until R20 is zero