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

Knight rider code:
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;
}
}

}

Using the knight rider code above, do the following:
Write an simple application (in C) which does the following:
1. Make the RUN LED(D10) toggle at every 1.45 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.
4. To toggle the RUN LED, precisely at 1.45 Seconds interval, you need to use the interrupt mechanism for the Timer1.

#include <xc.h>

// Define the delay for toggling the RUN LED
#define RUN_LED_DELAY 1450

// Define the LED pins
#define LED_MASK 0xFE
#define LED_REGISTER TRISD
#define LED_PORT PORTD

// Define the switch pin
#define SWITCH_PIN RD6

// Initialize Timer1 for interrupt every 1 ms
void timer1_init() {
T1CONbits.TMR1CS = 0; // Select internal clock source
T1CONbits.T1CKPS = 0b11; // Set prescaler to 1:8
TMR1 = 0x0000; // Clear Timer1 register
PR1 = 18431; // Set period for 1 ms interrupt
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
IPC0bits.T1IP = 7; // Set Timer1 interrupt priority
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
T1CONbits.TMR1ON = 1; // Turn on Timer1
}

// Toggle the RUN LED
void toggle_run_led() {
LATAbits.LATA0 = !LATAbits.LATA0;
}

// Knight rider pattern
void knight_rider() {
// Right to Left sequence
for(int i = 0; i < 7; i++) {
LED_PORT = (LED_PORT & LED_MASK) | (1 << (i + 3));
__delay_ms(100);
}

// Left to Right sequence
for(int j = 6; j >= 0; j--) {
LED_PORT = (LED_PORT & LED_MASK) | (1 << (j + 3));
__delay_ms(100);
}

// Left to Right sequence if switch is pressed
if(SWITCH_PIN == 0) {
LED_PORT = (LED_PORT & LED_MASK) | (1 << 3);
__delay_ms(100);

for(int j = 0; j < 7; j++) {
LED_PORT = (LED_PORT & LED_MASK) | (1 << (j + 3));
__delay_ms(100);
}
}
}

// Timer1 interrupt service routine
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
toggle_run_led();
knight_rider();
}

int main(void) {
// Configure LED pins as outputs
LED_REGISTER = 0;

// Configure switch pin as input
TRISDbits.TRISD6 = 1;

// Configure RUN LED
TRISAbits.TRISA0 = 0;

// Initialize Timer1
timer1_init();

// Enable global interrupts
INTCONbits.GIE = 1;

while(1) {
// Infinite loop
}

return 0;
}

To implement the desired functionality, follow these steps:

1. Configure the required registers and settings for Timer 1:
- Set the Timer 1 control register (T1CON) to enable Timer 1 and configure its mode.
- Set the Timer 1 interrupt enable bit in the interrupt control register (INTCONbits.GIE).
- Set the Timer 1 interrupt priority bits in the interrupt control register (INTCONbits.PEIE and IPR1bits.TMR1IP).

2. Create an interrupt service routine (ISR) for Timer 1:
- In the ISR, toggle the RUN LED (D10) using the appropriate register (e.g., LATBbits.LATB6 ^= 1).
- Clear the Timer 1 interrupt flag (PIR1bits.TMR1IF).

3. Inside the main function, configure the required registers and settings for the Knight Rider pattern:
- Set the TRIS and LATA registers to configure the LED pins.
- Read the state of switch S3 using the appropriate register (e.g., PORTDbits.RD6).

4. Implement the Knight Rider pattern:
- Use a for loop to iterate over the LED sequence in the desired direction (left to right or right to left).
- Inside the loop, set the corresponding LED bit high using the appropriate register (e.g., LATAbits.LATA3 = 1).
- Call the delayFunc function to introduce a delay between transitions.

5. Implement the switch condition to determine the direction of the Knight Rider pattern:
- If the switch S3 is open (PORTDbits.RD6 == 0), set the LED sequence direction to left to right.
- If the switch S3 is closed (PORTDbits.RD6 == 1), set the LED sequence direction to right to left.

Here's the modified code:

