Project#1:

In this project, you are going to design and develop a simple embedded Application to exploring interfacing through input/output ports. This project has two parts described as below:
Hardware:
In this project, you will be using the Explorer 16 development board. Specifically, you will be using the 8 LEDs on the board to generate a Knight Rider Pattern on those LEDs. Please, check Explorer16.pdf for pin connections.
Part A:
Write an application (in C) which implement the sequence as below:
1. Make all the LEDs OFF.
2. Make LED D3 (Right most) to be ON .
3. After a while (a delay of your choice), make next (to the Left) LED to be ON.
4. After a while, make the next LED to be ON. Repeat this process until all the 8 LEDs (from Right most to LEFT most) becomes ON.
5. Repeat the above processes (1-4), this time, starting from LEFT most LED and walk to the RIGHT most.
6. Repeat the above sequence (1-5) infinitely.

Part B:
Utilize SW3 of the explorer 16 board to change the direction of the Knight Rider Pattern designed in Part A. Any time, if SW3 is pushed down and hold, the above pattern starts from LEFT to RIGHT and when SW3 is released, the pattern walks from RIGHT to LEFT (as in Part A).

Make two seperate codes for Part A and Part B.

Here is an example code for Part A:

```c
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#pragma config WDT = OFF

#define _XTAL_FREQ 8000000

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
__delay_ms(200); // Delay between LEDs turning on
}

// Left to Right sequence
for(int i=6; i>=0; i--) {
LATA = (1 << i);
__delay_ms(200); // Delay between LEDs turning on
}
}

return;
}
```

And here is an example code for Part B:

```c
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>

#pragma config WDT = OFF

#define _XTAL_FREQ 8000000

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
__delay_ms(200); // Delay between LEDs turning on
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD7 == 0) {
for(int i=6; i>=0; i--) {
LATA = (1 << i);
__delay_ms(200); // Delay between LEDs turning on
}
}
}

return;
}
```

can you make part a simpler, please?

Certainly! Here is a simplified version of Part A code:

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

#pragma config WDT = OFF

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
__delay_ms(200); // Delay between LEDs turning on
}

// Left to Right sequence
for(int i=6; i>=0; i--) {
LATA = (1 << i);
__delay_ms(200); // Delay between LEDs turning on
}
}

return;
}
```

This simplified code does not include unnecessary header files and variables, making it easier to understand and follow.

can you make part b simpler, please?

Certainly! Here is a simplified version of Part B code:

```c
#include <xc.h>

#pragma config WDT = OFF

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
__delay_ms(200); // Delay between LEDs turning on
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD7 == 0) {
for(int i=6; i>=0; i--) {
LATA = (1 << i);
__delay_ms(200); // Delay between LEDs turning on
}
}
}

return;
}
```

This simplified code does not include unnecessary header files and comments, making it easier to understand and follow.

for the delay, can you make the main program call the delay

Certainly! Here is an example where the `__delay_ms()` function is called within the main loop:

```c
#include <xc.h>

#pragma config WDT = OFF

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

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delay_ms(200); // Delay between LEDs turning on
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD7 == 0) {
for(int i=6; i>=0; i--) {
LATA = (1 << i);
delay_ms(200); // Delay between LEDs turning on
}
}
}

return;
}
```

In this example, a new function `delay_ms()` is defined, which takes the desired delay time in milliseconds as an argument. The function uses a simple loop to call `__delay_ms(1)` for the specified number of milliseconds. This allows the main program to call `delay_ms()` and provide a delay value directly without needing to modify the `__delay_ms()` function itself.

Is this code correct for part a)?

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence
for(int i=6; i>=0; i--) {
LATA = (1 << i);
delayFunc();
}
}

return;
}

void delayFunc(void)
{
for(i = 0; i <100; i++)
{
for(j=0; j < 20; j++)
{
k=j;
}
}

}

The code you provided is almost correct. However, there are a couple of issues that need to be addressed:

1. You need to declare the variables `i`, `j`, and `k` in the `delayFunc()` function. Add the following line at the beginning of the `delayFunc()` function:
```c
int i, j, k;
```

2. In the outer loop of the `delayFunc()` function, the condition `i < 100` should be changed to `i < 1000` to achieve a longer delay.
```c
for(i = 0; i < 1000; i++)
```

Here's the corrected code for Part A:

```c
#include <xc.h>

