Simvik

ADC conversion sequence

Write a 5-bit channel number into a computed slot of a register. A medium embedded C problem (Registers and memory-mapped I/O), run and graded in the browser.

medium · Registers and memory-mapped I/O · fields · adc

An ADC sequence register holds six channel numbers of 5 bits each. Slot k (0 to 5) occupies bits 5k + 4 down to 5k.

Store channel in slot slot of *sqr. Use only the low 5 bits of channel and leave the other slots unchanged.

Starter code

void adc_set_sequence(volatile uint32_t *sqr, uint8_t slot, uint8_t channel) {
  // Slot k is the 5-bit field starting at bit 5 * k.
}

Example tests

Channel 9 in slot 0

uint32_t sqr = 0;
adc_set_sequence(&sqr, 0, 9);
printf("0x%08X\n", sqr);

Expected output:

0x00000009

Channel 17 in slot 2

uint32_t sqr = 0;
adc_set_sequence(&sqr, 2, 17);
printf("0x%08X\n", sqr);

Expected output:

0x00004400

Learn it first

  • Registers, volatile and const · How C talks to hardware: memory-mapped registers, what volatile and const promise, register structs and read-modify-write.

More Registers and memory-mapped I/O problems