In this project, you are going to add a RUN led to the embedded system that you have designed and developed in project 1. RUN Led is used to indicate continuous running of an electronic system, in general.

Hardware:
Implement the design on Explorer 16 as Project 1. LED D10 will be used as the RUN LED indicator.
Application:
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.

To implement the desired functionality, you will need to configure the Timer 1 module of the microcontroller to toggle the RUN LED (D10) at a 3.5-second interval. Additionally, you will need to implement the Knight Rider pattern on LEDs D9-D3, with the direction of the pattern determined by the state of switch S3. Finally, you need to ensure that the entire process repeats infinitely.

Here is an example implementation in C:

1. Timer Configuration for Toggling RUN LED (D10):
- Configure Timer 1 to generate an interrupt every 3.5 seconds.
- In the Timer 1 interrupt handler, toggle the state of the RUN LED (D10).

2. Knight Rider Pattern:
- Initialize a variable to track the current position of the pattern, initially set to 0.
- In a loop:
- If switch S3 is open, increment the position variable; otherwise, decrement it.
- Update the state of LEDs D9-D3 based on the current position.
- Delay for a short period to control the speed of the pattern.

3. Infinite Loop:
- Combine the timer toggling and Knight Rider pattern code into an infinite loop.
- Call the appropriate functions to toggle the RUN LED and implement the Knight Rider pattern.

It is important to note that the specific implementation may vary depending on the microcontroller and development environment being used.

Here is an example code in C that fulfills the requirements of the project:

```c
#include <xc.h>

// Configurations
#pragma config FNOSC = PRIPLL // Oscillator Selection
#pragma config POSCMOD = HS // Primary Oscillator Mode
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider
#pragma config FPLLMUL = MUL_20 // PLL Multiplier
#pragma config FPLLODIV = DIV_1 // PLL Output Divider
#pragma config FPBDIV = DIV_8 // Peripheral Bus Clock Divider
#pragma config FWDTEN = OFF // Watchdog Timer
#pragma config JTAGEN = OFF // JTAG Enable

// Function declarations
void initTimer();
void initIO();
void toggleLED();

// Global variables
unsigned int direction = 0; // 0: Left to Right, 1: Right to Left

int main() {
initTimer();
initIO();

while (1) {
// Toggle the RUN LED
toggleLED();

// Toggle the Knight Rider pattern
if (direction == 0) {
LATD <<= 1; // Shift LEDs from Left to Right
if (LATD == 0x08) {
direction = 1; // Switch direction to Right to Left
}
} else {
LATD >>= 1; // Shift LEDs from Right to Left
if (LATD == 0x01) {
direction = 0; // Switch direction to Left to Right
}
}

// Delay for 3.5 seconds
for (unsigned int i = 0; i < 50000; i++) {
for (unsigned int j = 0; j < 7000; j++);
}
}

return 0;
}

void initTimer() {
T1CON = 0x8000; // Enable Timer1, 1:1 prescaler
TMR1 = 0x0000; // Clear Timer1 register
PR1 = 45875; // Set period for 3.5 seconds
IEC0 |= 0x0002; // Enable Timer1 interrupt
IPC1 |= 0x1000; // Set Timer1 interrupt priority to 4
}

void initIO() {
TRISD = 0xFF00; // RD7-RD0 as input, RD15-RD8 as output
ODCD = 0x0000; // Normal output
LATD = 0x0000; // Clear output
TRISF &= 0xFFFE; // RF0 as output (RUN LED)
LATFbits.LATF0 = 1; // Set RUN LED initially
}

void toggleLED() {
LATFbits.LATF0 = !LATFbits.LATF0; // Toggle RUN LED
}

// Timer1 interrupt handler
void __attribute__((interrupt, auto_psv)) _T1Interrupt(void) {
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
}
```

Please note that this code assumes you are using the Explorer 16 development board with a compatible microcontroller. You may need to modify the code according to your specific hardware and microcontroller. Also, make sure to configure the oscillator settings and include the necessary header files before using this code.

To add a RUN LED to your embedded system, follow these steps:

Hardware:
1. Connect the RUN LED (LED D10) to the Explorer 16 board.
2. Ensure that the LED is connected to the appropriate GPIO pin on the microcontroller.

Application (in C):
1. Include the necessary header files for your microcontroller and timers.

```c
#include <xc.h> // Include the header file for your microcontroller
```

2. Configure the necessary registers and settings for Timer 1.

```c
void configureTimer1()
{
T1CONbits.TON = 0; // Disable Timer1 for configuration

// Configure Timer1 settings
T1CONbits.TCKPS = 0b11; // Set prescaler to 1:256
TMR1 = 0; // Clear Timer1 register
PR1 = 27343; // Set period for a 3.5 second interval (assuming a 16MHz clock frequency)

// Enable Timer1 interrupt
IEC0bits.T1IE = 1; // Enable Timer1 interrupt

T1CONbits.TON = 1; // Enable Timer1
}
```

3. Implement the Timer 1 interrupt handler function to toggle the RUN LED.

```c
void __attribute__((interrupt, auto_psv)) _T1Interrupt(void)
{
LATBbits.LATB9 ^= 1; // Toggle the RUN LED (D10)

// Clear Timer1 interrupt flag
IFS0bits.T1IF = 0;
}
```

4. Configure the necessary registers and settings for the Knight Rider pattern LED sequence.

```c
void configureKnightRider()
{
// Configure GPIO pins for LED sequence (D9-D3)
TRISBbits.TRISB9 = 0; // Set as output for RUN LED (D10)
TRISBbits.TRISB8 = 0; // Set as output for LED D9
TRISBbits.TRISB7 = 0; // Set as output for LED D8
// ... configure other LED pins (D7-D3) as necessary
}
```

5. Implement the Knight Rider pattern logic based on the state of switch S3.

```c
void knightRiderPattern()
{
static int direction = 1; // 1 = left to right, -1 = right to left
static int ledIndex = 2; // Start with LED D8 (assuming D10 is used for RUN LED)

LATBbits.LATB9 = 0; // Turn off RUN LED

if (PORTBbits.RB5 == 0) // Check the state of Switch S3
{
ledIndex += direction;

if (ledIndex > 8) // Reached the end of the sequence
{
direction = -1; // Change direction to right to left
ledIndex = 7;
}
else if (ledIndex < 2) // Reached the start of the sequence
{
direction = 1; // Change direction to left to right
ledIndex = 2;
}

// Update the LED sequence based on ledIndex
LATBbits.LATB8 = (ledIndex == 8) ? 1 : 0; // LED D9
LATBbits.LATB7 = (ledIndex == 7) ? 1 : 0; // LED D8
// ... update other LED pins (D7-D3) as necessary
}
}
```

6. Implement the main function to initialize the necessary components and continuously repeat the process.

```c
int main(void)
{
configureTimer1();
configureKnightRider();

while (1)
{
knightRiderPattern(); // Execute Knight Rider pattern logic
// Other tasks or code can be added here if necessary
}

return 0;
}
```

7. Build and program your microcontroller to run the application.

This implementation will make the RUN LED (D10) toggle at a precise 3.5-second interval using Timer 1. Additionally, it will execute the Knight Rider pattern on LEDs D9-D3 based on the state of switch S3, continuously repeating the process.