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

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

Step 1: Initialize the necessary variables and configurations.

```c
#include <p30Fxxxx.h>

// Function prototypes
void delay(int milliseconds);

// Global variables
unsigned int timer1_counter = 0;

void main(void) {
// Configure TRIS and LAT registers for LED pins
TRISA = 0x00; // Set all PORTA pins as outputs for LEDs
LATA = 0x00; // Clear all PORTA outputs for LEDs

// Configure TRIS and CNPU registers for Switch pin
TRISDbits.TRISD6 = 1; // Set PORTD pin 6 as input for switch
CNPUDbits.CNPUD6 = 1; // Enable internal pull-up resistor for switch

// Configure Timer1 for 1.45 second interval
T1CON = 0; // Timer1 off
T1CONbits.TCKPS = 0b11; // Prescaler 256
PR1 = 57500; // Period register for 1.45 seconds

// Configure Timer1 interrupt
IPC0bits.T1IP = 1; // Set Timer1 priority level 1
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt

// Enable interrupts
INTCON2bits.GIE = 1; // Enable global interrupts

// Start Timer1
T1CONbits.TON = 1; // Timer1 on

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

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

// Left to Right sequence
for(j=7; j>=-1; j--) {
LATA = (1 << j);
delay(100);
}

// Left to Right sequence if switch is pressed
if(PORTDbits.RD6 == 0) {
LATA = 0x00;
for(j=8; j>=-1; j--) {
LATA = (1 << j);
delay(100);
}
}
}
return -1;
}
```

Step 2: Implement the Timer1 interrupt service routine.

```c
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
// Toggle RUN LED (D10)
LATDbits.LATD9 ^= 1; // Toggle LATD9

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

Step 3: Implement the delay function.

```c
void delay(int milliseconds) {
unsigned int i;
for(i=0; i<milliseconds; i++) {
__delay32(63974); // Delay for 1 millisecond (16 MIPS instruction cycles)
}
}
```

Explanation:
The code initializes the necessary variables and configurations for the Explorer 16 board. It configures the TRIS and LAT registers for the LED pins and the TRIS and CNPU registers for the switch pin. It also configures the Timer1 module with a prescaler of 256 and a period register value of 57500, which corresponds to a time interval of 1.45 seconds. The code also sets up the Timer1 interrupt and enables global interrupts.

Inside the main while loop, the Knight Rider pattern is implemented with two for loops for right-to-left and left-to-right sequences. If the switch is pressed (RD6 is low), the pattern is reversed. The delay function is used to pause the execution for a specified number of milliseconds.

The Timer1Interrupt service routine is used to toggle the RUN LED (D10) every time the Timer1 interrupt occurs. The delay function is utilized to achieve the precise 1.45-second interval.

Note: This code assumes the use of the PIC32 microcontroller. Adjustments may be needed if using a different microcontroller.

#include <xc.h>

// Configuration bits
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select bit (MCLR pin function is reset)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3/PGM pin has digital I/O function, HV on MCLR must be used for programming)

#define _XTAL_FREQ 20000000 // Define oscillator frequency

unsigned int count = 0; // Variable to count Timer1 overflows

void interrupt ISR(void) // Timer1 interrupt service routine
{
if (TMR1IF) // Check if Timer1 overflow interrupt flag is set
{
TMR1IF = 0; // Clear Timer1 overflow interrupt flag

if (count == 3624) // 1.45 seconds has passed
{
count = 0; // Reset the count variable

LATAbits.LATA4 ^= 1; // Toggle the RUN LED (D10)
}
else
{
count++; // Increment the count variable
}
}
}

void main(void)
{
// Set the RUN LED (D10) as output
TRISAbits.TRISA4 = 0;
// Set the Knight Rider LEDs (D9-D3) as outputs
TRISDbits.TRISD0 = 0;
TRISDbits.TRISD1 = 0;
TRISDbits.TRISD2 = 0;
TRISDbits.TRISD3 = 0;
TRISDbits.TRISD4 = 0;
TRISDbits.TRISD5 = 0;
TRISDbits.TRISD6 = 0;

// Initialize the Knight Rider LEDs (D9-D3) off
LATDbits.LATD0 = 0;
LATDbits.LATD1 = 0;
LATDbits.LATD2 = 0;
LATDbits.LATD3 = 0;
LATDbits.LATD4 = 0;
LATDbits.LATD5 = 0;
LATDbits.LATD6 = 0;

// Configure Timer1
T1CONbits.TMR1CS = 0; // Timer1 clock source is internal instruction clock (Fosc/4)
T1CONbits.T1CKPS = 0b11; // Timer1 prescaler is 1:8
T1CONbits.TMR1ON = 1; // Enable Timer1
TMR1IE = 1; // Enable Timer1 overflow interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts

while(1)
{
// Right to Left sequence if switch S3 is open
if (PORTDbits.RD6 == 0)
{
LATDbits.LATD0 = 1;
__delay_ms(200);
LATDbits.LATD1 = 1;
__delay_ms(200);
LATDbits.LATD2 = 1;
__delay_ms(200);
LATDbits.LATD3 = 1;
__delay_ms(200);
LATDbits.LATD4 = 1;
__delay_ms(200);
LATDbits.LATD5 = 1;
__delay_ms(200);
LATDbits.LATD6 = 1;
__delay_ms(200);
LATDbits.LATD6 = 0;
__delay_ms(200);
LATDbits.LATD5 = 0;
__delay_ms(200);
LATDbits.LATD4 = 0;
__delay_ms(200);
LATDbits.LATD3 = 0;
__delay_ms(200);
LATDbits.LATD2 = 0;
__delay_ms(200);
LATDbits.LATD1 = 0;
__delay_ms(200);
LATDbits.LATD0 = 0;
__delay_ms(200);
}
// Left to Right sequence if switch S3 is closed
else
{
LATDbits.LATD6 = 1;
__delay_ms(200);
LATDbits.LATD5 = 1;
__delay_ms(200);
LATDbits.LATD4 = 1;
__delay_ms(200);
LATDbits.LATD3 = 1;
__delay_ms(200);
LATDbits.LATD2 = 1;
__delay_ms(200);
LATDbits.LATD1 = 1;
__delay_ms(200);
LATDbits.LATD0 = 1;
__delay_ms(200);
LATDbits.LATD0 = 0;
__delay_ms(200);
LATDbits.LATD1 = 0;
__delay_ms(200);
LATDbits.LATD2 = 0;
__delay_ms(200);
LATDbits.LATD3 = 0;
__delay_ms(200);
LATDbits.LATD4 = 0;
__delay_ms(200);
LATDbits.LATD5 = 0;
__delay_ms(200);
LATDbits.LATD6 = 0;
__delay_ms(200);
}
}
}

To implement the desired functionality, we need to follow these steps:

Step 1: Configure the necessary registers and modules
- Set the initial state of the TRIS and LAT registers for the necessary pins of the LEDs and the RUN LED (D10).
- Configure Timer1 for a specific time interval of 1.45 seconds.
- Configure the interrupt mechanism for Timer1.

Step 2: Write the main function to control the LED patterns and the RUN LED
- Use a variable to keep track of the current pattern direction (left to right or right to left).
- Use a loop to repeat the pattern indefinitely.
- Inside the loop, check the state of the switch (S3).
- If the switch is open, use a loop to cycle through the LED pattern from left to right and toggle the RUN LED according to the specified 1.45 seconds interval.
- If the switch is closed, use a loop to cycle through the LED pattern from right to left and toggle the RUN LED according to the specified 1.45 seconds interval.

Step 3: Implement the necessary delay function
- Create a delay function that contains nested loops to achieve the desired delay interval.
- The delay function should take into account the values of `count1` and `count2` variables from the provided code.

Here is an example implementation of the application:

```C
#include <xc.h>

