Design and Implementation of a High-Precision STM32 Digital Power Supply
Architecture and System Capabilities
This digital power supply is a high-performance laboratory instrument built on the STM32 platform. It provides adjustable Constant Voltage (CV) and Constant Current (CC) outputs with integrated safety mechanisms including Over-Voltage (OVP), Over-Current (OCP), and Over-Temperature (OTP) protection. The control logic utilizes a PID algorithm executed on an STM32 microcontroller to drive a synchronous Buck converter, ensuring stable output and high efficiency.
Hardware Implementation
1. Power Stage (Synchronous Buck)
The system uses a synchronuos Buck converter to step down the input voltage. Unlike traditional Buck converters, the synchronous approach replaces the freewheeling diode with a MOSFET (IRF540N) to minimize conduction losses. An inductor (typically 33μH) and high-quality capacitors filter the output to maintain low ripple.
2. Feedback and Sensing
To achieve closed-loop control, the MCU requires precise real-time data:
- Voltage Sensing: A precision resistor divider (100kΩ and 10kΩ) scales the output voltage down to the 0-3.3V range suitable for the STM32 ADC.
- Current Sensing: An INA240 high-side current sense amplifier monitors the drop across a 0.01Ω shunt resistor. With a fixed gain of 50V/V, it provides high noise immunity and accuracy.
Software Control Logic
PID Algorithm Implementation
The following code defines the structure and calculation for the dual-mode PID controller. This implementation includes anti-windup logic for the integral term.
typedef struct {
float gainP;
float gainI;
float gainD;
float target;
float currentVal;
float errSum;
float lastErr;
float outLimitMax;
float outLimitMin;
} PID_State;
float ComputePID(PID_State *p) {
float error = p->target - p->currentVal;
// Proportional term
float pTerm = p->gainP * error;
// Integral term with anti-windup
p->errSum += error;
if (p->errSum > p->outLimitMax) p->errSum = p->outLimitMax;
else if (p->errSum < p->outLimitMin) p->errSum = p->outLimitMin;
float iTerm = p->gainI * p->errSum;
// Derivative term
float dTerm = p->gainD * (error - p->lastErr);
p->lastErr = error;
float result = pTerm + iTerm + dTerm;
// Output saturation
if (result > p->outLimitMax) result = p->outLimitMax;
else if (result < p->outLimitMin) result = p->outLimitMin;
return result;
}
ADC Data Processing
Converting raw ADC values to physical units requires accounting for the reference voltage and hardware scaling factors.
#define ADC_REF_VOLT 3.3f
#define ADC_RES 4095.0f
#define VOLT_DIVIDER_RATIO 11.0f // (100k+10k)/10k
#define CURRENT_SHUNT_RES 0.01f
#define AMP_GAIN 50.0f
float RawToVoltage(uint16_t rawValue) {
float vSense = (rawValue / ADC_RES) * ADC_REF_VOLT;
return vSense * VOLT_DIVIDER_RATIO;
}
float RawToCurrent(uint16_t rawValue) {
float vSense = (rawValue / ADC_RES) * ADC_REF_VOLT;
return (vSense / AMP_GAIN) / CURRENT_SHUNT_RES;
}
System Task Scheduling
The application follows a background-foregruond architecture. A 1ms timer interrupt handles the high-priority PID calculations and PWM updates, while the main loop manages the UI and communication.
void Timer_Callback_1ms(void) {
float actV, actI;
SampleHardware(&actV, &actI);
if (operationMode == MODE_CV) {
voltPID.target = userSetVolt;
voltPID.currentVal = actV;
uint16_t duty = (uint16_t)ComputePID(&voltPID);
UpdatePWM(duty);
// Automatic crossover to CC mode
if (actI > userSetCurr * 1.02f) {
operationMode = MODE_CC;
}
} else {
currPID.target = userSetCurr;
currPID.currentVal = actI;
uint16_t duty = (uint16_t)ComputePID(&currPID);
UpdatePWM(duty);
// Return to CV mode if voltage drops below threshold
if (actV < userSetVolt * 0.98f) {
operationMode = MODE_CV;
}
}
}
System Calibration and Tuning
Accurate performance depends on properly tuned PID parameters and sensor calibration.
Safety Mechanisms
The firmware continuously monitors the system state to prevent hardware damage:
- Over-Voltage Protection: If the sampled voltage exceeds the set limit by 5%, the PWM output is immediately disabled.
- Thermal Management: An NTC thermistor monitors the heat sink. If the temperature exceeds 80°C, the system reduces the current limit or shuts down entirely.
- Short-Circuit Protection: A hardware comparator triggers an external interrupt on the STM32 to pull the PWM signal low in the event of a direct short, bypassing the software loop for faster response.