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.
4. To toggle the RUN LED, precisely at 1.45 Seconds interval, you need to use the interrupt mechanism for the Timer1.
Using the code below, do the the above.
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;
}
}
}
Make a very short and simple code?
1 answer
```c
#include <xc.h>
void __interrupt() Timer1Interrupt(void) {
if (TMR1IF) {
TMR1IF = 0; // Clear Timer1 interrupt flag
LATDbits.LATD0 = !LATDbits.LATD0; // Toggle RUN LED (D10)
}
}
void main(void) {
TRISD = 0b01111111; // Set D7 as output
LATD = 0x00; // Initialize LEDs to off state
// Configure Timer1
T1CONbits.TMR1ON = 0; // Turn off Timer1
T1CONbits.T1CKPS = 0b11; // Set Timer1 prescaler to 1:8
T1CONbits.T1OSCEN = 0; // Disable Timer1 oscillator
T1CONbits.TMR1CS = 0; // Set Timer1 clock source to internal clock
TMR1 = 0; // Clear Timer1 register
TMR1IE = 1; // Enable Timer1 interrupt
PEIE = 1; // Enable peripheral interrupt
GIE = 1; // Enable global interrupt
T1CONbits.TMR1ON = 1; // Turn on Timer1
while(1) {
unsigned int i;
// Right to Left sequence
for(i = 0; i < 8; i++) {
LATD = (1 << i);
__delay_ms(250);
}
// Left to Right sequence
for(i = 6; i >= 0; i--) {
LATD = (1 << i);
__delay_ms(250);
}
// Left to Right sequence if SW3 is pressed
if (PORTDbits.RD6 == 0) {
LATD = 0;
for(i = 0; i < 7; i++) {
LATD = (1 << i);
__delay_ms(250);
}
}
}
return;
}
```
This code sets up Timer1 to generate an interrupt every 1.45 seconds and toggle the RUN LED (D10) in the interrupt handler. The knight rider pattern is implemented using a simple for loop and GPIO manipulation. The code also checks if switch S3 (connected to RD6) is pressed and changes the direction of the knight rider pattern accordingly.