#pragma config WDT = OFF

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

for(i = 0; i < 1000; i++)
{
for(j=0; j < 20; j++)
{
k = j;
}
}
}

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence
for(int i=6; i>=0; i--) {
LATA = (1 << i);
delayFunc();
}
}

return;
}
```

This modified code should work correctly and generate the desired Knight Rider pattern. Note that the `delayFunc()` function provides a delay by looping for a certain number of iterations. Adjust the loops and counts as needed to achieve the desired delay duration.

is this correct for part b)?

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD7 == 0) {
for(int i=6; i>=0; i--) {
LATA = (1 << i);
delayFunc();
}
}
}

return;
}

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

for(i = 0; i < 1000; i++){
for(j=0; j < 20; j++){
k = j;
}
}
}

The code you provided for Part B is almost correct. However, there is one issue that needs to be addressed:

In the condition `if(PORTDbits.RD7 == 0)`, make sure to check the state of SW3 by comparing it to `1`, not `0`. Since SW3 is active-low (pressed when the corresponding pin is logic low), the condition should be `if(PORTDbits.RD7 == 1)`.

Here's the corrected code for Part B:

```c
#include <xc.h>

#pragma config WDT = OFF

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

for(i = 0; i < 1000; i++){
for(j=0; j < 20; j++){
k = j;
}
}
}

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD7 == 1) {
for(int i=6; i>=0; i--) {
LATA = (1 << i);
delayFunc();
}
}
}

return;
}
```

This modified code should work correctly, generating the Knight Rider pattern that changes direction based on the state of SW3. Remember to adjust the timing in the `delayFunc()` function as needed to achieve the desired delay duration.

are you sure about part b?

Apologies for the confusion earlier. You are correct, a correction is needed for Part B of the code. Here is the updated and corrected code for Part B:

```c
#include <xc.h>

#pragma config WDT = OFF

void delayFunc(void) {
for(int i = 0; i < 1000; i++) {
for(int j = 0; j < 20; j++) {
asm("NOP");
}
}
}

void main(void) {
TRISA = 0x00;
LATA = 0x00;

int direction = 0; // 0: Right to Left, 1: Left to Right

while(1) {
// Right to Left sequence
if(direction == 0) {
for(int i = 0; i < 8; i++) {
LATA = (1 << i);
delayFunc();
}
direction = 1;
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD7 == 0) {
direction = 0;
}

// Left to Right sequence
if(direction == 1) {
for(int i = 6; i >= 0; i--) {
LATA = (1 << i);
delayFunc();
}
}

// Right to Left sequence if SW3 is pressed
if(PORTDbits.RD7 == 0) {
direction = 1;
}
}

return;
}
```

In this updated code, a new variable `direction` is introduced to keep track of the current direction of the Knight Rider pattern. The variable is initially set to 0 (Right to Left). If the SW3 button is pressed, the direction is changed accordingly. Now, the pattern starts from Right to Left, and if SW3 is pressed, the direction changes to Left to Right. Note that the `delayFunc()` function uses an inner `NOP` (No operation) assembly instruction for the delay, providing a more accurate and consistent timing than the previous version.

Is my code correct for part A)?

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i=0; i<8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence
for(int i=6; i>=0; i--) {
LATA = (1 << i);
delayFunc();
}
}

return;
}

void delayFunc(void)
{
for(i = 0; i <100; i++)
{
for(j=0; j < 20; j++)
{
k=j;
}
}

}

There is an issue with your code for Part A. The `delayFunc()` function references variables `i`, `j`, and `k`, which are not declared within the function. You need to declare these variables within the `delayFunc()` function.

Here is the corrected code:

```c
void delayFunc(void)
{
int i, j, k;

for(i = 0; i < 100; i++)
{
for(j = 0; j < 20; j++)
{
k = j;
}
}
}

void main(void) {
TRISA = 0x00;
LATA = 0x00;

while(1) {
// Right to Left sequence
for(int i = 0; i < 8; i++) {
LATA = (1 << i);
delayFunc();
}

// Left to Right sequence
for(int i = 6; i >= 0; i--) {
LATA = (1 << i);
delayFunc();
}
}

return;
}
```

In this corrected code, the variables `i`, `j`, and `k` are declared within the `delayFunc()` function, allowing the function to compile without errors. The rest of the code remains the same.