0
0
Embedded Cprogramming~20 mins

Writing to a hardware register in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hardware Register Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this hardware register write simulation?

Consider this embedded C code simulating writing to a hardware register. What value will be stored in REG after execution?

Embedded C
volatile unsigned int REG = 0x00;

void write_register(unsigned int value) {
    REG = value | 0x0F;
}

int main() {
    write_register(0xF0);
    return REG;
}
A0xF0
B0xFF
C0x0F
D0x00
Attempts:
2 left
💡 Hint

Remember the bitwise OR operation sets bits that are set in either operand.

🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes volatile keyword in hardware register access?

Why is the volatile keyword important when declaring hardware registers in embedded C?

AIt allows the variable to be shared between multiple threads.
BIt makes the variable read-only to protect hardware registers.
CIt initializes the variable to zero automatically.
DIt tells the compiler the variable can change unexpectedly, preventing optimization.
Attempts:
2 left
💡 Hint

Think about how hardware registers can change outside program control.

🔧 Debug
advanced
2:30remaining
Why does this hardware register write code cause a runtime error?

Examine the code below. It attempts to write to a hardware register but causes a runtime error. What is the cause?

Embedded C
unsigned int *REG = NULL;

void write_register(unsigned int value) {
    *REG = value;
}

int main() {
    write_register(0xAA);
    return 0;
}
ADereferencing a NULL pointer causes a runtime error.
BThe value 0xAA is invalid for hardware registers.
CMissing volatile keyword causes undefined behavior.
DThe function write_register is missing a return statement.
Attempts:
2 left
💡 Hint

Check the pointer initialization before dereferencing.

📝 Syntax
advanced
2:00remaining
Which option correctly writes 0x55 to a hardware register at address 0x4000?

Choose the correct C code snippet to write the value 0x55 to a hardware register located at memory address 0x4000.

A*(volatile unsigned int *)0x4000 = 0x55;
Bvolatile unsigned int *reg = 0x4000; *reg = 0x55;
C*(unsigned int 0x4000) = 0x55;
Dunsigned int reg = (volatile unsigned int *)0x4000; reg = 0x55;
Attempts:
2 left
💡 Hint

Remember how to cast an address to a pointer and write through it.

🚀 Application
expert
3:00remaining
How many bits are set in the hardware register after this write?

Given the following code, how many bits are set to 1 in the REG after execution?

Embedded C
volatile unsigned char REG = 0x00;

void set_bits(unsigned char mask) {
    REG |= mask;
}

int main() {
    set_bits(0x0F);  // sets lower 4 bits
    set_bits(0x33);  // sets bits 0,1,4,5
    return 0;
}
A4
B5
C6
D7
Attempts:
2 left
💡 Hint

Count the bits set after both OR operations.