Question
Implement the design on Explorer 16. 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 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.
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.
Answers
Answered by
GPT 3.5
Here is an example implementation in C language using the PIC24F Microcontroller and MPLABX IDE:
```c
#include <xc.h>
// Configuration settings
#pragma config FNOSC = FRCDIV // Internal Fast RC oscillator with divided clock
#pragma config FWDTEN = OFF // Disable Watchdog Timer
#pragma config FSOSCEN = OFF // Disable secondary oscillator
#pragma config JTAGEN = OFF // Disable JTAG port
// Function prototypes
void initTimer1(void);
void toggleRunLED(void);
void initLEDs(void);
void initSwitch(void);
// Global variables
unsigned char knightRiderPattern[7] = {0b0000001, 0b0000010, 0b0000100, 0b0001000, 0b0010000, 0b0100000, 0b1000000}; // LED patterns for Knight Rider
unsigned char currentPatternIndex = 0; // Current index of the pattern array
unsigned char direction = 1; // Direction flag for Knight Rider pattern
unsigned int runLEDPeriod = 44100; // 1.45 seconds period for RUN LED
unsigned int runLEDCounter = 0; // Counter for RUN LED toggling
unsigned int timer1InterruptCounter = 0; // Counter for Timer1 interrupts
int main(void) {
// Initialize peripherals
initTimer1();
initLEDs();
initSwitch();
while (1) {
// Knight Rider pattern
if (direction) {
LATB = knightRiderPattern[currentPatternIndex];
currentPatternIndex++;
if (currentPatternIndex >= 7) {
currentPatternIndex = 0;
}
} else {
LATB = knightRiderPattern[currentPatternIndex];
if (currentPatternIndex == 0) {
currentPatternIndex = 6;
} else {
currentPatternIndex--;
}
}
// RUN LED toggling
if (runLEDCounter >= runLEDPeriod) {
toggleRunLED();
runLEDCounter = 0;
}
}
return 0;
}
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void) {
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
timer1InterruptCounter++;
runLEDCounter++;
if (timer1InterruptCounter >= 303) { // 303 Timer1 interrupts = 44100 cycles (1.45 seconds)
toggleRunLED();
timer1InterruptCounter = 0;
}
}
void initTimer1(void) {
T1CONbits.TON = 0; // Disable Timer1
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TCKPS = 0b00; // Set Timer1 prescale value to 1:1
TMR1 = 0; // Clear Timer1 register
PR1 = 303; // Set period for 1.45 seconds
IPC0bits.T1IP = 1; // Set Timer1 interrupt priority to 1
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Enable Timer1
}
void toggleRunLED(void) {
LATAbits.LATA0 ^= 1; // Toggle RUN LED (D10)
}
void initLEDs(void) {
TRISAbits.TRISA0 = 0; // Set RUN LED (D10) pin as output
TRISB = 0x0000; // Set all PORTB pins as output
LATB = 0x0000; // Clear all PORTB pins
}
void initSwitch(void) {
TRISBbits.TRISB4 = 1; // Set switch (S3) pin as input
CNPU2bits.CN22PUE = 1; // Enable internal pull-up resistor for switch (S3) pin
}
```
```c
#include <xc.h>
// Configuration settings
#pragma config FNOSC = FRCDIV // Internal Fast RC oscillator with divided clock
#pragma config FWDTEN = OFF // Disable Watchdog Timer
#pragma config FSOSCEN = OFF // Disable secondary oscillator
#pragma config JTAGEN = OFF // Disable JTAG port
// Function prototypes
void initTimer1(void);
void toggleRunLED(void);
void initLEDs(void);
void initSwitch(void);
// Global variables
unsigned char knightRiderPattern[7] = {0b0000001, 0b0000010, 0b0000100, 0b0001000, 0b0010000, 0b0100000, 0b1000000}; // LED patterns for Knight Rider
unsigned char currentPatternIndex = 0; // Current index of the pattern array
unsigned char direction = 1; // Direction flag for Knight Rider pattern
unsigned int runLEDPeriod = 44100; // 1.45 seconds period for RUN LED
unsigned int runLEDCounter = 0; // Counter for RUN LED toggling
unsigned int timer1InterruptCounter = 0; // Counter for Timer1 interrupts
int main(void) {
// Initialize peripherals
initTimer1();
initLEDs();
initSwitch();
while (1) {
// Knight Rider pattern
if (direction) {
LATB = knightRiderPattern[currentPatternIndex];
currentPatternIndex++;
if (currentPatternIndex >= 7) {
currentPatternIndex = 0;
}
} else {
LATB = knightRiderPattern[currentPatternIndex];
if (currentPatternIndex == 0) {
currentPatternIndex = 6;
} else {
currentPatternIndex--;
}
}
// RUN LED toggling
if (runLEDCounter >= runLEDPeriod) {
toggleRunLED();
runLEDCounter = 0;
}
}
return 0;
}
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void) {
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
timer1InterruptCounter++;
runLEDCounter++;
if (timer1InterruptCounter >= 303) { // 303 Timer1 interrupts = 44100 cycles (1.45 seconds)
toggleRunLED();
timer1InterruptCounter = 0;
}
}
void initTimer1(void) {
T1CONbits.TON = 0; // Disable Timer1
T1CONbits.TCS = 0; // Select internal instruction cycle clock
T1CONbits.TCKPS = 0b00; // Set Timer1 prescale value to 1:1
TMR1 = 0; // Clear Timer1 register
PR1 = 303; // Set period for 1.45 seconds
IPC0bits.T1IP = 1; // Set Timer1 interrupt priority to 1
IFS0bits.T1IF = 0; // Clear Timer1 interrupt flag
IEC0bits.T1IE = 1; // Enable Timer1 interrupt
T1CONbits.TON = 1; // Enable Timer1
}
void toggleRunLED(void) {
LATAbits.LATA0 ^= 1; // Toggle RUN LED (D10)
}
void initLEDs(void) {
TRISAbits.TRISA0 = 0; // Set RUN LED (D10) pin as output
TRISB = 0x0000; // Set all PORTB pins as output
LATB = 0x0000; // Clear all PORTB pins
}
void initSwitch(void) {
TRISBbits.TRISB4 = 1; // Set switch (S3) pin as input
CNPU2bits.CN22PUE = 1; // Enable internal pull-up resistor for switch (S3) pin
}
```
There are no human answers yet. A form for humans to post answers is coming very soon!