Simvik

ADC counts to millivolts

Convert a 12-bit ADC reading to a voltage with correct rounding. An easy embedded C problem (Fixed-point and signal processing), run and graded in the browser.

easy · Fixed-point and signal processing · adc · scaling · rounding

A 12-bit ADC returns 0 for 0 V and 4095 for the reference voltage.

Convert raw (0 to 4095) to millivolts for a reference of vref_mv, rounded to the nearest millivolt. Use integer arithmetic only.

Starter code

uint32_t adc_to_mv(uint16_t raw, uint32_t vref_mv) {
  // raw * vref / 4095, rounded to nearest.
  return 0;
}

Example tests

Mid-scale

printf("%u\n", adc_to_mv(2048, 3300));

Expected output:

1650

Full scale

printf("%u\n", adc_to_mv(4095, 3300));

Expected output:

3300

Learn it first

More Fixed-point and signal processing problems