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 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.

Make a very simple code.

#include <p18f4520.h>

#pragma config WDT = OFF // Disable Watchdog timer
#pragma config LVP = OFF // Disable Low Voltage Programming

#define LEFT_DIRECTION 0 // Switch open
#define RIGHT_DIRECTION 1 // Switch closed

void initTimer1(void);
void initPorts(void);
void toggleRunLED(void);
void updateKnightRiderPattern(void);
void delay(void);

volatile unsigned char direction = LEFT_DIRECTION;

#pragma interruptlow isr
void lowISR(void)
{
if (PIR1bits.TMR1IF) // If Timer1 interrupt flag is set
{
toggleRunLED(); // Toggle the RUN LED
updateKnightRiderPattern(); // Update the Knight Rider pattern

TMR1H = 0xC0; // Set Timer1 to count for 1.45 seconds
TMR1L = 0x00;

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

void main(void)
{
initPorts(); // Initialize ports
initTimer1(); // Initialize Timer1

RCONbits.IPEN = 1; // Enable interrupt priority levels
INTCONbits.GIEL = 1; // Enable low priority interrupts

while (1)
{
// Infinite loop
}
}

void initPorts(void)
{
TRISDbits.TRISD0 = 0; // Set RD0 as output (RUN LED)
TRISDbits.TRISD1 = 0; // Set RD1 as output (D9)
TRISDbits.TRISD2 = 0; // Set RD2 as output (D8)
TRISDbits.TRISD3 = 0; // Set RD3 as output (D7)
TRISDbits.TRISD4 = 0; // Set RD4 as output (D6)
TRISDbits.TRISD5 = 0; // Set RD5 as output (D5)
TRISDbits.TRISD6 = 0; // Set RD6 as output (D4)
TRISDbits.TRISD7 = 0; // Set RD7 as output (D3)

TRISCbits.TRISC1 = 1; // Set RC1 as input (S3)

PORTD = 0x00; // Clear all outputs
PORTC = 0x00; // Clear all inputs
}

void initTimer1(void)
{
T1CONbits.TMR1CS = 0; // Internal clock source (Fosc/4)
T1CONbits.T1CKPS1 = 1; // Prescale set to 1:4
T1CONbits.T1CKPS0 = 1;
T1CONbits.T1SYNC = 0; // Do not synchronize external clock input
T1CONbits.TMR1ON = 1; // Enable Timer1
PIE1bits.TMR1IE = 1; // Enable Timer1 interrupt
}

void toggleRunLED(void)
{
LATDbits.LATD0 = ~LATDbits.LATD0; // Toggle RD0 (RUN LED)
}

void updateKnightRiderPattern(void)
{
PORTDbits.RD1 = direction == LEFT_DIRECTION ? 1 : 0; // D9
PORTDbits.RD2 = direction == LEFT_DIRECTION ? 1 : 0; // D8
PORTDbits.RD3 = direction == LEFT_DIRECTION ? 1 : 0; // D7
PORTDbits.RD4 = 1; // D6 (always on)
PORTDbits.RD5 = direction == RIGHT_DIRECTION ? 1 : 0; // D5
PORTDbits.RD6 = direction == RIGHT_DIRECTION ? 1 : 0; // D4
PORTDbits.RD7 = direction == RIGHT_DIRECTION ? 1 : 0; // D3

if (direction == LEFT_DIRECTION)
{
if (PORTDbits.RD7) // Check if D3 is on
{
direction = RIGHT_DIRECTION; // Change direction to Right
}
else
{
PORTDbits.RD7 = 1; // Turn on next LED
PORTDbits.RD6 = 0; // Turn off previous LED
}
}
else
{
if (PORTDbits.RD1) // Check if D9 is on
{
direction = LEFT_DIRECTION; // Change direction to Left
}
else
{
PORTDbits.RD1 = 1; // Turn on next LED
PORTDbits.RD2 = 0; // Turn off previous LED
}
}
}

void delay(void)
{
unsigned int i;
for (i = 0; i < 10000; i++); // Delay loop
}

Here is a simple code in C that implements the mentioned design on Explorer 16:

```c
#include <xc.h>

// PIC24FJ128GA010 Configuration Bit Settings
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog timer enabled/disabled by user software)
#pragma config FNOSC = PRIPLL // Oscillator Selection (Primary Oscillator with PLL module (HSPLL, ECPLL))
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Enable if using fixed crystal/oscillator)
#pragma config IESO = OFF // Internal/External Switch Over (Internal/External Switchover mode enabled)
#pragma config POSCMOD = HS // Primary Oscillator Configuration bits (HS oscillator mode selected)
#pragma config OSCIOFNC = OFF // CLKO Enable Configuration bit (CLKO output signal is active on the CLKOUT pin)
#pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock switching enabled, Fail-Safe Clock Monitor disabled)
#pragma config WDTPS = PS1 // Watchdog Timer Postscaler (1:1)
#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer in Non-Window mode)
#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window size is 25%)

#define _XTAL_FREQ 8000000 // External Crystal Frequency

// Function to initialize Timer1 module
void InitTimer1()
{
T1CONbits.TON = 0; // Turn off Timer1
T1CONbits.TCS = 0; // Select internal clock source for Timer1
T1CONbits.TGATE = 0; // Gated time accumulation is disabled
T1CONbits.TCKPS = 0b11; // 1:256 prescale value
TMR1 = 0x00; // Clear Timer1 register
PR1 = 28821; // Timer1 interrupt period = 1.45 seconds (with 1:256 prescaler)
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
}

// Function to initialize the GPIO pins
void InitGPIO()
{
TRISBbits.TRISB2 = 0; // Configure LED D10 (RUN LED) as output
LATBbits.LATB2 = 0; // Clear the pin initially

TRISBbits.TRISB10 = 0; // Configure LEDs D9-D3 as output
LATBbits.LATB10 = 0; // Clear the pins initially

TRISBbits.TRISB15 = 1; // Configure switch S3 as input

// Enable weak pull-up on switch S3
CNPU2bits.CN30PUE = 1;
}

// Function to toggle the RUN LED (D10)
void ToggleRUNLED()
{
LATBbits.LATB2 = !LATBbits.LATB2; // Toggle the pin state
}

// Function to toggle the knight rider pattern
void ToggleKnightRiderPattern()
{
if (LATBbits.LATB10 == 0b00000111) // If pattern is at rightmost position
LATBbits.LATB10 = 0b10000000; // Set pattern at leftmost position
else if (LATBbits.LATB10 == 0b10000000) // If pattern is at leftmost position
LATBbits.LATB10 = 0b00000111; // Set pattern at rightmost position
else // If pattern is in between positions
LATBbits.LATB10 = LATBbits.LATB10 >> 1; // Shift the pattern position one step

// If switch S3 is closed, reverse the pattern direction
if (PORTBbits.RB15 == 0)
LATBbits.LATB10 = LATBbits.LATB10 << 1; // Shift the pattern position one step to the opposite direction
}

// Timer1 interrupt handler
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void)
{
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
ToggleRUNLED(); // Toggle the RUN LED
ToggleKnightRiderPattern(); // Toggle the knight rider pattern
}

// Main function
int main()
{
InitGPIO(); // Initialize GPIO pins
InitTimer1(); // Initialize Timer1 module

T1CONbits.TON = 1; // Turn on Timer1

while(1)
{
// Infinite loop to repeat the process
}

return 0;
}
```

This code initializes the necessary modules and pins, sets up the Timer1 interrupt, and toggles the RUN LED (D10) and the knight rider pattern based on the specified timings and switch state.

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

1. Set up the necessary hardware connections:
- Connect LED D10 to the appropriate GPIO pin for the RUN LED indicator.
- Connect LEDs D9-D3 to the appropriate GPIO pins for the Knight Rider pattern.
- Connect switch S3 to a GPIO pin that you will use to determine the direction of the pattern (Left to Right or Right to Left).

2. Configure Timer 1 module for the desired interval:
- Set Timer 1 to operate in interrupt mode with a period of 1.45 seconds.
- Enable the Timer 1 interrupt.

3. Write the application code in C, which should include the following logic:

- Declare global variables to keep track of the pattern direction and LED states:

```c
#include <xc.h>

// Pattern direction (0: Left to Right, 1: Right to Left)
volatile unsigned char patternDirection = 0;

// LED states
volatile unsigned char ledStates = 0x1C; // D9-D3 initial states
volatile unsigned char runLedState = 0; // RUN LED initial state
```

- Implement the Timer 1 interrupt service routine to toggle the RUN LED:

```c
void __interrupt(high_priority) Timer1Interrupt(void)
{
TMR1 = 0; // Reset the Timer 1 value

// Toggle the RUN LED state
runLedState ^= 1;
LATAbits.LATA0 = runLedState;
return;
}
```

- Implement the main function to control the Knight Rider pattern and repeat the process:

```c
int main(void)
{
// Initialize the GPIO pins and Timer 1
// ...

// Enable global interrupts
INTCONbits.GIEH = 1;

while (1)
{
// Toggle the Knight Rider pattern LEDs based on the direction
if (patternDirection == 0)
{
// Left to Right
ledStates <<= 1; // Shift the LED states to the left
if (ledStates == 0)
{
ledStates = 0x02; // Reset to the initial state if all LEDs are turned off
}
}
else
{
// Right to Left
ledStates >>= 1; // Shift the LED states to the right
if (ledStates == 0)
{
ledStates = 0x80; // Reset to the initial state if all LEDs are turned off
}
}

// Update the Knight Rider pattern LEDs
LATB = (LATB & 0x01FF) | (ledStates << 9);

// Delay for 1.45 seconds
__delay_ms(1450);
}

return 0;
}
```

4. Finally, compile and program the code onto the microcontroller using a suitable compiler and programming toolchain.

This code should make the RUN LED (D10) toggle at a precise interval of 1.45 seconds using Timer 1 interrupt. The Knight Rider pattern LEDs (D9-D3) will alternate based on the direction set by switch S3, and the whole process will repeat infinitely.