Sure! Here is a simple code that implements the mentioned functionality:
```c
#include <xc.h>
// Configuration bits
#pragma config FNOSC = FRC // Oscillator Selection Bits (Fast RC Oscillator)
#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT disabled)
#define _XTAL_FREQ 8000000 // FRC frequency is set to 8MHz
// Function prototypes
void setup();
void toggleRunLed();
void toggleKnightRiderPattern();
// Global variables
unsigned char knightRiderDirection = 1; // 1 = Left to Right, 0 = Right to Left
// Interrupt Service Routine for Timer1
void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void)
{
// Toggle the RUN LED
toggleRunLed();
// Reload Timer1 register with necessary value for 1.45 seconds delay
TMR1 = 0xBDC;
// Clear Timer1 interrupt flag
IFS0bits.T1IF = 0;
}
int main()
{
// Setup the hardware configuration
setup();
// Main loop
while(1)
{
// Check the status of switch S3
if(S3 == 1)
knightRiderDirection = 1; // Left to Right
else
knightRiderDirection = 0; // Right to Left
// Toggle the Knight Rider pattern
toggleKnightRiderPattern();
}
return 0;
}
// Function to setup the hardware configuration
void setup()
{
// Set the I/O ports direction
TRISDbits.TRISD0 = 0; // RUN LED Pin (Output)
TRISD |= 0xFE; // Knight Rider LED Pins (Output)
TRISDbits.TRISD9 = 1; // S3 Pin (Input)
// Setup Timer1
T1CONbits.TON = 0; // Turn off Timer1
T1CONbits.TCS = 0; // Internal clock source (Fcy)
T1CONbits.TGATE = 0; // Gated time accumulation is disabled
T1CONbits.TCKPS = 0b11; // Prescaler value is 256
TMR1 = 0xBDC; // Initial value for 1.45 seconds delay
PR1 = 0xFFFF; // Period register to maximum value
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Turn on Timer1
}
// Function to toggle the RUN LED
void toggleRunLed()
{
LATDbits.LATD0 = !LATDbits.LATD0; // Toggle the LED state
}
// Function to toggle the Knight Rider pattern
void toggleKnightRiderPattern()
{
if(knightRiderDirection == 1)
{
// Left to Right pattern
LATD = (LATD << 1) | (LATD >> 6);
}
else
{
// Right to Left pattern
LATD = (LATD >> 1) | (LATD << 6);
}
__delay_ms(200); // Delay for smoother pattern movement
}
```
Please note that this code assumes the necessary header files are included and that the Explorer 16 development board is properly set up with the required connections and configuration bits. The code uses Timer1 interrupt to toggle the RUN LED at precise intervals and the switch S3 to control the direction of the Knight Rider pattern.
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.
Can you make a very simple code?
1 answer