15. Comparator (COMP)

This section applies to STM32F05x and STM32F07x and STM32F09x devices only.

15.1 Introduction

STM32F05x and STM32F07x and STM32F09x devices embed two general purpose comparators COMP1 and COMP2, that can be used either as standalone devices (all terminals are available on I/Os) or combined with the timers.

The comparators can be used for a variety of functions including:

15.2 COMP main features

15.3 COMP functional description

15.3.1 COMP block diagram

The block diagram of the comparators is shown in Figure 56: Comparator 1 and 2 block diagrams .

Figure 56. Comparator 1 and 2 block diagrams

Figure 56. Comparator 1 and 2 block diagrams. The diagram shows two comparators, COMP1 and COMP2. COMP1 has inputs COMP1_INP (+) and COMP1_INM (-). COMP1_INP is connected to PA1. COMP1_INM is connected to a multiplexer that selects between PA0, PA4 (DAC_OUT1), PA5 (DAC_OUT2), V_REFINT, 3/4 V_REFINT, 1/4 V_REFINT, and 1/2 V_REFINT. The output of COMP1 is COMP1_OUT, which can be connected to PA0, PA6, or PA11. COMP1_OUT is also connected to a polarity selection block, which generates a COMP interrupt request (to EXTI) and connects to various timer inputs: TIM1_BK1, TIM1_OCref_clr, TIM1_IC1, TIM2_IC4, TIM2_OCref_clr, TIM3_IC1, and TIM3_OCref_clr. COMP2 has inputs COMP2_INP (+) and COMP2_INM (-). COMP2_INP is connected to PA3. COMP2_INM is connected to a multiplexer that selects between PA2, PA4 (DAC_OUT1), PA5 (DAC_OUT2), V_REFINT, 3/4 V_REFINT, 1/4 V_REFINT, and 1/2 V_REFINT. The output of COMP2 is COMP2_OUT, which can be connected to PA7, PA2, or PA12. COMP2_OUT is also connected to a polarity selection block, which generates a COMP interrupt request (to EXTI) and connects to various timer inputs: TIM1_BK1, TIM1_OCref_clr, TIM1_IC1, TIM2_IC4, TIM2_OCref_clr, TIM3_IC1, and TIM3_OCref_clr. A 'Window mode' is indicated between the two comparators. The diagram is labeled MS19824V2.
Figure 56. Comparator 1 and 2 block diagrams. The diagram shows two comparators, COMP1 and COMP2. COMP1 has inputs COMP1_INP (+) and COMP1_INM (-). COMP1_INP is connected to PA1. COMP1_INM is connected to a multiplexer that selects between PA0, PA4 (DAC_OUT1), PA5 (DAC_OUT2), V_REFINT, 3/4 V_REFINT, 1/4 V_REFINT, and 1/2 V_REFINT. The output of COMP1 is COMP1_OUT, which can be connected to PA0, PA6, or PA11. COMP1_OUT is also connected to a polarity selection block, which generates a COMP interrupt request (to EXTI) and connects to various timer inputs: TIM1_BK1, TIM1_OCref_clr, TIM1_IC1, TIM2_IC4, TIM2_OCref_clr, TIM3_IC1, and TIM3_OCref_clr. COMP2 has inputs COMP2_INP (+) and COMP2_INM (-). COMP2_INP is connected to PA3. COMP2_INM is connected to a multiplexer that selects between PA2, PA4 (DAC_OUT1), PA5 (DAC_OUT2), V_REFINT, 3/4 V_REFINT, 1/4 V_REFINT, and 1/2 V_REFINT. The output of COMP2 is COMP2_OUT, which can be connected to PA7, PA2, or PA12. COMP2_OUT is also connected to a polarity selection block, which generates a COMP interrupt request (to EXTI) and connects to various timer inputs: TIM1_BK1, TIM1_OCref_clr, TIM1_IC1, TIM2_IC4, TIM2_OCref_clr, TIM3_IC1, and TIM3_OCref_clr. A 'Window mode' is indicated between the two comparators. The diagram is labeled MS19824V2.

