To implement the requirements, you can follow the steps below:
1. Set up the necessary configurations for the Explorer 16 development board and initialize the required peripherals (GPIO, Timer1).
2. Define and configure the necessary variables and flags.
3. Implement the interrupt service routine (ISR) for Timer1 to toggle the RUN LED.
4. Write the main application loop to handle the Knight Rider pattern and button state.
5. Inside the loop, check the state of the switch (S3) to determine the direction of the pattern.
6. Implement the Knight Rider pattern using a loop and GPIO toggling for the LEDs.
7. Continuously repeat the pattern by wrapping the code in an infinite loop.
Here's an example code snippet in C to demonstrate the implementation:
```c
#include <xc.h>
// Constants
#define LED_RUN LATBbits.LATB10
#define SWITCH_S3 PORTDbits.RD6
// Function prototypes
void initialize(void);
// Global variables
unsigned char ledPattern = 0b0000010; // Initial pattern
unsigned char direction = 1; // Default direction (Left to Right)
// Timer1 interrupt service routine
void __attribute__((interrupt, auto_psv)) _T1Interrupt(void)
{
// Toggle the RUN LED
LED_RUN ^= 1;
// Clear the interrupt flag
IFS0bits.T1IF = 0;
}
int main(void)
{
initialize();
while (1)
{
// Check switch state to change direction
if (SWITCH_S3)
{
direction = 1; // Left to Right
}
else
{
direction = -1; // Right to Left
}
// Update the Knight Rider pattern
ledPattern <<= direction;
if (ledPattern == 0b0000001 || ledPattern == 0b1000000)
{
direction = -direction;
}
// Display the pattern on LEDs (D9-D3)
LATD = (LATD & 0xFF80) | ledPattern;
// Delay for 1.45 seconds
__delay_ms(1450);
}
return 0;
}
void initialize(void)
{
// Configure RUN LED (D10) as output
TRISBbits.TRISB10 = 0;
// Configure LEDS D9-D3 as output
TRISD &= 0xFF80;
// Configure switch S3 as input
TRISDbits.TRISD6 = 1;
// Configure Timer1 for interrupt at every 1.45 seconds
T1CONbits.TCS = 0; // Use internal clock (Fosc/2)
T1CONbits.TCKPS = 0b11; // 1:256 prescaler
PR1 = 15624; // Period for 1.45 seconds (Fosc=16 MHz)
TMR1 = 0; // Clear Timer1
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
// Enable interrupts
INTCON1bits.NSTDIS = 0; // Interrupt nesting enabled
INTCON2bits.GIE = 1; // Enable global interrupts
}
```
Note: The code snippet assumes the use of the XC16 compiler and that the proper header files are included. Make sure to adjust the register names and pin assignments according to your specific microcontroller and development board.
In this project, you are going to add a RUN led to the embedded system that you have designed and developed in project 1. RUN Led is used to indicate continuous running of an electronic system, in general.
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 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.
Note:
1. To toggle the RUN LED, precisely at 1.45 Seconds interval, you need to use the interrupt mechanism for the Timer1.
1 answer