 What's up guys ? Welcome to Lab 6 of our STM42C0 MOOC. So this is a more advanced slab. In this slab we use the ADC, the DMA and the timers. The objective of this slab is to learn about the ADC, so the analog to digital converter, with the DMA, so direct memory access and the timers. So we will convert a signal that is on pin, so PA12, which is connected or which has an alternate function for ADC1 channel 12. And we will store the converted values inside the buffer, inside the RAM, thanks to the DMA and scheduled by the timer. This is a block diagram of what we want to achieve. So as you can see, so we have the signal here, so analog signal that we want to convert, connect it to PA12, which is ADC1 channel 12. So connect it to the ADC. And we will have a trigger coming from the timer 3, every hertz, so every seconds basically. And this will trigger also the DMA to store the value converted to the buffer inside the RAM. So the ADC1 is going to measure 8-bit data on PA12, so which is ADC1 channel 12. And we'll get a conversions every seconds, which is triggered by the timer 3, so which will be working as a time base. And its update event is going to trigger the ADC conversions. So the data coming from the ADC1 are transferred by the DMA into a buffer that is in the RAM. So the DMA1 channel 2 is the one that we'll be using in this case. And this is going to be used in a circular mode using bytes for both sides. The first part of the configuration will be the timer, so team 3 in this case. We are going to use team 3 as a time base and we'll use the up counter mode with internal clock. And the pre-scaler and counter period will be configured to generate a one second overflow event and will enable also the trigger event selection, TRGO, as an update event. So create a new project. You know how to do that. For the part number, you know what it is by now. So STM42C031C6T6. Select it here and press OK. We're going to give a name to this project. So STM42C0 underscore ADC, for example. And then click finish. Now, so in the timer section here, you're going to select timer 3 and you're going to select the clock source to be internal. Now, for the pre-scaler, so we said 11999. For the counter period, we'll be using 999. OK, up counter. And we'll enable the TRGO, so the trigger output. So enable it and select the update event. So we use this event to trigger the ADC and also trigger the DMA. Next step is the DMA configuration. So in ADC1, we'll use the DMA mode. So for this, so we'll add the DMA settings. So what we'll do is inside the configuration in the DMA settings, we'll add the DMA1 channel 2. This is a peripheral to memory configuration we'll use. We'll use a circular mode and for the memory, we will increment the address. So on the memory side, for data width, we'll use both bytes for source and destination. OK, in analog, look for ADC1 and we're going to add... So first, you want the ADC channel 12. So we'll enable this. So this is PA12, right there, ADC1 channel 12. And now, we're going to add the DMA support. So in DMA settings tab, add, select ADC1. So this will select DMA1 channel 1. This is peripheral to memory. So this is a good configuration for us. We're going to use a circular mode and for the rest, so the memory will increment. So every time, you know, we'll have a buffer. So we have a new conversion. We'll increment, you know, the pointer to the buffer. And for the data width, we don't need a half word. We're going to use bytes. OK, and that's it. We can continue the ADC1 conversion. So we want, you know, to have ADC1 channel 12 that is converted. So we'll have a trigger coming from the timer free trigger output. We'll use 12.5 cycles for sampling time. So this is, you know, something you can configure for the resolution. So we're going to go down to 8 bits and we'll do transfer over DMA. So let's do all these configurations here. So go back to the parameter settings for your ADC right here. So we said we don't need, you know, 12 bits resolution. We're going to go down to 8 bits. So you see, you can select, you know, the resolution. So up to 12 bits. Then we're going to enable the DMA continuous request. Like this. OK, now for the sampling time, we'll increase it, you know, to 12.5. So this is selectable all the way from 1.5 all the way to 160.5 cycles. So we select 12.5 for the external trigger. So we're going to select timer 3 trigger output event. And for the edge, we'll use the raising edge. OK, now expand a little bit the rank 1. So rank 1. So basically we have only one, you know, channel to convert. So there is only one rank and we see our channel 12 here, which is PA12. OK. For the clock configuration of the SM42C0, we select 12 MHz because this will be so the frequency of your system clock but also your peripheral clock. And so your timer 3 clock. So all the calculation we did, all the settings we did previously for the pre-scaler and for the counter period are based on 12 MHz, you know, clock. So we're going to clock configuration and we'll keep the default 12 MHz. So 12 MHz for the system clock and 12 MHz also for the peripheral and the timers. Now save the project to generate the code. If you are using SM42C3ID version 1.10.1 and SM42C0C0 library version 1.0.0 there is an issue with the code generation with DMA usage. The init function of the DMA is wrongly located after the peripherals initialization. So ADC in this case. MX underscore DMA underscore init should be manually moved before any other peripherals which is using it. So because I'm using version 1.10.1 of Q by D and the version 1.0.0 of the Q library I will have to do the manipulation of the code. So change the order of the init functions. In main.c and main function look for the init functions right there. You see the problem is in my case the init for the DMA is not located at the proper place. It should be made before the ADC configuration. So just move it to the first place for example that will solve the problem. So this issue is going to be fixed of course in future versions. So but if you are using the 1.10.1 you will have to make this change. At this point we just need to add a few lines of code. So one in our line to basically declare the data buffer we'll be using to store the data, the coveted data. And then some peripherals starts. So we'll have one functions to start the calibration of the ADC. One functions to start the DMA in our conversions for the ADC. And then we'll start the timer base which is used to transfer the data from the ADC to the RAM thanks to the DMA. So let's add the declaration for the buffer that we're going to use and we're going to add it to the PV section right there. So this is 8 bits and 8 data. Now in the main function after the init and just before the while loop we're going to add some code. So first we'll add a line of code to start the calibration of the ADC. Then we'll add one line of code to start the DMA conversions of the ADC. So we'll give as a parameter the handler and also the destination so which is our buffer that we declared and we'll do a transfer of 8 so because we want to convert 8 data inside the buffer. Last we will start the timer so timer 3 which is going to schedule the DMA transfer and the ADC conversions. So again all this code to be added you can find it in the description of the video or in the comment section. You can now build a project so remember the icon then enter the debug session. Next step will be to add the buffer to the live expression so that we can monitor it in real time inside the debugger inside QID then run the application and you should see the variables inside the buffer with different data that are going to be updated every seconds thanks to the timer and so the transfer from the ADC data register so the conversions of the PA12 to the buffer inside the RAM so you're going to see that happening so that's thanks to the ADC the DMA and the timer. Build your project with your board connected you're going to enter debug mode switch to debug session now open the live expression window so located here click on it so live expression and we're going to add an expression so we're going to add a value which is actually our buffer so enter buffer and you see now you see your buffer with the eight different values so far you know there's like all zero initials to zero okay that's fine so on your board so PA12 is located right here so this will be on CN7 so this is the last one so down and on the right so right here so I connected a jumper from so PA12 that I can connect to the ground here first and then connect it also to VDD that is located right there so that's the number three on the left so three down on the left that's a VDD so at first we start by a connection to the ground so you can find a ground right here for example now we can execute a code so run a code with this icon and now I'm going to change you know the input of PA12 so this is for now connected to the ground I'm going to connect it to VDD so going to connect here and as you can see now in our table right there in our live expression of the buffer we see that every second you know we have FFF that has been displayed there so that's the conversion of the ADC so FFF which is because you know it's 8-bit conversion data so FFF will be the maximum and now if I disconnect and connect back to the ground we now see the new data that are being added to the buffer so this is because of the timer so every second and the DMA that made the transfer from the ADC data register to the RAM so inside our buffer again so if I connect to VDD you can still you know now that the values are being updated to FFF instead of 0-0 as before so our code is running properly as we expected so perfect this is a very good exercise and now you can stop the execution and close the project