Car window motor: set the driver's register
Enable the motor and set its speed field without disturbing the register's other settings. An easy embedded C problem (Bit manipulation), run and graded in…
easy · Bit manipulation · masks · fields · read-modify-write · product
Product: Car window-lift motor driver (Automotive)
Requirement. The window-lift motor driver chip has one 8-bit control register:
| Bits | Meaning |
|---|---|
| 7 | BRAKE: 1 holds the motor shorted |
| 5–4 | SPEED: 0 (slowest) to 3 (fastest) |
| 3 | ENABLE: 1 drives the motor |
| 6, 2–0 | other settings (current limit, direction): never change them |
Return the new register value from the current one. Always write the SPEED field (a speed above 3 means 3). When enabling, also clear BRAKE, or the driver fights its own brake. When disabling, clear only ENABLE.
Starter code
uint8_t motor_set(uint8_t reg, bool enable, uint8_t speed) {
return enable ? (uint8_t)(1 << 3 | speed << 4) : 0;
}Example tests
Enable at speed 2, then stop
printf("0x%02X 0x%02X\n", motor_set(0x00, true, 2), motor_set(0x28, false, 2));Expected output:
0x28 0x20The other settings survive
printf("0x%02X\n", motor_set(0x41, true, 1));Expected output:
0x59Learn it first
- Bit manipulation · Masks, shifts, fields and the idioms every register access is built from.
More Bit manipulation problems
- Set a bit · Turn on one bit of a register value and leave the rest alone.
- Clear a bit · Turn off one bit of a register value.
- Toggle a bit · Flip one bit of a 32-bit value.
- Test a bit · Report whether one bit of a 32-bit value is set.
- Write a bit · Force one bit to a given state.
- Extract a bit field · Read a field of a given width and position out of a register value.
- Write a bit field · Replace a multi-bit field inside a register value.
- Count set bits · Count how many bits of a 32-bit value are 1.
- Parity of a byte · Compute the parity bit a UART would send with a byte.
- Power of two · Decide whether a value is a power of two without a loop.
- Lowest set bit · Find the index of the least significant set bit.
- Highest set bit · Find the index of the most significant set bit.
- Reverse the bits of a byte · Mirror a byte so bit 7 becomes bit 0.
- Swap nibbles · Exchange the high and low halves of a byte.
- Rotate left · Rotate a 32-bit value left by any amount.
- Swap byte order · Reverse the four bytes of a 32-bit value.
- Build a bit mask · Create a mask with a contiguous range of bits set.
- Round up to a power of two · Find the smallest power of two that is not less than a value.
- Decode a Gray code · Convert a reflected Gray code back to binary.
- Interleave two bytes · Spread the bits of two bytes into alternating positions of a 16-bit value.
- Fix: the ready check fires too early · A check that two status flags are both set passes when only one of them is.
- Fix: reading a whole register as one field faults · A field extractor works for every field but the full 32-bit one, where its shift is undefined.