 Hello and welcome to the STM32L4 MOOC online training. My name is Andre Barata and this is the dedicated session for the operational amplifier. In this session we will test the embedded operational amplifier on our L4 Discovery Board. It will be used as a programmable gain amplifier or PGA. This exercise will be quite complex as we are going to make use of the O-Pamp, ADC and DAC. The O-Pamp will be internally connected as a non-inverting amplifier with a gain of 2. The voltage provided by the DAC will be amplified by the O-Pamp and the output signal will be sampled by the ADC. The connection between DAC output and ADC input are managed internally. ADC has a 12-bit resolution or in other words the maximum output value of 4096. The O-Pamp is configured with a gain of 2. So the DAC can output values from 0 to 2048. We will now open the STM32QBMX to generate our project. As a first step we will click on new project. On the open window we will type our part number, STM32L476VG and we will double click on the desired part. Let's start with DAC configuration. We open the DAC1 tab and we will select Out 1 mode connected to our on-chip peripheral only. So this means the output of the DAC will be connected to the positive input of the O-Pamp. Then we go to O-Pamp number 1 configuration and it will be configured as DAC Out 1 in positive. As you see this O-Pamp will be working as PGA or programmable gain amplifier. The next step is to configure the ADC. We select the channel aid which will be internally connected to the O-Pamp output. ADC will be used in single ended mode. We finish the pinout configuration but as you can see the clock configuration shows an error sign. We open the clock configuration and we let STM32QBMX solve the issues for us. As we can see the synchronous clock from the ADC was activated and it is equal to 32 MHz. As we intend to use DMA we will increase the clock frequency to 80 MHz as we prevent any possible timing constraints. We press OK and we will proceed to the configuration tab. Starting with the ADC we will enable continuous conversion mode and DMA continuous requests. Let's now configure the parameters of conversion. The sampling time is quite short. Let's increase it to 92.5 cycles. On DMA settings we will add ADC1 in circular mode. The rest of the parameters are correct. On the ADC we will leave the default configuration. Then we proceed to open the O-Pamp1 just to verify that the PGA gain is set to 2. As everything is set we can just save our project and generate the code using SystemWorkbench4STM32 as our IDE. After the code is generated by the STM32QBMX and our project is fully loaded on SystemWorkbench4STM32 we will open our main.c file stored inside the source folder. On main.c file you can see the initialization of all peripherals defined on STM32QBMX. We will start by defining the global variables in the private variables section. We need to store an output value for the ADC conversion and an input value for the DAC converter. Both variables will be half-word long as the maximum ADC resolution is 12 bits. Then in the user code begin to section we will start our peripherals. So first we start with DAC. HAL DAC start with the first parameter being the peripheral handler and the second the designated channel which in this case is the channel number 1. We continue by starting the opamp. HAL opamp start the only parameter is the handler. Then to initialize the ADC it takes us 2 steps. First we need to calibrate the ADC with HAL ADC EEX calibration. Parameters will be the handler and the mode. In this case the mode will be ADC single-ended. The suffix EEX denominated that this function is specific from this family. So its implementation is included on the extension files. Then we are ready to start the ADC conversion with DMA transferring the ADC output value to memory. So HAL ADC start DMA. Parameters are ADC handler, a pointer to the output buffer in memory and the last parameter is the length of the output buffer. The output buffer needs to be casted to a 30-bit long value. Now in the while loop we can set the DAC value and for that we will use the function HAL DAC set value. First parameter is the DAC handler. The second is the DAC channel in this case number 1 channel. Third will be the alignment which in this case will be 12-bit alignment to the right. And the last one will be the output value of the DAC. Inside the loop we will increase the value of the DAC output up to 2047. The DAC output voltage will be amplified by 2 so it matches the input range of the ADC. When the value reaches the maximum of 2047 we will set it back to 0. The waveform which is coming from the DAC will resemble a sawtooth. We will add a 10ms delay in the loop so the period of the sawtooth is approximately 20 seconds. Here we have some typo highlighted by the IDE that we must correct before proceeding. Let's go to the STM32L4XX-HAL-MSP.C and on all-pamp MSP in it. Here we can see that the pins for the input and output are configured in analog mode. Yet in this case the output of the PGA is connected to the ADC input. For this case we need to select a specific mode. If we go to the definition of the function we can see that not only we have the GPIO mode analog but we have a specific mode of operation called analog ADC control. We will overwrite the previous configuration by defining the new GPIO mode to analog with ADC control and launch the HAL GPIO in it for part A with a modified structure. We can now return to our main.c file and build our application. Let's now enter the debug mode. On the watch expression tab we will add the value underscore DAC and value underscore ADC variables. We start our application and after a while we will stop it. We can see that the value of the ADC is approximately 2 times bigger than the value of the DAC which means that the PGA is working correctly as we intended to. We just finish our op-amp hands-on. Thank you for your attention.