STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 6 ADC Multi channels - Updated Nov 2017

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
however Andaz muhammad and of this video are updating the ADC multiple channel or scan mode tutorial if you recall from the last two videos on ADC we only discussed the case where we have a single analog input and that was relatively straightforward however if you have multiple analog on boats things become slightly more complex and the main source of complexity is that the ADC has got only one output data register and only take a single analog invert at a time and this means that we are multiple analogs then it will take the first channel and stored in the out day to register and then converting the second channel will overwrite the with a register and that's what the problem is now there are many simple ways to solve this but they are ineffective forests and the simplest one is to use the single conversion mode that we talked about in the first ADC tutorial and that's by linking the first channel with the ADC get the value converted read the output data register and then start the ADC all over again but with a different channel this is possible but it's not efficient because it takes a lot of CPU cycles because you have to start the ADC again wait for the ADC clock to stabilize and you already wasted a lot çb your sources but the scan mode is a hardware solution so the people who designed the SDM chip already thought about this problem and they design you a nice straightforward way of solving multiple channel problem and that's what I'm gonna attempt to explain to you at this video so well what scan mode does is that it employed two things it employs something in the input and something at the output side on the input side it uses something called a sequencer and the most basic sense what this does is that you store the sequence of channels that you want to convert and the sequencer table and it will schedule the conversion one at a time and on the output side we link a DMA from the output data register of the ADC to the RAM memory so that at the end of every single conversion the the data will be sent to their armory via DMA so that they don't get overwritten let me give you a very easy and simple example to simplify all this out let's say that you have four potential and it connected them to channel zero to five and eight the first thing you need to do in scan mode is you need to program the sequence register to schedule channel zero two five eight and the right order then you've got to link the output data register to an array of four bytes in this case because I have four in volts so that is store the results in that array then you start the ADC was simply gonna happen is that the result of channel zero will be stored in the first index channel 2 in the next index and so on and so forth I'm sure you've got the idea by now so without any delay let's straight start on Cuba mix and this would hopefully clarify anything that you may have missed here allows you to click on your project and select match board - team 30 or 4 7 v GT and now we need to enable our analog pins I've got three potentiometers and the input and I connected them to Basel wanna do I've got to enable PA 0 city as analog input so I've got to choose the same ADC and scan motor a DC 1 input 0 a DC 1 input 1 and a DC 1 input 2 so I've been able all my analog inputs next let me go to the configuration and here we're gonna configure our ATC parameters so first thing I'm gonna set the prescaler to 8 so this will divide the ADC clock by 8 and I've got I want to change the resolution to 8 bits you've got to enables commode it's quite important here because that's what this tutorial all about and I want to have continuous conversion so continuous + scan mode mean I will keep scanning through the channels continuously and I need to enable DMA because that's how I'm gonna transfer the data from the output register to the memory so this need to be enabled and I'm I'm doing 3 convergence I have 3 inputs and the first convergent here you see the secret I talked to you about the sequencer register and that's how you said that you said to throw something Quran so rank one the conversion that will be converted first I want this to be channel zero unless indeed already set up to channel zero I want to change sampling time to 480 I explained a lot about this in my previous ADC video so check it out further than what about the sampling time and the rank two the second converted channel I want this to be channel 1 by default is set to channel 0 so you've got to change it to channel 1 and again the cycle to 480 and rank 3 a third and final conversion is channel 2 and again same for the sampling time next we've got to do things and we need to add a DMA cubics don't add it automatically so we've got to add it manually the only option is ADC one and only change the data words to buy it because my ATC resolution is 8 bits and that's just about mama increment is correct because I wanna store the next channel into a different location in the array so a memory increment will ensure that it increments the index of the array and on it select the mode to circular which mean it will circle around the array if we go to the end of the array it will store the next value again on the first location of the array and that's what this means so that's it for the ADC settings click OK and we're ready to generate the source code you generate source code by clicking on this icon and then click on open project and I'll take you to kalmyk revision IDE on Cal micro version you need to expand this folder and go through application user and open the main and as you can see cubics have generated the low-level code for us and all you've got to do now is just to start programming the first thing I need to do is I need to declare a variable to store the ADC values it should be an array array of 8 bits because ADC resolution is 8 bits in my case so I'll call it ADC well array and all be an array of 3 elements because I've got 3 inputs then I need to start the ADC as dmeir those function called hull ADC start as DMA and it takes 3 parameters the first parameter is a handle tab diff let's define party remix at the table so this is the ADC handle table if each ADC on the second parameter is the destination of the data where you want to store the ADC date then and sure enough I wanna store them in this ADC valiree i defined it as 8-bit but this one expect a 32-bit pointer so I've got to todd cast it then the data size three bytes so I'll write three and that's simply it that's all you need to do really and I've just got to call this function once at the start because I enable continuous mood if I didn't enable continuous mode I've got to call it whenever I want to get a series of channels converted so that's it I can now compile the code compiled without any arrows warnings now I can upload it by clicking on this button successfully uploaded now I can enter debugging world to watch the ADC values and on debugging mode the first thing I need to do is I need to add this variable to this array to watch memory so that I can see it in real time so I can see what the ADC values are I'll disable Hicks display and I'll expand it okay I've already connected my potentiometers I've got one I've got three potentiometers already connected them now I can start they the code okay perfect playing around with my first or second one so minimum 0 and maximum 255 and then the second one I think it was I thought you lose yeah minimum zero maximum 255 another third one minimum and maximum perfect all of them are walking so scan mode works perfectly I must simply help to implement ADC scan mode with the help of Cuba mix is absolutely simple and straightforward however now I'm gonna show you how to implement this without the help of Cuba X but if cubics was all you're looking for you may stop the video here and don't forget to hit like or subscribe but now I'm gonna show you how to do this without cuba mix and to do this without qmx we've got to start everything on carlac revision so click on a project unselective your micro vision project you need to select a location to store the file now I've got select the board esteemed 32 of 407 and I need to select these software components so first thing I need to enable the core bit and I need to go to device and enable startup STM cube framework and I will use a classic framework and you've got to enable some other software components that this classic depends on if you click resolve they will be enabled automatically so click resolve next I need to enable ADC because I need to use a DC I've got to enable DMA as well so resolve I can I think that's all I need so I'm ready I can click OK now and as you can see a karmic revision included some software libraries that you would need and there the hull libraries however kalmyk revision doesn't add um a in files you've got at our main manually so select see file and name it main and in my empty main i need to include the header file of the hello every and also write the main function as follows then i'd need to setup my TBR opens I've got three potentiometers connected to be a zero PA one and PA too so I've got to enable those three pens and set them to analog and I'll do this in a function called GPIO config function takes no parameters and return no parameter and in this function I will define it to the bottom here so if you only get an idea on how to set up a debugger config I described this in my last ATC tutorial but I'll do that again see you've got you go to the GPIO library and it's very well documented and will take you step-by-step and how to set up a BIM according to this description how to use this driver the first thing is I need to enable the board clock I need to enable the port clock and this case is bought a then I need to do the config using t-value any type def and then pass this as a parameter to this function and I'll call it my pens config or in it and then I use this one just like using a structure to set up my pin as analog so you will see now if I do this one dot I can select the pin and I want three pens GPIO pin pin 0 1 & 2 I can use this all sine to do all of them at the same time then I can select the mode mode is analog obviously so GPIO mode and you can do the auto text by control space and all suggest I need analog that's all I need now I can call in the function according to the driver and pass this configuration parameter and it will implement them the GPIO init function takes two parameters it take report which is both a and the handle type diff that we use to initialize the pin and it's a pointer to that type diff so I need to do ampersand sign of then paste the name of that an adaptive about simply how you configure the plan and set it to analog now we're gonna configure our ADC device using a similar method so I'll define a support function to configure my IDC I'll function takes no parameters return or parameter similar thing and I'll define it at the bottom here and although the same thing will refer to the ADC driver to read the instruction on how to use this driver I'm sure by now you've got the idea if you use anything in the whole driver the easiest way is to refer to the driver self so refer to a DC driver and see what suggests and clearly here how to use this driver first disturb is to enable a DC clock a DC peripheral clock and we using a DC one so we need to write one here the next thing is to configure the ADC parameters so things like resolution data alignment prescaler and so on we use this function to configure those parameters but this function will be called at the end unless function expect a handle type death an ADC handle captive for this one we need to define it because we're gonna we need to use it in the main so we need to make it a global variable so ATC handles our death I'll call it my ADC handle so we need to do this one to configure the ADC parameters so as usual dot the first thing is the instance this will select hey excuse me which ADC the file is visiting I'm sure enough it's a DC one then we'll do a series of initialization the first one perhaps is the clock prescaler and I want to set the clock prescaler to eight just like what I did in Cuba max and then the resolution and all the others I'll do them all possibly do now I'll copy and paste them just to save time okay so here are the rest of the initialization for the ADC clock prescaler to eight while another clock pushkin is deleted resolution to eight bit scan mode enable continuous mode enable discontinuous mode obviously disabled external trigger non data alignment right number of conversion to 3d error quest must be enabled and end of convergence election is at the end of every single conversion actually I want to change this to the end of the series of conversion yet sequence conversion and then when you finish with this he called the whole ADC in it to implement those configurations so earning capacity handle or a pointer to the hunt alternative and I'm done and the next thing according to the ABC driver is to configure the channels and we use a different function this time but I'm sure you already got the idea that we are going to call the function at the end we need to use some sort of structure and it's a DC channel conflict type death so I need to define it I don't have to define it globally because I don't need to use it in our anywhere else so ABC channel config type def yep I'll call it my ADC channel ID on this one my deceased an old channel Ani select channel 0 first and then the rank to one because I want channel one to be converted channel 0 to be converted first that's why I need to set the rank to 1 and the sampling time I need to set the sampling time to 480 as I did with Cuba mix yeah I wanted to call in the ATC config channel function to implement those conversion parameter for channel 0 and then we'll do the same for channel 1 so this one takes two problems that it takes the ATC handle type diff the original one and a pointer to that now the second parameter is the channel type diff it uses both of them for some reason so that's it no and it could be the same setting but for channel 1 and 2 except I need to change the channel and rank so for channel 1 the wrong could have to be 2 because this needs to be converted 2nd and same thing you calling this function multiple time to implement the specific sitting for that specific channel now for channel 2 the rowing could be 3 because that's gonna be the 3rd converted value so that's all for ADC configuration and finally we need to configure our DNA and I'll do this in a separate function as well call it D Meg config but this time it will take a parameter we'll take a DC handle time def parameter I know you will say that it's already globally defined but there is a reason for doing it this way and I'll define it at the bottom here and the first thing to do as as always with all hell driver is to enable the demon clock but there are two dmas and on this DM so you'll first need to read the date sheet slightly and see which DMA the ADC Hardware is connected to so going to the datasheet reference manual rather let me scroll down to the DMA section and you need to go to deem a channel selection and according to these two tables DMA two table shows that a DC one is in fact connected to the MA - so I need to enable the MA - then according to the DEA driver I need to open the MA driver it says that I need to do some initialization by calling hull DMA in it just like what we did with the ADC call it at the end and this function takes a one and only parameter D may handle type diff call it by DMA TARDIS and we use this to set the DMA parameters so this handles active docked instance and that's gonna be deemed a DMA to stream zero and then they will do some visualization the first one perhaps is the channel on this channel zero according to this table in the date sheet its own channel 0 because stream 0 has got eight channels and we've got select channel 0 yeah I'll do a series of initialization I'll copy and paste them to save time and I'll explain it to you so beside channel we also set the direction to from peripheral to memory because we transferring data from a peripheral device which is ADC to the memory and profile increment is disabled so we don't want to increment the address of the profile it's gonna be the same peripheral memory increment is enabled because we need to increment the memory location to store them in a different memory location four different channels a peripheral data alignment to white memory data alignment to bite as well this is rather the data size and the reason why we bought byte is because our ADC set to eight bit position to eight bits which is just a bite we enable deem a circular so that continuously automatically go back to the first position in the array and deem a priority to low and we disable the FIFO and now we need to pass this to the function to implement those parameters or those configurations I also need to link the DMA device to the ADC and that's just this will just link the two hundred times if together so that when we start in a an ADC transfer it can refer to the start if and get the DMA settings and vice versa so this doesn't do anything on hardware I just do the software linking of the two doctors together one final thing going to do and the demon sitting is to enable the DMA interpreter will use these two functions to enable the DMA interrupt so the first one was set the perfect priority of the interrupt and the second one would enable the interrupt and what demand table does is at the end of every DM a transaction it does things like address increment and so on so that's all for the a configuration we converted the DMA device we link it to the ADC type death and we enable its interrupting now all we've got left is to call in those functions those configuration function and the main to implement the configurations the first one is to call hull init function this one is a as well initialized everything to the default value it's a very good practice to call this at the start of every program and then I obviously need to call in d3 function and this one takes this parameter so by calling these three functions I would set up my pen configure my ADC and configure and enable my DNA then I need to call ADC start to start the ADC transaction as DNA unless expect three parameter the first one is 8300 kamdev and then the destination where you want to store your data and undersized so I need to define a variable here I'll call it it's an unsigned 8-bit because my data are invites and it has to be a an array of three elements because I've got three inputs in fact similar to what we did in cube of X but because this function expect unsigned 32 again I need to type casted to 32-bit pointer and the latest size to three so this would start the DMA get the volley converter and I store them automatically into this array so I think we're done we're ready to compile the code and upload it to the boss so let's compile the code now and see if we got any errors or warnings okay there was one bug that we need to fix before we get into debugging mode I just found it now and that is the main handle time if this was defined locally but it's meant to be defined globally because it's going to be used by other files as external variable so we need to define it as a global variable in this file now we should compile it again upload it to the board and back to debug mode I know it should work okay so only button more the first thing is we need to add this variable to the watch memory so that we can see it I will disable the hex display and expand it so let's run the code it straight away okay that is walking so let me play around with the first potentiometer yeah exchanging zero to maximum 255 and then the second potentiometer 255 0 and the third potentiometer still 5 5 and back to 0 so scan mode is working perfectly and this is how to implement ATT scan mode using Hull drivers without the help of cuba mix and if you didn't follow along with the code that's fine i'm gonna link this down the description so that you can just copy and paste it to the main and i'll do the same thing now to the third part really quickly i'm gonna show you how to do this using direct register access doctors districts this is very complex but I'll give you a rough idea and we'll see how that goes so the first thing we need to do if you want to use the I'll delete all the main code because we were to write all of that again and what the director district says you don't need any of those health drivers so you can disable them so I'll go to the software components out I'll disable all the hull drivers I'll leave the basic one but I'll disable all the hull driver so stm32 cube hull I'll disable all of them and I also got you this all the classic so I just need the star top and click ok so you see all of them disappeared now I have already written this in advance so I'll just copy and paste it I'll show you how that works and here is the code for the direct register access might look horrible but it's not too hard if you know the idea so the first thing is we include stm32f4xx without the whole extension so this is not the hard driver this is just the basic STM driver we had we have the same function prototype almost the same except we had to define our own start a DC function because you're not using how la drivers anymore and down the debugger configuration is we directly accessing our CC register to enable port o'clock and GPIO modal register to set the mode to analog so when you go to the datasheet or reference manual and go to TBI registers so you have this modal register that can select the analog mode and you can also go to the RCC register for the clock things do a config similar idea so you go RCC registers to enable any clocks you've got to go to the DMA register if you want to set all of these settings I can walk you through all of them but I will take me at least half an hour so I'll leave that for you to discover it the idea is you look at the name of this register and the reference manual and see what the settings are I don't have a written description on the queue and comments as much as I possibly can so you can refer to my comments and refer to the day cheat at the same time and last for ADC configures well I talked a little bit about this in my previous ADC video but this is far more detailed and I have some comments so you can read the comments and go to ATC one register and see what I'm exactly doing in here and this is the routine to start the ADC what the team aim well has got something like clearing some bits and setting source address and so on so I'll put this I put a link for this down the description so that you can or you can read it in your own time for now I'll just compile it and upload it to the board I'll show you that I actually walk okay compiled and uploaded successfully now let's go to debug mode and see that we get the same pattern okay in debugging mode our variable is already in the watch memory so you can start straightaway the effect you see the values are appearing there so it's walking out by all my potentiometers and the third one okay all nice and good all working and that's how to implement ADC scan mode and the directory structures method and this brings me to the end of my tutorial today I hope you found it useful please don't forget to hit like and subscribe thanks very much and I'll see you in the next video
Info
Channel: Mutex Embedded - Education
Views: 30,126
Rating: undefined out of 5
Keywords: STM32F4, ARM Cortex, Embedded ARM
Id: 23tILSLmQLw
Channel Id: undefined
Length: 27min 16sec (1636 seconds)
Published: Tue Nov 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.