15.3.2 COMP pins and internal signals

The I/Os used as comparators inputs must be configured in analog mode in the GPIOs registers.

The comparator output can be connected to the I/Os using the alternate function channel given in “Alternate function mapping” table in the datasheet.

The output can also be internally redirected to a variety of timer input for the following purposes:

It is possible to have the comparator output simultaneously redirected internally and externally.

15.3.3 COMP reset and clocks

The COMP clock provided by the clock controller is synchronous with the PCLK (APB clock).

There is no clock enable control bit provided in the RCC controller. Reset and clock enable bits are common for COMP and SYSCFG.

Important: The polarity selection logic and the output redirection to the port works independently from the PCLK clock. This allows the comparator to work even in Stop mode.

15.3.4 Comparator LOCK mechanism

The comparators can be used for safety purposes, such as over-current or thermal protection. For applications having specific functional safety requirements, it is necessary to insure that the comparator programming cannot be altered in case of spurious register access or program counter corruption.

For this purpose, the comparator control and status registers can be write-protected (read-only).

Once the programming is completed, using bits 30:16 and 15:0 of COMP_CSR, the COMPx LOCK bit can be set to 1. This causes the whole COMP_CSR register to become read-only, including the COMPx LOCK bit.

The write protection can only be reset by a MCU reset.

15.3.5 Hysteresis

The comparator includes a programmable hysteresis to avoid spurious output transitions in case of noisy signals. The hysteresis can be disabled if it is not needed (for instance when exiting from low-power mode) to be able to force the hysteresis value using external components.

Figure 57. Comparator hysteresis

Figure 57: Comparator hysteresis diagram showing input and output waveforms. The top graph plots voltage over time with a sinusoidal signal INP and two horizontal threshold levels: INM and INM - Vhyst. The bottom graph shows the digital output COMP_OUT. When INP rises above INM, COMP_OUT goes high. When INP falls below INM - Vhyst, COMP_OUT goes low. Vertical dashed lines indicate these switching points.
Figure 57: Comparator hysteresis diagram showing input and output waveforms. The top graph plots voltage over time with a sinusoidal signal INP and two horizontal threshold levels: INM and INM - Vhyst. The bottom graph shows the digital output COMP_OUT. When INP rises above INM, COMP_OUT goes high. When INP falls below INM - Vhyst, COMP_OUT goes low. Vertical dashed lines indicate these switching points.

15.3.6 Power mode

The comparator power consumption versus propagation delay can be adjusted to have the optimum trade-off for a given application. The bits COMPxMODE[1:0] in COMP_CSR register can be programmed as follows:

15.4 COMP interrupts

The comparator outputs are internally connected to the Extended interrupts and events controller. Each comparator has its own EXTI line and can generate either interrupts or events. The same mechanism is used to exit from low-power modes.

Refer to Interrupt and events section for more details.

15.5 COMP registers

15.5.1 COMP control and status register (COMP_CSR)

Address offset: 0x1C
Reset value: 0x0000 0000

31302928272625242322212019181716
COMP2 LOCKCOMP2 OUTCOMP2HYST [1:0]COMP2 POLCOMP2OUTSEL[2:0]WNDW ENCOMP2INSEL[2:0]COMP2MODE [1:0]Res.COMP2 EN
rworrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/r

1514131211109876543210
COMP1 LOCKCOMP1 OUTCOMP1HYST [1:0]COMP1 POLCOMP1OUTSEL[2:0]Res.COMP1INSEL[2:0]COMP1MODE [1:0]COMP1 SW1COMP1 EN
rworrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/rrw/r
Bit 31 COMP2LOCK : Comparator 2 lock

This bit is write-once. It is set by software. It can only be cleared by a system reset.

It allows to have all control bits of comparator 2 as read-only.

0: COMP_CSR[31:16] bits are read-write.

1: COMP_CSR[31:16] bits are read-only.

Bit 30 COMP2OUT : Comparator 2 output

This read-only bit is a copy of comparator 2 output state.

0: Output is low (non-inverting input below inverting input).

