STM32-Peripheral’s : Comparator

STM32-Peripheral’s : Comparator

Figure 1: Comparator block diagram

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

Figure 2: STM32G4 Reference Manual


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

  1. 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].
  2. 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.
Figure 3

3. Select SMT32F429ZI in MCU Selector tab

Figure 4

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.

Figure 5 Project Name

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)

Figure 6 Select Firmware Package

6. wait for project creation process

Figure 7 Project Creation process

7. Initial view of STM32CUBE ide after project generation

Figure 8

8. Go to System core > SYS > Enable Serial wire debug

Figure 8 Enable Serial Wire Debug

9. If require enable external clock

Figure 9 Enable external clock

10. System clock setting as per your application requirement

Figure 10 clock settings

11. Enable Comparator Peripheral Configuration

Figure 11: Enable Comparator

12. other settings

Figure 12: Enable two DAC’s

13. Pin configuration

DAC1 is connected internally to comparator

PA5 : DAC2 connected to INP pin of comparator

PA6 : Comparator Output

Figure 13: Pin configuration

14. Press below icon for code generation

Figure 14 Cube IDE 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

Figure 15: Tera term output
Software Tools:
  1. STM32CubeIDE
  2. STM32CubeMx
  3. Teraterm
Hardware Setup:
  1. STM32G4 Nucleo-64
  2. Mini USB Cable
  3. Jumper wire
Conclusion:

Successfully demonstrated comparator functionality using DAC1 & DAC2. output captured in serial terminal.

If you enjoyed this article, share your feedback.

References:
  1. STM32G4 HAL Library
  2. STM32G4 Reference manual
Similar topics:
  1. https://kalapiinfotech.in/stm32-cube-ide-freertos-code-generation-using-cubemx/
  2. STM32 Peripherals: GPIO
  3. STM32 Peripherals: DAC
  4. STM32 Peripherals: ADC using Polling Mode
  5. STM32 Peripherals: UART Interrupt Mode
  6. STM32-Peripheral’s-SPI: Polling Mode

Leave a Reply

Your email address will not be published. Required fields are marked *