Project#2: RUN Led.

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.
2. Submit reports using and following Project Report Template available in SLATE. The marking scheme and submission procedure is as stated in that template.
3. The deadline, please see the class plan.

To complete this project, you will need the following components and tools:

Hardware:
- Explorer 16 development board
- LED D10
- Switch S3
- Timer 1 module of the Microcontroller

Software:
- MPLAB IDE software
- C programming language

Here is the step-by-step guide to implement the project:

1. Connect LED D10 to the designated pin on the Explorer 16 development board.
2. Connect switch S3 to the designated pin on the Explorer 16 development board.
3. Set up the Timer 1 module of the Microcontroller to generate an interrupt at 1.45 second intervals. You can refer to the Microcontroller's datasheet for detailed instructions on setting up the Timer 1 module.
4. Write a C program that toggles the state of LED D10 in the Timer 1 interrupt service routine (ISR). When the Timer 1 interrupt occurs, check the current state of LED D10 and toggle it accordingly.
5. Implement the Knight Rider pattern using LEDs D9 to D3. If switch S3 is open, iterate through the LEDs from left to right. If switch S3 is closed, iterate through the LEDs from right to left. Use delays between the LED toggling to create the desired effect.
6. Repeat the entire process in an infinite loop.

Remember to follow the specified requirements and guidelines provided in the project description.
Don't forget to document your progress, code, and findings in a project report using the provided Project Report Template. Follow the designated marking scheme and submission procedure to ensure proper evaluation of your project.

Good luck with your project!

To add a RUN LED to your embedded system and implement the desired functionality, follow these steps:

Step 1: Prepare the Hardware
1. Set up the Explorer 16 development board as in Project 1.
2. Connect the RUN LED to D10 on the development board.

Step 2: Write the Application Code
1. Open your favorite Integrated Development Environment (IDE) to write the C code.
2. Create a new C file and include the necessary header files.

```c
#include <xc.h> // Include the XC8 Compiler's built-in library
#include <stdint.h> // Include standard integer types
```

3. Define the configuration bits to configure the microcontroller.

```c
#pragma config FOSC = HSPLL // High-Speed Oscillator with PLL enabled
#pragma config PLLDIV = DIV2 // Divide 8 MHz by 2 for PLL input (4 MHz input to PLL)
#pragma config CPUDIV = OSC1_PLL2// Divide PLL output by 2 (16 MHz CPU clock)
#pragma config FCMEN = OFF // Disable Fail-Safe Clock Monitor
#pragma config IESO = OFF // Disable Oscillator Switchover mode
```

4. Define constants for the LED and switch pins.

```c
#define LED_RUN LATBbits.LATB11
#define LED_KNIGHT_RIDER PORTBbits.RB3
#define SWITCH_S3 PORTDbits.RD6
```

5. Declare global variables for keeping track of timings and LED patterns.

```c
uint16_t timer1OverflowCount = 0;
uint16_t timer1ReloadValue = 0;
uint16_t ledPatternIndex = 0;
uint8_t isLedPatternDirectionLeft = 1;
```

6. Implement the Timer1 interrupt service routine to toggle the RUN LED at the desired interval.

```c
void __interrupt() Timer1_ISR(void)
{
if (PIR1bits.TMR1IF) // Check if Timer1 interrupt flag is set
{
PIR1bits.TMR1IF = 0; // Clear Timer1 interrupt flag

timer1OverflowCount++;

if (timer1OverflowCount >= timer1ReloadValue)
{
timer1OverflowCount = 0;
LED_RUN ^= 1; // Toggle the RUN LED
}
}
}
```

7. Implement the main function.

```c
void main(void)
{
TRISBbits.TRISB11 = 0; // Set RUN LED (D10) as output

// Configure Timer1
T1CONbits.TMR1CS = 0; // Internal clock (Fosc/4)
T1CONbits.T1CKPS = 0b11; // Prescaler 1:8
T1CONbits.TMR1ON = 1; // Enable Timer1
PIR1bits.TMR1IF = 0; // Clear Timer1 interrupt flag
PIE1bits.TMR1IE = 1; // Enable Timer1 interrupt
INTCONbits.GIE = 1; // Enable global interrupts

// Configure switch S3
TRISDbits.TRISD6 = 1; // Set switch S3 (RD6) as input

while (1)
{
// Check switch S3 state to determine LED pattern direction
if (SWITCH_S3 == 0) // Switch S3 is open
isLedPatternDirectionLeft = 1;
else // Switch S3 is closed
isLedPatternDirectionLeft = 0;

// Update and display LED pattern
if (isLedPatternDirectionLeft)
{
if (ledPatternIndex < 7)
ledPatternIndex++;
else
ledPatternIndex = 3; // Start from the leftmost LED
}
else
{
if (ledPatternIndex > 3)
ledPatternIndex--;
else
ledPatternIndex = 7; // Start from the rightmost LED
}

// Display LED pattern
LATB = (LATB & 0b111110001111) | (1 << (ledPatternIndex + 3)); // Update LEDs D9-D3

// Set the Timer1 reload value based on the desired toggle interval of 1.45 seconds
timer1ReloadValue = 0xFFFF - ((uint16_t)(1450000 / (8 * 65536UL)));
}
}
```

Step 3: Compile and Program the Microcontroller
1. Compile the C code using your IDE or build tool.
2. Connect your development board to the computer using a suitable programming tool (e.g., PICkit or MPLAB ICD).
3. Program the microcontroller with the compiled code.

Step 4: Test the Functionality
1. Power up the system and verify that the RUN LED (D10) toggles at the specified 1.45-second interval.
2. Test the Knight Rider LED pattern by opening and closing the switch (S3) to change the pattern direction.
3. Ensure that the LED pattern repeats indefinitely.

These steps should help you add a RUN LED to your embedded system and implement the required functionality. Remember to refer to the project report template for additional requirements and instructions.

To implement Project #2: RUN Led, follow these steps:

1. Hardware Setup:
- Use the same setup as Project 1 on the Explorer 16 board.
- Connect LED D10 as the RUN LED indicator.

2. Application Development:
- Write the application code in C to perform the required tasks.
- Use the Timer 1 module of the microcontroller to toggle the RUN LED at a precise interval of 1.45 seconds using an interrupt mechanism.
- Implement the Knight Rider pattern with 7 LEDs (D9-D3).
- Use switch S3 to control the direction of the pattern (open means Left to Right, closed means Right to Left).

3. Infinite Loop:
- Ensure that the entire process is repeated infinitely.

Note: Make sure to use the provided Project Report Template for reporting and follow the instructions in that template for marking and submission. Check the class plan for the project deadline.

For more details and specific implementation steps, refer to the project requirements and guidelines provided.