Implement the design on Explorer 16 as Project 1. 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.

Can you make a short and simple code?

Certainly! Here's a short and simple code that fulfills the requirements mentioned:

```c
#include <pic18f4620.h> // Assuming PIC18F4620 microcontroller is being used

// Define macro to achieve a 3.5 second delay
#define DELAY_3_5_SECONDS (unsigned long)((3.5 * 1000000) / 256)

// Configure Timer1 for desired delay
void configureTimer1() {
T1CONbits.TMR1CS = 0; // Timer1 source = internal clock (Fosc/4)
T1CONbits.T1CKPS = 3; // Prescaler value = 8
T1CONbits.TMR1ON = 1; // Start Timer1
}

// Toggle the RUN LED (D10) at regular intervals
void toggleRunLED() {
LATBbits.LATB5 = !LATBbits.LATB5; // Toggle RB5 (connected to LED D10)
TMR1H = (65535 - DELAY_3_5_SECONDS) >> 8; // Set Timer1 high byte
TMR1L = (65535 - DELAY_3_5_SECONDS) & 0x00FF; // Set Timer1 low byte
PIR1bits.TMR1IF = 0; // Clear Timer1 interrupt flag
}

// Initialize the microcontroller
void init() {
// Configure RUN LED (D10) as output
TRISBbits.TRISB5 = 0;

// Configure S3 switch as input
TRISBbits.TRISB2 = 1;

// Configure LED pattern pins (D9-D3) as outputs
TRISDbits.TRISD2 = 0; // D9
TRISDbits.TRISD3 = 0; // D8
TRISDbits.TRISD4 = 0; // D7
TRISDbits.TRISD5 = 0; // D6
TRISDbits.TRISD6 = 0; // D5
TRISDbits.TRISD7 = 0; // D4
TRISBbits.TRISB4 = 0; // D3

// Initialize LED pattern to Left to Right direction
LATDbits.LATD2 = 1;
LATDbits.LATD3 = 1;
LATDbits.LATD4 = 1;
LATDbits.LATD5 = 1;
LATDbits.LATD6 = 1;
LATDbits.LATD7 = 1;
LATBbits.LATB4 = 1;

configureTimer1(); // Configure Timer1
}

// Update LED pattern direction based on switch S3 state
void updatePatternDirection() {
if (PORTBbits.RB2 == 0) { // If S3 is open
// Left to Right direction
LATDbits.LATD2 = !LATDbits.LATD2;
LATDbits.LATD3 = !LATDbits.LATD3;
LATDbits.LATD4 = !LATDbits.LATD4;
LATDbits.LATD5 = !LATDbits.LATD5;
LATDbits.LATD6 = !LATDbits.LATD6;
LATDbits.LATD7 = !LATDbits.LATD7;
LATBbits.LATB4 = !LATBbits.LATB4;
} else { // If S3 is closed
// Right to Left direction
LATDbits.LATD2 = !LATDbits.LATD2;
LATDbits.LATD3 = !LATDbits.LATD3;
LATDbits.LATD4 = !LATDbits.LATD4;
LATDbits.LATD5 = !LATDbits.LATD5;
LATDbits.LATD6 = !LATDbits.LATD6;
LATDbits.LATD7 = !LATDbits.LATD7;
LATBbits.LATB4 = !LATBbits.LATB4;
}
}

// Main program loop
void main() {
init(); // Initialize microcontroller

while (1) {
toggleRunLED(); // Toggle RUN LED every 3.5 seconds
updatePatternDirection(); // Update LED pattern direction
}
}
```

This code should fulfill the requirements mentioned. It utilizes Timer1 module to toggle the RUN LED at every 3.5 second interval and also handles the Knight Rider pattern based on the state of switch S3. The program runs in an infinite loop, repeating the whole process continuously.

Sure! Here's a short and simple C code that implements the given requirements on the Explorer 16 board with the necessary hardware configurations:

