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

Can you make a simple code

Sure! Here's a simple code that implements the requirements:

```C
#include <xc.h>

// Function prototypes
void setup();
void delay_ms(unsigned int milliseconds);

// Global variables
volatile unsigned int timerOverflowCount = 0;

void main(void) {
// Setup and configuration
setup();

while (1) {
// Knight Rider pattern from Right to Left
if (PORTDbits.RD6 == 0) {
for (int j = 2; j <= 9; j++) {
LATA |= (1 << j);
delay_ms(200);
}
}
// Knight Rider pattern from Left to Right
else {
for (int j = 9; j >= 2; j--) {
LATA |= (1 << j);
delay_ms(200);
}
}
}

return 0;
}

void interrupt isr() {
// Check for Timer1 overflow interrupt
if (PIR1bits.TMR1IF) {
// Clear Timer1 interrupt flag
PIR1bits.TMR1IF = 0;

// Increment timer overflow count
timerOverflowCount++;

// If 1.45 seconds have passed, toggle RUN LED (D10)
if (timerOverflowCount == 1048) {
LATA ^= (1 << 0); // Toggle D10
timerOverflowCount = 0; // Reset timer overflow count
}
}
}

void setup() {
// Configure oscillator
OSCCON = 0x72; // 8 MHz, Internal

// Configure I/O ports
TRISA = 0x00; // All PORTA pins as output
LATA = 0x00; // Initialize PORTA outputs as low
TRISD = 0x40; // RD6 as input for switch

// Configure Timer1
T1CONbits.TMR1CS = 0; // Internal clock (Fosc/4)
T1CONbits.T1CKPS = 0b11; // 1:8 prescaler
T1CONbits.TMR1ON = 1; // Enable Timer1
TMR1H = 0x85; // Set initial Timer1 value
TMR1L = 0xEE;

// Enable Timer1 interrupt
PIE1bits.TMR1IE = 1; // Enable Timer1 overflow interrupt
INTCONbits.PEIE = 1; // Enable peripheral interrupts
INTCONbits.GIE = 1; // Enable global interrupts
}

void delay_ms(unsigned int milliseconds) {
for (unsigned int i = 0; i < milliseconds; i++) {
__delay_ms(1);
}
}
```

Please note that this code assumes you are using a Microcontroller with PIC16F676 or a similar device. Make sure to check the datasheet of your specific Microcontroller to ensure the register names and configurations are correct.

Here is a simple code that implements the requirements mentioned:

#include <p24Fxxxx.h>

// Configuration bits
_CONFIG1( JTAGEN_OFF & FWDTEN_OFF)
_CONFIG2( POSCMOD_HS & I2C1SEL_PRI & IOL1WAY_OFF)

// Variables
unsigned int count1 = 725; // Timer1 period to achieve 1.45 seconds (725 * 2us = 1.45s)
unsigned int count2 = 200;
unsigned char LEDpattern = 0x0E; // Initial LED pattern from left to right
unsigned char LEDdirection = 1; // Direction flag (1 = left to right, 0 = right to left)

// Function prototypes
void initialize(void);
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void);
void delayFunc(void);

int main(void) {
initialize();

// Main loop
while(1) {
if(PORTDbits.RD6 == 0) {
// Switch S3 is closed, change direction to right to left
LEDdirection = 0;
} else {
// Switch S3 is open, change direction to left to right
LEDdirection = 1;
}

// Toggle RUN LED
LATAbits.LATA0 ^= 1;

// Delay for 1.45 seconds using Timer1 interrupt
T1CONbits.TON = 1; // Start Timer1
asm("nop"); // Wait for Timer1 interrupt
T1CONbits.TON = 0; // Stop Timer1
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
}

return 0;
}

void initialize() {
// Initialize oscillator
CLKDIVbits.RCDIV = 0; // Set RCDIV to 0 (8 MHz)
CLKDIVbits.DOZEN = 0; // No DOZE mode
CLKDIVbits.DOZE = 0; // DOZE = 1:1
OSCTUNbits.TUN = 0; // No oscillator tuning

// Initialize Timer1
T1CON = 0; // Clear Timer1 config
T1CONbits.TCKPS = 0b11; // Prescaler 1:256
T1CONbits.TCS = 0; // Internal clock source
PR1 = count1; // Set Timer1 period
_T1IP = 1; // Set Timer1 interrupt priority
_T1IE = 1; // Enable Timer1 interrupt
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag

// Initialize I/Os
TRISA = 0xFF80; // Set RA7-RA1 as outputs
LATA = 0; // Clear all outputs
TRISD = 0xFF7F; // Set RD6 as input

// Enable interrupts
INTCON1bits.NSTDIS = 0; // Enable nested interrupts
INTCON2bits.INT0EP = 0; // INT0 interrupt on positive edge
asm("disi #0x0004"); // Disable interrupts for next 4 instructions
asm("mov.b #0x7FFF, W0");
asm("mov.b W0, _Ancestor_SEC"); // Enable interrupts for next instructions

return;
}

