AN4112
Application note
Using STM32F05xx analog comparators in application cases
Introduction
This document describes six application cases of the two analog comparators embedded in
the ultra-low power STM32F05xx product line. The application cases are:
●
●
●
●
●
●
Analog voltage monitoring
Analog watchdog during Stop mode
Pulse width measurement
Pulse width modulation (PWM) signal control
Capacitance measurement
Brightness control using a light dependent resistor (LDR)
The six application cases demonstrate the usefulness of analog comparators and show how
they are integrated with other peripherals, for example, the digital-to-analog-converter (DAC)
and timers.
To ensure a quick start, four application cases presented in this document are implemented
in C language and available in
Project\STM32F0xx_StdPeriph_Examples\COMP
within the
STM32F0xx_StdPeriph_Lib
package.
Please note that this document is not intended to replace the comparator section in the
product reference manual RM0091 (for STM32F05xx ).
The peripheral power consumption should be consulted in the device datasheets.
Table 1.
Applicable products
Type
Microcontrollers
STM32F05xx
Part numbers
June 2012
Doc ID 023195 Rev 1
1/17
www.st.com
Contents
AN4112
Contents
1
2
3
4
5
6
Analog voltage monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Analog watchdog during Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Pulse width measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
PWM signal control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Capacitance measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Brightness control using a light dependent resistor
(LDR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
7
2/17
Doc ID 023195 Rev 1
AN4112
List of figures
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
Figure 12.
Sensor output connection to COMP2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Power consumption in an analog voltage monitoring application . . . . . . . . . . . . . . . . . . . . . 5
COMP2 configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Analog comparators combined in window mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Analog watchdog during Stop mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
COMP2 with output redirection feature. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Pulse width measurement: COMP2 output redirection to timer . . . . . . . . . . . . . . . . . . . . . 10
PWM signal control: COMP2 output redirection to timer . . . . . . . . . . . . . . . . . . . . . . . . . . 11
RC network connection for capacitance measurement . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Capacitance measurement using COMP2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Connecting an LDR resistor to an STM32F05xx device . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Comparator output behavior versus light intensity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Doc ID 023195 Rev 1
3/17
Analog voltage monitoring
AN4112
1
Analog voltage monitoring
The STM32F05xx devices embed a 12-bit analog-to-digital converter (ADC) which is very
fast with a sampling rate of 1 Msample/s. However, with a 1.5 mA typical consumption, it can
jeopardize battery life time if left powered-on continuously. It is therefore recommended to
use analog comparators in application cases where the analog input voltage (sensor output)
needs to be measured as soon as a pre-defined threshold is exceeded.
In the STM32F05xx devices, analog comparators are useful for monitoring the analog input
voltage and powering-on the ADC when required. While monitoring the analog voltage, the
device can enter Stop mode at the same time that both comparators are still powered on.
Consequently, better consumption is achieved and power is saved.
Note:
Analog comparators are powered by the internal reference voltage, V
REFINT
, which is still
powered-on in Stop mode. Once V
REFINT
is disabled, the comparators can no longer be
used.
In an analog voltage monitoring application, where the sensor output voltage is lower than
the threshold, the MCU remains in Stop mode, thereby saving power. As soon as the sensor
output exceeds the threshold, the MCU is woken up, the ADC is powered on and the analog
input voltage is measured. When the sensor output is under the threshold, the MCU re-
enters Stop mode.
The average power consumption is dramatically reduced when compared with an
application that continuously measures the analog voltage whatever the input value.
Figure 1
shows how to connect a sensor output (temperature sensor, pressure sensor,
pyroelectric infrared detector, photodiode sensor) to an STM32F05xx device in an analog
voltage monitoring application using comparator 2 (COMP2). COMP2 monitors the analog
voltage in Stop mode while the ADC measures it in Run mode.
Figure 1.
Sensor output connection to COMP2
1. ADC_CH1: AC channel 1
COMP2_INP: comparator 2 non-inverting input
2. Only if required.
4/17
Doc ID 023195 Rev 1
AN4112
Figure 2.
Analog voltage monitoring
Power consumption in an analog voltage monitoring application
The input analog voltage can be connected to PA1 for COMP1 or PA3 for COMP2. The
analog threshold can be provided internally through V
REFINT
and its submultiples or via an
external pin through PA0 or PA5 for COMP1 or PA2 or PA5 for COMP2. DAC channel 1
(DAC_OUT) cannot be used in such application cases since the DAC channel is powered off
in Stop mode. COMPx wakes up the device from Stop mode through external interrupt lines
(EXTI lines 21 and 22).
Figure 3
shows the available configurations for inverting and non-inverting inputs.
Doc ID 023195 Rev 1
5/17