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 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.
Note:
1. To toggle the RUN LED, precisely at 1.45 Seconds interval, you need to use the interrupt mechanism for the Timer1.

To implement the requirements, you can follow the steps below:

1. Set up the necessary configurations for the Explorer 16 development board and initialize the required peripherals (GPIO, Timer1).
2. Define and configure the necessary variables and flags.
3. Implement the interrupt service routine (ISR) for Timer1 to toggle the RUN LED.
4. Write the main application loop to handle the Knight Rider pattern and button state.
5. Inside the loop, check the state of the switch (S3) to determine the direction of the pattern.
6. Implement the Knight Rider pattern using a loop and GPIO toggling for the LEDs.
7. Continuously repeat the pattern by wrapping the code in an infinite loop.

Here's an example code snippet in C to demonstrate the implementation:

```c
#include <xc.h>

// Constants
#define LED_RUN LATBbits.LATB10
#define SWITCH_S3 PORTDbits.RD6

// Function prototypes
void initialize(void);

// Global variables
unsigned char ledPattern = 0b0000010; // Initial pattern
unsigned char direction = 1; // Default direction (Left to Right)

// Timer1 interrupt service routine
void __attribute__((interrupt, auto_psv)) _T1Interrupt(void)
{
// Toggle the RUN LED
LED_RUN ^= 1;

// Clear the interrupt flag
IFS0bits.T1IF = 0;
}

int main(void)
{
initialize();

while (1)
{
// Check switch state to change direction
if (SWITCH_S3)
{
direction = 1; // Left to Right
}
else
{
direction = -1; // Right to Left
}

// Update the Knight Rider pattern
ledPattern <<= direction;
if (ledPattern == 0b0000001 || ledPattern == 0b1000000)
{
direction = -direction;
}

// Display the pattern on LEDs (D9-D3)
LATD = (LATD & 0xFF80) | ledPattern;

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

return 0;
}

void initialize(void)
{
// Configure RUN LED (D10) as output
TRISBbits.TRISB10 = 0;

// Configure LEDS D9-D3 as output
TRISD &= 0xFF80;

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

// Configure Timer1 for interrupt at every 1.45 seconds
T1CONbits.TCS = 0; // Use internal clock (Fosc/2)
T1CONbits.TCKPS = 0b11; // 1:256 prescaler
PR1 = 15624; // Period for 1.45 seconds (Fosc=16 MHz)
TMR1 = 0; // Clear Timer1
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt

// Enable interrupts
INTCON1bits.NSTDIS = 0; // Interrupt nesting enabled
INTCON2bits.GIE = 1; // Enable global interrupts
}
```

Note: The code snippet assumes the use of the XC16 compiler and that the proper header files are included. Make sure to adjust the register names and pin assignments according to your specific microcontroller and development board.

To complete this project and add a RUN LED to the embedded system, follow these steps:

Step 1: Prepare the Hardware
- Set up the Explorer 16 development board as in Project 1.
- Locate LED D10 on the board, which will be used as the RUN LED indicator.

Step 2: Write the Application in C

#include <p24FJ128GA010.h>

// Configuration bits
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
_CONFIG2(IESO_OFF & FNOSC_PRI & FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS)

// Define constants
#define RUN_LED PORTEbits.RE8 // Define the pin for the RUN LED
#define SWITCH_S3 PORTBbits.RB6 // Define the pin for switch S3
#define LED_MASK 0xFE07 // Mask for LEDs D9-D3 (outputs)

// Function prototypes
void runLedToggle();
void knightRiderPattern();

// Global variables
unsigned int delay; // Delay count for RUN LED toggling

int main()
{
// Initialize the RUN LED pin as an output
TRISEbits.TRISE8 = 0;

// Initialize the switch S3 pin as an input
TRISBbits.TRISB6 = 1;

// Configure Timer 1 for interrupt every 1.45 seconds
T1CON = 0x8030; // Timer 1 ON, prescaler 1:256
TMR1 = 0x0000;
PR1 = 0x9C3F; // Set Timer 1 period register for 1.45 seconds

// Configure interrupts
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer 1 interrupt
IPC0bits.T1IP = 4; // Set Timer 1 interrupt priority to 4

// Enable global interrupts
INTCON1bits.NSTDIS = 0; // Nested interrupts enabled
INTCON2bits.GIE = 1; // Global interrupt enabled

// Initialize delay and run the process infinitely
delay = 0;
while (1) {
runLedToggle();
knightRiderPattern();
}

return 0;
}

// Timer 1 interrupt service routine
void _ISR _T1Interrupt(void)
{
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag

// Increment delay count
delay++;

// Check if delay reached the desired value (1.45 seconds)
if (delay >= 1000) {
// Toggle the RUN LED
RUN_LED = !RUN_LED;

// Reset delay count
delay = 0;
}
}

// Toggle the RUN LED
void runLedToggle()
{
RUN_LED = !RUN_LED; // Toggle the RUN LED state
__delay_ms(725); // Delay for half the desired toggle interval
}

// Knight Rider pattern
void knightRiderPattern()
{
static unsigned int pattern = 0x0008; // Initial pattern (LED D4 on)
static unsigned char direction = 0; // 0 = Left to Right, 1 = Right to Left

// Check the state of switch S3
if (SWITCH_S3 == 0) {
direction = 0; // Switch open, set Left to Right direction
} else {
direction = 1; // Switch closed, set Right to Left direction
}

// Shift the pattern according to the direction
if (direction == 0) {
pattern = (pattern << 1) | 0x0001; // Shift Left
if (pattern == 0x00FF) {
pattern = 0x0081; // Reached the end, start again
}
} else {
pattern = (pattern >> 1) | 0x0080; // Shift Right
if (pattern == 0x0001) {
pattern = 0x0081; // Reached the end, start again
}
}

// Output the pattern to LEDs D9-D3
PORTD = (PORTD & LED_MASK) | pattern;
}

Step 3: Program the Microcontroller
- Compile the code using a suitable compiler for the chosen microcontroller.
- Program the microcontroller with the compiled code using your preferred programming method (e.g., MPLAB X, PICKit, etc.).

Step 4: Connect the Hardware
- Connect the RUN LED (D10) on the Explorer 16 board to the appropriate pin on your designed and developed embedded system.
- Connect the Knight Rider LEDs (D9-D3) on the Explorer 16 board to the corresponding pins on your designed and developed embedded system.
- Connect switch S3 on the Explorer 16 board to the corresponding pin on your designed and developed embedded system.

Step 5: Test and Verify
- Power on the embedded system and observe the behavior of the RUN LED.
- Verify that the RUN LED toggles at an interval of precisely 1.45 seconds.
- Verify that the Knight Rider pattern is displayed correctly, with the direction determined by the state of switch S3.

Note:
- The code assumes the use of a specific microcontroller model (PIC24FJ128GA010). Modify the code accordingly for other microcontroller models.
- Adjust the delay value in the runLedToggle() function if the desired toggle interval is different from 1.45 seconds.

To add a RUN LED to the embedded system and implement the desired functionality, you can follow these steps:

1. Connect the RUN LED to pin D10 on the Explorer 16 board.
2. Set up the Timer1 module in the microcontroller to generate an interrupt at a frequency of 1.45 seconds. Here's how to do it in C:

a. Configure Timer1 to operate in Timer mode and set the prescaler and period values to achieve the desired interrupt frequency. You can consult the microcontroller's datasheet for specific details on how to configure Timer1.

b. Enable the Timer1 interrupt by setting the corresponding interrupt enable bit in the microcontroller's interrupt control register.

c. Implement an interrupt service routine (ISR) for Timer1. In the ISR, toggle the state of the RUN LED to make it blink.

3. Implement the Knight Rider pattern for the LEDs (D9-D3). Here's how to do it in C:

a. Determine the direction of the pattern based on the state of switch S3. Use a GPIO pin to read the state of switch S3.

b. Implement a loop that iterates through the LEDs in the desired pattern direction. Use GPIO pins to control the state of the LEDs.

4. Repeat the whole process in an infinite loop.

Note: To precisely toggle the RUN LED at a 1.45 second interval, you need to use the interrupt mechanism for Timer1. The microcontroller's datasheet will provide details on how to configure and use the interrupt mechanism for Timer1.

Keep in mind that the specific implementation can vary depending on the microcontroller used and the development environment. Consult the microcontroller's datasheet and the development environment's documentation for more detailed instructions on how to implement the desired functionality.