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 simple 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.
Can you make a simple code, please?
1 answer
```c
#include <xc.h>
// Function prototypes
void initTimer1(void);
void initSwitch(void);
void initLEDs(void);
void main(void) {
initTimer1();
initSwitch();
initLEDs();
while (1) {
// Check if switch is pressed
if (PORTDbits.RD6 == 0) {
LATB = 0x1C; // Right to Left sequence (D9-D3 LEDs)
} else {
LATB = 0xE0; // Left to Right sequence (D9-D3 LEDs)
}
__delay_ms(100); // Delay between LED changes
}
return -1;
}
void initTimer1(void) {
T1CON = 0x8030; // Timer1 enabled, prescaler 1:256
TMR1 = 0; // Reset Timer1 value
PR1 = 46062; // Set PR1 value for 1.45 seconds delay
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Start Timer1
}
void initSwitch(void) {
TRISDbits.TRISD6 = 1; // Set RD6 (S3) as input
CNENDbits.CNIED6 = 1; // Enable change notification for RD6
IEC1bits.CNDIE = 1; // Enable change notification interrupt
IPC8bits.CNIP = 1; // Set change notification interrupt priority
IFS1bits.CNDIF = 0; // Clear change notification interrupt flag
}
void initLEDs(void) {
TRISB = 0x00; // Set port B as output
LATB = 0x00; // Turn off all LEDs initially
}
// Timer1 interrupt handler
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
LATAbits.LATA0 = !LATAbits.LATA0; // Toggle RUN LED (D10)
}
// Change notification interrupt handler
void __attribute__((interrupt, no_auto_psv)) _CNInterrupt(void) {
IFS1bits.CNDIF = 0; // Clear change notification interrupt flag
__delay_ms(50); // Debounce delay
}
```
Please note that this code assumes you're using the PIC24FJ256GB206 microcontroller, as in the Explorer 16 board. The pin assignments and register names may vary for different microcontrollers. Make sure to adjust them accordingly if you're using a different microcontroller.