In this project, you are going to add a RUN led to the embedded system that you have designed and developed in project 1. RUN Led is used to indicate continuous running of an electronic system, in general.

Hardware:
Implement the design on Explorer 16 as Project 1. LED D10 will be used as the RUN LED indicator.
Application:
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.
Note:
1. 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 very simple code?

Sure! Here's a simple code that achieves the required functionality:

```c
#include <xc.h>

// Configuration bits
#pragma config FOSC = INTOSCIO // Internal oscillator
#pragma config WDTE = OFF // Watchdog timer disabled
#pragma config PWRTE = OFF // Power-up timer disabled
#pragma config MCLRE = ON // MCLR pin is enabled
#pragma config CP = OFF // Code protection disabled
#pragma config CPD = OFF // Data code protection disabled
#pragma config BOREN = OFF // Brown-out reset disabled
#pragma config IESO = OFF // Internal/External Switchover mode disabled
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor disabled

// Function prototypes
void initialize();
void toggleRUNLed();
void toggleKnightRiderLed();

// Global variables
volatile unsigned int timerOverflowCount = 0;
unsigned char knightRiderDirection = 1; // 1 for right-to-left, 0 for left-to-right

void __interrupt() isr() {
if (TMR1IF) {
TMR1IF = 0;
TMR1 = 15536; // For 1.45 sec delay with 1 MHz oscillator and prescaler 1:8
timerOverflowCount++;
}
}

void main() {
initialize();

while (1) {
toggleKnightRiderLed();

if (timerOverflowCount >= 9) { // 1.45 sec (9 overflow counts) has passed
toggleRUNLed();
timerOverflowCount = 0;
}
}
}

void initialize() {
// Configure oscillator
OSCCONbits.IRCF = 0b111; // Select 8 MHz internal oscillator
OSCCONbits.SCS = 0b00; // Use oscillator defined by FOSC

// Configure I/O
TRISD = 0b00000000; // PORTD as output
TRISEbits.TRISE0 = 1; // RE0 as input

// Enable global and peripheral interrupts
INTCON = 0b11000000; // GIE and PEIE enabled

// Configure Timer1
T1CON = 0b00000001; // Prescaler 1:8
TMR1 = 15536; // For 1.45 sec delay with 1 MHz oscillator and prescaler 1:8
TMR1IF = 0; // Clear Timer1 overflow interrupt flag
TMR1IE = 1; // Enable Timer1 overflow interrupt
}

void toggleRUNLed() {
PORTDbits.RD0 = ~PORTDbits.RD0; // Toggle RD0 (D10) LED
}

void toggleKnightRiderLed() {
if (knightRiderDirection) { // Right-to-left
PORTD <<= 1;
if (PORTDbits.RD9) {
PORTDbits.RD2 = 1; // Activate the rightmost LED (D2)
}
} else { // Left-to-right
PORTD >>= 1;
if (PORTDbits.RD2) {
PORTDbits.RD9 = 1; // Activate the leftmost LED (D9)
}
}

// Change direction if switch S3 is pressed
if (PORTEbits.RE0) { // Switch S3 is pressed
knightRiderDirection = !knightRiderDirection; // Toggle direction
}

// Delay for LED visibility
for (unsigned int i = 0; i < 8000; i++) {
for (unsigned int j = 0; j < 10; j++) {
__asm__("NOP");
}
}
}
```

Please note that the code assumes that you are using a Microchip PIC microcontroller and MPLAB X IDE with XC8 compiler. Also, make sure you have correctly connected the LEDs and switches to the appropriate pins on the microcontroller board.

Sure! Here is a simple code that implements the mentioned functionality:

```c
#include <xc.h>

// Configuration bits
#pragma config FNOSC = FRC // Oscillator Selection Bits (Fast RC Oscillator)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT disabled)

#define _XTAL_FREQ 8000000 // FRC frequency is set to 8MHz

// Function prototypes
void setup();
void toggleRunLed();
void toggleKnightRiderPattern();

// Global variables
unsigned char knightRiderDirection = 1; // 1 = Left to Right, 0 = Right to Left

// Interrupt Service Routine for Timer1
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void)
{
// Toggle the RUN LED
toggleRunLed();

// Reload Timer1 register with necessary value for 1.45 seconds delay
TMR1 = 0xBDC;

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

int main()
{
// Setup the hardware configuration
setup();

// Main loop
while(1)
{
// Check the status of switch S3
if(S3 == 1)
knightRiderDirection = 1; // Left to Right
else
knightRiderDirection = 0; // Right to Left

// Toggle the Knight Rider pattern
toggleKnightRiderPattern();
}

return 0;
}

// Function to setup the hardware configuration
void setup()
{
// Set the I/O ports direction
TRISDbits.TRISD0 = 0; // RUN LED Pin (Output)
TRISD |= 0xFE; // Knight Rider LED Pins (Output)
TRISDbits.TRISD9 = 1; // S3 Pin (Input)

// Setup Timer1
T1CONbits.TON = 0; // Turn off Timer1
T1CONbits.TCS = 0; // Internal clock source (Fcy)
T1CONbits.TGATE = 0; // Gated time accumulation is disabled
T1CONbits.TCKPS = 0b11; // Prescaler value is 256
TMR1 = 0xBDC; // Initial value for 1.45 seconds delay
PR1 = 0xFFFF; // Period register to maximum value
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Turn on Timer1
}

// Function to toggle the RUN LED
void toggleRunLed()
{
LATDbits.LATD0 = !LATDbits.LATD0; // Toggle the LED state
}

// Function to toggle the Knight Rider pattern
void toggleKnightRiderPattern()
{
if(knightRiderDirection == 1)
{
// Left to Right pattern
LATD = (LATD << 1) | (LATD >> 6);
}
else
{
// Right to Left pattern
LATD = (LATD >> 1) | (LATD << 6);
}

__delay_ms(200); // Delay for smoother pattern movement
}
```

