{"id":12479,"date":"2024-03-31T11:03:34","date_gmt":"2024-03-31T09:03:34","guid":{"rendered":"https:\/\/deepbluembedded.com\/?p=12479"},"modified":"2024-03-31T11:03:36","modified_gmt":"2024-03-31T09:03:36","slug":"stm32-comparator-example-tutorial-internal-analog-comparator","status":"publish","type":"post","link":"https:\/\/deepbluembedded.com\/stm32-comparator-example-tutorial-internal-analog-comparator\/","title":{"rendered":"STM32 Comparator Tutorial + Example (Internal Analog Comparator)"},"content":{"rendered":"\n
In this tutorial, we’ll discuss how the STM32 Internal Analog Comparator<\/strong> works, and how to use the STM32 Comparator<\/strong> with the timer in input capture mode for signal measurement. The STM32 comparator’s output can be externally and\/or internally routed to different peripherals which opens the door for a wide range of applications and design ideas as we shall see in this tutorial.<\/p>\n\n\n\n The practical example project we’ll implement in this tutorial will be a very good starting point for your STM32 Comparator-based project. Without further ado, let’s get right into it!<\/p>\n\n\n The STM32 comparator peripheral is a hardware analog comparator integrated inside the STM32 microcontroller itself. This analog comparator can be configured to operate in different modes which makes it so flexible and suitable for a wide range of applications.<\/p>\n\n\n Let’s now discuss the detailed functionality of the STM32 internal analog comparators, how they work, and what are the possible configurations and operating modes for the analog comparators.<\/p>\n\n\n This is the block diagram for the STM32 analog comparators showing its input signal sources and output internal routing options.<\/p>\n\n\n\n <\/p>\n\n\n\n The I\/Os used as comparator inputs must be configured in analog mode in the GPIO registers. The comparator output can be connected to the I\/Os using the alternate function channel given in the \u201cAlternate function mapping\u201d table in the datasheet. The output can also be internally redirected to a variety of timer inputs for the following purposes:<\/p>\n\n\n\n It is possible to have the comparator output simultaneously redirected internally and externally.<\/p>\n\n\n The STM32 analog comparator includes a programmable hysteresis to avoid aggressive fluctuating output transitions in case of noisy input signals.<\/p>\n\n\n\n <\/p>\n\n\n Two embedded comparators can be utilized to create a window comparator. The monitored analog voltage is connected to the non-inverting (Vin+) inputs of comparators connected together and the upper and lower threshold voltages are connected to the inverting (Vin-) inputs of the comparators.<\/p>\n\n\n\n <\/p>\n\n\n The STM32 analog comparator can be used in so many applications just like a standalone analog comparator but it’s integrated inside the microcontroller itself. Here are some common applications for the integrated STM32 analog comparator peripheral:<\/p>\n\n\n\n In this example project, we’ll set up the STM32 internal analog comparator to compare the (Vin+), which is coming from a signal generator, against the internal (Vref\/4 = 0.825v). Therefore the comparator’s digital output will toggle on the zero-crossing points of the input sinusoidal waveform.<\/p>\n\n\n\n We’ll internally route the comparator’s output (rising edges) as a trigger source for Timer1 which will operate in “input capture” mode. Therefore, an input capture event will be fired on the rising edges of the signal. By taking 2 time stamps for every consecutive ICU event, we’ll easily be able to find out the input signal’s period (T) and, consequently solve for the frequency (F = 1\/T).<\/p>\n\n\n\n The resulting frequency measurement will be printed on an I2C LCD. Below is a functional and connection diagram that shows you how this project is working and how things are wired up.<\/p>\n\n\n\n Example Project Diagram<\/strong><\/p>\n\n\n\n <\/p>\n\n\n\n Example Project Steps Summary:<\/strong><\/p>\n\n\n\n Highly Recommended References Needed For This Project:<\/strong><\/p>\n\n\n This article will give you more in-depth information about the STM32 I2C LCD 16×2 library, how it works, and how to integrate it into your projects.<\/p>\n\n<\/div><\/div><\/div>\n<\/div>\n<\/div><\/div>\n\nTable of Contents<\/h2>\n
\n
\n\n\nSTM32 Comparator<\/strong><\/h2>\n\n\n
STM32 Internal Analog Comparator Main Features<\/strong><\/h3>\n\n\n
\n
STM32 Internal Analog Comparator Functional Description<\/strong><\/strong><\/h3>\n\n\n
1. STM32 Analog Comparator Block Diagram & Pins<\/strong><\/h4>\n\n\n
\n
2. STM32 Analog Comparator Hysteresis<\/strong><\/h4>\n\n\n
3. STM32 Analog Comparator (Window Comparartor) Mode<\/strong><\/h4>\n\n\n
STM32 Comparator Applications<\/strong><\/h3>\n\n\n
\n
\n\n\nSTM32 Comparator Example (With Interrupt + Timer ICU)<\/strong><\/h2>\n\n\n
\n