Implement the design on Explorer 16 as Project 1. LED D10 will be used as the RUN LED indicator.

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.

I need help in making the RUN LED(D10) toggle at every 3.5 Seconds (exact) interval using one of the Timer (Timer 1) module of the Microcontroller.

unsigned int count1 = 200;
unsigned int count2 = 200;

int main(void){
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

int i;
int j;

while(1) {
// Toggle RUN LED (D10) every 3.5 seconds using Timer1
LATDbits.LATD0 = !LATDbits.LATD0;
delayFunc();

// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 6; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for( j = 0; j <= 7; 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;
}
}
}

1 answer

To implement the RUN LED toggling at every 3.5 seconds using Timer1, you will need to configure and utilize Timer1 interrupts. Here's an updated version of the code to achieve this:

```c
#include <xc.h>

// Function Declarations
void Timer1_Init(void);
void delayFunc(void);

// Global Variables
unsigned int count1 = 200;
unsigned int count2 = 200;
unsigned int timerCount = 0;
bit isToggle = 0;

void __interrupt(high_priority) HighIsr(void) {
if (TMR1IF) {
TMR1IF = 0; // Clear Timer1 interrupt flag

// Check if 3.5 seconds have passed
if (timerCount >= 350) {
LATAbits.LATA0 = !LATAbits.LATA0; // Toggle RUN LED (D10)
timerCount = 0; // Reset timer count
}

timerCount++;
}
}

int main(void) {
ANSELA = 0x00; // Configure AN0-AN7 pins as digital I/O
TRISA = 0x00; // Set PORTA as output
TRISDbits.TRISD6 = 1; // Set SW3 input

Timer1_Init(); // Initialize Timer1 for 1 ms interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts

int i;
int j;

while(1) {
// Knight Rider pattern from Left to Right direction
for(i = 0; i <= 6; i++) {
LATA = (1 << i);
delayFunc();
}

// Knight Rider pattern: Left to Right or Right to Left depending on SW3 state
if (PORTDbits.RD6 == 0) {
for(j = 6; j >= 0; j--) {
LATA = (1 << j);
delayFunc();
}
} else {
for( j = 0; j <= 7; j++) {
LATA = (1 << j);
delayFunc();
}
}
}

return -1;
}

void Timer1_Init(void) {
// Set Timer1 to operate in Timer mode, prescaler 1:8, internal clock source
T1CONbits.TSIDL = 0; // Continue operation in Idle mode
T1CONbits.TGATE = 0; // Disable Timer1 gated time accumulation
T1CONbits.TCKPS = 0b10; // Set Timer1 prescaler to 1:8
T1CONbits.TCS = 0; // Use internal clock source
TMR1 = 0; // Initialize Timer1 register to 0
PR1 = 17500; // Set Timer1 period to achieve 1 ms interrupt
TMR1IE = 1; // Enable Timer1 interrupt
TMR1ON = 1; // Turn on Timer1
}

void delayFunc(void) {
int j,k;
int a;

for(j = 0; j < count1; j++) {
for(k=0; k < count2; k++) {
a = 0;
}
}
}
```

In this code, Timer1 is initialized with a prescaler of 1:8, which means the timer will increment every 1 μs. The Timer1 period register (PR1) is set to 17500 to achieve an interrupt every 1 ms. In the interrupt handler, a variable "timerCount" is used to keep track of the time passed, and when it reaches 350 (indicating 3.5 seconds), the RUN LED (D10) is toggled and the timerCount is reset.

Make sure to update your configuration bits and include the necessary header files for your specific microcontroller.