Skip to main content
Associate
October 2, 2026
Solved

About comparators in STM32G474.

I am enquiring about comparators in STM32G474.

For COMP4, it says that the INP can be connected to PB0 and INM can be connected to DAC1_CH1. But when PB0=1.65V and PA4(which is the output of DAC1_CH1)=3.15V , the VALUE bit in COMP_CSR is 1, which should be 0. I measured in from nucleo board and they are exactly the values I expected. And VALUE bit remains 1 when I changed the DAC1_CH1 to other values.

It makes me wonder if there is an issue in the manual. It seems that DAC1 is not really connected to COMP4.

Thank you!

mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
October 6, 2026

Hello,

I’ve just created a project with CubeMx with the same config as yours:

COMP4_INP (PB0): input connected to an external power supply.

COMP4_INM: input from DAC1_CH1_OUT.

DAC1_CH1_OUT (PA4) is monitored with an oscilloscope as well as COMP4_INP (PB0).

The result is as the following:

When the voltage on COMP4_INP > DAC1_CH1_OUT: COMP_CSR.VALUE = 1

When the voltage on COMP4_INP < DAC1_CH1_OUT: COMP_CSR.VALUE = 0

→ There is no issue with the documentation nor with the HAL nor with the hardware.

It seems the issue is in your bare metal code.

I’ve attached a project for your reference that you can test to confirm my saying.

In the project, the on board green LED turns on when COMP_CSR.VALUE=1 and it turns off when COMP_CSR.VALUE=0.

In next time, I recommend to start with HAL/CubeMx then move on to the bare metal coding.

Please confirm if it worked for you.

Hope that helps.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.

6 replies

mƎALLEm
ST Technical Moderator
October 2, 2026

Hello ​@xiaoyu wang and welcome to the ST community,

Please attach your project you’ve used for this test.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Associate
October 2, 2026

The code related to the COMP4 and DAC1 configuration is as follows:

/* overcurrent protection configuration

* current limiting, max 3A (3.5A max for inductor,3A max for ADC) */

//Enable DAC1 clock

RCC->AHB2ENR |= (uint32_t) 0x10000U;

//DAC_out=V_ref*DOR/4096=1.65(offset)+1.5(3A in reality,gain for IN240A1=20,3*(25e-3)*20=1.5)=3.15,V_ref=3.3V

DAC1->DHR12R1 = (uint32_t)(12902.0f/3.3f);

//DAC enable

DAC1->CR |= (uint32_t)0x1U;

for(volatile int i=0; i<10000; i++);

//COMP4 configuration

RCC->APB2ENR |= (uint32_t)0x1U;

/* Select DAC1 CH1 as negative input

* Non-inverting input selection (PB0 over-current protection) for positive input

* inverted output(make enable bit in gate driver work)

* Enable comparator */

COMP4->CSR = (uint32_t)0x8051U;

waclawek.jan
Super User
October 2, 2026

Measuring PA4 shows the expected voltage?

Read out and check/post the relevant COMP and GPIO registers’ content.

JW

Associate
October 5, 2026

Yes, PA4 and PB0 all shows the expected voltage. And I checked PB14, which is COMP4_OUT, it is not the expected voltage.

mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
October 6, 2026

Hello,

I’ve just created a project with CubeMx with the same config as yours:

COMP4_INP (PB0): input connected to an external power supply.

COMP4_INM: input from DAC1_CH1_OUT.

DAC1_CH1_OUT (PA4) is monitored with an oscilloscope as well as COMP4_INP (PB0).

The result is as the following:

When the voltage on COMP4_INP > DAC1_CH1_OUT: COMP_CSR.VALUE = 1

When the voltage on COMP4_INP < DAC1_CH1_OUT: COMP_CSR.VALUE = 0

→ There is no issue with the documentation nor with the HAL nor with the hardware.

It seems the issue is in your bare metal code.

I’ve attached a project for your reference that you can test to confirm my saying.

In the project, the on board green LED turns on when COMP_CSR.VALUE=1 and it turns off when COMP_CSR.VALUE=0.

In next time, I recommend to start with HAL/CubeMx then move on to the bare metal coding.

Please confirm if it worked for you.

Hope that helps.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Associate
October 8, 2026

I got it. I missed a register configuration DAC1->MCR so the DAC is not connected to COMP4 but only connect to output pin PA4