STM32F4 Discovery board - Keil 5 IDE with CubeMX: Tutorial 17 Timers - Output Compare

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone this is me Muhammad yaqoob I'm making the newest video series about the steamboat timers this is requested by many people in the past so I decided to make some videos about them and explain what they're all about a steamboat tunnels or generally any microcontroller timers do three main functionalities output compare input compare an internal event triggers I will talk about them in three separate videos at this particular video I will talk to you about output compare output compare with the help of Wikipedia's definition a navigator an output based on a term Samba memory without interrupting the execution of the CPU and this is in fact a very useful feature which means that I can turn estelle event using the internal timers for example I can trigger an LED every half a second using timers without having to write that conventional code turn LED on wait half a second and turn it off wait another half a second so I don't have to write all of that I can just program the timer to do all of that for me at my demo today I'll total my 4 LEDs using the output compare functionality of the SDM timers so I'm gonna to call them in a cycle green orange red blue and continuously circular on them the way I prefer to do it as the I prefer to dimmer what using cuba make first and then get back to the diagram and explain to you what each part is doing because there are so many things to talk about and it will be much easier after visualization you will find it so easy trust me so let me start this way and open cuba mix on cuba mix first select new project and select right MCU stm32 or four seven in my case vgt and on the panel of the first thing i want to do is i need to connect my leds to the internal Tomas whatever time are available to that burn so for PD 12 I click on it it seems like the only available time for his timer for channel 1 so I click on this a same thing for BD 13 is timer for Perez channel 2 PD 14 of channel 3 and 15 is apparently channel 4 exactly I don't need to go here scroll down to time on four and enable it's a timer so you select the clock source and it will be enabled internal clock so for channel one it's going to be the output compared and you see this one will turn the green now and same thing for the others all of them are output compare channel 2 channel 3 channel 4 and this connects my external LEDs to time or for channels 1 to 4 now let's go to the configuration window and do some both date timer settings and it's timer for the first thing is to set a prescaler so the main clock frequency is 16 megahertz and I need to slow it down I want to divide it by 16,000 to slow it down to 1 kilohertz and I'll make the period varied is the counter so the timer for will count from 0 to whatever value is specified here I wanted to count to 1,000 which mean the after the prescaler being 16000 every tick is 1 millisecond and thousand texts correspond to one second and I've got four channels so I wanted to build my first LED the green one after 250 clock ticks which is a quarter of a second and at 500 I want to tickle the second LED and so on at 750 is the third LED and at one second I wanna throw that the fourth led and you've got circa the mode to be toggle toggle of match they supposed to go the whatever output connected to the timer on that pulse and same thing for the others so I think it's a good time to refer to diagram and explain why I bought 257 500 750 and 1000 I think it's a perfect time to get back to diagram so here's the diagram again it shows this pre square block so I've got my clock pulse coming from the internal clocks in my case is 16 megahertz according to Cuba Max and I as a priest quel of sixteen thousand to slow it down to one kilohertz each tick of one kilohertz clock correspond to one millisecond and I said the counter or the period to one thousand so the counter will count from zero to 1,000 and row beat and these smaller blocks the four comparators correspond to the 4 channel pulses that I set in here 250 500 750 and 1000 and what they simply do is that when the counter when the counter value gets to 250 this will tickle the first LED and will wait for the next 250 which is 500 and it will tickle the second one and we'll wait until this counter get to 750 and so on and this one will take a last when the counter gets to the maximum value of the counter will repeat and the cycle will start again so I hope now you got a better idea of what the block diagram content is I know back to the term configurations I think that's everything we need to do so click a and click on this icon to generate the source code give the project a name I'll call it a t I am Lucy for output compare I will store it at this location as team project select my correct ID mdk-arm P 5 and click OK and once this is done click on open project to launch Kyle macrovision and in here we need to expand it for applications are on Alban domain and as you can see Cuba next generation all the role of a configuration for us and all you've got to do is to write the main code and in here we just need to start timer for but we need to start each channel independently and with a function called hull GIM output compare start and this function takes two parameter it takes the time or handle type def and it's defined by qmx 40mph all defined as HT i ôm for and the second parameter of the channel and it's channel 1 I don't need to do the same thing for the other channels so timer 2 3 & 4 and that is all I need to do now I'm ready to compile and upload it neatly how they do react so let me compile it collected to put it to the board perfect this is working and that's how to implement output convert functionality of his TMT marks with the help of cuba max so that's all looking for you may stop the video here and if you found the videos well don't forget to Like and subscribe but I'm gonna carry on and I'll show you how to do this without cuba max this particular useful for people who want to learn more about level programming of this yen and specifically using color libraries so i'm gonna do it the first thing we need to do is to click on project and click on you micro vision project and we need to select a folder to store our protocols and and give the project a name now when you select the board STM 30 or 4 or 7 v dt and now we need to select the software component first in a system syscall as always and the device startup we need to enable the classic let's require some software components when I click resolve they will be added automatically I need to go to HTM cueball I don't need to add the time or software component which is this one others require DMA although I'm not gonna be ma but I'm gonna add it anyway that's all I need so click OK expand this holder and as you can see compact revision generated a few files for us and these are mainly the hard libraries but I did not add any main so I need to add the main manually so right click on this and add a new item to the Grove it has to be a C file and I'll call it main and the main file the first thing we need to do is we include the STM whole library header file and this will add the libraries I need to create my main function to int main void and a third while look at the end now we need to configure our TV I'll opens that is to enable pd12 to be d15 and set them as timer outputs and I'm gonna do this in a separate function I'll call it GPIO config and this function takes no parameter and returns no parameter and I'll define this function at the bottom here and inside this function we need to do the following I'm not gonna go step by step now I'm just gonna copy and paste the code in my earlier videos I still go step by step well I hope by now you already got the idea so I'm just gonna paste it and explain it to you line by line so the first one is to enable port D o'clock quite simple one and then initialization type death for the GPIO we use this type diff to configure the parameter it's used as a structure so my LED dots Bend and we select the best from 12 to 15 Paul nor polyp Noble down speed frequency medium and the mode to alternate function and the reason why I use alternate function is that for Steven board if you wanna match any pen to anything different than digital inverter digital output or analog has to be alternate function let me clarify this more if you go to the user manual of the extreme of for discovery board and go to the pen description table for PD 12 you see the alternate functions can either be efficiency time or for users so that's why we select alternate function and then the alternate is gonna be time or for because we have three options but I want to select timer for I hope this is I hope this makes sense I don't recall how GPIO enter to implement the initialization pretty simple one really and the next thing we need to do is to configure our timer and I'm gonna do it in a separate function - I'll call it t IM config alice function similar thing doesn't take any parameter doesn't return any promises but before we define the body of this fan of this function I'm gonna define ATM handle type def and I'm going to find it globally because we're gonna use it in the main i'll call it t IM for handle and this is used to configure the time it does a similar thing to my led here for GPIO but except this one is for timers so let me define the function TM config down here the first thing we need to do here is to enable timer for peripheral clock and then we need to do some basic initialization to the base timer so to select an instance to be timer for clock division to 1 counter mode is count up so it's gonna count up from 0 to whatever period value is prescaler to 16000 just like what we did in cuba max to scale the clock down to 1 kilohertz and pair it to 400 I don't recall healthy I am base in it to implement those initializations the next thing is to configure the source clock and so we need to do the following we need to define a clock config type def similar thing several are due to this but this one to configure the clock source and we need to select the clock source to be the internal clock and then call this function to implement those initialization there isn't just only one initialization here I want configuration so to speak then we implement the output compare initialization we just call an output compare init function and we'll do the default initialization for the output compare clock the next thing we need to do is to configure something called master configurations to select the trigger output to reset and master slave to disable this is describing detail of the date sheet so I'm not going to talk much about it and by the way if you want during where did I get this from and how can I know which function to call him first they're all described in the time our driver function the driver file rather so if you open the whole time or driver file and scroll down to learn 48 where it says how to use this driver you will see the steps are described very clearly we first enable time our clock and then we do divide initializations time initialization configure clock source time base image and so on so I just follow the same ones and I summarize everything right so finally what we need to do is to configure our output combat channels because each timer has got four channels and we won't use output compare and we connected them to our LEDs you need to configure them first thing this this configuration applied to the four channels so disable fast mode the output compare mode to toggle so it's going to toggle the EDS each time the comparator triggers and polarity to high which mean active high then we set each channels as pulse so for channel one I would supposed to be 100 so when the counter get to 100 this will trigger the first LED and similarly for the other channels so they I want the second channel to be 200 third channel to be 300 and 400 for the last one now we're done and just need to call those functions in the main to implement the configurations so the first function I want to call is the hull in it this is a function and the out driver will initialize everything to the default State it's quite useful to call it at the start of every program then I'll call the talk of the Gaussian functions that I defined so G by a config and T item config and then I need to start the timer for each channel a function is called hull GIM output compare start and it takes two parameters it takes the GIM handle type def which is which I called it t I am for handle understand parameter of the channel so T I am channel 1 I need to cook I need to call it four times for each channel so this will enable timer one channel one channel two or three and four that's everything we need to do now we're ready to compile and upload it to the board compiled successfully without any URLs and with one minor warning which is not going to care about now let's upload it to the board and look at the LEDs so click this icon to load it to the board ok so the compiler the debugger is wrong so I need to go to options for target and go to debug window and select a stealing and go to settings as well trace and enable try set the core clock to 16 mega similar to this teen one I know it should load properly perfect now let's have a look at the board okay this is walking to accept I've just noticed that it's walking much faster than the previous one unless assembly because I set the period to 400 instead of 1000 and I took out the LEDs at 100 millisecond intervals but last no problem it walks the functionality works the same so that brings me to the end of her library demo and I'm not gonna take you very quickly through the direct register access method as well I'm just gonna copy and paste the code I'll try to explain it to you very briefly I'll put a link down in the description for people who are interested but I'm not gonna do it step-by-step okay so for the productid store access method i've deleted all the main so I'm gonna paste the dark to gesture method the of written in advance so here it is it's doing the same thing using two functions you buy your config and T&M config and if you buy a config I'm doing everything using it dollars to adjust your access method if you watch one of my earlier videos you will have explained how to do with a TBI or one but I did not go through the tee item config in my previous videos but this is not too complex all you've got to do is to search for the item for on the date sheet and look at that register and you'll see the content of control register one prescaler register and auto reload status register and so on so an outer element are commented to the best I can so you see I've written lots of comments I'm gonna put it down the description for you just follow a tree decline pile on if it didn't work for you or if you had any questions feel free to ask me down in the comments but I'll leave that for you to try it out now just to convince you that it works the same I'll compile it and load it to the board okay compiled without any errors and not loading it to the board perfect let's have a look at the board okay very good this is working as well so I've shown you today how to use STM for Thomas and output compare mood using three methods using cuba max using haul drivers directly and direct register access and this brings me to the end of my tutorial today if you found it helpful don't forget to Like and subscribe and thanks for watching goodbye
Info
Channel: Mutex Embedded - Education
Views: 18,119
Rating: undefined out of 5
Keywords: STM32F4, ARM Cortex, Embedded, Microcontroller
Id: NXGjhOa5oR8
Channel Id: undefined
Length: 17min 59sec (1079 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.