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;
bool toggleDirection = true;
bool pressed = false;

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

#include <xc.h>

// Configuration bits
#pragma config FOSC = HS // High-Speed Crystal Oscillator
#pragma config WDTE = OFF // Watchdog Timer Disabled
#pragma config PWRTE = OFF // Power-Up Timer Disabled
#pragma config BOREN = ON // Brown-Out Reset Enabled
#pragma config LVP = OFF // Low-Voltage Programming Disabled

#define _XTAL_FREQ 20000000 // Crystal Oscillator frequency

// Function prototypes
void initialize(void);
void delay_ms(unsigned int milliseconds);
void toggleRUNLED(void);

// Global variables
volatile bool direction = true;
volatile bool switchPressed = false;

// Interrupt service routine for Timer1
void __interrupt() ISR(void)
{
// Check if Timer1 interrupt flag is set
if (TMR1IF)
{
// Toggle RUN LED
toggleRUNLED();

// Clear Timer1 interrupt flag
TMR1IF = 0;
}

// Check if RB0/INT interrupt flag is set
if (INTF)
{
// Toggle switchPressed flag
switchPressed = !switchPressed;

// Clear RB0/INT interrupt flag
INTF = 0;
}
}

void main(void) {
initialize();

while(1) {
// Right to Left sequence
if (!direction) {
for(int i=1; i<=7; i++) {
LATD = (1 << (i+2));
delay_ms(200);
}
}
// Left to Right sequence
else {
for(int i=7; i>=1; i--) {
LATD = (1 << (i+2));
delay_ms(200);
}
}

// Left to Right sequence if switch is pressed
if (switchPressed) {
LATD = 0x00;
for(int i=7; i>=1; i--) {
LATD = (1 << (i+2));
delay_ms(200);
}
}
}

return -1;
}

void initialize(void)
{
// Configure PORTD as output
TRISD = 0x00;

// Configure PORTB0 as input
TRISB0 = 1;

// Configure INT pin (RB0) for interrupt-on-change
INTEDG = 0; // Interrupt on falling edge (pressing the switch)
INTE = 1; // Enable RB0/INT external interrupt

// Configure Timer1
TMR1CS = 0; // Timer1 clock source is instruction clock (Fosc/4)
T1CKPS1 = 0; // Prescaler value is 1
T1CKPS0 = 0;
TMR1ON = 1; // Enable Timer1

// Configure Timer1 interrupt
TMR1IE = 1; // Enable Timer1 interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts

// Initialize LED patterns
LATD = 0b00011100;
}

void toggleRUNLED(void) {
LATDbits.LATD2 = !LATDbits.LATD2;
}

void delay_ms(unsigned int milliseconds) {
while (milliseconds > 0) {
__delay_ms(1);
milliseconds--;
}
}

To implement the provided design on Explorer 16 as Project 1, follow these steps:

Step 1: Set up the necessary configurations
- Include the required header files for the microcontroller you are using.
- Define the necessary constants and variables.

Step 2: Set the TRISA and LATA registers
- In the main function, set the TRISA register to configure PORTA as output (0x00).
- Set the initial value of LATA as 0x00.

Step 3: Implement the delay function
- Create a function named "delayFunc" with the following signature: void delayFunc(void).
- In the delayFunc, define variables j, k, and a.
- Use two nested for loops to create a delay: the outer loop for j and the inner loop for k.
- The number of iterations for each loop will determine the delay length and can be adjusted by changing the values of count1 and count2.
- Inside the inner loop, assign 0 to the variable a to consume CPU cycles and create the delay.

Step 4: Create an infinite loop
- In the main function, add a while loop that runs indefinitely (1).

Step 5: Implement the Knight Rider pattern
- Inside the while loop, create two for loops: one for the right to left sequence (i from 0 to 7) and another for the left to right sequence (j from 7 to -1).
- For each iteration of the loops, set the corresponding bit of LATA to turn on the corresponding LED and call the delayFunc to create the delay.
- After the loops, check the status of switch S3 (PORTDbits.RD6).
- If the switch is pressed (logic low), clear LATA to turn off all LEDs and create another loop for the left to right sequence (j from 8 to -1).
- Again, set the corresponding bit of LATA and call the delayFunc to create the delay.

Step 6: Implement Timer1 interrupt for toggling the RUN LED
- Configure Timer1 to generate interrupts at a frequency of approximately 1.45 seconds.
- In the interrupt service routine (ISR) for Timer1, toggle the state of the RUN LED (D10) by toggling the corresponding bit of LATA.
- Clear the Timer1 interrupt flag.

Step 7: Enable interrupts and start the timer
- Enable global interrupts.
- Enable Timer1 interrupt.
- Start Timer1.

Here is the modified code with the implementation:

