A Controller is Essentially a Comparator: Understanding the STM32G473 Comparator and DAC

A controller, at its core, functions as a comparator. It continuously monitors input signals, comparing them against a predetermined setpoint or threshold. When the input exceeds the threshold, the controller triggers a corresponding action. This fundamental principle applies across various control systems, from simple thermostats to complex industrial automation. This article delves into a specific application of this concept using the STM32G473 microcontroller’s internal comparator and DAC, highlighting a common challenge and exploring potential solutions.

Comparator Configuration and Unexpected Behavior

The STM32G473’s COMP1 is configured with DAC3-CH1 as the negative input and PA1, set to analog mode, as the positive input. An interrupt is triggered via EXTI Line 21 when the comparator output changes state. The goal is to set a voltage threshold using DAC3 and trigger an interrupt when the voltage at PA1 reaches or exceeds this threshold.

However, testing reveals an unexpected offset in the comparator’s switching threshold. For instance, when the DAC output is set to 400mV, the comparator switches when the voltage at PA1 reaches only 100mV, exhibiting a 300mV offset. This offset varies with the threshold, as illustrated in the graph below.

The blue line represents the expected switching threshold set by the DAC, while the orange line represents the actual voltage at PA1 when the comparator switches. This discrepancy indicates that the comparator consistently switches at a voltage lower than the intended threshold. This behavior has been replicated across multiple microcontrollers, suggesting a systematic issue rather than a component defect.

Potential Causes of the Comparator Offset

Several factors could contribute to the observed offset in the comparator’s switching behavior:

Input Offset Voltage:

Comparators inherently possess an input offset voltage, a small voltage difference between the inputs required to produce a zero output. This inherent offset could contribute to the observed discrepancy.

DAC Accuracy and Resolution:

The DAC’s accuracy and resolution play a crucial role in setting the precise threshold voltage. Limitations in these parameters could introduce errors in the threshold setting, leading to the observed offset.

External Voltage Reference:

The accuracy and stability of the external 2.048V voltage reference used for the DAC can directly impact the accuracy of the generated threshold voltage. Fluctuations or inaccuracies in the reference voltage can translate into errors in the comparator’s switching point.

Analog Input Pin Characteristics:

The analog input pin PA1 might have its own input impedance and bias current, potentially affecting the voltage level presented to the comparator.

Hysteresis:

While hysteresis is currently set to zero, a small amount of hysteresis might be beneficial in preventing spurious switching due to noise.

Investigating and Resolving the Issue

To pinpoint the root cause and resolve the offset issue, consider the following steps:

  • Verify Voltage Reference: Confirm the accuracy and stability of the external voltage reference using a high-precision multimeter.

  • Measure DAC Output Directly: While challenging due to the internal nature of DAC3, explore alternative methods to directly measure the DAC output voltage and verify its accuracy. For example, use a high impedance probe.

  • Characterize Input Offset Voltage: Determine the comparator’s input offset voltage using a known input voltage and observing the output switching point. This will help quantify its contribution to the overall offset.

  • Adjust for Input Offset Voltage: If the input offset voltage is significant, consider compensating for it in software by adjusting the DAC output value accordingly.

  • Implement Hysteresis: Introduce a small amount of hysteresis to improve noise immunity and potentially mitigate spurious switching.

  • Consult Datasheet: Thoroughly review the STM32G473 datasheet for specific details regarding the comparator and DAC characteristics, including offset voltage, accuracy, and resolution. This information can provide valuable insights into the observed behavior.

By systematically investigating these potential causes and implementing appropriate solutions, the comparator’s accuracy can be improved, ensuring its reliable operation as the core of the control system. Understanding that A Controller Is Essentially A Comparator allows for a more focused approach to troubleshooting and optimization.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *