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?

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

b. In the above diagram, determine the current flowing through each of the LED, if R = 220 ohm and VLED = 1.8 Volts?

c. If R7 = 0 ohm, what is the current that will flow through that line?

d. We want to connect a switch to pin 30 and a LED to pin 19 in the above diagram. Draw the circuit accordingly. Also write the configuration code.

a. Based on the given configuration codes, the PORTD has been configured as follows:

- TRISD = 0xAA; sets the lower 4 bits of PORTD as inputs (0) and the upper 4 bits as outputs (1).
- PORTD = 0xAA; sets the initial values for each bit of PORTD.

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

- PORTDbits.RD0 = 1; sets the RD0 bit as high, which turns ON the LED connected to RD0.
- PORTDbits.RD6 = 1; sets the RD6 bit as high, which turns ON the LED connected to RD6.
- The other bits of PORTD are set to their default values based on the configuration code.

b. To determine the current flowing through each LED, we need to use Ohm's Law:

VLED = IR

Given:
R = 220 ohm
VLED = 1.8 Volts

Using Ohm's Law:
I = VLED / R

For each LED connected to RD0 and RD6, the current can be calculated as follows:

For RD0: I_RD0 = VLED / R = 1.8 / 220 ≈ 0.00818 Amps (or 8.18 mA)

For RD6: I_RD6 = VLED / R = 1.8 / 220 ≈ 0.00818 Amps (or 8.18 mA)

c. If R7 = 0 ohm, the circuit becomes a short circuit, and the current flowing through that line will be determined by the internal resistance of the power supply and the wiring. The exact value of the current cannot be determined without additional information or measurements.

d. To connect a switch to pin 30 and an LED to pin 19, the circuit can be drawn as follows:

```
------------
| |
| |
| SW LED |
| |
------------

```

The configuration code to set pin 30 as input for the switch and pin 19 as output for the LED can be written as follows:

```c
TRISBbits.TRISB14 = 1; // Set pin 30 (RB14) as input
TRISEbits.TRISE8 = 0; // Set pin 19 (RE8) as output
```

Note: The exact pin numbers mentioned in the question may vary depending on the specific pin mapping of the Explorer 16 board.

a. Based on the configuration codes:

- TRISD = 0xAA; sets the direction of the PORTD pins. Since 0xAA in binary is 10101010, it means that pins RD1, RD3, RD5, and RD7 are set as inputs (TRISD = 1) and pins RD0, RD2, RD4, and RD6 are set as outputs (TRISD = 0).
- PORTD = 0xAA; sets the initial values of the PORTD pins. Since 0xAA in binary is 10101010, it means that pins RD0, RD2, RD4, RD6 are set to high logic level (1) and pins RD1, RD3, RD5, RD7 are set to low logic level (0).
- PORTDbits.RD0 = 1; sets the pin RD0 to high logic level (1).
- PORTDbits.RD6 = 1; sets the pin RD6 to high logic level (1).

Based on the configuration codes, PORTD is configured as follows:

Pin RD0: Output, High logic level (1) - LED ON
Pin RD1: Input
Pin RD2: Output, High logic level (1) - LED ON
Pin RD3: Input
Pin RD4: Output, High logic level (1) - LED ON
Pin RD5: Input
Pin RD6: Output, High logic level (1) - LED ON
Pin RD7: Input

b. To determine the current flowing through each LED, we can use Ohm's Law: V = I*R, where V is the voltage across the LED, I is the current, and R is the resistor value.

Assuming the voltage across the LED is 1.8V and the resistor value is 220 ohm, we can calculate the current (I):

I = V / R
I = 1.8V / 220 ohm
I ≈ 0.0082 A or 8.2 mA

Therefore, the current flowing through each LED is approximately 8.2 mA.

c. If R7 = 0 ohm, it means there is no resistor in series with pin RD7. This would result in a short circuit, and a large amount of current would flow through that line. The exact value of current would depend on the internal resistance and the power supply capabilities of the Explorer 16 board.

d. To connect a switch to pin 30 and a LED to pin 19 in the above diagram, you can follow these steps:

1. Connect one terminal of the switch to pin 30 (RD6) and the other terminal to ground (GND).
2. Connect one terminal of the LED to pin 19 (RD2) and the other terminal to a current-limiting resistor R (e.g., 220 ohm).
3. Connect the other end of the resistor R to the positive supply voltage (VDD).
4. Make sure to connect pin 19 (RD2) and pin 30 (RD6) to their respective connections on the Explorer 16 board.

