Part A:
Write an application (in C) on the Explorer 16 board 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).
3 answers
```c
#include <p24Fxxxx.h>
#include <xc.h>
#define LEDS_MASK 0xFF00
#define SW3_MASK 0x0100
void delay() {
int i;
for(i = 0; i < 50000; i++) {
// Delay loop
}
}
void init() {
// Configure LATD as output for LEDs
TRISD = 0;
// Configure TRISB15 as input for SW3
TRISB |= 0x8000;
}
int main(void) {
init();
int direction = 1; // 1 for right to left, -1 for left to right
int led = 1; // Mask for current LED
while(1) {
LATD = LEDS_MASK & led; // Turn on only the selected LED
if((PORTB & SW3_MASK) == 0) {
// SW3 is pressed, change direction
direction = -direction;
delay(); // Debounce delay
}
delay(); // Wait for some time
LATD = 0; // Turn off all LEDs
led = led << direction; // Shift the LED mask in the selected direction
if(direction == -1 && led == 0x0100) {
// Reaching leftmost LED, change direction
direction = 1;
} else if(direction == 1 && led == 0x8000) {
// Reaching rightmost LED, change direction
direction = -1;
}
}
return 0;
}
```
Please note that the implementation assumes that the LEDs are connected to the D0-D7 pins of the board and the SW3 switch is connected to the B15 pin. The code sets up the necessary configurations, including using the `LATD` register to control the LED states and the `PORTB` register to read the state of the SW3 switch. The main loop goes through the steps described in Part A, toggling the LEDs one by one in the selected direction and handling the SW3 switch to change the direction if necessary.
To implement Part B) and utilize SW3 to change the direction of the sequence, you would need to modify the code as follows:
```c
#include <p24Fxxxx.h>
#include <xc.h>
#define LEDS_MASK 0xFF00
#define SW3_MASK 0x0100
void delay() {
int i;
for(i = 0; i < 50000; i++) {
// Delay loop
}
}
void init() {
// Configure LATD as output for LEDs
TRISD = 0;
// Configure TRISB15 as input for SW3
TRISB |= 0x8000;
}
int main(void) {
init();
int direction = 1; // 1 for right to left, -1 for left to right
int led = 1; // Mask for current LED
while(1) {
LATD = LEDS_MASK & led; // Turn on only the selected LED
if((PORTB & SW3_MASK) == 0 && direction == 1) {
// SW3 is pressed and direction is right to left
direction = -direction;
delay(); // Debounce delay
}
else if((PORTB & SW3_MASK) != 0 && direction == -1) {
// SW3 is released and direction is left to right
direction = -direction;
delay(); // Debounce delay
}
delay(); // Wait for some time
LATD = 0; // Turn off all LEDs
led = led << direction; // Shift the LED mask in the selected direction
if(direction == -1 && led == 0x0100) {
// Reaching leftmost LED, change direction
direction = 1;
}
else if(direction == 1 && led == 0x8000) {
// Reaching rightmost LED, change direction
direction = -1;
}
}
return 0;
}
```
In this modified code, the SW3 switch is checked in two places: once to change the direction from right to left to left to right (when SW3 is pressed while the direction is right to left), and again to change the direction back from left to right to right to left (when SW3 is released while the direction is left to right). This change ensures that the LED sequence direction can be controlled by the SW3 switch as specified in Part B).