1: Output is high (non-inverting input above inverting input).

Bits 29:28 COMP2HYST[1:0] Comparator 2 hysteresis

These bits control the hysteresis level.

00: No hysteresis

01: Low hysteresis

10: Medium hysteresis

11: High hysteresis

Please refer to the electrical characteristics for the hysteresis values.

Bit 27 COMP2POL : Comparator 2 output polarity

This bit is used to invert the comparator 2 output.

0: Output is not inverted

1: Output is inverted

Bits 26:24 COMP2OUTSEL[2:0] : Comparator 2 output selection

These bits select the destination of the comparator output.

000: No selection

001: Timer 1 break input

010: Timer 1 Input capture 1

011: Timer 1 OCrefclear input

100: Timer 2 input capture 4

101: Timer 2 OCrefclear input

110: Timer 3 input capture 1

111: Timer 3 OCrefclear input

Bit 23 WNDWEN : Window mode enable

This bit connects the non-inverting input of COMP2 to COMP1's non-inverting input, which is simultaneously disconnected from PA3.

0: Window mode disabled

1: Window mode enabled

Bits 22:20 COMP2INSEL[2:0] : Comparator 2 inverting input selection

These bits allows to select the source connected to the inverting input of the comparator 2.

000: 1/4 of V REFINT

001: 1/2 of V REFINT

010: 3/4 of V REFINT

011: V REFINT

100: COMP2_INM4 (PA4 with DAC_OUT1 if enabled)

101: COMP2_INM5 (PA5 with DAC_OUT2 if present and enabled)

110: COMP2_INM6 (PA2)

111: Reserved

Bits 19:18 COMP2MODE[1:0] : Comparator 2 mode

These bits control the operating mode of the comparator 2 and allows to adjust the speed/consumption.

Bit 17 Reserved, must be kept at reset value.

Bit 16 COMP2EN : Comparator 2 enable

This bit switches ON/OFF the comparator2.

Bit 15 COMP1LOCK : Comparator 1 lock

This bit is write-once. It is set by software. It can only be cleared by a system reset.

It allows to have all control bits of comparator 1 as read-only.

Bit 14 COMP1OUT : Comparator 1 output

This read-only bit is a copy of comparator 1 output state.

Bits 13:12 COMP1HYST[1:0] Comparator 1 hysteresis

These bits are controlling the hysteresis level.

Please refer to the electrical characteristics for the hysteresis values.

Bit 11 COMP1POL : Comparator 1 output polarity

This bit is used to invert the comparator 1 output.

Bits 10:8 COMP1OUTSEL[2:0] : Comparator 1 output selection

These bits selects the destination of the comparator 1 output.

Bit 7 Reserved, must be kept at reset value.

Bits 6:4 COMP1INSEL[2:0] : Comparator 1 inverting input selection

These bits select the source connected to the inverting input of the comparator 1.

Bits 3:2 COMP1MODE[1:0] : Comparator 1 mode

These bits control the operating mode of the comparator 1 and allows to adjust the speed/consumption.

Bit 1 COMP1SW1 : Comparator 1 non inverting input DAC switch

This bit closes a switch between comparator 1 non-inverting input on PA1 and PA4 (DAC) I/O.

Note: This switch is solely intended to redirect signals onto high impedance input, such as COMP1 non-inverting input (highly resistive switch).

Bit 0 COMP1EN : Comparator 1 enable

This bit switches COMP1 ON/OFF.

15.5.2 COMP register map

The following table summarizes the comparator registers.

Table 54. COMP register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x1CCOMP_CSRCOMP2LOCKCOMP2OUTCOMP2HYST1:0COMP2POLCOMP2OUTSEL[2:0]WNDWENCOMP2INSEL[2:0]COMP2MODE1:0COMP2ENCOMP1LOCKCOMP1OUTCOMP1HYST1:0COMP1POLCOMP1OUTSEL[2:0]COMP1INSEL[2:0]COMP1MODE1:0COMP1SW1COMP1EN
Reset value000000000000000000000000000000

Refer to Section 2.2 on page 46 for the register boundary addresses.