// Function prototypes
void configureGPIO(void);
void configureTimer1(void);
void configureInterrupt(void);
void delayFunc(void);

// Global variables
unsigned int count1 = 200;
unsigned int count2 = 200;
int patternDirection = 1; // 1 for left to right, -1 for right to left

void main(void) {
configureGPIO();
configureTimer1();
configureInterrupt();

while (1) {
// Toggle RUN LED at every 1.45 seconds interval
if (TMR1 >= 46706) { // 1.45 seconds delay
LATBbits.LATB4 ^= 1; // Toggle RUN LED (D10)
TMR1 = 0; // Reset Timer1
}

// Control LED pattern based on the switch state
if (PORTDbits.RD6 == 0) { // Switch (S3) is open
if (patternDirection == 1) { // Left to right direction
LATB = 0b1111111; // Turn on LEDs from D9 to D3
delayFunc();
LATB = 0; // Turn off all LEDs
delayFunc();
} else { // Right to left direction
LATB = 0b0000001; // Turn on LED D3
delayFunc();
LATB = 0b0000010; // Turn on LED D4
delayFunc();
LATB = 0b0000100; // Turn on LED D5
delayFunc();
LATB = 0b0001000; // Turn on LED D6
delayFunc();
LATB = 0b0010000; // Turn on LED D7
delayFunc();
LATB = 0b0100000; // Turn on LED D8
delayFunc();
LATB = 0b1000000; // Turn on LED D9
delayFunc();
LATB = 0b0000000; // Turn off all LEDs
delayFunc();
}
}
}

return -1;
}

void configureGPIO(void) {
TRISD = 0b01000000; // Set RD6 as input (for switch SW3)
TRISB = 0b00000000; // Set all RB pins as output
LATB = 0b00000000; // Initially turn off all LEDs
}

void configureTimer1(void) {
T1CON = 0b10000000; // Enable Timer1 with 1:1 prescaler
TMR1 = 0; // Clear Timer1 value
}

void configureInterrupt(void) {
TMR1IE = 1; // Enable Timer1 interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts
}

void delayFunc(void) {
int j, k, a;
for (j = 0; j < count1; j++) {
for (k = 0; k < count2; k++) {
a = 0;
}
}
}

void interrupt high_priority ISR(void) {
if (TMR1IF) {
TMR1IF = 0; // Clear Timer1 interrupt flag

if (patternDirection == 1) {
patternDirection = -1; // Change pattern direction to right to left
} else {
patternDirection = 1; // Change pattern direction to left to right
}
}
}
```

This code should implement the desired functionality with the RUN LED toggling at a precise 1.45 seconds interval using Timer1 and the LED pattern altering directions based on the state of switch S3.