Implement the design on Explorer 16.

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.
4. The Potentiometer (R6) and the temperature sensor (U4) on Explorer 16 board will be read, converted and processed. And the values (pot voltage and temperature in centrigrade) will be sent out through UART2 and will be displayed on a terminal program (Putty/TeraTerm, running in your PC). The UART2 will be running at 9600 baud rate.

Here is an example implementation of the given application in C for the Explorer 16 board:

```c
#include <xc.h>
#include <stdio.h>

// Configuration bits
#pragma config FNOSC = PRI
#pragma config POSCMOD = XT
#pragma config OSCIOFNC = OFF
#pragma config FPBDIV = DIV_1
#pragma config FPLLIDIV = DIV_2
#pragma config FPLLMUL = MUL_16
#pragma config FPLLODIV = DIV_1
#pragma config FWDTEN = OFF

// Function prototypes
void UART2_Init(void);
void UART2_Write(char data);
void UART2_WriteString(char *str);
void Timer1_Init(void);
void Delay(unsigned long milliseconds);
void ReadPotentiometer(void);
void ReadTemperature(void);

// Global variables
unsigned int potValue;
float temperature;

int main(void) {
// Initialize UART2 and Timer1
UART2_Init();
Timer1_Init();

while(1) {
// Toggle RUN LED at 3.5s interval
LATDbits.LATD10 = !LATDbits.LATD10;
Delay(3500); // 3.5s delay

// Knight Rider pattern from left to right or right to left based on S3 switch
LATDbits.LATD3 = 1;
if (PORTDbits.RD6 == 1) {
for (int i = 9; i >= 3; i--) {
Delay(200); // 200ms delay
LATDbits.LATD |= (1 << i);
LATDbits.LATD &= ~(1 << (i + 1));
}
Delay(200); // 200ms delay
LATDbits.LATD &= ~(1 << 3);
} else {
for (int i = 3; i <= 9; i++) {
Delay(200); // 200ms delay
LATDbits.LATD |= (1 << i);
LATDbits.LATD &= ~(1 << (i - 1));
}
Delay(200); // 200ms delay
LATDbits.LATD &= ~(1 << 9);
}

// Read and send potentiometer voltage and temperature over UART2
ReadPotentiometer();
ReadTemperature();

char buffer[32];
sprintf(buffer, "Potentiometer: %u, Temperature: %.2f\r\n", potValue, temperature);
UART2_WriteString(buffer);
}

return 0;
}

void UART2_Init(void) {
// Configure UART2 pins
U2RXR = 0b0111; // Set U2RX to RB11
RPB10R = 0b0011; // Set RB10 to U2TX

// Configure UART2
U2MODEbits.ON = 0; // Disable UART2 during configuration
U2MODEbits.BRGH = 0; // Standard baud rate
U2MODEbits.PDSEL = 0; // 8-bit data, no parity
U2MODEbits.STSEL = 0; // 1 stop bit
U2BRG = 64; // Set baud rate to 9600

U2STAbits.URXEN = 1; // Enable receiver
U2STAbits.UTXEN = 1; // Enable transmitter

U2MODEbits.ON = 1; // Enable UART2
}

void UART2_Write(char data) {
while (U2STAbits.UTXBF); // Wait if the transmit buffer is full
U2TXREG = data; // Write the data to the transmit buffer
}

void UART2_WriteString(char *str) {
while (*str) {
UART2_Write(*str); // Send a single character
str++; // Move to next character
}
}

void Timer1_Init(void) {
// Configure Timer1
T1CONbits.ON = 0; // Disable Timer1 during configuration
T1CONbits.TCKPS = 0b10; // Set prescaler to 1:64
TMR1 = 0; // Clear Timer1 register
PR1 = 54687; // Set period for 3.5s interval at 40MHz clock

T1CONbits.ON = 1; // Enable Timer1
}

void Delay(unsigned long milliseconds) {
for (unsigned long i = 0; i < milliseconds; i++) {
for (unsigned long j = 0; j < 4000; j++) {
asm("NOP"); // Small delay loop
}
}
}

void ReadPotentiometer(void) {
potValue = ADC1BUF0; // Assuming Potentiometer is connected to AN0 (RB0)
}

void ReadTemperature(void) {
// Assuming Temperature sensor is connected to AN1 (RB1)
ADC1CFG2bits.CSCNA = 0; // Disable scanning
AD1CHSbits.CH0SA = 0b00001; // Set MUXA to AN1
ADC1CFG2bits.BUFALOC = 0; // Set result buffer to ADC1BUF0

ADC1CON1bits.SAMP = 1; // Start sampling
Delay(1); // Delay for sampling time
ADC1CON1bits.SAMP = 0; // Stop sampling

while (!ADC1CON1bits.DONE); // Wait for conversion to complete

temperature = (ADC1BUF0 * 3.3 / 1023 - 0.5) * 100; // Assuming temperature sensor formula
}
```

This code initializes the UART2 module to send data to the PC using a baud rate of 9600. The Timer1 module is used to toggle the RUN LED at a 3.5-second interval. The Knight Rider pattern is controlled by the S3 switch, with LEDs D9 to D3 being lit in a sequence based on the switch's state. The potentiometer voltage and temperature are read using the ADC module and sent over UART2 to be displayed on a terminal program on the PC. The code runs in an infinite loop, repeating the entire process indefinitely.