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.

Where can I put the 3.5 seconds in 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;
}
}
}

To implement a 3.5 second delay using Timer1, you can modify the code as follows:

First, include the necessary header files:

```c
#include <xc.h>
#include <pic16f877a.h>
```

Declare a global variable to keep track of the number of Timer1 overflows:

```c
unsigned int timer1Overflows = 0;
```

Configure Timer1 for the desired 3.5 second interval:

```c
T1CONbits.TMR1ON = 0; // Turn off Timer1

TMR1H = 0xFF; // Initialize Timer1 register
TMR1L = 0xFF;

T1CONbits.T1CKPS0 = 1; // Set Timer1 prescaler to 1:8
T1CONbits.T1CKPS1 = 1;

T1CONbits.TMR1CS = 0; // Set Timer1 clock source to internal oscillator
T1CONbits.TMR1ON = 1; // Turn on Timer1
```

Create an interrupt service routine that will be called whenever Timer1 overflows:

```c
void __interrupt() timer1ISR(void) {
if (PIR1bits.TMR1IF) {
PIR1bits.TMR1IF = 0; // Clear Timer1 overflow flag
timer1Overflows++;
}
}
```

Modify the delay function to use the timer instead of counting loops:

```c
void delayFunc(void) {
while (timer1Overflows < 35) {
// Do nothing
}
timer1Overflows = 0; // Reset Timer1 overflow count
}
```

In the main function, enable Timer1 interrupts:

```c
INTCONbits.PEIE = 1; // Enable peripheral interrupts
PIE1bits.TMR1IE = 1; // Enable Timer1 overflow interrupt
INTCONbits.GIE = 1; // Enable global interrupts
```

Now, the code will toggle the RUN LED (D10) every 3.5 seconds using Timer1.

Note: Make sure to enable Timer1 interrupts in the configuration bits as well.

To make the RUN LED (D10) toggle at every 3.5 seconds interval using Timer1, you can modify the delayFunc() function and use Timer1 interrupt to achieve the desired timing. Here's an updated version of the code:

```c
#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

unsigned int count1 = 200;
unsigned int count2 = 200;
int toggleFlag = 0;

void delayFunc(void);

void __interrupt(high_priority) isr(void) {
if (TMR1IF) {
TMR1IF = 0; // Clear Timer1 interrupt flag
TMR1H = 0x80; // Reload Timer1 count to achieve 3.5 seconds interval
TMR1L = 0x00;
toggleFlag = !toggleFlag; // Toggle the flag
}
}

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

// Timer1 Setup
T1CON = 0b10110001; // Prescaler = 1:8, Timer1 oscillator = Fosc/4
TMR1IF = 0; // Clear Timer1 interrupt flag
TMR1IE = 1; // Enable Timer1 interrupt
PEIE = 1; // Enable peripheral interrupt
GIE = 1; // Enable global interrupt
TMR1H = 0x80; // Initialize Timer1 count to achieve 3.5 seconds interval
TMR1L = 0x00;

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

// Knight Rider pattern from Left to Right direction
for (int 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 (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 delayFunc(void) {
int j, k;
int a;

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

By reloading the Timer1 count register in the interrupt service routine, you can achieve an exact 3.5 seconds interval for toggling the RUN LED (D10).

To implement the design on Explorer 16 and make the RUN LED toggle at every 3.5 seconds, you can use Timer1 module of the microcontroller by configuring its registers.

First, you need to find the appropriate values to set in the Timer1 registers to achieve a 3.5 seconds interval. The formula to calculate the value for Timer1 registers is:

Timer1Value = (Desired_Delay / (4 * Prescaler * TMR1_Scalar)) - 1

Where:
- Desired_Delay is the desired delay in seconds (in this case, 3.5 seconds).
- Prescaler is the prescaler value (1, 2, 4, 8, 16, 32, 64, or 256) which determines the rate at which the timer increments.
- TMR1_Scalar is the timer scalar value (1, 8, 64, or 256) which determines the number of oscillator cycles per timer increment.

Let's assume you want to use a prescaler of 8 (since it is a common value) and a timer scalar of 256. Substitute these values into the formula:

Timer1Value = (3.5 / (4 * 8 * 256)) - 1
Timer1Value = 271

Now you can update the code to set the Timer1 registers appropriately:

```c
#include <xc.h>

#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

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

void configureTimer1(void);

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

configureTimer1(); // Configure Timer1

while(1) {
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
if (TMR1 >= 271) {
TMR1 = 0; // Reset Timer1
LATDbits.LATD0 = !LATDbits.LATD0; // Toggle RUN LED (D10)
}

// Rest of the code...
}

return -1;
}

void configureTimer1(void) {
T1CON = 0x30; // Set Timer1 to use FCY as clock source, prescaler 1:8
TMR1 = 0; // Reset Timer1
PR1 = 271; // Set Timer1 period value
T1CONbits.TON = 1; // Enable Timer1
}
```

In the updated code, the function `configureTimer1()` is added to set up Timer1 with the appropriate prescaler and period values. Within the `main()` function, we check if Timer1 has reached its period value (271) to toggle the RUN LED (D10). You may need to adjust the values of `count1` and `count2` in the `delayFunc()` function to achieve the desired delay between LED pattern transitions.

Note: The code assumes you are using a PIC microcontroller with Timer1. Make sure to include the necessary header files and adjust the register names if you are using a different microcontroller.