Please note that this code assumes the necessary header files are included and that the Explorer 16 development board is properly set up with the required connections and configuration bits. The code uses Timer1 interrupt to toggle the RUN LED at precise intervals and the switch S3 to control the direction of the Knight Rider pattern.

Certainly! Here's a simplified code that achieves the requirements mentioned:

```c
#include <xc.h>

// Define the configuration bits
#pragma config FOSC = INTOSC // Internal oscillator
#pragma config WDTE = OFF // Watchdog Timer disabled
#pragma config PWRTE = OFF // Power-up Timer disabled
#pragma config MCLRE = ON // MCLR pin is enabled
#pragma config CP = OFF // Code protection disabled
#pragma config LVP = OFF // Low voltage programming disabled
#pragma config BOREN = OFF // Brown-out Reset disabled
#pragma config CLKOUTEN = OFF // CLKOUT function disabled

// Define constants for timer
#define _XTAL_FREQ 4000000 // Set the oscillator frequency

// Function prototypes
void setup();
void toggleRunLed();
void knightRider();

// Global variables
unsigned char runLedState = 0; // Represents the state of the RUN LED (0 or 1)

void main() {
// Call setup function to initialize the system
setup();

// Infinite loop
while(1) {
// Toggle the RUN LED every 1.45 seconds
toggleRunLed();

// Execute the Knight Rider pattern
knightRider();
}
}

void setup() {
// Set the oscillator frequency
OSCCONbits.IRCF = 0b110; // 4MHz

// Configure Timer 1 for interrupt
T1CONbits.TMR1CS = 0; // Set Timer 1 clock source as Fosc/4
T1CONbits.T1CKPS = 0b10; // Set Timer 1 prescaler to 1:4
T1CONbits.T1SYNC = 0; // External clock not synchronized
TMR1H = 0x7C; // Initial value for Timer 1 high byte
TMR1L = 0x00; // Initial value for Timer 1 low byte
PIR1bits.TMR1IF = 0; // Clear Timer 1 interrupt flag
PIE1bits.TMR1IE = 1; // Enable Timer 1 interrupt
INTCONbits.PEIE = 1; // Enable peripheral interrupts
INTCONbits.GIE = 1; // Enable global interrupts

// Set PORTC as output
TRISC = 0x00;

// Set initial states for ports
PORTCbits.RC0 = 0; // Set RUN LED initially off
PORTCbits.RC1 = 0; // Set LED D9 initially off
// ...
PORTCbits.RC7 = 0; // Set LED D3 initially off
}

void toggleRunLed() {
// Toggle the RUN LED
runLedState = !runLedState;
PORTCbits.RC0 = runLedState;
}

void knightRider() {
// Read the state of switch S3
unsigned char switchState = PORTBbits.RB0;

// Set the direction
if(switchState == 0) { // Switch is open
// Left to right direction
PORTC = 0b11111100;
__delay_ms(250); // Delay for 250ms
}
else { // Switch is closed
// Right to left direction
PORTC = 0b01111110;
__delay_ms(250); // Delay for 250ms
}
}
```

This code assumes that you are using a Microchip PIC microcontroller and the XC8 compiler. It sets up the oscillator frequency, configures Timer 1 for interrupt, sets PORTC as output, and initializes the initial states for the ports.

The `toggleRunLed()` function is called in the main loop to toggle the state of the RUN LED at the specified interval of 1.45 seconds.

The `knightRider()` function checks the state of switch S3 and sets the direction for the Knight Rider pattern accordingly. If the switch is open, the pattern goes from left to right; if it is closed, the pattern goes from right to left.

Note: This code assumes that the necessary register definitions and clock frequency settings are already included in the `xc.h` header file for your specific microcontroller. You may need to modify the code based on your specific hardware configuration and microcontroller.