For any reference or data value unless otherwise mentioned, Explorer 16 board will be used as default. Show all your work for evaluation.

Question 1.
a. Consider the following configuration codes. Based on the codes below, how PORTD has been configured. Also determine the status (ON/OFF) of the LEDs connected to PORTD as the diagram? [4 Marks]

TRISD = 0xAA;
PORTD = 0xAA;
PORTDbits.RD0 = 1;
PORTDbits.RD6 = 1;

To determine how PORTD has been configured and the status of the LEDs connected to PORTD, we need to analyze the given configuration codes.

1. TRISD = 0xAA;
The TRISD register is used to configure the data direction of the pins on PORTD. Setting a bit to 1 makes the corresponding pin an input, while setting it to 0 makes it an output.

In binary, 0xAA is 10101010. So, by setting TRISD = 0xAA, we are configuring the pins on PORTD as follows:
- RD0: Input
- RD1: Output
- RD2: Input
- RD3: Output
- RD4: Input
- RD5: Output
- RD6: Input
- RD7: Output

2. PORTD = 0xAA;
The PORTD register is used to write data to the pins on PORTD.

In binary, 0xAA is 10101010. By setting PORTD = 0xAA, we are setting the pins on PORTD as follows:
- RD0: High (1) - This turns ON the connected LED.
- RD1: Low (0) - This turns OFF the connected LED.
- RD2: High (1) - This turns ON the connected LED.
- RD3: Low (0) - This turns OFF the connected LED.
- RD4: High (1) - This turns ON the connected LED.
- RD5: Low (0) - This turns OFF the connected LED.
- RD6: High (1) - This turns ON the connected LED.
- RD7: Low (0) - This turns OFF the connected LED.

3. PORTDbits.RD0 = 1;
This code sets the RD0 pin on PORTD to 1, which turns ON the connected LED.

4. PORTDbits.RD6 = 1;
This code sets the RD6 pin on PORTD to 1, which turns ON the connected LED.

So, based on the given configuration codes, the configuration of PORTD is as follows:
- RD0: Input
- RD1: Output
- RD2: Input
- RD3: Output
- RD4: Input
- RD5: Output
- RD6: Input
- RD7: Output

And the status of the LEDs connected to PORTD is as follows:
- RD0 LED: ON
- RD1 LED: OFF
- RD2 LED: ON
- RD3 LED: OFF
- RD4 LED: ON
- RD5 LED: OFF
- RD6 LED: ON
- RD7 LED: OFF.

To determine how PORTD has been configured based on the given codes, let's break down each line of code:

1. TRISD = 0xAA;
This line of code sets the data direction for each pin of PORTD. Since 0xAA is in binary 10101010, it means that all odd-numbered pins (RD1, RD3, RD5, RD7) are configured as inputs, while all even-numbered pins (RD0, RD2, RD4, RD6) are configured as outputs.

2. PORTD = 0xAA;
This line of code assigns the value 0xAA to PORTD. Since 0xAA is in binary 10101010, it means that all even-numbered pins (RD0, RD2, RD4, RD6) will be set to high logic level (1), while all odd-numbered pins (RD1, RD3, RD5, RD7) will be set to low logic level (0).

3. PORTDbits.RD0 = 1;
This line of code sets the RD0 pin specifically to high logic level (1).

4. PORTDbits.RD6 = 1;
This line of code sets the RD6 pin specifically to high logic level (1).

Based on the above configurations, the status (ON/OFF) of the LEDs connected to PORTD can be determined as follows:

- RD0 LED: ON (High logic level)
- RD1 LED: OFF (Low logic level)
- RD2 LED: ON (High logic level)
- RD3 LED: OFF (Low logic level)
- RD4 LED: ON (High logic level)
- RD5 LED: OFF (Low logic level)
- RD6 LED: ON (High logic level)
- RD7 LED: OFF (Low logic level)

Please note that the configuration assumes that the LEDs are active-low, meaning they turn on when the RDx pin is low (0) and turn off when the RDx pin is high (1).

To understand how PORTD has been configured and the status of the LEDs connected to it, let's break down the given code step by step.

1. TRISD = 0xAA;
The TRISD register is used to configure the data direction of each pin on PORTD. A bit value of 1 sets the corresponding pin as an input, and a value of 0 sets it as an output. The binary representation of 0xAA is 10101010 in binary.

Breaking it down by bit:
- RD0, RD2, RD4, and RD6 are set as inputs (1).
- RD1, RD3, RD5, and RD7 are set as outputs (0).

So, based on this code, RD0, RD2, RD4, and RD6 are configured as input pins, and RD1, RD3, RD5, and RD7 are configured as output pins.

2. PORTD = 0xAA;
The PORTD register stores the current values of the pins on PORTD. The binary representation of 0xAA is 10101010 in binary.

Breaking it down by bit:
- RD0, RD2, RD4, and RD6 are set as logic high (1).
- RD1, RD3, RD5, and RD7 are set as logic low (0).

So, based on this code, the logic voltage on RD0, RD2, RD4, and RD6 is high, while the logic voltage on RD1, RD3, RD5, and RD7 is low.

3. PORTDbits.RD0 = 1;
This code sets the output pin RD0 to logic high (1), overriding its previous configuration from the TRISD register.

4. PORTDbits.RD6 = 1;
This code sets the output pin RD6 to logic high (1), overriding its previous configuration from the TRISD register.

Based on the above explanations, we can conclude the following:

- The configuration of PORTD is such that RD0, RD2, RD4, and RD6 are input pins, and RD1, RD3, RD5, and RD7 are output pins.
- The status of the LEDs connected to PORTD is as follows:
- RD0: Input pin, so the status of the LED connected to RD0 cannot be determined.
- RD1: Output pin set to logic 0, so the LED connected to RD1 is OFF.
- RD2: Input pin, so the status of the LED connected to RD2 cannot be determined.
- RD3: Output pin set to logic 0, so the LED connected to RD3 is OFF.
- RD4: Input pin, so the status of the LED connected to RD4 cannot be determined.
- RD5: Output pin set to logic 0, so the LED connected to RD5 is OFF.
- RD6: Input pin, so the status of the LED connected to RD6 cannot be determined.
- RD7: Output pin set to logic 0, so the LED connected to RD7 is OFF.

Please note that the status of the LEDs connected to the input pins (RD0, RD2, RD4, and RD6) cannot be determined because their configuration is set as input.