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 3.5 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.
Add the 3.5 seconds to this code
#define _XTAL_FREQ 8000000 // Specify the oscillator frequency
unsigned int count1 = 200;
unsigned int count2 = 200;
int main(void){
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input
while(1) {
int i;
int j;
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
delayFunc();
// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}
// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 7; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for(j = 0; j <= 7; j++) {
LATA = (1 << j);
delayFunc();
}
}
}
return -1;
}
void delayFunc(void)
{
int j,k;
int a;
for(j = 0; j < count1; j++)
{
for(k=0; k < count2; k++)
{
a = 0;
}
}
}
1 answer
```c
#define _XTAL_FREQ 8000000 // Specify the oscillator frequency
unsigned int count1 = 200;
unsigned int count2 = 200;
void delayFunc(void);
int main(void){
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input
while(1) {
int i;
int j;
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
__delay_ms(3500); // 3.5 second delay
// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}
// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 7; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for(j = 0; j <= 7; j++) {
LATA = (1 << j);
delayFunc();
}
}
}
return -1;
}
void delayFunc(void)
{
for(int j = 0; j < count1; j++)
{
for(int k=0; k < count2; k++)
{
asm("nop");
}
}
}
```
This code includes the 3.5-second delay using the built-in `__delay_ms()` function in XC8 compiler. Note that the `delayFunc()` is also modified to use an assembly instruction `nop` for the delay loop.