Simvik

Scan the multiplexer channels

Convert each enabled ADC channel in turn from the conversion-complete interrupt. A medium embedded C problem (ADC), run and graded in the browser.

medium · ADC · adc · interrupts · multiplexer

A microcontroller has one ADC behind a multiplexer: it converts one channel at a time. Firmware scans several by switching the multiplexer in the conversion-complete interrupt.

  • scan_start(s) selects the lowest enabled channel and returns it, or returns 0xFF if no channel is enabled.
  • scan_done(s, result) stores result for the channel just converted, moves on to the next enabled channel (wrapping from 7 back to 0), and returns it.

Starter code

uint8_t scan_start(scan_t *s) {
  s->current = 0;
  return 0;
}

uint8_t scan_done(scan_t *s, uint16_t result) {
  s->value[s->current] = result;
  s->current++;
  return s->current;
}

Example tests

Channels 0, 1 and 3

scan_t s = { 0x0B };
run(&s, 5);

Expected output:

0 1 3 0 1 3

Results land in the right channel

scan_t s = { 0x05 };
run(&s, 3);
printf("%u %u\n", s.value[0], s.value[2]);

Expected output:

0 2 0 2
300 200

Learn it first

  • ADC · Resolution, sampling, noise, oversampling and DMA.

More ADC problems