Implement the design on Explorer 16. 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.

Where can I put the 3.5 seconds in the code below?

#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

You can add a delay of 3.5 seconds using Timer1 in the code by configuring Timer1 to generate an interrupt every 3.5 seconds and toggling the RUN LED in the interrupt service routine (ISR). Here is an updated version of the code with the Timer1 interrupt added:

```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

// Configure Timer1 for 3.5 seconds interrupt
T1CON = 0b00110001; // Timer1 with 1:8 prescaler, internal clock source
TMR1 = 3036; // Initialize Timer1 to overflow after 3.5 seconds
PIE1bits.TMR1IE = 1; // Enable Timer1 overflow interrupt
INTCONbits.PEIE = 1; // Enable peripheral interrupts
INTCONbits.GIE = 1; // Enable global interrupts

while(1) {
int i;
int j;
// The RUN LED (D10) will be toggled in the Timer1 ISR

// 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 __interrupt() isr(void) {
if (PIR1bits.TMR1IF) {
// Toggle the RUN LED (D10)
LATDbits.LATD0 = !LATDbits.LATD0;

// Reset Timer1
TMR1 = 3036;
PIR1bits.TMR1IF = 0; // Clear the interrupt flag
}
}

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

for(j = 0; j < count1; j++) {
for(k=0; k < count2; k++) {
a = 0;
}
}
}
```

In this code, I have added the Timer1 configuration and interrupt service routine (`isr`) to toggle the RUN LED (D10) every 3.5 seconds. The ISR is triggered when Timer1 overflows, which occurs after 3.5 seconds (assuming the oscillator frequency is set to 8 MHz as specified by `_XTAL_FREQ`). The delayFunc function remains unchanged and is used for generating delays in the LED patterns.

Note: Make sure to enable the Timer1 interrupt by setting the `GIE` (Global Interrupt Enable) and `PEIE` (Peripheral Interrupt Enable) bits in the `INTCON` register.