Explorer 16 board will be used. 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:

TRISD = 0xAA; means that the lower 8 bits of PORTD (RD0-RD7) are set as inputs (high impedance), while the upper 8 bits are set as outputs.
PORTD = 0xAA; sets all the bits of PORTD to the value 0xAA, which is 10101010 in binary.

Therefore, the configuration of PORTD is as follows:
- RD0 and RD1 are inputs
- RD2 and RD3 are outputs
- RD4 and RD5 are inputs
- RD6 and RD7 are outputs

The status of the LEDs connected to PORTD can be determined as follows:
- RD0 is an input, so it does not control an LED.
- RD2 is an output and set to 1, so the LED connected to RD2 is ON.
- RD6 is an output and set to 1, so the LED connected to RD6 is ON.

b) To determine the current flowing through each LED, we can use Ohm's law:
VLED = IR

Given:
R = 220 ohms
VLED = 1.8 Volts

For each LED connected to RD2 and RD6:
I = V/R
I = 1.8V / 220 ohms
I ≈ 0.00818 Amperes or 8.18 mA

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

c) If R7 = 0 ohms, it means there is no resistor connected to the line controlling RD7. In this case, the LED connected to RD7 should not be used (or a current-limiting resistor must be added externally). Without a resistor, the current flowing through the LED can exceed its maximum rating, potentially damaging it.

d) To connect a switch to pin 30 and an LED to pin 19, the following circuit can be used:

________________
| |
| | |
| I/O Pin ----- R (220 ohms) ------ LED ----- GND
| 30 |

The configuration code for the switch connected to pin 30 would be:
TRISFbits.TRISF2 = 1; // Set pin 30 (RF2) as an input

The configuration code for the LED connected to pin 19 would be:
TRISEbits.TRISE6 = 0; // Set pin 19 (RE6) as an output
PORTEbits.RE6 = 0; // Set pin 19 (RE6) to turn off the LED initially

a. Based on the given configuration codes:

TRISD = 0xAA; // sets PORTD pins RD1, RD3, RD5, and RD7 as inputs, and RD0, RD2, RD4, and RD6 as outputs
PORTD = 0xAA; // sets the RD1, RD3, RD5, and RD7 outputs to high, and RD0, RD2, RD4, and RD6 outputs to low
PORTDbits.RD0 = 1; // sets RD0 output to high
PORTDbits.RD6 = 1; // sets RD6 output to high

Considering the status of the LEDs connected to PORTD, we can summarize as follows:

- RD0: LED is ON
- RD1: LED is OFF (input pin)
- RD2: LED is OFF (output pin, set to low)
- RD3: LED is OFF (input pin)
- RD4: LED is OFF (output pin, set to low)
- RD5: LED is OFF (input pin)
- RD6: LED is ON
- RD7: LED is OFF (input pin)

b. To determine the current flowing through each LED, we can use Ohm's Law. Given that R = 220 ohm and VLED = 1.8 Volts, the current (I) can be calculated using the formula:

I = V/R

For each LED connected to an output pin (RD0 and RD6) that is set to high (ON), the current flowing through them is:

I = (VLED - Voutput_pin) / R

Since Voutput_pin = 0 Volts (for a high output), the current flowing through these LEDs is:

I = VLED / R = 1.8 / 220 = 0.008182 A or 8.182 mA

For the LEDs connected to input pins (RD1, RD3, RD5, and RD7), no current flows through them because they are not connected to a voltage source.

c. If R7 is 0 ohms (shorted), then the current flowing through that line would be determined by Ohm's Law using the formula:

I = V/R

Since R7 = 0 ohms, the resistance in the circuit becomes effectively zero. Therefore, according to Ohm's Law, an infinite amount of current would flow through the line, which is not ideal and could potentially damage the circuit.

d. To connect a switch to pin 30 and an LED to pin 19 in the given diagram, the circuit can be set up as follows:

