51. Compute ADC Voltage Scale

A microcontroller reads an analog input using a 12-bit ADC with a digital output range of 0–4095.

In embedded firmware, ADC readings are commonly converted into physical units using fixed-point arithmetic to avoid floating-point overhead and to ensure deterministic behavior. A common pattern is to compute a scale factor at compile time and apply it at runtime using integer math.

Your task is to compute an ADC-to-microvolts scale factor at compile time using a constexpr function, and then use it at runtime to convert an ADC reading into microvolts (µV).

Task Requirements

  1. Create a constexpr function named computeScale.
    • It must compute microvolts per ADC count using the formula:

      (maxVoltage_mV * 1000) / maxAdc 
    • The computation must use integer division, meaning the result is truncated.
  2. Define a constexpr int32_t constant named SCALE_UV by calling:

    computeScale(4095, 3300)
    
  3. Read a 32-bit integer input x, representing the ADC digital value.
  4. Compute the voltage in microvolts using:

    voltage_uv = x * SCALE_UV
    
  5. Print the computed voltage in microvolts.

 

Input Format

  • A single 32-bit integer x representing the ADC digital value

Output Format

  • A single 32-bit integer representing the voltage in microvolts (µV)

 

Example 1

Input:

1000 

Output:

805000

 

Example 2

Input:

4095 

Output:

3296475

 

Constraints

  • 0 ≤ x ≤ 4095
  • maxAdc = 4095
  • maxVoltage_mV = 3300 (3.3 V reference)
  • All calculations must use 32-bit fixed-width integers (int32_t)
  • No floating-point math (float, double) is allowed
  • SCALE_UV must be evaluated at compile time

 

 

 

 

 

Loading...

Input

0

Expected Output

0