 Hello and welcome to the STM32L4 MOOC online training. My name is Andre Barata and this is the dedicated session for the ADC Peripheral. ADC stands for Analog to Digital Converter and this is one of the most common peripherals in the MCU. This peripheral is meant to convert a voltage into a numerical value. In STM32L4 Discovery Board, ADC can be configured in continuous conversion or one-shot mode. It can operate as fast as 5 Mbps and it has 12-bit resolution. In this session we will learn how to set up and generate code for the ADC peripheral in STM32 Cubemix. The main objective is to make use of the DAC configured in the previous session, generate a known voltage and sample it with the ADC in its ADC input pin. The ADC conversion values will be transferred into memory by DMA controller, as we want to offload the CPU as much as possible. In this case DMA controller will take care of the data transfer making the CPU available to preform other tasks. We will do so by making use of the HL-ADC functions. We will learn how to use them appropriately to succeed in sampling with ADC. Let's now open STM32 Cubemix to start configuring our peripherals. As a first step we will click on the new project. In the new open window we will type our part number STM32L476VG and we will double-click on the desired part. Or as an alternative we can select and press the start project button. We will start by setting ADC1 on channel 6 as single-ended, since we are not measuring differential signals. As a consequence PA1 is highlighted in green. Then we will enable DAC1 output 2 and set it to connect to external pin-only mode. As a consequence PA5 will be highlighted in green. Fortunately PA1 or ADC input or PA5 or DAC output are located next to each other and they just need to be connected via a jumper, which can be found on the bottom layer of the L4 Discovery board. We can now proceed to the clock configuration window and to be in line with the recent configuration we will accept the STM32 Cubemix automatic resolution. As we can see in the resolution proposed the external oscillator was selected as clock source for the ADC while the MCU is kept at a lower frequency. This setup is typical in low-power applications as we can still make use of the full capabilities of the peripherals while keeping the consumption to a minimum. In the configuration tab we will start by opening the DAC configuration. We can leave the default settings here and let's have a look on the ADC configuration now. On ADC settings we have a synchronous clock source, 12-bit data resolution. Here we just need to change the DMA continuous request settings to enabled. And then we need to configure sample and hold period, so let's open rank options group. Here we have channel number 6 which is the right one and then on sampling time we have 2.5 cycles. As you remember the ADC clock source is 32 MHz, 2.5 cycles seems to be very small number so I would suggest to increase it to 92.5 cycles. We can apply settings and go to the DMA settings. On DMA settings we will add DMA channel by clicking add and we select ADC1. As we are using continuous data transfers we will change the mode to circular buffer. The memory address will be incremented after each transfer and on data size we can leave half word as ADC has 12-bit resolution. So as everything is properly set we can just save our project and generate the code using system workbench for STM32 as our IDE. After the code is generated by STM32 Cubemix and our project is fully loaded on system workbench for STM32 we will open our main.c file stored inside of the source folder. In the main.c file you can see the initialization of all the peripherals defined on STM32 Cubemix. In this specific case you can see the system clock configuration DMA, ADC and DAC. As a first step we will initialize two variables in user code private variable section one for DAC output value and another for ADC input value. Let's initialize value DAC to zero. Let's now go to the user code section 2 and start our peripherals. It is important to calibrate the ADC before starting the conversion. So we will start using the HAL ADC calibration. In this case we will use HAL ADC EEX calibration start. The suffix EEX tells us that this function is specific for this family so its implementation is included in the extension files. This function expects two input parameters, the peripheral handler and the operation mode. As we are operating in the single ended mode we need to set the correct mode. We can see the available options if we enter the function declaration. So we copy it and we are ready to start the ADC conversion with DMA transferring the ADC output value to the memory. HAL ADC start DMA. Parameters are ADC handler, the second is a pointer to the buffer in memory and the last parameter is the length of the buffer. The buffer needs to be type cast to a 32-bit unsigned integer. The length is going to be one as we just want to transfer one ADC output value. The configuration of ADC is now complete. Let's now start the DAC. We will use the function HAL DAC start the parameters are DAC handler and the channel number. This information can be seen if we go into the function declaration. As you might recall from the STM32 QMX configuration the selected channel was the channel number 2. We will copy this and paste it in the second parameter field. Let's now go to the infinite loop in the main function as we want to make this example as dynamic as possible. We will generate a sawtooth signal on the DAC output and measure it with the configured ADC. We will start by setting the DAC output value so we will use the function HAL DAC set value and the parameters will be the DAC handler, the DAC channel number, the alignment which we will use and in this case is the most common, the 12-bit long alignment and finally the output value for the DAC. Inside of a NIF condition we will check if the DAC value is lower than 4095 which is our maximum value for a 12-bit resolution DAC. If the value is lower we will increment it and if not on an else statement we will reset it to 0. We will add some delay, it could be 20ms in order to have a stable output DAC value. The code that we just typed will sweep through all the DAC output possible values in roughly 80 seconds and then it resets to 0, restarting the process all over again. As the ADC was configured with software trigger we need to start it in our code. So the function will be HAL, ADC start and the only parameter is the ADC handler. Thanks to the ADC's DMA mode after the end of each conversion it will automatically invoke a DMA transfer to memory. This will be all from the source code so we can build the project. We will upload the project to our discovery board as we are not going to enter the debug mode in this example. In this hands-on we will use the STMS Studio to monitor the ADC value. This tool allows us to observe all the global variables in runtime. To monitor the ADC value we will go to file import variables from an executable file. We will open the file which is inside the debug folder and it has the extension dot ELF. Here we have a list of global variables and now let's search for the value ADC and import it. Now the variable will be seen in the display variable settings box. We will drag and drop it to the display box and then we need to redefine the axis limits. We will define it bigger than the upper value so 4100 would be enough. As you can see the value is increasing until the maximum value is reached and then it will go back to zero and the whole process will restart. We can change the graphical display of the information and to do so we just need to select other formats from this box. We will use a bar graph as an example. This will be all from this session. Thank you for watching.