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

Can you make a short and simple code?

Here is a short and simple code to implement the given functionality using Timer 1 module for toggling the RUN LED and controlling the Knight Rider pattern.

```c
#include <xc.h>

#pragma config FNOSC = PRIPLL // Primary Oscillator (XT, HS, EC)
#pragma config POSCMOD = HS // Primary Oscillator Configuration Bits (HS osc mode)
#pragma config FPLLMUL = MUL_20 // PLL Multiplier Value (Multiplier 20x)
#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
#pragma config FPBDIV = DIV_2 // Peripheral Clock divisor (Pb_Clk is Sys_Clk/2)
#pragma config FPLLODIV = DIV_1 // PLL Output Divider (PLL Divide by 1)

#define SWITCH_DOWN PORTDbits.RD7 // Switch connected to RD7 pin
#define RUN_LED LATAbits.LATA0 // RUN LED connected to RA0 pin

void delay_ms(unsigned int milliseconds){
unsigned int count = 0;
while(count < milliseconds){
// Each asm instruction takes 4 cycles
// Assuming a clock frequency of 24 MHz, one instruction takes 0.166667us
// Therefore, 4 instructions will take 0.66667us
// Approximately 1500us is required for 1.45 seconds delay
asm("repeat #2998"); // Repeat 4 instructions 3000 times
asm("nop"); // No operation
count++;
}
}

void setup(){
ANSELA = 0; // Configure PORTA pins as digital
LATA = 0; // Clear PORTA pins
ANSELB = 0; // Configure PORTB pins as digital
LATB = 0; // Clear PORTB pins
ANSELD = 0; // Configure PORTD pins as digital
LATD = 0; // Clear PORTD pins

TRISA = 0xFF00; // Configure pins 0-7 of PORTA as outputs (for LEDs)
TRISB = 0xFFFF; // Configure all pins of PORTB as inputs (for switches)

T1CON = 0; // Clear Timer 1 control register

// Configure Timer 1 with a prescaler of 256 and enable the timer
T1CONbits.TCKPS = 3; // Set prescaler to 256
T1CONbits.TON = 1; // Enable Timer 1

// Set Timer 1 period register to generate an interrupt every 1.45 seconds
PR1 = 36864; // 36864 = 1.45 seconds / (1.36us * 256)

// Clear Timer 1 interrupt flag and enable Timer 1 interrupt
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag
IPC1bits.T1IP = 1; // Set Timer 1 interrupt priority to 1
IEC0bits.T1IE = 1; // Enable Timer 1 interrupt
}

void __attribute__((interrupt, auto_psv)) _T1Interrupt(void){
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag

// Toggle RUN LED
RUN_LED = !RUN_LED;

// Check switch state
if(SWITCH_DOWN){
// Knight Rider pattern from Left to Right
LATA = 0x00FF; // Turn on LEDs D9-D3
delay_ms(100); // Delay to show the pattern
LATA = 0x0000; // Turn off all LEDs
} else {
// Knight Rider pattern from Right to Left
LATA = 0x0080; // Turn on LED D3
delay_ms(100); // Delay to show the pattern
LATA = 0x0000; // Turn off all LEDs
}
}

int main(void) {
setup();

while (1) {
// Infinite loop
}

return 0;
}
```

You can add this code to your project on the Explorer 16 development board and it should implement the desired functionality.

Sure! Here's a short and simple code that implements the desired functionality on the Explorer 16 board using Timer 1 for LED toggling and switch S3 for controlling the direction of the Knight Rider pattern.

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

// Configuration bits
#pragma config FNOSC = PRIPLL // Primary Oscillator (HS+PLL, 4x multiplier)
#pragma config POSCMOD = HS // Primary Oscillator mode (HS oscillator mode)
#pragma config FPLLIDIV = DIV_2 // PLL input divider (Divide by 2)
#pragma config FPLLMUL = MUL_20 // PLL multiplier (Multiply by 20)
#pragma config FPLLODIV = DIV_2 // PLL output divider (Divide by 2)
#pragma config FPBDIV = DIV_1 // Peripheral bus clock divider (Pb_Clk is Sys_Clk/1)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (Watchdog Timer is disabled)
#pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)

// Function prototypes
void initGPIO(void);
void initTimer1(void);
void toggleLed(void);

// Global variables
bool isSwitchOpen = true; // Switch S3 status

// Interrupt service routine for Timer 1
void __attribute__((interrupt, auto_psv)) _T1Interrupt(void)
{
toggleLed();
_T1IF = 0; // Clear Timer 1 interrupt flag
}

// Interrupt service routine for External Interrupt 0 (Switch S3)
void __attribute__((interrupt, auto_psv)) _INT0Interrupt(void)
{
isSwitchOpen = !_RD6; // Update switch status
_INT0IF = 0; // Clear External Interrupt 0 flag
}

