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 configuration codes:

- TRISD = 0xAA sets the pins RD0, RD2, RD4, RD6 as inputs and pins RD1, RD3, RD5, RD7 as outputs.
- PORTD = 0xAA sets the output values of pins RD1, RD3, RD5, RD7 to 1 and the output values of pins RD0, RD2, RD4, RD6 to 0.
- PORTDbits.RD0 = 1 sets the output value of pin RD0 to 1.
- PORTDbits.RD6 = 1 sets the output value of pin RD6 to 1.

Based on this configuration, the status of the LEDs connected to PORTD is as follows:
- RD0: ON
- RD1: OFF
- RD2: OFF
- RD3: OFF
- RD4: OFF
- RD5: OFF
- RD6: ON
- RD7: OFF

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 (1.8 Volts) and R is the resistance (220 ohms).

For the LEDs that are ON (RD0 and RD6), the current flowing through each LED can be calculated as follows:
I = V/R = 1.8/220 = 0.0081818 Amps or 8.1818 mA.

c. If R7 = 0 ohm, it means there is no resistor connected in series with the LED connected to RD7. This would result in a high current flow through that line, limited only by the output impedance of the microcontroller.

d. To connect a switch to pin 30 and a LED to pin 19 in the above diagram:

Switch:
- Connect one terminal of the switch to pin 30.
- Connect the other terminal of the switch to GND.

LED:
- Connect the positive terminal of the LED to pin 19.
- Connect the negative terminal of the LED to a current-limiting resistor (for example, 220 ohms).
- Connect the other end of the resistor to GND.

Configuration code:
- TRISDbits.TRISD30 = 1; // Configure pin 30 as input
- TRISDbits.TRISD19 = 0; // Configure pin 19 as output

a. The configuration codes provided indicate that PORTD has been configured as follows:

- TRISD = 0xAA: This sets the lower nibble (bits RD3-RD0) and bit RD6 of the TRISD register as inputs, and the upper nibble (bits RD7-RD4) as outputs.
- PORTD = 0xAA: This sets the lower nibble and bit RD6 of the PORTD register to logic high (1), and the upper nibble to logic low (0).
- PORTDbits.RD0 = 1: This sets bit RD0 of the PORTD register as logic high (1).
- PORTDbits.RD6 = 1: This sets bit RD6 of the PORTD register as logic high (1).

Based on the configuration codes and the provided diagram, the status of the LEDs connected to PORTD can be determined as follows:

The lower nibble of PORTD (bits RD3-RD0) is configured as inputs, so the LEDs connected to those pins will not be turned on/off directly by the PIC microcontroller. The upper nibble of PORTD (bits RD7-RD4) is configured as outputs, and based on the provided configuration codes, both bits RD6 and RD0 are set to logic high (1). Therefore, the LEDs connected to RD6 and RD0 will be turned on.

b. To determine the current flowing through each of the LEDs, we can use Ohm's law:

VLED = IR

Given:
VLED = 1.8 Volts
R = 220 ohms

Using Ohm's law, we can rearrange the formula to solve for current (I):

I = VLED / R

For each LED connected to the PIC microcontroller, the current flowing through it can be calculated using the above formula:

I = 1.8 / 220 = 0.0082 Amps (rounded to four decimal places or amps)

c. If R7 is 0 ohm, it effectively short circuits the line. In this case, the current that will flow through that line can be determined using Ohm's law:

I = V / R

Given:
V = 1.8 Volts
R = 0 ohms

Using Ohm's law, we can solve for current (I):

I = 1.8 / 0 = Undefined (division by zero)

Since dividing by zero is undefined mathematically, the current flowing through that line cannot be determined.

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

VCC
|
R
|
Switch (Pin 30)
|
---------
| |
| |
| LED |
| |
---------
|
GND

The configuration code for the switch (connected to pin 30) would involve configuring the corresponding TRIS register (TRISB in this case) to set the pin as an input, and reading the corresponding PORT register (PORTB) to monitor the switch state. Assuming the switch is connected to RB6, the configuration code would be:

TRISBbits.TRISB6 = 1; // Set pin RB6 as input

Similarly, the configuration code for the LED (connected to pin 19) would involve configuring the corresponding TRIS register (TRISE) to set the pin as an output, and writing the corresponding PORT register (PORTE) to control the LED state. Assuming the LED is connected to RE1, the configuration code would be:

TRISEbits.TRISE1 = 0; // Set pin RE1 as output

a. To determine how PORTD has been configured, we need to understand the documentation for the Explorer 16 board.

The TRIS register (TRISD) controls the direction (input or output) of the pins on PORTD. Each bit in TRISD corresponds to a pin on PORTD. A value of 1 for a specific bit means the corresponding pin is configured as an input, while a value of 0 means it is configured as an output.

Based on the configuration code TRISD = 0xAA, the binary representation of 0xAA is 10101010. This means that pins RD1, RD3, RD5, RD7 are configured as inputs (1 bit) while pins RD0, RD2, RD4, RD6 are configured as outputs (0 bit).

Next, the code PORTD = 0xAA sets the initial value of PORTD to 0xAA, which is the binary representation of 170 in decimal. In binary, it is 10101010, which means the corresponding pins RD1, RD3, RD5, and RD7 are set to logic high (1) while RD0, RD2, RD4, and RD6 are set to logic low (0).

Finally, the code PORTDbits.RD0 = 1 sets pin RD0 to logic high (1). Similarly, PORTDbits.RD6 = 1 sets pin RD6 to logic high (1).

Based on the above configurations, the LEDs connected to pins RD0 and RD6 would be turned ON.

b. To determine the current flowing through each LED, we need to use Ohm's Law, which states that current (I) equals voltage (V) divided by resistance (R).

Given VLED = 1.8 Volts and R = 220 ohms, we can calculate the current flowing through each LED using the formula:

Current (I) = Voltage (V) / Resistance (R)
= 1.8 Volts / 220 ohms
≈ 0.00818 Amperes (or 8.18 milliamperes)

Therefore, the current flowing through each LED connected to pins RD0 and RD6 would be approximately 8.18 milliamperes.

c. If R7 = 0 ohms, it means that there is no resistor connected in series with pin RD7. In this case, the current flowing through pin RD7 would only be limited by the internal resistance of the microcontroller. The datasheet of the microcontroller or the Explorer 16 board should provide the value of this internal resistance.

d. To connect a switch to pin 30 and a LED to pin 19, you will need the following components:

1. A momentary push-button switch.
2. A current-limiting resistor (RLED) for the LED.
3. An LED.

Here is the circuit diagram:

--------------- -------------
SW1 ---| |-----RD6 ----| |
| | | |
| Explorer | | Explorer |------ RD0 ------- LED
| 16 Board |-----RD30 ----| 16 Board |
| | | |
| | | |
GND ---| |--------------| |
--------------- -------------

The configuration code for the switch (SW1) connected to pin RD30 would be:

TRISDbits.TRISD6 = 0; // Configure RD6 as output
PORTDbits.RD6 = 0; // Set RD6 to logic low initially

The configuration code for the LED connected to pin RD0 would be:

TRISDbits.TRISD0 = 0; // Configure RD0 as output

Note: The above configuration codes assume that the necessary header files and initialization code for the Explorer 16 board have been included/setup in the project.