Compute ADC Voltage Scale

#include <iostream>
#include <cstdint>

using namespace std;

/**
 * Computes the microvolts per ADC step at compile time.
 * @param maxAdc The maximum digital value (e.g., 4095 for 12-bit).
 * @param maxVoltage_mV The reference voltage in millivolts.
 * @return Truncated integer microvolts per step.
 */
constexpr int32_t computeScale(int32_t maxAdc, int32_t maxVoltage_mV) {
    // Formula: (mV * 1000) / maxAdc
    return (maxVoltage_mV * 1000) / maxAdc;
}

// SCALE_UV is evaluated at compile time. 
// For 3300mV and 4095, this evaluates to 805.
constexpr int32_t SCALE_UV = computeScale(4095, 3300);

int main() {
    int32_t x;
    
    // Read the ADC digital value from input
    if (!(cin >> x)) return 0;

    // Ensure x is within the valid 12-bit range for safety
    if (x < 0 || x > 4095) {
        return 0;
    }

    // Runtime conversion using the pre-computed scale
    int32_t voltage_uv = x * SCALE_UV;

    // Output the result in microvolts
    cout << voltage_uv << endl;

    return 0;
}

Solving Approach

 

 

 

 

 

Upvote
Downvote
Loading...

Input

0

Expected Output

0