```c
#include <xc.h>

unsigned int count1 = 200;
unsigned int count2 = 200;
volatile unsigned int timer1OverflowCount = 0;
volatile int ledDirection = 1; // 1 for left to right, -1 for right to left

void main(void) {
// Configure Timer 1
T1CONbits.TMR1ON = 0; // Disable Timer 1 before configuring
T1CONbits.T1OSCEN = 0; // Turn off Timer 1 oscillator
T1CONbits.RD16 = 1; // Enable 16-bit read/write mode
T1CONbits.T1CKPS = 0b11; // Prescaler 1:8
TMR1H = 0xFD; // Set Timer 1 initial value (adjust if needed)
TMR1L = 0xFF;
PIE1bits.TMR1IE = 1; // Enable Timer 1 interrupt
INTCONbits.PEIE = 1; // Enable peripheral interrupts
INTCONbits.GIE = 1; // Enable global interrupts

// Configure LED pins
TRISA = 0x00;
LATA = 0x00;

while (1) {
int i, j;

// Knight Rider pattern
if (ledDirection == 1) {
// Left to Right sequence
for (i = 3; i <= 9; i++) {
LATAbits.LATAi = 1; // Set LED pin high
delayFunc();
}
} else {
// Right to Left sequence
for (j = 9; j >= 3; j--) {
LATAbits.LATAj = 1; // Set LED pin high
delayFunc();
}
}
}

return -1;
}

void __interrupt() isr(void) {
if (PIR1bits.TMR1IF) {
PIR1bits.TMR1IF = 0; // Clear Timer 1 interrupt flag
timer1OverflowCount++;
if (timer1OverflowCount >= 230) { // Adjust for precise 1.45 seconds interval
timer1OverflowCount = 0;
LATBbits.LATB6 ^= 1; // Toggle RUN LED (D10)
}
}
}

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

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

Please note that the above code assumes that the RUN LED (D10) is connected to pin RB6. You may need to adjust the pin and register configurations according to your specific hardware setup.

To implement the given design on Explorer 16 as Project 1, follow these steps:

1. Set up the necessary hardware:
- Connect the LEDs D3 to D9 to the appropriate pins on the microcontroller (refer to the datasheet for pin mappings).
- Connect the switch S3 to the RD6 pin on the microcontroller.

2. Set up Timer1 as an interrupt timer:
- Configure Timer1 with the desired prescaler and period to achieve a 1.45-second interval.
- Enable Timer1 interrupt.

3. Implement the main function:
- Set up the TRISA and LATA registers to configure the LED pins as output and initialize them to 0.
- Enter an infinite loop using `while(1)`.

4. Inside the loop:
a. Toggle the RUN LED (D10) using an if statement and the value of Timer1 flag.
b. Implement the Knight Rider pattern based on the state of the switch S3 using nested for loops.
c. If the switch (S3) is closed (RD6 is low), repeat the Left to Right sequence for the LEDs.

5. Implement the interrupt service routine (ISR) for Timer1:
- Clear the Timer1 interrupt flag.
- Toggle the RUN LED by changing its state.
- Reset Timer1 to achieve a precise 1.45-second interval.

Here's an example implementation in C:

```c
#include <xc.h>

volatile unsigned int count1 = 200;
volatile unsigned int count2 = 200;
volatile unsigned char runLEDState = 0;

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

// Set up Timer1 as an interrupt timer
T1CONbits.TCKPS = 0b11; // Set prescaler to 1:256
PR1 = 56940; // Set period to achieve 1.45s interval
IEC0bits.T1IE = 1; // Enable Timer1 interrupt

while (1) {
// Implement the Knight Rider pattern
int i, j;

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

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

return -1;
}

void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
// Clear Timer1 interrupt flag
IFS0bits.T1IF = 0;

// Toggle the RUN LED
runLEDState ^= 1;
LATDbits.LATD2 = runLEDState;

// Reset Timer1
TMR1 = 0;
}

void delayFunc(void) {
// Use the provided delay function
int j, k, a;

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

Note:
- Modify the pin assignments, prescaler, period, and other configuration settings to match your specific microcontroller and hardware setup.
- Ensure that the necessary device configuration bits (FOSC, FWDT, etc.) are set correctly for your project.

This implementation sets up Timer1 as an interrupt timer to toggle the RUN LED precisely at a 1.45-second interval. The Knight Rider pattern is implemented using nested for loops, and the direction is based on the state of the switch S3 (RD6). The interrupt service routine (ISR) for Timer1 is used to toggle the RUN LED and reset Timer1 for the next interval.