4. Switches and LEDs are connected as shown in Figure 2 (SW1 is pressed). Assume that GPIOA

pins have been configured as outputs and GPIOB pins have been configured as inputs. Determine
the status of LED1 (ON or OFF) after the following code fragment:
GPIOA->ODR = 0x0000;
if((GPIOB->IDR & MASK1) == 0)
{
GPIOA->BSRR = DAT1;
}

MASK1 is 0x0810
DAT1 is 0x00000220

LED1 would be ON or OFF?

In this code fragment, the value of GPIOA->ODR is set to 0x0000, meaning all output pins on GPIOA are set to low (OFF).

Then, the code checks if the bitwise AND of GPIOB->IDR and MASK1 is equal to 0. Since MASK1 is 0x0810, the binary representation is 1000001000010000.

If GPIOB->IDR AND MASK1 results in 0, it means that the values of GPIOB->IDR and MASK1 do not have any common set bits, indicating that SW1 is not pressed.

Therefore, the condition in the if statement will evaluate to false, and the code block inside the if statement (GPIOA->BSRR = DAT1) will not be executed. Hence, the LED1 status will remain unchanged, meaning it will be OFF.

5. Switches and LEDs are connected as shown in Figure 2 (SW1 is pressed). Assume that GPIOA

pins have been configured as outputs and GPIOB pins have been configured as inputs. Determine
the status of the LEDs (ON or OFF) after the following code fragment:
GPIOA->BSRR = 0xffff0000;
if((GPIOB->IDR & MASK1) == 0)
{
GPIOA->BSRR = (1 << DAT2);
}
else
{
GPIOA->BSRR = (1 << DAT3);
}

DAT2 is 0x0b
DAT3 is 0x05

LED0 would be ON or OFF?
LED1 would be ON or OFF?

In this code fragment, GPIOA->BSRR is set to 0xffff0000, which means all output pins on GPIOA are set to high (ON).

Then, the code checks if the bitwise AND of GPIOB->IDR and MASK1 is equal to 0. Since MASK1 is 0x0810, the binary representation is 1000001000010000.

If GPIOB->IDR AND MASK1 results in 0, it means that the values of GPIOB->IDR and MASK1 do not have any common set bits, indicating that SW1 is not pressed.

Therefore, the condition in the if statement will evaluate to false, and the code block inside the else statement (GPIOA->BSRR = (1 << DAT3)) will be executed.

DAT3 is 0x05, the binary representation of which is 00000101. Shifting 1 left by 0x05 (5) positions gives us 0b00000001, which sets the corresponding pin on GPIOA to high (ON).

So, after executing this code fragment:
- LED0 will be ON.
- LED1 will be OFF.