```c
#include <xc.h>

#define _XTAL_FREQ 8000000 // Define your desired clock frequency

#define RUN_LED_LAT LATAbits.LATA0
#define SWITCH_S3 PORTDbits.RD6

unsigned int count1 = 200;
unsigned int count2 = 200;
bool toggleDirection = true;
bool pressed = false;

void delayFunc(void)
{
int j, k;
int a;

for (j = 0; j < count1; j++)
{
for (k = 0; k < count2; k++)
{
a = 0;
}
}
}

void __interrupt() Timer1_ISR(void)
{
if (PIR1bits.TMR1IF) // Check if Timer1 interrupt flag is set
{
RUN_LED_LAT ^= 1; // Toggle the state of RUN LED (D10)
PIR1bits.TMR1IF = 0; // Clear Timer1 interrupt flag
}
}

void main(void)
{
TRISA = 0x00; // Set PORTA as output
LATA = 0x00; // Set initial value of LATA

// Configure Timer1 for desired interval of approximately 1.45 seconds
T1CONbits.TMR1CS = 0; // Set Timer1 clock source as internal clock (Fosc/4)
T1CONbits.T1CKPS = 0b11; // Set Timer1 prescaler to 1:8
T1CONbits.TMR1ON = 1; // Enable Timer1
T1GCONbits.TMR1GE = 0; // Disable Timer1 Gate Control

// Enable Timer1 interrupt
PIE1bits.TMR1IE = 1; // Enable Timer1 interrupt

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

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

while (1)
{
int i, 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 (SWITCH_S3 == 0)
{
LATA = 0x00;

for (j = 8; j >= -1; j--)
{
LATA |= (1 << j);
delayFunc();
}
}
}
return;
}
```

Make sure to choose and configure the correct microcontroller and adjust any pin assignments, clock frequency, and delay values according to your specific requirements.

To implement the given requirements, you can follow these steps:

Step 1: Setting up Timer1
- Configure Timer1 module to work in interval mode with interrupt enabled.
- Set the prescaler value (T1CONbits.TCKPS) according to the desired interval (1.45 seconds).

Step 2: Configuring I/O pins
- Set TRISA and LATA registers to configure the I/O pins for LED control.
- Assign the appropriate pins for D10 (RUN LED) and D9-D3 (Knight Rider pattern LEDs).

Step 3: Implementing the Main Function
- Start an infinite loop.
- Inside the loop, implement the Knight Rider pattern with the desired direction:
- If switch S3 (PORTDbits.RD6) is open, iterate over the LEDs from left to right.
- If switch S3 is closed, iterate over the LEDs from right to left.
- Call the delay function (delayFunc) after each pattern iteration.

Step 4: Implementing the Delay Function (delayFunc)
- Implement the delay function to introduce the desired delay between LED toggles using nested loops.
- Add a counter (count1 and count2 variables) to adjust the delay duration.
- The formula to calculate the delay duration is: delay duration = count1 * count2 * A.

Step 5: Implementing the Timer1 Interrupt Handler
- Create an interrupt handler function for Timer1 interrupts.
- In the handler, toggle the RUN LED (D10) by updating the LATA register.
- Clear the Timer1 interrupt flag (IFS0bits.T1IF) to reset the interrupt.

Below is the modified code implementing the given requirements:

```c
#include <xc.h>

#define _XTAL_FREQ 8000000 // Set the oscillator frequency (e.g., 8MHz)

unsigned int count1 = 200;
unsigned int count2 = 200;
bool toggleDirection = true;
bool pressed = false;

void main(void) {
// Step 1: Setting up Timer1
T1CONbits.TON = 0; // Turn off Timer1 during setup
T1CONbits.TCS = 0; // Use internal clock (Fosc/2)
T1CONbits.TCKPS = 0b11; // Set prescaler to 1:256
PR1 = 9205; // Set period to achieve 1.45 seconds interval

// Step 2: Configuring I/O pins
TRISA = 0x00; // Configure PORTA as output
LATA = 0x00; // Clear PORTA initially

// Step 3: Implementing the Main Function
while (1) {
if (toggleDirection) {
// Right to Left sequence
for (int i = 0; i < 8; i++) {
LATA |= (1 << i);
delayFunc();
}
} else {
// Left to Right sequence
for (int j = 7; j >= -1; j--) {
LATA |= (1 << j);
delayFunc();
}
}

// Step 4: Implementing the Delay Function (delayFunc)
// Left to Right sequence if SW3 is pressed
if (PORTDbits.RD6 == 0) {
LATA = 0x00;
for (int j = 8; j >= -1; j--) {
LATA |= (1 << j);
delayFunc();
}
}
}
return -1;
}

// Step 4: Implementing the Delay Function (delayFunc)
void delayFunc(void) {
int j, k;
int a;

for (j = 0; j < count1; j++) {
for (k = 0; k < count2; k++) {
a = 0;
}
}
}

// Step 5: Implementing the Timer1 Interrupt Handler
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
LATAbits.LATA4 ^= 1; // Toggle the RUN LED (D10)
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
}
```

Make sure to set the appropriate oscillator frequency (_XTAL_FREQ) value at the beginning of the code to match your specific microcontroller configuration.