Implement the design on Explorer 16 as Project 1. LED D10 will be used as the RUN LED indicator.

Knight rider code:
unsigned int count1 = 200;
unsigned int count2 = 200;

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

while(1) {
int i;
int j;

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

// Left to Right sequence
for(j=7; j>=-1; j--) {
LATA |= (1<<j);
delayFunc();
}

// Left to Right sequence if SW3 is pressed
if(PORTDbits.RD6 == 0) {
LATA = 0x00;
for(j=8; j>=-1; 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;
}
}

}

Using the knight rider code above, do the following:
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.
4. To toggle the RUN LED, precisely at 1.45 Seconds interval, you need to use the interrupt mechanism for the Timer1.

1 answer

#include <xc.h>

// Configuration bits
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select bit (MCLR pin function is reset)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3/PGM pin has digital I/O function, HV on MCLR must be used for programming)

#define _XTAL_FREQ 20000000 // Define oscillator frequency

unsigned int count = 0; // Variable to count Timer1 overflows

void interrupt ISR(void) // Timer1 interrupt service routine
{
if (TMR1IF) // Check if Timer1 overflow interrupt flag is set
{
TMR1IF = 0; // Clear Timer1 overflow interrupt flag

if (count == 3624) // 1.45 seconds has passed
{
count = 0; // Reset the count variable

LATAbits.LATA4 ^= 1; // Toggle the RUN LED (D10)
}
else
{
count++; // Increment the count variable
}
}
}

void main(void)
{
// Set the RUN LED (D10) as output
TRISAbits.TRISA4 = 0;
// Set the Knight Rider LEDs (D9-D3) as outputs
TRISDbits.TRISD0 = 0;
TRISDbits.TRISD1 = 0;
TRISDbits.TRISD2 = 0;
TRISDbits.TRISD3 = 0;
TRISDbits.TRISD4 = 0;
TRISDbits.TRISD5 = 0;
TRISDbits.TRISD6 = 0;

// Initialize the Knight Rider LEDs (D9-D3) off
LATDbits.LATD0 = 0;
LATDbits.LATD1 = 0;
LATDbits.LATD2 = 0;
LATDbits.LATD3 = 0;
LATDbits.LATD4 = 0;
LATDbits.LATD5 = 0;
LATDbits.LATD6 = 0;

// Configure Timer1
T1CONbits.TMR1CS = 0; // Timer1 clock source is internal instruction clock (Fosc/4)
T1CONbits.T1CKPS = 0b11; // Timer1 prescaler is 1:8
T1CONbits.TMR1ON = 1; // Enable Timer1
TMR1IE = 1; // Enable Timer1 overflow interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts

while(1)
{
// Right to Left sequence if switch S3 is open
if (PORTDbits.RD6 == 0)
{
LATDbits.LATD0 = 1;
__delay_ms(200);
LATDbits.LATD1 = 1;
__delay_ms(200);
LATDbits.LATD2 = 1;
__delay_ms(200);
LATDbits.LATD3 = 1;
__delay_ms(200);
LATDbits.LATD4 = 1;
__delay_ms(200);
LATDbits.LATD5 = 1;
__delay_ms(200);
LATDbits.LATD6 = 1;
__delay_ms(200);
LATDbits.LATD6 = 0;
__delay_ms(200);
LATDbits.LATD5 = 0;
__delay_ms(200);
LATDbits.LATD4 = 0;
__delay_ms(200);
LATDbits.LATD3 = 0;
__delay_ms(200);
LATDbits.LATD2 = 0;
__delay_ms(200);
LATDbits.LATD1 = 0;
__delay_ms(200);
LATDbits.LATD0 = 0;
__delay_ms(200);
}
// Left to Right sequence if switch S3 is closed
else
{
LATDbits.LATD6 = 1;
__delay_ms(200);
LATDbits.LATD5 = 1;
__delay_ms(200);
LATDbits.LATD4 = 1;
__delay_ms(200);
LATDbits.LATD3 = 1;
__delay_ms(200);
LATDbits.LATD2 = 1;
__delay_ms(200);
LATDbits.LATD1 = 1;
__delay_ms(200);
LATDbits.LATD0 = 1;
__delay_ms(200);
LATDbits.LATD0 = 0;
__delay_ms(200);
LATDbits.LATD1 = 0;
__delay_ms(200);
LATDbits.LATD2 = 0;
__delay_ms(200);
LATDbits.LATD3 = 0;
__delay_ms(200);
LATDbits.LATD4 = 0;
__delay_ms(200);
LATDbits.LATD5 = 0;
__delay_ms(200);
LATDbits.LATD6 = 0;
__delay_ms(200);
}
}
}