0
0
Embedded Cprogramming~10 mins

Volatile variables in ISR context in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a variable that can change unexpectedly in an ISR.

Embedded C
volatile int [1] = 0;
Drag options to blanks, or click blank then click option'
Aflag
Btemp
Ccounter
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a non-volatile variable for data changed in ISR.
Choosing a variable name unrelated to counting.
2fill in blank
medium

Complete the code to correctly declare a flag variable modified inside an ISR.

Embedded C
volatile [1] flag = 0;
Drag options to blanks, or click blank then click option'
Aint
Bchar
Cfloat
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using floating point types for flags.
Not declaring the variable as volatile.
3fill in blank
hard

Fix the error in the ISR by correctly declaring the variable as volatile.

Embedded C
void ISR_Handler() {
    [1] count;
    count++;
}
Drag options to blanks, or click blank then click option'
Aint
Bconst int
Cstatic int
Dvolatile int
Attempts:
3 left
💡 Hint
Common Mistakes
Using const or static instead of volatile.
Omitting volatile keyword.
4fill in blank
hard

Fill both blanks to declare and use a volatile flag in main and ISR.

Embedded C
volatile int [1] = 0;

void ISR() {
    [2] = 1;
}
Drag options to blanks, or click blank then click option'
Aflag
Bcounter
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in declaration and ISR.
Not declaring the variable as volatile.
5fill in blank
hard

Fill all three blanks to declare a volatile counter, increment it in ISR, and check it in main.

Embedded C
volatile int [1] = 0;

void ISR() {
    [2]++;
}

int main() {
    if ([3] > 0) {
        // handle event
    }
    return 0;
}
Drag options to blanks, or click blank then click option'
Aevent_count
Dcounter
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in different places.
Not declaring the variable as volatile.