Configuration code for the switch (pin 30):

TRISDbits.TRISD6 = 1; // Set RD6 as input

Configuration code for the LED (pin 19):

TRISDbits.TRISD2 = 0; // Set RD2 as output

Note: The actual configuration code may depend on the specific microcontroller or development board being used. The codes provided above assume the use of the Explorer 16 board.

a. To determine how PORTD has been configured based on the given codes, we need to understand how the TRISD and PORTD registers work on the Explorer 16 board.

The TRISD register is used to configure the direction of each individual pin in PORTD. A value of 1 configures the corresponding pin as an input, while a value of 0 configures it as an output.

The PORTD register stores the current state (logic level) of each individual pin in PORTD. When a pin is configured as an output (TRISD bit is 0), the value written to PORTD will determine the logic level of the corresponding pin.

Now let's analyze the given codes step by step:

TRISD = 0xAA;
This code sets the TRISD register to the hexadecimal value 0xAA, which in binary is 10101010. Each bit in 0xAA corresponds to a pin in PORTD. Since the binary value has alternating bits of 1 and 0, it means that some pins will be configured as inputs (TRISD = 1) and some as outputs (TRISD = 0).

PORTD = 0xAA;
This code sets the PORTD register to the same hexadecimal value 0xAA. Since some pins in PORTD are configured as inputs (TRISD = 1), writing a value to PORTD does not affect the logic level on those pins. Therefore, the logic levels of the pins configured as inputs remain unchanged.

PORTDbits.RD0 = 1;
This code sets the RD0 pin (pin 0 in PORTD) to a logic level of 1 (high) regardless of how it was configured previously. This pin is connected to an LED based on the diagram.

PORTDbits.RD6 = 1;
Similarly, this code sets the RD6 pin (pin 6 in PORTD) to a logic level of 1 (high) regardless of how it was configured previously. This pin is also connected to an LED based on the diagram.

Based on the above analysis, the configuration of PORTD is as follows:
- RD0 (pin 0) is configured as an output and set to logic level 1 (high).
- RD1 (pin 1) is configured as an input.
- RD2 (pin 2) is configured as an output.
- RD3 (pin 3) is configured as an input.
- RD4 (pin 4) is configured as an output.
- RD5 (pin 5) is configured as an input.
- RD6 (pin 6) is configured as an output and set to logic level 1 (high).
- RD7 (pin 7) is configured as an input.

The status (ON/OFF) of the LEDs connected to PORTD would depend on their connection. Based on the information provided in the diagram, the LEDs connected to RD0 (pin 0) and RD6 (pin 6) would be ON since these pins are set to logic level 1.

b. To determine the current flowing through each LED, we can use Ohm's Law, which states that current (I) is equal to the voltage (V) divided by the resistance (R). Given that VLED is 1.8 volts and R is 220 ohms, we can calculate the current flowing through each LED.

I = V / R
I = 1.8 / 220
I ≈ 0.00818 Amperes (or 8.18 milliamperes)

Therefore, the current flowing through each LED is approximately 8.18 milliamperes.

c. If R7 is 0 ohms, it means that there is no resistance in that line. According to Ohm's Law, if the resistance is 0 ohms, the current flowing through the circuit would be infinite. In practical terms, this could result in damage to the circuit or a short circuit condition.

d. To connect a switch to pin 30 and an LED to pin 19 in the above diagram, you would need to create the following circuit and write appropriate configuration codes:

Circuit:
- Connect one terminal of the switch to pin 30 (RD6) and the other terminal to ground (GND).
- Connect one terminal of the LED to pin 19 (RD0) and the other terminal to a current-limiting resistor (R) and then to a positive supply voltage (V+).

Configuration Code:
To configure pin 30 (RD6) as an input for the switch and pin 19 (RD0) as an output for the LED, you can use the following code:

TRISDbits.TRISD6 = 1; // Configure RD6 (pin 30) as an input
TRISDbits.TRISD0 = 0; // Configure RD0 (pin 19) as an output

This code sets the corresponding TRISD bits to 1 for pin 30 (RD6) and 0 for pin 19 (RD0), which configures them as input and output, respectively.