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:

#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

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

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

while(1) {
int i;
int j;
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
delayFunc();

// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 7; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for(j = 0; j <= 7; 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;
}
}

}

The code provided implements the requested functionality using Timer1 for the LED toggling and a for loop for the Knight Rider pattern. Here's a breakdown of what each part of the code does:

1. The oscillator frequency is specified as 8MHz using the `_XTAL_FREQ` macro.

2. Two global variables `count1` and `count2` are declared and set to 200. These variables are used in the `delayFunc()` function for creating delays.

3. The main function starts by setting the TRISA register to make all bits of PORTA as output. It also sets TRISDbits.TRISD6 as input to read the state of SW3.

4. The main while(1) loop runs infinitely, executing the following steps repeatedly:

4.1. The RUN LED (D10) is toggled by flipping the value of LATDbits.LATD0, which represents the state of the D0 pin on LATD register. The `delayFunc()` is called to introduce a delay of 3.5 seconds between each toggle.

4.2. The Knight Rider pattern is created by sequentially lighting up the LEDs from D9 to D3. Each LED is turned on by shifting `1` to the left by `i` positions and then assigning it to the LATA register. The `delayFunc()` is called to introduce a delay between each LED lighting up.

4.3. Depending on the state of SW3 (read from PORTDbits.RD6), the Knight Rider pattern continues in either the Left to Right or Right to Left direction. In the Left to Right direction, a for loop is used to turn on the LEDs from D7 to D0. In the Right to Left direction, the for loop is used to turn on the LEDs from D0 to D7. The `delayFunc()` is called to introduce a delay between each LED lighting up.

5. The main function returns -1 to indicate that the program has completed execution.

6. The `delayFunc()` function is defined to introduce a delay based on the values of `count1` and `count2`. It uses nested for loops to create an artificial delay by executing a meaningless assignment inside. The actual delay time can be adjusted by changing the values of `count1` and `count2`.

Note: The implementation assumes that the necessary configuration settings for Timer1 module and interrupts have been made before entering the main function. Also, the necessary header files and device-specific definitions are assumed to be included in the code.

Use this code to make the RUN LED(D10) toggle at every 3.5 Seconds (exact) interval using one of the Timer (Timer 1) module of the Microcontroller.

#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

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

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

while(1) {
int i;
int j;
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
delayFunc();

// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 7; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for(j = 0; j <= 7; 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 make the RUN LED (D10) toggle at every 3.5 seconds using Timer1, you need to configure the Timer1 module and implement an interrupt handler for it. Here's an updated version of the code that includes the necessary Timer1 configuration and interrupt handler:

```c
#include <xc.h>

#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

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

void interrupt Timer1_ISR(void);

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

// Configure Timer1
T1CONbits.TMR1CS = 0; // Use internal clock (Fosc/4)
T1CONbits.T1CKPS = 0b11; // 1:8 Prescaler
TMR1H = 0xC1; // Set TMR1H and TMR1L to generate a 3.5 second delay
TMR1L = 0x82;
PIE1bits.TMR1IE = 1; // Enable Timer1 interrupt
INTCONbits.PEIE = 1; // Enable peripheral interrupts
INTCONbits.GIE = 1; // Enable global interrupts

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

return -1;
}

void interrupt Timer1_ISR(void) {
if (PIR1bits.TMR1IF == 1) {
// Toggle RUN LED (D10)
LATDbits.LATD0 = !LATDbits.LATD0;

// Clear Timer1 interrupt flag
PIR1bits.TMR1IF = 0;

// Reload Timer1 value for the next 3.5 second delay
TMR1H = 0xC1;
TMR1L = 0x82;
}
}

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

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

In this updated code, the Timer1 module is configured with a prescaler of 1:8 and a period value that generates a 3.5 second delay. The Timer1 interrupt is enabled, and an interrupt handler function `Timer1_ISR` is defined.

Inside the interrupt handler, the RUN LED (D10) is toggled and the Timer1 interrupt flag is cleared. The Timer1 value is reloaded with the appropriate period value for the next 3.5 second delay.

Note: Make sure to include the necessary header files for your specific microcontroller and update any register names or configurations as needed for your particular hardware setup.

To implement the given design on the Explorer 16 board, follow these steps:

1. Connect LED D10 to Port D0 on the board. This LED will be used as the RUN LED indicator.

2. Open the code editor or IDE of your choice and create a new C project.

3. Copy and paste the provided code into the main.c file of your project.

4. Set the oscillator frequency by defining the macro `_XTAL_FREQ`. In this case, the frequency is set to 8MHz.

5. Define two variables `count1` and `count2` of type `unsigned int` and initialize them to 200. These variables will be used in the delay function.

6. In the `main` function, set PORTA as output by assigning 0x00 to the TRISA register. This will configure all the pins on PORTA as output.

7. Set the TRISD6 bit to 1 to make SW3 an input.

8. Enter an infinite loop using `while(1)` to repeat the process continuously.

9. Toggle the RUN LED (D10) every 3.5 seconds using Timer1. This is done by toggling the value of LATDbits.LATD0 using `!LATDbits.LATD0`. Call the `delayFunc()` to introduce a delay of 3.5 seconds.

10. Implement the Knight Rider pattern from the left to right direction by iterating from i=0 to i<=6. Set the bits on PORTA to shift the LED pattern from left to right. Call `delayFunc()` to introduce a delay between each movement of the pattern.

11. Check the state of SW3 (S3). If it is open (PORTDbits.RD6 == 0), implement the Knight Rider pattern from right to left by iterating from j=7 to j>=0. Set the bits on PORTA to shift the LED pattern from right to left. Call `delayFunc()` to introduce a delay between each movement of the pattern.

12. If SW3 is closed, implement the Knight Rider pattern from left to right by iterating from j=0 to j<=7. Set the bits on PORTA to shift the LED pattern from left to right. Call `delayFunc()` to introduce a delay between each movement of the pattern.

13. Compile and upload the code to the microcontroller on the Explorer 16 board.

14. The code will now continuously run and toggle the RUN LED every 3.5 seconds and implement the Knight Rider pattern based on the state of SW3 (S3).

Note: The `delayFunc()` is a simple delay function that introduces a delay based on the values of `count1` and `count2`. Adjust the values of these variables to change the delay duration. Make sure to configure these values to achieve a delay of exactly 3.5 seconds as required.