0
0
Embedded Cprogramming~20 mins

Direction register vs data register in Embedded C - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Direction and Data 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 embedded C code snippet?

Consider a microcontroller port with a direction register (DDR) and a data register (PORT). The DDR controls pin direction (1 for output, 0 for input). The PORT register controls output value if the pin is output, or enables pull-up resistor if input.

What will be the output on the port pins after running this code?

Embedded C
#include <stdint.h>
#include <stdio.h>

uint8_t DDR = 0x00;  // Direction register
uint8_t PORT = 0x00; // Data register

int main() {
    DDR = 0b00001111;  // Lower 4 pins output, upper 4 pins input
    PORT = 0b10101010; // Set alternating bits

    // Simulate reading port pins
    uint8_t pin_values = PORT & DDR; // Output pins show PORT value
    printf("Pin values: 0x%02X\n", pin_values);
    return 0;
}
APin values: 0x0F
BPin values: 0x0A
CPin values: 0xAA
DPin values: 0x00
Attempts:
2 left
💡 Hint

Remember that only pins set as output in DDR show the PORT value on pins.

🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes the difference between DDR and PORT registers?

Choose the correct description of the roles of the Direction Register (DDR) and Data Register (PORT) in microcontroller I/O ports.

ADDR and PORT both set output values but for different pins.
BDDR sets output value; PORT sets pin direction.
CDDR sets pin direction; PORT sets output value or pull-up resistor depending on DDR.
DDDR enables interrupts; PORT controls analog input levels.
Attempts:
2 left
💡 Hint

Think about what controls if a pin is input or output, and what controls the voltage level.

🔧 Debug
advanced
2:30remaining
Why does this code not set the output pins correctly?

The code below tries to set pins 0-3 as outputs and set them high, but the output pins remain low. What is the cause?

Embedded C
#include <stdint.h>
#include <stdio.h>

uint8_t DDR = 0x00;
uint8_t PORT = 0x00;

int main() {
    DDR = 0b00001111; // Set pins 0-3 as output
    PORT = 0b00001111; // Set pins 0-3 high

    // Simulate reading pins
    uint8_t pins = PORT & DDR;
    printf("Pins: 0x%02X\n", pins);
    return 0;
}
AThe PORT register must be set before DDR to take effect.
BThe pins variable should be DDR & ~PORT to read output pins.
CThe code should use |= instead of = to set bits in DDR and PORT.
DThe DDR and PORT variables are not linked to actual hardware registers, so setting them has no effect.
Attempts:
2 left
💡 Hint

Think about what DDR and PORT represent in real embedded systems versus this simulation.

📝 Syntax
advanced
1:30remaining
What error does this code produce?

Analyze the following embedded C code snippet. What error will it produce when compiled?

Embedded C
#include <stdint.h>

uint8_t DDR = 0x00;
uint8_t PORT = 0x00;

int main() {
    DDR = 0b00001111;
    PORT = 0b10101010;
    return 0;
}
ASyntaxError: missing semicolon after DDR assignment
BTypeError: invalid binary literal
CNo error, code compiles successfully
DRuntime error: uninitialized variable usage
Attempts:
2 left
💡 Hint

Check each line carefully for missing punctuation.

🚀 Application
expert
3:00remaining
How many pins are configured as output and set high after this code?

Given the following code, how many pins are configured as output and set to high voltage?

Embedded C
#include <stdint.h>

uint8_t DDR = 0b11001100;
uint8_t PORT = 0b10101010;

int main() {
    uint8_t output_pins = DDR & PORT;
    int count = 0;
    for (int i = 0; i < 8; i++) {
        if ((output_pins >> i) & 1) {
            count++;
        }
    }
    return count;
}
A2
B3
C4
D5
Attempts:
2 left
💡 Hint

Count bits set to 1 in DDR and PORT at the same positions.