Implement the design on Explorer 16. LED D10 will be used as the RUN LED indicator.

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.

Where can I put the 3.5 seconds in the code below?

#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

unsigned int count1 = 200;
unsigned int count2 = 200;

int main(void){
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

while(1) {
int i;
int j;
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
delayFunc();

// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 7; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for(j = 0; j <= 7; 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;
}
}
}

You can add a delay of 3.5 seconds using Timer1 in the code by configuring Timer1 to generate an interrupt every 3.5 seconds and toggling the RUN LED in the interrupt service routine (ISR). Here is an updated version of the code with the Timer1 interrupt added:

```c
#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

unsigned int count1 = 200;
unsigned int count2 = 200;

void delayFunc(void);

int main(void) {
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

// Configure Timer1 for 3.5 seconds interrupt
T1CON = 0b00110001; // Timer1 with 1:8 prescaler, internal clock source
TMR1 = 3036; // Initialize Timer1 to overflow after 3.5 seconds
PIE1bits.TMR1IE = 1; // Enable Timer1 overflow interrupt
INTCONbits.PEIE = 1; // Enable peripheral interrupts
INTCONbits.GIE = 1; // Enable global interrupts

while(1) {
int i;
int j;
// The RUN LED (D10) will be toggled in the Timer1 ISR

// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 7; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for(j = 0; j <= 7; j++) {
LATA = (1 << j);
delayFunc();
}
}
}
return -1;
}

void __interrupt() isr(void) {
if (PIR1bits.TMR1IF) {
// Toggle the RUN LED (D10)
LATDbits.LATD0 = !LATDbits.LATD0;

// Reset Timer1
TMR1 = 3036;
PIR1bits.TMR1IF = 0; // Clear the interrupt flag
}
}

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

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

In this code, I have added the Timer1 configuration and interrupt service routine (`isr`) to toggle the RUN LED (D10) every 3.5 seconds. The ISR is triggered when Timer1 overflows, which occurs after 3.5 seconds (assuming the oscillator frequency is set to 8 MHz as specified by `_XTAL_FREQ`). The delayFunc function remains unchanged and is used for generating delays in the LED patterns.

Note: Make sure to enable the Timer1 interrupt by setting the `GIE` (Global Interrupt Enable) and `PEIE` (Peripheral Interrupt Enable) bits in the `INTCON` register.

To make the RUN LED toggle at every 3.5-second interval using Timer1, you need to configure Timer1 to generate an interrupt every 3.5 seconds.

First, you need to initialize Timer1 to use the internal oscillator with a prescaler of 1:8. This will make Timer1 increment every 0.5 microseconds (assuming an 8 MHz oscillator frequency).

Add the following code to your program before the main() function:

```c
void timer1_init(void)
{
T1CON = 0x30; // Configure Timer1 with 1:8 prescaler and internal oscillator
TMR1 = 0; // Initialize Timer1 register to 0

// Calculate the timer value required for a 3.5-second interval
unsigned int timer_value = (65536 - (3.5 * 1000000) / (0.5 * 8));
TMR1 = timer_value; // Set Timer1 register to the calculated value

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

Then, in your main() function, call the timer1_init() function to initialize Timer1:

```c
int main(void) {
// ...

timer1_init();

// ...
}
```

Next, you need to write an interrupt service routine (ISR) to handle the Timer1 interrupt. In this ISR, you will toggle the RUN LED and reset the Timer1 value to generate the next interrupt after 3.5 seconds.

Add the following code after the main() function to define the ISR:

```c
void __interrupt() isr(void)
{
if (TMR1IF) // Check if Timer1 interrupt flag is set
{
LATDbits.LATD0 = !LATDbits.LATD0; // Toggle RUN LED (D10)

// Reset Timer1 value for the next 3.5-second interval
unsigned int timer_value = (65536 - (3.5 * 1000000) / (0.5 * 8));
TMR1 = timer_value;

TMR1IF = 0; // Clear Timer1 interrupt flag
}
}
```

Finally, remove the delayFunc() function since the delay will now be handled by the Timer1 interrupt.

Your code should now look like this:

```c
#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

unsigned int count1 = 200;
unsigned int count2 = 200;

void timer1_init(void)
{
T1CON = 0x30; // Configure Timer1 with 1:8 prescaler and internal oscillator
TMR1 = 0; // Initialize Timer1 register to 0

// Calculate the timer value required for a 3.5-second interval
unsigned int timer_value = (65536 - (3.5 * 1000000) / (0.5 * 8));
TMR1 = timer_value; // Set Timer1 register to the calculated value

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

int main(void){
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

timer1_init();

while(1) {
int i;
int j;

// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 7; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for(j = 0; j <= 7; j++) {
LATA = (1 << j);
delayFunc();
}
}
}
return -1;
}

void __interrupt() isr(void)
{
if (TMR1IF) // Check if Timer1 interrupt flag is set
{
LATDbits.LATD0 = !LATDbits.LATD0; // Toggle RUN LED (D10)

// Reset Timer1 value for the next 3.5-second interval
unsigned int timer_value = (65536 - (3.5 * 1000000) / (0.5 * 8));
TMR1 = timer_value;

TMR1IF = 0; // Clear Timer1 interrupt flag
}
}

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

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

Now, the RUN LED should toggle at every 3.5-second interval using Timer1.

To make the RUN LED (D10) toggle at every 3.5 seconds, you can use Timer1. Here's how you can modify the code:

1. Add the necessary libraries at the top of your code:

```c
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
```

2. Implement the Timer1 setup code inside the `main` function:

```c
// Timer1 Setup
T1CONbits.TMR1CS = 0b00; // Select internal clock (Fosc/4)
T1CONbits.T1CKPS = 0b11; // Prescaler 1:8
TMR1 = 43981; // Initialize Timer1 value for 3.5 seconds
T1CONbits.TMR1ON = 1; // Enable Timer1
```

3. Modify the while loop to include Timer1 check and toggle the RUN LED:

```c
while(1) {
// Check Timer1 overflow - 3.5 second delay
if (PIR1bits.TMR1IF) {
PIR1bits.TMR1IF = 0; // Clear Timer1 overflow flag

// Toggle the RUN LED (D10)
LATDbits.LATD0 = !LATDbits.LATD0;

// Reset Timer1 value for next interval
TMR1 = 43981;
}

// Rest of the code...
}
```

Now, the RUN LED will toggle at every 3.5 seconds using Timer1.

Note: The value 43981 is calculated based on the oscillator frequency (_XTAL_FREQ) and the desired time period (3.5 seconds). You can adjust this value accordingly if your oscillator frequency is different.

Here's the modified code:

```c
#include <xc.h>
#include <stdint.h>
#include <stdbool.h>

#define _XTAL_FREQ 8000000 // Specify the oscillator frequency

unsigned int count1 = 200;
unsigned int count2 = 200;

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

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

int main(void) {
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

// Timer1 Setup
T1CONbits.TMR1CS = 0b00; // Select internal clock (Fosc/4)
T1CONbits.T1CKPS = 0b11; // Prescaler 1:8
TMR1 = 43981; // Initialize Timer1 value for 3.5 seconds
T1CONbits.TMR1ON = 1; // Enable Timer1

while (1) {
// Check Timer1 overflow - 3.5 second delay
if (PIR1bits.TMR1IF) {
PIR1bits.TMR1IF = 0; // Clear Timer1 overflow flag

// Toggle the RUN LED (D10)
LATDbits.LATD0 = !LATDbits.LATD0;

// Reset Timer1 value for next interval
TMR1 = 43981;
}

int i;
int j;

// Knight Rider pattern from Left to Right direction
for (i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for (j = 7; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for (j = 0; j <= 7; j++) {
LATA = (1 << j);
delayFunc();
}
}
}

return -1;
}
```

Hope this helps!