 Hello and welcome to the STM32L4 MOOC online training. My name is Andre Baratt and this is the dedicated session for the internal comparator peripheral. In this session we will learn how to set up and generate code for the comparator peripheral in STM32 Cubemix. To properly test and understand the behavior of the comparator, we will connect the DAC output to the positive pin of the comparator and the input reference voltage Vref to the negative input of the comparator. When the DAC output reaches a certain limit, the output of the comparator will change the state and generate an interrupt. Inside this interrupt routine we will toggle our green LED notifying the user that the DC value is higher than Vref and we will turn off the LED in case the value is lower than Vref. Let's now open STM32 to configure the LED comparator and DAC for this hands-on. As a first step we will click on new project. On the new open window we will type our part number STM32L476VG and we will double click on the desired part number. Let's start with the comparator configuration. We open the Comp1 tab and we will enable the positive input and on negative input we will select internal Vref voltage. Comparator 1 is fully configured. Let's now go to the DAC1. On DAC1 we will select Out2 and the option connected to external pin only. As the default comparator input pin PC5 is connected to other external components on the discovery board we need to remap it to a more suitable pin. We do that by holding the CTRL key and we will drag and drop it to the pin PB2. Don't forget that we need to connect the output of the DAC PA5 to the comparator input PB2 by a wire. The last pre-fro to be initialized is the green LED on PE8 so we will enable it as GPIO output. The LED will be used during our debug process. Now going to the clock configuration tab we will leave the clock configuration as default so we can skip this step. In the configuration tab we will start by configuring the Comp1. The speed mode can stay at high speed but the trigger mode has to be set to falling edge interrupt. Next we configure the extra resist level to medium. As the connection from the comparator and DAC is done through a wire the extra resist level should be set to medium or high as the wire will act as an antenna and it's possible to capture some spurious triggering. On the DAC we will leave everything as default. As everything is set we can just save our project and generate the code using system workbench for STM32 as our IDE. It is important to start by reminding you that it's necessary to physically connect the output of the DAC on PE5 pin to the comparator input on the PB2 pin. After the code is generated by STM32 QBMX and our project is loaded on system workbench for STM32 we will open our main.c file stored inside of the source folder. In main.c file you can see the initialization of all peripherals defined in STM32 QBMX. Before we go any further in our coding session we need to define a suitable DAC range of values which will have to be in relation to Vref. The internal Vref is 1.22V and the Vref for the DAC is equal to VDD analog which on the L4 board is 3V. The ratio between Vref and Vref DAC is 0.41V As the DAC full scale is 12 bits the DAC maximum value is 4095. If we multiply our full scale for the ratio we will get the value of 1665. For this example a functional range which would allow us to see both states of the comparator could be from 1600 to 1700. After this small mathematical introduction we will initialize the DAC starting value and the first value within the range is 1600. Let's now go to the user code section 2 and initialize the comparator and DAC peripherals. Let's start with the DAC. The function we need will be HALDACStart The parameters will be the DAC handler and the channel number. In this case we will be using the channel number 2. This information can be seen if you go into the function declaration. To start the comparator we must use the HALCompStart and the only parameter is the comparator handler. After we start all peripherals let's go now to the infinite loop in the main function. We will start by setting the DAC output value so we will use the function HALSetValue and the parameters will be. The DAC handler, the DAC channel number the alignment which we will use is the most common the 12-bit long alignment and finally the output value for the DAC. Then inside of an if condition we will check if the DAC value is lower than 1700. Which was our maximum value within the range. If the value is lower we will increment it. If not, on an else statement we will reset it to 1700. We will add some delay, could be 10 milliseconds in order to have a stable output DC value. And this is all the code we need in the infinite loop. As you recall from the theoretical presentation we need to set the LED to toggle whenever the comp interrupt is triggered. To find the necessary callbacks we need to go to the folders drivers slash STM32 L4 HAL driver slash and find the HAL underscore comp dot H. With the keyword callback we can find the HAL underscore comp underscore trigger callback and we will copy it to our main dot C file. We will paste it in the user code section 4 and on the callback function we will just implement a simple LED toggling function. So HAL GPIO toggle pin GPIO E pin 8. Now we can build the project and upload our code to the discovery board and if we restart the board after uploading we should see the green LED toggling as the threshold is being met. Toggling the LED inside of the comparator's interrupt routine. This will be all on this hands-on session. Thank you for watching.