void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
T1CONbits.TON = 0; // Stop Timer1
TMR1 = 0; // Reset Timer1
LATA = LEDpattern; // Update LED pattern

// Shift LED pattern left or right based on direction flag
if(LEDdirection == 1) {
LEDpattern <<= 1;
if(LEDpattern == 0x80) {
LEDpattern = 0x7F;
}
} else {
LEDpattern >>= 1;
if(LEDpattern == 0x01) {
LEDpattern = 0xFE;
}
}

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

return;
}

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

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

return;
}

Note: This code assumes that Timer1 interrupt is used to toggle the RUN LED at exactly 1.45 seconds. The LED pattern is shifted left or right based on the state of switch S3. The code also configures the microcontroller's oscillator, Timer1, I/O pins, and enables interrupts.

Certainly! Here's a simple code that implements the requirements you mentioned:

```c
#include <xc.h>

#define _XTAL_FREQ 16000000 // Crystal frequency

// Function prototypes
void setup(void);
void timer1Setup(void);
void knightRiderPattern(void);
void __interrupt() isr(void);

// Global variables
unsigned int timerCounter = 0;

void main(void) {
setup(); // Configure the necessary settings
knightRiderPattern(); // Run the knight rider pattern indefinitely

return;
}

void setup(void) {
// Configure GPIO
TRISAbits.TRISA0 = 0; // Set RA0 (RUN LED D10) as output
TRISBbits.TRISB0 = 1; // Set RB0 (S3 switch) as input

// Configure Interrupts
INTCONbits.GIE = 1; // Enable global interrupts
INTCONbits.PEIE = 1; // Enable peripheral interrupts
PIE1bits.TMR1IE = 1; // Enable Timer1 interrupt

// Configure Timer1
timer1Setup();

// Initialize LED and switch states
LATA = 0; // Turn off all LEDs
PORTBbits.RB0 = 0; // Set initial state of switch (S3) to open
}

void timer1Setup(void) {
// Configure Timer1
TMR1 = 0; // Reset Timer1 register
T1CONbits.T1CKPS = 0b11; // Prescaler 1:8
T1CONbits.TMR1CS = 0; // Clock source: Internal instruction cycle clock (Fosc/4)
T1CONbits.T1SYNC = 0; // Do not synchronize external clock input
T1CONbits.TMR1ON = 1; // Enable Timer1
}

void knightRiderPattern(void) {
while (1) {
// Right to Left sequence if S3 is closed
if (PORTBbits.RB0 == 0) {
for (int i = 0; i < 8; i++) {
LATA = (1 << i); // Turn on current LED
__delay_ms(200); // Delay for 200 milliseconds
}
}
// Left to Right sequence if S3 is open
else {
for (int i = 7; i >= 0; i--) {
LATA = (1 << i); // Turn on current LED
__delay_ms(200); // Delay for 200 milliseconds
}
}
}
}

void __interrupt() isr(void) {
// Check if Timer1 interrupt flag is set
if (PIR1bits.TMR1IF == 1) {
// Reset interrupt flag
PIR1bits.TMR1IF = 0;

// Increment timer counter
timerCounter++;

// Check if 1.45 seconds has passed
if (timerCounter == 1450) {
// Toggle RUN LED (RA0)
LATAbits.LATA0 = !LATAbits.LATA0;

// Reset timer counter
timerCounter = 0;
}
}
}
```

Explanation:

1. The `setup()` function is responsible for configuring the necessary settings, including GPIO, interrupts, and Timer1. It also sets the initial LED and switch states.

2. The `timer1Setup()` function configures Timer1 with a prescaler of 1:8 and enables the timer.

3. The `knightRiderPattern()` function contains the main logic for the knight rider pattern. It continuously checks the state of switch S3 (RB0) and performs the appropriate LED sequence (right to left or left to right).

4. The `isr()` function is the interrupt service routine for Timer1. It is called when the Timer1 interrupt flag is set. Inside the ISR, it increments the `timerCounter` variable and checks if 1.45 seconds have passed. If so, it toggles the RUN LED (RA0) and resets the `timerCounter`.

5. In the `main()` function, we call the `setup()` function to configure the settings and then start the knight rider pattern by calling the `knightRiderPattern()` function.

Make sure to configure the microcontroller to use the appropriate oscillator frequency (`_XTAL_FREQ`) in your particular setup. Also, note that the `__delay_ms()` function provides a delay in milliseconds, so it might require adjusting the delay duration depending on your specific requirements.