Comparator introduction
The Analog Comparator is used to compare the voltage of two analog inputs, with a digital output
indicating which input voltage is higher. Inputs can either be one of the selectable internal references or
from external pins. The comparator output can be sent directly to GPIO.
The comparators can be used for a variety of functions including:
• Wake-up from low-power mode triggered by an analog signal,
• Analog signal conditioning,
• Cycle-by-cycle current control loop when combined with a PWM output from a timer.
Comparator main features
• Each comparator has configurable plus and minus inputs used for flexible voltage
selection:
– Multiplexed I/O pins
– DAC channels
– Internal reference voltage and three submultiple values (1/4, 1/2, 3/4) provided by
a scaler (buffered voltage divider)
• Programmable hysteresis
• Output redirection to I/Os or to timer inputs for triggering break events for fast PWM
shutdowns
• Output blanking for immunity to switching noise
• Per-channel interrupt generation with wake-up from Sleep and Stop modes
Comparator 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:
• Emergency shut-down of PWM signals, using BKIN and BKIN2 inputs
• Cycle-by-cycle current control, using OCREF_CLR inputs
• Input capture for timing measures
Creating STM32 executable projects steps are available on this link , please follow steps 1 to 10 as per blog, here we will start from step 11. PA9 & PA10 Pin used for debugging purpose as UART Tx & UART Rx respectively. this blog is specially for analog comparators configuration, DAC1 connected to INM pin of comparator (Internally) & DAC2 is connected to INP pin of comparator n & output will be available in serial terminal. First 10 steps represents STM32 project creation from scratch, only difference will be board selection or controller selection part, user can select according to there requirements but below steps will remain same for all the projects.
Creating a new STM32 executable project
- The easiest way to create a new STM32 C/C++ project is to use the STM32 project wizard. It is selected through the menu [File]>[New STM32 Project].
- The MCU/MPU selector and Board Selector tabs can be selected at the top of the window. Use the first tab to create project for a specific device and the second if a project for a specific board is needed. today we will select specific device.
3. Select SMT32F429ZI in MCU Selector tab
4. According to the settings in Figure 3, the project is meant to be stored in the default location with the following
options set:
• C project
• Executable binary type
• STM32CubeIDE targeted project type
Press [Next] to open the Firmware Library Package Setup page.
5. In this page, it is possible to select the STM32Cube firmware package to use when creating the project. In this case, the default settings are used. Press [Finish] to create the project. (Fig 4)
6. wait for project creation process
7. Initial view of STM32CUBE ide after project generation
8. Go to System core > SYS > Enable Serial wire debug
9. If require enable external clock
10. System clock setting as per your application requirement
11. Enable Comparator Peripheral Configuration
12. other settings
13. Pin configuration
DAC1 is connected internally to comparator
PA5 : DAC2 connected to INP pin of comparator
PA6 : Comparator Output
14. Press below icon for code generation
15. Sample Code:
#define HIGH 1u #define DAC2_MAX_VALUE 2500 #define DAC2_MIN_VALUE 1500 /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ uint16_t dac_data1 = 2000u; //Comparator threshold value uint16_t dac_data2 = 1980u; /* USER CODE END PM */ int main(void) { /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_COMP1_Init(); MX_DAC1_Init(); MX_USART1_UART_Init(); HAL_DAC_Start(&hdac1, DAC_CHANNEL_1); HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, dac_data1); HAL_DAC_Start(&hdac1, DAC_CHANNEL_2); HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, dac_data2); HAL_COMP_Start(&hcomp1); /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { HAL_Delay(1000); uart1_printf_debug("DAC Value = %d === ",dac_data2); HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_2, DAC_ALIGN_12B_R, dac_data2); dac_data2 += 50u; //Increasing DAC count by 50 if (HAL_COMP_GetOutputLevel(&hcomp1) == HIGH) { uart1_printf_debug("High\n"); } else { uart1_printf_debug("Low\n"); } if (dac_data2 > DAC2_MAX_VALUE) { dac_data2 = DAC2_MIN_VALUE; } } }
16. Output at Serial terminal
Software Tools:
- STM32CubeIDE
- STM32CubeMx
- Teraterm
Hardware Setup:
- STM32G4 Nucleo-64
- Mini USB Cable
- Jumper wire
Conclusion:
Successfully demonstrated comparator functionality using DAC1 & DAC2. output captured in serial terminal.
If you enjoyed this article, share your feedback.
References:
- STM32G4 HAL Library
- STM32G4 Reference manual