Here's an example of how you can implement the mentioned hardware design and application using C language on the Explorer 16 board with a Microcontroller:
```c
#include <xc.h>
// Function to configure Timer 1
void configureTimer1() {
T1CONbits.TON = 0; // Disable Timer 1
T1CONbits.TCS = 0; // Select internal clock
T1CONbits.TGATE = 0; // Disable gated timer mode
T1CONbits.TCKPS = 0b11; // Set Timer 1 prescaler to 1:256
TMR1 = 0x00; // Clear Timer 1 register
PR1 = 54799; // Set Timer 1 period value for 3.5 seconds
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer 1 interrupt
IPC0bits.T1IP = 0x01; // Set Timer 1 interrupt priority
}
// Timer 1 interrupt service routine
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void) {
LATDbits.LATD10 ^= 1; // Toggle RUN LED (D10)
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag
}
// Function to configure IO ports and pins
void configureIO() {
// Configure LED pins as outputs
TRISDbits.TRISD10 = 0; // RUN LED (D10)
TRISDbits.TRISD9 = 0; // Knight Rider LED 1 (D9)
TRISDbits.TRISD8 = 0; // Knight Rider LED 2 (D8)
TRISDbits.TRISD7 = 0; // Knight Rider LED 3 (D7)
TRISDbits.TRISD6 = 0; // Knight Rider LED 4 (D6)
TRISDbits.TRISD5 = 0; // Knight Rider LED 5 (D5)
TRISDbits.TRISD4 = 0; // Knight Rider LED 6 (D4)
TRISDbits.TRISD3 = 0; // Knight Rider LED 7 (D3)
// Configure Switch pin as input
TRISFbits.TRISF6 = 1; // Switch (S3)
// Set initial LED states
LATDbits.LATD10 = 1; // RUN LED (D10) off
LATDbits.LATD9 = 0; // Knight Rider LED 1 (D9) off
LATDbits.LATD8 = 0; // Knight Rider LED 2 (D8) off
LATDbits.LATD7 = 0; // Knight Rider LED 3 (D7) off
LATDbits.LATD6 = 0; // Knight Rider LED 4 (D6) off
LATDbits.LATD5 = 0; // Knight Rider LED 5 (D5) off
LATDbits.LATD4 = 0; // Knight Rider LED 6 (D4) off
LATDbits.LATD3 = 0; // Knight Rider LED 7 (D3) off
}
// Function to toggle Knight Rider pattern
void toggleKnightRiderPattern() {
// Check switch state
if (PORTFbits.RF6 == 0) {
// Switch (S3) open, Left to Right direction
LATDbits.LATD3 = !LATDbits.LATD3;
__delay_ms(100);
LATDbits.LATD4 = !LATDbits.LATD4;
__delay_ms(100);
LATDbits.LATD5 = !LATDbits.LATD5;
__delay_ms(100);
LATDbits.LATD6 = !LATDbits.LATD6;
__delay_ms(100);
LATDbits.LATD7 = !LATDbits.LATD7;
__delay_ms(100);
LATDbits.LATD8 = !LATDbits.LATD8;
__delay_ms(100);
LATDbits.LATD9 = !LATDbits.LATD9;
__delay_ms(100);
} else {
// Switch (S3) closed, Right to Left direction
LATDbits.LATD9 = !LATDbits.LATD9;
__delay_ms(100);
LATDbits.LATD8 = !LATDbits.LATD8;
__delay_ms(100);
LATDbits.LATD7 = !LATDbits.LATD7;
__delay_ms(100);
LATDbits.LATD6 = !LATDbits.LATD6;
__delay_ms(100);
LATDbits.LATD5 = !LATDbits.LATD5;
__delay_ms(100);
LATDbits.LATD4 = !LATDbits.LATD4;
__delay_ms(100);
LATDbits.LATD3 = !LATDbits.LATD3;
__delay_ms(100);
}
}
// Main function
int main() {
configureIO(); // Configure IO ports and pins
configureTimer1(); // Configure Timer 1
while (1) {
toggleKnightRiderPattern(); // Toggle Knight Rider pattern
// Delay for 3.5 seconds (exact)
T1CONbits.TON = 1; // Enable Timer 1
while (!IFS0bits.T1IF); // Wait for Timer 1 interrupt
T1CONbits.TON = 0; // Disable Timer 1
IFS0bits.T1IF = 0; // Clear Timer 1 interrupt flag
}
return 0;
}
```
In this code, the `configureTimer1()` function is used to configure Timer 1 to generate interrupts every 3.5 seconds. The Timer 1 interrupt service routine will toggle the RUN LED (D10) every time the interrupt occurs.
The `toggleKnightRiderPattern()` function is responsible for toggling the Knight Rider pattern based on the state of the switch (S3). If the switch is open, the pattern moves from left to right, and if the switch is closed, the pattern moves from right to left.
The `while (1)` loop in the `main()` function repeats the process indefinitely, with a delay of 3.5 seconds between each iteration.
Make sure to include the necessary header files for the microcontroller and configure the oscillator and other settings according to your specific requirements.
Note: This code assumes the use of a specific Microcontroller and its associated registers and pins. If you are using a different Microcontroller, you may need to modify the code accordingly.
Hardware:
Implement the design on Explorer 16 as Project 1. LED D10 will be used as the RUN LED indicator.
Application:
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.
1 answer