Question
Implement the design on Explorer 16 as Project 1. LED D10 will be used as the RUN LED indicator.
Knight rider code:
unsigned int count1 = 200;
unsigned int count2 = 200;
bool toggleDirection = true;
bool pressed = false;
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;
}
}
}
Using the knight rider code above, do the following:
Write an simple 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.
Knight rider code:
unsigned int count1 = 200;
unsigned int count2 = 200;
bool toggleDirection = true;
bool pressed = false;
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;
}
}
}
Using the knight rider code above, do the following:
Write an simple 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
#include <xc.h>
// Configuration bits
#pragma config FOSC = HS // High-Speed Crystal Oscillator
#pragma config WDTE = OFF // Watchdog Timer Disabled
#pragma config PWRTE = OFF // Power-Up Timer Disabled
#pragma config BOREN = ON // Brown-Out Reset Enabled
#pragma config LVP = OFF // Low-Voltage Programming Disabled
#define _XTAL_FREQ 20000000 // Crystal Oscillator frequency
// Function prototypes
void initialize(void);
void delay_ms(unsigned int milliseconds);
void toggleRUNLED(void);
// Global variables
volatile bool direction = true;
volatile bool switchPressed = false;
// Interrupt service routine for Timer1
void __interrupt() ISR(void)
{
// Check if Timer1 interrupt flag is set
if (TMR1IF)
{
// Toggle RUN LED
toggleRUNLED();
// Clear Timer1 interrupt flag
TMR1IF = 0;
}
// Check if RB0/INT interrupt flag is set
if (INTF)
{
// Toggle switchPressed flag
switchPressed = !switchPressed;
// Clear RB0/INT interrupt flag
INTF = 0;
}
}
void main(void) {
initialize();
while(1) {
// Right to Left sequence
if (!direction) {
for(int i=1; i<=7; i++) {
LATD = (1 << (i+2));
delay_ms(200);
}
}
// Left to Right sequence
else {
for(int i=7; i>=1; i--) {
LATD = (1 << (i+2));
delay_ms(200);
}
}
// Left to Right sequence if switch is pressed
if (switchPressed) {
LATD = 0x00;
for(int i=7; i>=1; i--) {
LATD = (1 << (i+2));
delay_ms(200);
}
}
}
return -1;
}
void initialize(void)
{
// Configure PORTD as output
TRISD = 0x00;
// Configure PORTB0 as input
TRISB0 = 1;
// Configure INT pin (RB0) for interrupt-on-change
INTEDG = 0; // Interrupt on falling edge (pressing the switch)
INTE = 1; // Enable RB0/INT external interrupt
// Configure Timer1
TMR1CS = 0; // Timer1 clock source is instruction clock (Fosc/4)
T1CKPS1 = 0; // Prescaler value is 1
T1CKPS0 = 0;
TMR1ON = 1; // Enable Timer1
// Configure Timer1 interrupt
TMR1IE = 1; // Enable Timer1 interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts
// Initialize LED patterns
LATD = 0b00011100;
}
void toggleRUNLED(void) {
LATDbits.LATD2 = !LATDbits.LATD2;
}
void delay_ms(unsigned int milliseconds) {
while (milliseconds > 0) {
__delay_ms(1);
milliseconds--;
}
}
// Configuration bits
#pragma config FOSC = HS // High-Speed Crystal Oscillator
#pragma config WDTE = OFF // Watchdog Timer Disabled
#pragma config PWRTE = OFF // Power-Up Timer Disabled
#pragma config BOREN = ON // Brown-Out Reset Enabled
#pragma config LVP = OFF // Low-Voltage Programming Disabled
#define _XTAL_FREQ 20000000 // Crystal Oscillator frequency
// Function prototypes
void initialize(void);
void delay_ms(unsigned int milliseconds);
void toggleRUNLED(void);
// Global variables
volatile bool direction = true;
volatile bool switchPressed = false;
// Interrupt service routine for Timer1
void __interrupt() ISR(void)
{
// Check if Timer1 interrupt flag is set
if (TMR1IF)
{
// Toggle RUN LED
toggleRUNLED();
// Clear Timer1 interrupt flag
TMR1IF = 0;
}
// Check if RB0/INT interrupt flag is set
if (INTF)
{
// Toggle switchPressed flag
switchPressed = !switchPressed;
// Clear RB0/INT interrupt flag
INTF = 0;
}
}
void main(void) {
initialize();
while(1) {
// Right to Left sequence
if (!direction) {
for(int i=1; i<=7; i++) {
LATD = (1 << (i+2));
delay_ms(200);
}
}
// Left to Right sequence
else {
for(int i=7; i>=1; i--) {
LATD = (1 << (i+2));
delay_ms(200);
}
}
// Left to Right sequence if switch is pressed
if (switchPressed) {
LATD = 0x00;
for(int i=7; i>=1; i--) {
LATD = (1 << (i+2));
delay_ms(200);
}
}
}
return -1;
}
void initialize(void)
{
// Configure PORTD as output
TRISD = 0x00;
// Configure PORTB0 as input
TRISB0 = 1;
// Configure INT pin (RB0) for interrupt-on-change
INTEDG = 0; // Interrupt on falling edge (pressing the switch)
INTE = 1; // Enable RB0/INT external interrupt
// Configure Timer1
TMR1CS = 0; // Timer1 clock source is instruction clock (Fosc/4)
T1CKPS1 = 0; // Prescaler value is 1
T1CKPS0 = 0;
TMR1ON = 1; // Enable Timer1
// Configure Timer1 interrupt
TMR1IE = 1; // Enable Timer1 interrupt
PEIE = 1; // Enable peripheral interrupts
GIE = 1; // Enable global interrupts
// Initialize LED patterns
LATD = 0b00011100;
}
void toggleRUNLED(void) {
LATDbits.LATD2 = !LATDbits.LATD2;
}
void delay_ms(unsigned int milliseconds) {
while (milliseconds > 0) {
__delay_ms(1);
milliseconds--;
}
}
There are no human answers yet. A form for humans to post answers is coming very soon!