STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 18 Timers - Input Capture

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is the second video on STM tiles tutorial series and of this video I'll talk about in vulture mode and what capture will record a timestamp in memory when an input signal is received as opposed to output compare that we talked about in the first video where we use on stamp and memory tutor and out event I want to show you how to do this with and without cuba max the second part would be particularly useful for students who need to learn how to do this but are not supposed to use the cuba eggs in my demo today i'm gonna connect my user push button on this DM to one of the timers and we'll set this up as input capture so the way it's gonna work as that hunter will count from zero to whatever maximum value I set it to and the instance I press the push button because the register will talk Haunter value at that instance so when I press button when the counter is 247 then n capital register will hold the value of 247 okay I hope you got some ideas I'll start the demo now with Cuba Max and I guarantee you that you'll find it very straightforward okay as usual click on new project and select the right board is team 30 or 4 or 7 of this case and it's VG t and for the pin out I need to enable my push button which is connected to be a zero and tied it internally to one of the timers so when I click on it I have the option to connect it to time or 2 or time or 5 I'm just gonna connect it to time or to channel 1 and you need to go to time or 2 here scroll down and you need to enable the select a clock source internal clock and set channel 1 to n book capture mode n book capture direct mode that's the one we're looking for and that's it now let me go to the configuration window and do some of the timer settings so click on TI m2 and the first thing here is I'm gonna set a prescaler of 16,000 so this will scale the clock down to one kilohertz and then I'll say a period of I'll say a very long period thousand because Perry is just gonna count from zero to 100 thousand until it gets pulse from the push button so I have a period of 100 seconds till I press the pause button if I didn't present between this one gonna restart anyway and then I need to select the polarity selection - rising edge because I want to take out the input temperature 1 3 at the rising edge of the push-button signal and it's a direct no division and input filter onset 15 so this is a low-pass filter to remove some of the switch bounds so 15 is the maximum filter value and I need to enable time or two global interrupts because I want to do I want implement few codes when the interrupt happens I need to record the counter value and reset the counter so that's why I would need to enable it you will see in the moon alright my main you will see I'll write a callback function so that's all I need to do here let me click OK and generate the code give the project a name I'm gonna call it t I am in what capture I'm gonna store to this location choose the right IDE Carlmont revision 5 in my case and click OK once the source code is generated click on open project unless lunched called micro vision and here we need to expand this folder and go to the main on the main you see cuba max generated all the low-level codes for us enabling the clock setting up the timer and the GPIO configuration all we need to do here is we need to call T I am in bot compare start as interrupt and we need to define a callback you start the timer is in a function called healthy I am in Port capture start as interrupts and this takes two parameter takes the timer handle type def which is HT im2 as defined by cuba mix here at the top and the second parameter of the channel and its channel 1 now I need to define the interrupt callback function there is a template defined and the timer draw I need to modify it and add my user user code and you can find this when you go to to expand the drivers the hell drivers rather and it's in this driver go to functions and the timer let's see you should find a input capture callback this one so I'll need to redefine it again in my main and both my own code but will redefine it without that weak object so paste it in there without the weak obviously can put your own user code in here and what I want to do here is that I want to check if this interrupt coming from timer two because this callback is universal for all the timers so I want to check if it's coming from time or two and then I want to get the current counter value and then we'll reset the counter and that's how the code should look like so HTI MDOT instance if it's time or two then we proceed and get the current counter value and store it in a variable that I would like to call input capture value but I need to define this globally so that I can watch it while debugging and it's unsigned 32-bit variable and then use this function healthy I am set counter to set the counter to zero again I'm just wanting on a clarify in case you lost this interrupt invert capture callback will be called whenever the input capture block detects a rising edge on the watch button and that's effectively when I press the post pattern the SDM scheduler should jump into this get the counter value and reset the counter okay so we're almost done except I also need the value of the counter continuously in my while loop just so that you see it's going to be reset whenever I press the push button okay so counter value equal to the function that gets the counter value but I would need to use the proper 100 I have different here and I need to define this variable as unsigned 32-bit globally again now that's pretty much it so now I'm ready to compile and load it to the board so alright compiled without any errors now let me load it to the board and I'm gonna use STM studio to read the values instead of Carl debugging mode because Carl debugging mode is very slow okay so it's loaded successfully now let me go open STM studio I have it in here and if you haven't got schemes told you please install it okay so the first thing we need to do here is we need to add in your project click on this and go to the location which your file is stored hopefully I'll remember mind ok so this is where my folder is I need to go to mdk-arm TI mi c 1 and select this axf file now this will add all the variables in my project and I want to watch two variables counter value and n both capture value so control still highlight both of them and click on port and close so this will be added ok here are they so I'm gonna highlight both of them a righty click send to variable viewer I'm gonna select table just click run press the reset button on your HTML you should see it running okay so there's my counter value and when I click the button you see the value gets down here and the counter is reset so again 1000 2000 1800 and the counter reset so have you got the idea so this is a way to detect an input signal a rising edge of an input signal but I use my possible joint for simplicity and this is how to implement invert captures timer with cube X so if you are one of those that just want to see q-max implementation you may stop the video here and if you found the video helpful don't forget to like and sub crowd but now I'm gonna show you how to do this without Cubans using hull libraries very useful for students as I mentioned early so I'll go back to Cal macrovision and start everything from scratch so you need to go to a project a new micro vision project select the folder to store your files in I'm going to throw them in this folder and I'm gonna call it t IM and no cube and now we need to select the device it's a steam 30 or 407 VGT and click OK now we need to select the software components the first one we need to enable is the symphysis core then the device is startup cube framework we need to enable classic which require which would need other software components that we can enable but click and resolve then I just need to enable the timer software component and this required DMA although we're not gonna use demate but I'm gonna enable it and click OK okay so Cal macrovision added a few files to the project mainly the hull drivers by need to add my main functions to add a new item to the group it's a C file I need to call it main I'm gonna go here very quickly the first thing I need to do is I need to include the hull library hit a file it's stm32f4xx Sarge underscore hull dot H I don't need to create the main function most of these tips are repeated so I'm going to try to go very quick here there are two main things I need to do I need to enable my TBI open PA 0 and set it to alternate function time or two and I need to configure time or two and I'm gonna do them in two separate functions that I called you Biocon flag and t IM config and i'll define the first function at the bottom here so d bio config is defined as follows the first thing i do is to enable port o'clock because using PA 0 as our timer input and then I any type diff structure that I can use to configure the parameters so the pen is zero mode is alternate function because I need to link it to a timer and timer is one of the alternate functions the nopales novel down speed low and the alternate function is connected to alternate function one which is time or two and I call NT bio-energy to implement those configurations I no need to define the T I am called the function and I'm going to find it here below and the function is the follows GIM handle I need to define time handle at the top as well t I'm handle is a type diff that used to configure at the time are similar to 0-5 diff but this one more time outs and I need to define this globally because I'm going to need to use it in the main so I need to define it at the top somewhere here now that all should disappear and all this function is doing starting with enabling TI m to clock using this function that you might be already familiar with from the last couple of videos and then T I am to base initialization to set the instance to Tomas two o'clock Division two one counter mode to count up from zero to whatever period value is prescale up to 16,000 similar to I explained in Cuba max and period to 100000 underneath call T I am basinet function to implement those initializations the next thing is to select clock source so click source is the kernel clock you might weather from Cuba Max and then I call this function to implement this configuration and then the we initialize the end what captures would initialize the handle type def somehow but it's not going to add any configurations but important to call it in here then master config so we can have the trigger configuration to reset disable master/slave mode this can be described in diagram if you get back to the dock clock diagram you see the top layer trigger controller so we we disable slave mode and we set the trigger to reset and we do when we do reset it will be connected directly to the a PP one o'clock which is the internal clock that we set up a few lines of code earlier and by the way if you think I'm going too fast I'll recommend that you pause the video and try your best but in the worst cases I'm also going to put the link for the code down the description so if you try it yourself and didn't work you can always refer to the original code okay alright so the next thing is input capture configuration these can be clearly described in the diagram again so input capture will total 15 polarity to rising edge prescaler to 1 selection is the direct side the diagram the first thing is the invert filter this block and edge detector H detector is rising rising edge prescaler to 1 and the selection to direct which means the input T I am in but one gets connected to invert capture one but if it's not the direct then we can connect eim 2 to n but capture one like select the second option that's what direct means and again this function will limit those connection parameters album here we enabling the timer to interrupt and all of this stick interrupt and now I need to add the callback function I'm just going to be copy and paste it because I explain it to you in a cube of X this one require a variable that I'll define here to answer 32 but I explained all of this in cubics in addition to that we need to add timer we also need to add the timer irq handler it's in two lower levels than this and Cuba mixer was added in good IT file but we don't have that folder here so I added I have to add it manually same thing for systick interrupt I also need to add the irq handler for it or rather systick handler in this case and finally we need to write our main function so I need to call in the configure functions at the top here starting with the hull in it and then TBI config and t IM config then how t IM i see start to start the timer as input capture interrupt and get counter value inside the infinite wire loop and this will require a variable that called counter while that I need to define at the top here and that's all so let me compile it and upload it to the board okay compile without the arrows now let me try to load it to the board yeah as expected I need to go to options for target and select a stealing debugger instead so options for target debug and select st-link debugger and go to setting and do trace enable and select the clock core clock to 16 mega it's similar to the HTM one and now it should load successfully perfect now I'm going to go to a steam studio and try to watch the values honesty mode you need to start a new blank project and add the file so click on this this one and select the right folder and I start the project file in a folder called no cube and I need to go to object and select the dot axf file now the variables will appear here and I need to watch two variables counter value and invoke capture value and I'll click on port and they're added here so I highlight both of them right clicks into a variable viewer and select table okay so just hit run and click the reset button in your steam board and when you click the user push button the blue one you see the value will get hold from the counter and the counter will reset so now it gets to 30 something so 34 and so on so this one works as well and this brings me to the end of my tutorial today I've shown you how to implement input temperature timer with and without the Cuban eggs and if you found it helpful and if you wanna see more videos like this don't forget to Like and subscribe thank you very much for watching and I'll see you later
Info
Channel: Mutex Embedded - Education
Views: 22,166
Rating: undefined out of 5
Keywords: STM32F4, ARM Cortex, Embedded ARM, Microcontroller
Id: z3VBbj42NPs
Channel Id: undefined
Length: 17min 11sec (1031 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.