Implement the design on Explorer 16. LED D10 will be used as the RUN LED indicator.

Write an application (in C) which does the following:
1. Make the RUN LED(D10) toggle at every 3.5 Seconds (exact) interval using one of the Timer (Timer 1) module of the Microcontroller.
2. The Knight Rider pattern now consists of 7 LEDs (D9-D3). If the switch (S3) is open, the pattern is from Left to Right direction. If the switch (S3) is closed, the pattern is from Right to Left direction.
3. Repeat the whole process in an infinite way.

Do the above using the code below

unsigned int count1 = 200;
unsigned int count2 = 200;

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
int i;
int j;

// Right to Left sequence
for(i=0; i<8; i++) {
LATA |= (1 << i);
delayFunc();
}

// Left to Right sequence
for(j=7; j>=-1; j--) {
LATA |= (1<<j);
delayFunc();
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD6 == 0) {
LATA = 0x00;
for(j=8; j>=-1; j--) {
LATA |= (1<<j);
delayFunc();
}
}

}

return -1;
}

void delayFunc(void)
{
int j,k;
int a;

for(j = 0; j < count1; j++)
{
for(k=0; k < count2; k++)
{
a = 0;
}
}

}

To implement the design on Explorer 16 and achieve the desired functionality, you can use the following code:

```c
#include <xc.h>

#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

unsigned int count1 = 200; // Timing delay variables
unsigned int count2 = 200;

void main(void) {
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

while(1) {
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
__delay_ms(3500);

// Knight Rider pattern from Left to Right direction
for(int i = 3; i <= 9; i++) {
LATA = (1 << i);
__delay_ms(100);
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(int j = 9; j >= 3; j--) {
LATA = (1 << j);
__delay_ms(100);
}
} else {
for(int j = 3; j <= 9; j++) {
LATA = (1 << j);
__delay_ms(100);
}
}
}

return -1;
}
```

Note: This code assumes that the LED D10 is connected to RA0 and the Knight Rider LEDs (D3-D9) are connected to PORTA. You may need to modify the pin mappings based on your specific hardware configuration.

To implement the design on Explorer 16 with the given requirements, you can update the code below:

```c
#include <p24Fxxxx.h>

unsigned int count1 = 200;
unsigned int count2 = 200;

// Function prototypes
void initTimer1(void);
void delayFunc(void);

int main(void) {
TRISA = 0xFE; // Set RA0 as output for RUN LED (D10)
TRISD = 0x40; // Set RD6 as input for switch (S3)
LATA = 0x00; // Set initial output for LED pattern (D9-D3)

initTimer1(); // Initialize Timer1

while(1) {
int i;
int j;

// Toggle RUN LED (D10) every 3.5 seconds
LATA ^= 0x01;
delayFunc();

// Right to Left sequence
for(i=0; i<8; i++) {
LATA |= (1 << (i+1)); // Enable current LED in the pattern
delayFunc();
LATA &= ~(1 << (i+1)); // Disable current LED in the pattern
}

// Left to Right sequence
for(j=6; j>=0; j--) {
LATA |= (1 << (j+1)); // Enable current LED in the pattern
delayFunc();
LATA &= ~(1 << (j+1)); // Disable current LED in the pattern
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD6 == 0) {
LATA = 0x00; // Turn off all LEDs
for(j=1; j<=7; j++) {
LATA |= (1 << j); // Enable current LED in the pattern
delayFunc();
LATA &= ~(1 << j); // Disable current LED in the pattern
}
}

}

return -1;
}

void initTimer1(void) {
T1CONbits.TON = 0; // Disable Timer1 while initializing
T1CONbits.TCKPS = 0b11; // Set Timer1 prescaler to 1:256
TMR1 = 0x0000; // Clear Timer1 register
PR1 = 0xC34F; // Set Timer1 period to achieve 3.5 seconds
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
T1CONbits.TON = 1; // Enable Timer1
}

void delayFunc(void) {
int j, k;
int a;

for(j = 0; j < count1; j++) {
for(k = 0; k < count2; k++) {
a = 0;
}
}
}
```

This code first initializes the necessary registers for the LEDs and the switch. Then, it sets up Timer1 to achieve a 3.5-second interval for toggling the RUN LED (D10).

The main loop consists of the Knight Rider light pattern (right to left and left to right) for the LEDs (D9-D3). If the switch (S3) is pressed, it performs the pattern in the opposite direction. The code uses bit manipulation to enable and disable the LEDs in the pattern.

Make sure to update the values of `count1` and `count2` according to the desired delay timing for the `delayFunc` function.

Note: This code assumes that the Fosc (oscillator frequency) of the microcontroller is properly configured to match the timing requirements.

To implement the given design on Explorer 16, you need to follow these steps:

1. Set up the necessary configurations and variables:
- Include the necessary header files for the microcontroller you are using.
- Define the required variables for the delay and LED patterns.

2. Configure the I/O ports:
- Set the TRISA and LATA registers accordingly to configure the LED pins as output.

3. Create an infinite loop (while loop) to repeat the whole process indefinitely:
- Inside the loop, implement the following functionalities:

3.1. Toggle the RUN LED (D10) at every 3.5-second intervals using Timer1:
- Implement a delay function (delayFunc) that creates a delay of 3.5 seconds using nested loops. Adjust the values of count1 and count2 variables in the delayFunc to achieve the desired delay interval.
- Trigger the delayFunc and toggle the LED (D10) on and off by setting or clearing the appropriate bit in the LATA register.

3.2. Implement the Knight Rider pattern on LEDs D9-D3:
- In the Right to Left sequence, iterate from 0 to 7 and set the corresponding bit in the LATA register to turn on the LEDs.
- Call the delayFunc to create a delay.
- In the Left to Right sequence, iterate from 7 to -1 (in reverse order) and set the corresponding bit in the LATA register to turn on the LEDs.
- Call the delayFunc to create a delay.

3.3. Implement the Left to Right sequence if S3 switch is pressed:
- Check if the S3 switch (connected to RD6 pin) is pressed (logic low).
- If S3 switch is pressed, iterate from 8 to -1 (in reverse order) and set the corresponding bit in the LATA register to turn on the LEDs.
- Call the delayFunc to create a delay.

4. Return -1 to indicate that the main function has completed.