Circuit:
- Connect one terminal of the switch to pin 30 (RD6).
- Connect the other terminal of the switch to the positive end (anode) of the LED.
- Connect the negative end (cathode) of the LED to pin 19 (RD0).
- Connect a resistor (e.g., 220 ohm) in series with the LED, between the negative end of the LED and the ground (GND).

Configuration code:
- Set TRISDbits.TRISD6 = 1; // configure pin 30 (RD6) as an input
- Set TRISDbits.TRISD0 = 0; // configure pin 19 (RD0) as an output

Note: The exact code may vary depending on the specific microcontroller and development environment being used.

To answer the question, let's go step by step.

a. To determine how PORTD has been configured, we need to understand the meanings of the configuration codes:

- TRISD = 0xAA means that the TRISD register has been set to 0xAA. In binary, 0xAA is equal to 10101010. This configuration means that PORTD pins RD0, RD2, RD4, and RD6 are configured as outputs (since the corresponding bits in TRISD are set to 1), while pins RD1, RD3, RD5, and RD7 are configured as inputs (since the corresponding bits in TRISD are set to 0).

- PORTD = 0xAA means that the PORTD register has been set to 0xAA. In binary, 0xAA is equal to 10101010. This means that the output pins RD0, RD2, RD4, and RD6 are set to logic level 1 (HIGH) while pins RD1, RD3, RD5, and RD7 retain their previous states (since they are configured as inputs).

- PORTDbits.RD0 = 1 means that we are explicitly setting RD0 to logic level 1 (HIGH).

- PORTDbits.RD6 = 1 means that we are explicitly setting RD6 to logic level 1 (HIGH).

Based on these configuration codes, the status (ON/OFF) of the LEDs connected to PORTD can be determined as follows:
- RD0 is explicitly set to 1, so the LED connected to RD0 is ON.
- RD1 is configured as an input, so the state of the LED connected to RD1 is undetermined.
- RD2 is configured as an output and set to 1 in PORTD, so the LED connected to RD2 is ON.
- RD3 is configured as an input, so the state of the LED connected to RD3 is undetermined.
- RD4 is configured as an output and set to 1 in PORTD, so the LED connected to RD4 is ON.
- RD5 is configured as an input, so the state of the LED connected to RD5 is undetermined.
- RD6 is explicitly set to 1, so the LED connected to RD6 is ON.
- RD7 is configured as an input, so the state of the LED connected to RD7 is undetermined.

b. To determine the current flowing through each LED, we need to use Ohm's Law. Assuming all LEDs have the same characteristics, and R = 220 ohms and VLED = 1.8 Volts, we can use the formula: current (I) = VLED / R.

Since the configuration codes provided above indicate that certain LEDs are ON (logic level 1), we can calculate the current through each of those LEDs.

For the LEDs that are ON (RD0, RD2, RD4, and RD6):
- Current (I) = VLED / R = 1.8 V / 220 ohms = 0.00818 A or 8.18 mA (rounded to two decimal places).

c. If R7 = 0 ohms, it means that pin RD7 is directly connected to ground (0 volts). In this case, no current will flow through that line because there is no voltage difference across it.

d. To connect a switch to pin 30 and an LED to pin 19, we need to create the circuit accordingly:

```
+5V --------------------|
|
R
|
Switch -----------------|
|
LED
|
GND -------------------|
```

In this circuit, pin 30 is connected to one terminal of the switch, and the other terminal of the switch is connected to one end of the resistor (R). The other end of the resistor is connected to the positive terminal of the LED. The negative terminal of the LED is connected to GND (ground).

For the configuration code, you would need to set the respective TRIS and PORT registers to configure pins 30 and 19. Assuming pin 30 corresponds to RD7 and pin 19 corresponds to RD2, the code would be:

```
TRISD = 0b10000000; // Set RD7 as input and all other pins as output
PORTDbits.RD7 = 1; // Enable internal pull-up resistor for RD7

TRISD &= ~(1 << 2); // Set RD2 as output (clear the corresponding bit in TRISD)
```

Note: The specific code may vary depending on the microcontroller or development environment being used. Please refer to the appropriate documentation for the exact code syntax.