int main(void)
{
initGPIO();
initTimer1();

while (1) {
// Wait for switch state to change
while (_RD6 == !isSwitchOpen) {}
}

return 0;
}

// Initialize GPIO pins
void initGPIO(void)
{
TRISDbits.TRISD9 = 0; // Set D9 as output
TRISDbits.TRISD8 = 0; // Set D8 as output
TRISDbits.TRISD7 = 0; // Set D7 as output
TRISDbits.TRISD6 = 1; // Set D6 as input (Switch S3)

TRISDbits.TRISD0 = 0; // Set D0 as output

CNEN2bits.CN22IE = 1; // Enable Change Notification on D6 (Switch S3)

_TRISG2 = 0; // Set RG2 as output (LED D10)
}

// Initialize Timer 1
void initTimer1(void)
{
T1CON = 0; // Stop Timer 1 and clear control register
TMR1 = 0; // Clear Timer 1 counter value
PR1 = 9282; // Set Period register for 1.45s delay at 40MHz

_T1IP = 4; // Set Timer 1 interrupt priority level
_T1IF = 0; // Clear Timer 1 interrupt flag
_T1IE = 1; // Enable Timer 1 interrupt

T1CONbits.TON = 1; // Start Timer 1

_INT0IP = 5; // Set External Interrupt 0 priority level
_INT0IF = 0; // Clear External Interrupt 0 flag
_INT0IE = 1; // Enable External Interrupt 0
}

// Toggle LED D10
void toggleLed(void)
{
_LATG2 ^= 1; // Toggle RG2 (LED D10)

if (isSwitchOpen) {
// Perform Left to Right Knight Rider pattern
_LATD9 ^= 1;
_LATD8 ^= 1;
_LATD7 ^= 1;
_LATD0 ^= 1;
} else {
// Perform Right to Left Knight Rider pattern
_LATD0 ^= 1;
_LATD7 ^= 1;
_LATD8 ^= 1;
_LATD9 ^= 1;
}
}
```

Please note that this code assumes that you are using a PIC24 microcontroller with XC16 compiler. You may need to adjust the code for your specific microcontroller and development environment.

Sure! Here is a short and simple code in C that you can use to implement the given design on Explorer 16:

```c
#include <xc.h>

// Configuration bits settings
#pragma config FOSC = INTIO67 // Internal oscillator block, Port Function on RA6/7
#pragma config WDTEN = OFF // Watchdog Timer disabled

#define _XTAL_FREQ 8000000 // Fosc frequency, used by the __delay_ms() macro

void initGPIO() {
// Set LED pins as output
TRISDbits.TRISD0 = 0; // LED D10
TRISDbits.TRISD3 = 0; // LED D9
TRISDbits.TRISD4 = 0; // LED D8
TRISDbits.TRISD5 = 0; // LED D7
TRISDbits.TRISD6 = 0; // LED D6
TRISDbits.TRISD7 = 0; // LED D5
TRISEbits.TRISE0 = 0; // LED D4
TRISEbits.TRISE1 = 0; // LED D3

// Set switch pins as input
TRISBbits.TRISB0 = 1; // Switch S3
}

void toggleLED_D10() {
LATDbits.LATD0 = !LATDbits.LATD0; // Toggle LED D10
}

void knightRiderPattern(int direction) {
LATDbits.LATD3 = 1; // Turn on first LED (D9)

if (direction == 0) { // Left to Right direction
for (int i = 3; i <= 7; i++) {
__delay_ms(300); // Delay for LED ON time
LATDbits.LATD3 = 0; // Turn off previous LED
LATDbits.LATD = (LATDbits.LATD & 0b11100000) >> 1; // Shift LED pattern to right
__delay_ms(300); // Delay for LED OFF time
LATDbits.LATD |= 1 << i; // Turn on next LED
}
__delay_ms(300); // Delay for LED ON time
LATDbits.LATD3 = 0; // Turn off last LED (D3)
} else { // Right to Left direction
for (int i = 7; i >= 3; i--) {
__delay_ms(300); // Delay for LED ON time
LATDbits.LATD3 = 0; // Turn off previous LED
LATDbits.LATD = (LATDbits.LATD & 0b00011111) << 1; // Shift LED pattern to left
__delay_ms(300); // Delay for LED OFF time
LATDbits.LATD |= 1 << i; // Turn on next LED
}
__delay_ms(300); // Delay for LED ON time
LATDbits.LATD3 = 0; // Turn off last LED (D7)
}
}

void main() {
initGPIO();

while (1) { // Repeat the whole process infinitely
toggleLED_D10(); // Toggle LED D10 every 1.45 seconds

// Check switch S3 state
if (PORTBbits.RB0 == 0)
knightRiderPattern(0); // Left to Right direction
else
knightRiderPattern(1); // Right to Left direction
}
}
```

Please note that this code assumes you are using a PIC microcontroller and have correctly configured the oscillator and other settings in the configuration bits. You might need to adjust the delays and pin connections to match your specific hardware setup.