All submissions

Macro-Based Register Config Helper

Code


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

#define SET_ENABLE(val)   ((val & 0x01) << 0)
#define SET_MODE(val)     ((val & 0x03) << 1)
#define SET_SPEED(val)    ((val & 0x07) << 3)

uint16_t build_register(uint8_t enable, uint8_t mode, uint8_t speed) {
    uint16_t reg = 0;
    reg |= SET_ENABLE(enable);
    reg |= SET_MODE(mode);
    reg |= SET_SPEED(speed);
    // RESERVED bits (6–7) are left as 0
    return reg;
}

int main() {
    uint8_t enable, mode, speed;
    scanf("%hhu %hhu %hhu", &enable, &mode, &speed);
    printf("%u\n", build_register(enable, mode, speed));
    return 0;
}

Solving Approach

🧠 Solving Approach in 4 Clear Steps

  1. Understand the Register Layout
    • Break down each field: number of bits, starting position, and constraints (e.g., reserved bits must be 0).
    • Example: MODE is 2 bits starting at bit 1 → mask = 0x03, shift = << 1.
  2. Define Bitfield Macros
    • Use masking and shifting to isolate and position each field:

      #define SET_MODE(val) ((val & 0x03) << 1)
      
    • This ensures only valid bits are used and placed correctly.
  3. Pack the Register Value
    • Use bitwise OR (|) to combine all fields into a single register:

      reg |= SET_ENABLE(enable) | SET_MODE(mode) | SET_SPEED(speed);
      
  4. Validate Reserved Bits
    • Ensure reserved bits are untouched or explicitly cleared:

      reg &= ~(0x03 << 6);  // Clear bits 6–7
      

🔧 Embedded Best Practices

  • Always use & masks to avoid overflow or invalid values.
  • Use const or macros for field widths and positions to improve maintainability.
  • Comment each macro with its field name and bit position.
  • For complex registers, consider using bitfield structs (with caution for portability).

This approach works whether you're configuring TCCR0A, ADCSRA, or a custom peripheral register. Want to see how this maps to real datasheet specs or auto-generate macros from a register map? I can help with that too!

 

 

Loading...

Input

1 2 4

Expected Output

37