```c
#include <xc.h>

// Configuration bits
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config IESO = OFF // Internal External Switchover bit (Internal Switchover mode disabled)
#pragma config WDTEN = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on SWDTEN bit))
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

// Function prototypes
void init(void);
void runLedToggle(void);
void knightRiderPattern(bool direction);

// Global variables
bool isPatternLeftToRight;

// Main function
int main(void) {
init(); // Initialize hardware configurations

while(1) {
runLedToggle();
knightRiderPattern(isPatternLeftToRight);
}

return 0;
}

// Function to initialize hardware
void init(void) {
// Configure Timer1
T1CONbits.TCKPS = 0b10; // Prescaler value 1:64
TMR1 = 0; // Clear Timer1 register
PR1 = 54688; // time delay = 3.5s / (1 / (8MHz / 64)) = 54688
T1CONbits.TON = 1; // Enable Timer1

// Configure I/O ports
TRISDbits.TRISD0 = 0; // RD0 (LED D10) is output
TRISDbits.TRISD1 = 1; // RD1 (Switch S3) is input

TRISB = 0b00000000; // Port B bits RB7-RB0 are configured as outputs (LEDs D9-D3)

// Initialize variables
isPatternLeftToRight = true;
}

// Function to toggle the RUN LED (LED D10) at every 3.5s interval using Timer1
void runLedToggle(void) {
if (IFS0bits.T1IF) {
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag

// Toggle the RUN LED
LATDbits.LATD0 ^= 1;
}
}

// Function to generate Knight Rider pattern based on the direction (Left to Right or Right to Left)
void knightRiderPattern(bool direction) {
static unsigned char pattern = 0b00000010;
static bool isPatternIncreasing = false;
static int delayCounter = 0;

if (!PORTDbits.RD1) { // Check the state of the switch (S3)
// If switch is open, change the direction to Left to Right
direction = true;
} else {
// If switch is closed, change the direction to Right to Left
direction = false;
}

// Generate Knight Rider pattern according to the direction
if (direction) {
if (isPatternIncreasing) {
pattern <<= 1;
if (pattern == 0b10000000) {
isPatternIncreasing = false;
}
} else {
pattern >>= 1;
if (pattern == 0b00000010) {
isPatternIncreasing = true;
}
}
} else {
if (isPatternIncreasing) {
pattern >>= 1;
if (pattern == 0b00000001) {
isPatternIncreasing = false;
}
} else {
pattern <<= 1;
if (pattern == 0b01000000) {
isPatternIncreasing = true;
}
}
}

// Display the pattern on LEDs (D9-D3)
LATB = pattern;

// Delay for a short duration to make the pattern visible
for (delayCounter = 0; delayCounter < 10000; delayCounter++);
}
```

This code sets up the hardware configurations and implements the required functionalities using the Timer1 module and input switch S3. The RUN LED (LED D10) will toggle every 3.5 seconds, and the Knight Rider pattern with 7 LEDs (D9-D3) will be generated based on the direction set by the position of switch S3. The whole process will be repeated infinitely.

Sure! Here's a short and simple code in C to implement the design on Explorer 16 as Project 1:

```c
#include <p24Fxxxx.h> // Include the appropriate header file for your microcontroller

// Function to initialize Timer 1 module
void InitializeTimer1() {
// Configure Timer 1
T1CONbits.TON = 0; // Disable Timer
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TGATE = 0; // Disable Gated Timer mode
T1CONbits.TCKPS = 0b10; // Select 1:64 Prescaler

// Set Timer 1 period to achieve 3.5 seconds interval
PR1 = 54687; // ((3.5s / (1/16MHz)) / 64) - 1

// Clear Timer 1 value
TMR1 = 0;

// Enable Timer 1 interrupt
IEC0bits.T1IE = 0; // Disable Timer 1 interrupt
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag
T1CONbits.TON = 1; // Enable Timer
}

// Function to initialize IO ports and switches
void InitializeIO() {
// Configure LED pins as outputs
TRISDbits.TRISD10 = 0; // LED D10 as output
TRISDbits.TRISD9 = 0; // LED D9 as output
TRISDbits.TRISD8 = 0; // LED D8 as output
TRISDbits.TRISD7 = 0; // LED D7 as output
TRISDbits.TRISD6 = 0; // LED D6 as output
TRISDbits.TRISD5 = 0; // LED D5 as output
TRISDbits.TRISD4 = 0; // LED D4 as output
TRISDbits.TRISD3 = 0; // LED D3 as output

// Configure Switch S3 pin as an input
TRISDbits.TRISD0 = 1; // Switch S3 as input
}

// Function to toggle the RUN LED (D10)
void ToggleRunLED() {
LATDbits.LATD10 = ~LATDbits.LATD10; // Toggle the RUN LED (D10)
}

// Function to toggle the Knight Rider pattern LEDs (D9-D3)
void ToggleKnightRiderLEDs() {
static int direction = 1;
static int led = 0x0E;

LATD = led; // Output the current LED pattern

// Update the LED pattern based on the direction
if (direction == 1) {
if (led == 0x02) {
direction = 0;
led = 0x04;
} else {
led >>= 1;
}
} else {
if (led == 0x08) {
direction = 1;
led = 0x04;
} else {
led <<= 1;
}
}
}

// Interrupt Service Routine for Timer 1
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag

ToggleRunLED(); // Toggle the RUN LED (D10)
ToggleKnightRiderLEDs(); // Toggle the Knight Rider pattern LEDs (D9-D3)
}

// Function to initialize the necessary components and start the program
void InitializeAndStart() {
InitializeIO(); // Initialize IO ports and switches
InitializeTimer1(); // Initialize Timer 1

while (1) {
// Wait for the Timer 1 interrupt to toggle the RUN LED and Knight Rider pattern LEDs
}
}

// Main function
int main(void) {
InitializeAndStart(); // Initialize and start

return 0;
}
```

Please note that this code assumes you are using a Microchip PIC24F microcontroller and the necessary header files are included for that microcontroller. Please make sure to properly configure the microcontroller and adjust the pin assignments according to your specific board and microcontroller model.