Stm32 Intro To timers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so welcome to another one of my little tutorial videos and today I'm going to be talking about timers and this first part of this video we're gonna go over to the basic operation of a timer how to get it set up and how to get it counting and and from there we'll move on to other videos where we'll progress onto PWM output compare input capture mode which is very useful mode and well that's uh let's get it going then so to get a visual of what we're gonna be doing here I'm going to be using timer for just for this example again you can use any timer you want because as basic timers most of them can be used as a basic timer and from here you can see that uh Alzheimer's basically have four channels you can see you they all have four channels I'm gonna be using time before channel four and we're gonna be using what we're not gonna be actually in the beginning we're not gonna really use a channel we're just gonna get it counting up but once we get into the PWM and all this other stuff I'll be using channel 4 and that's going to be on on port B or B let's get the output here you go port b9 ok so that's how you're gonna that's what the hardware parts gonna look like we're gonna use a channel 4 on time before and port b9 okay so timers are a very extensive subject the datasheet alone has over a hundred pages just on the timers just because there's uh there's so many of them let's close this up here we go you have basic timers you have your real talk time clock which doesn't really count as a timer but I mean it does have to do with time so but anyways actual timer peripherals these three you advance timers your general-purpose timer and your basic timer the basic timer doesn't do much I mean it is very useful just as a as a no counting and stuff like that but the general-purpose timers and the advanced timers have a lot more features as you can see here capture modes output modes PWM and this is all the stuff that everybody is asking about so I'm gonna be using a timer for so we look at this section here and just look at the main features it's telling us adding up down it's an up-down up-down auto reload counter okay so that means we don't have to concern ourselves with our reloading it or restarting it because it does it by itself it has four channels like I told you for input capture output PWM output compare PWM in one pause mode now this would these four modes four channels it does not mean channel one is this channel two is that channel 3 channel 4 no each channel can do any of these so channel 1 can do that can do that that that same thing for channel 2 3 & 4 they can all do everything ok when you first kind of look at this you kind of get the idea that there's four channels so channel 1 channel 2 channel 3 2004 but no it's it's every channel can do all of these obviously not at the same time you can only set it up as one of these at one time but it can do any of these ok what else can we tell you about the the timer so yeah so if I could give a I guess a brief description of what you would use these for let's say input capture mode uh I've used that before I had a case where I have those let me pause it I'm going to show you something ok so what I was going to show you for input capture mode this is what I used it for I had a case where I have I have a couple of these controllers this one this is really expensive but you know you've all seen these controllers and you you press the knobs to go up or down or whatever and they they send out a signal to one of these guys these are usually the these are the receivers okay and if you were to look they don't really show the inside of it but there's there's these little pins here maybe this one should okay see these pins that's every line of those corresponds to one of the buttons or knobs on the controller so I had I was controlling let's say this lever moving it up and down it sends a signal to the receiver on one of those pins and I had those pins connected to my stm32 and I was using input capture mode to capture the signal that I was getting from from from that from that receiver and that signal that it produces is nothing more than a servo signal right because these are from model airplanes they use the servos and when I had the when I had the lever that I was not touching the lever this was the signal I was getting and then when I pushed the lever all the way one way I would get a one millisecond signal and when I pushed it to let the lever the other way out to get a two millisecond signal so that's all it was so to measure what value I was getting I was using input capture mode and so you're basically getting a value from one millisecond all the way up to two milliseconds where zero is basically right in the middle that the center of the waveform here is when you're not pressing anything when when it's see how it's centered so if you press it all the way up it would be that say to two milliseconds if you press it all the way down it wouldn't be one millisecond and I had that and I was reading it by the through the stm32 on the receiver and I was using it to control something else I think I was controlling the motor to have it go full speed forward or full-speed reverse so yeah so that's one very useful case for input capture mode let's see what else we got then you have output compare output compare is a good mode to generate I guess certain times certain delays and because you can use interrupts with this so what output compare does is it counts up to the counter is just counting up or down whatever and then you have a value like let's say 500 and it's it's counting up to a thousand so when it gets up to 500 which is your compare value then it outputs a signal or interrupts or whatever it is right so you use that to generate things like delays or stuff like that then there's PWM PWM well you guys know what that is and you guys know what you can use that for one pulse mode I've also used it one post mode it's it's like a PWM it outputs a signal but it only it only does one time so for example there was a time where I was using those sonic sensors that everybody knows um she I'll show you guys these one of these guys and they they required I think a 10 millisecond pulse on the trigger pin so what I did was I had um I had one channel and I would do a one post mode off and then I would do a ten millisecond pulse and then I would switch it to input capture mode right that that's for example here I had this I could have done it separately but I just had it all on the same pin where I had it first as our first adds one post mode I would do the ten millisecond pulse pulse and then I would switch it to input capture and receive the signal from the sonic sensor to get you know the distance or whatever it is so yeah so those are some I guess I use cases for these those from those different modes so let's look at the registers for the most part here also you know what let's look at the PW now let's not even look at PWM would look at PW and when I'm actually gonna do the video on it let's just look at the registers and we'll just get the timer going really fast and see what we can do it so here's the controller he's the first control register and here are some of the important things we're gonna be using here bit 0 the direction bit this CMS bit and when we get to the pwn we'll be using this bit but for now it's concerning ourselves with just these 2 bits the 4th bit here direction the counter used as a down counter or counter used as an up counter so you can control whether the it's counting up or if it's counting down okay and then the 0th bit is just enable bit that actually gets it going so if we just look at here's some code that I have for you guys so the first thing we're gonna do is obviously when using any profile you you're gonna enable your clock for that specific peripheral and you're gonna enable the whatever ports it needs now this is you don't really need to enable the GPIO B right now because we're not gonna output anything we're just gonna get it counting and play with the different modes but once we get to actually doing the PWM at that point then we will definitely need to enable the port B register so that we can get the output here and on our channel for for the PWM but at the moment you don't really need this again the alternate function you don't need it because that has to do with the pins this is alternate function for I know this is the audit function this is GPIO a I'm using it for my my print message to print to the comport and I have a separate video for that if you guys want to see that for debugging and that's what I'm gonna be using to show you guys what the timer is doing and as well as the logic analyzer to show you the the signals that it's outputting well but I'll get to this when we do the PWM okay so again I'm configuring the pins here but I don't need to do that right now because we're not going to output anything on the actual no I need that we're not gonna output anything on the actual pin so we don't have to configure the pins that's right now so really to get your timer going you just need these three lines right here right and we're gonna set a prescaler and an auto reload now what are these and what do they do okay so if we go to the datasheet we'll go to that prescaler register you know timer to register prescale so prescaler here it is and it's on the next page it just devised the frequency of your clock takes your clock frequency and it divides it by this prescaler right which is a number from 0 to 65535 so it's a 16-bit number and all it's going to do is it's gonna divide the clock that's feeding the timer so for example your timer or your timer let's say here we're using timer for which is on the APB 1 bus starting at 72 megahertz so we're gonna divide that 72 megahertz by whatever values in the priest we put in there and ultimately we're doing this to get a specific frequency that we need ok and then in our code we also have something right into this AR arbiter this is the auto reload register so this takes that whatever we got from here whatever frequency we got by dividing the microcontroller frequency by dividing it by the priests this divides it even more because now the counter is only going to count up to this value so let me explain what that what that looks like so here's some little graphics I created for you guys so first I guess we can start with some of the math here so we're gonna take our peripheral clock which is going to be the art crystal or internal oscillator that we're using to drive our my controller we're gonna divide that by the pre scalar which can be from 0 to 65535 and then we're going to divide whatever that output by the auto reload register and ultimately we'll have our timer frequency so what does that look like it looks like this we have the peripheral clock divided by prescaler divided by the auto reload register timer frequency okay algebraically speaking that's nothing more than peripheral clock this divided by pre scalar times that is that right the time of frequency so in a more useful version which we just take this and you know do regular algebra you can take your peripheral clock and divided by the desired frequency that you want and then you'll get a value and you just have to make sure that your pre scalar and your AR are multiplied to be that value so if you want a frequency of you know whatever let's say 2 megahertz or whatever let's say gives you a value of 10,000 okay so now you need to make these to multiply to 10,000 so you can put 5,000 and then 2 here right or you could put 2 here and then 5,000 over here doesn't matter as long as they multiply to be 10,000 then you'll get your your frequency that you want so per if o'clock divided by the desired frequency you'll get a certain value and that value you have to make sure these two multiply to be that value right it's it's not that hard and a lot of times you can just set this to 0 you know to just not have it pre scale it at all and you can just you know use one of them too you have to this has to be set to something because this is what your counter is going to count up to this way your timer is counting up to right so you can you cannot divide the clock but you have to count to something this is the value your timer is counting up to so let me show you another graphic that explains that a little bit better okay so here's what's going on in your what's actually going on so you're this is a register right which is right over here this registers right here so this register is basically this is the heartbeat of your of your counter this is that sounded terrible babe anyways this is the this holds the value that the counter is counting your timer's counting right because remember the timer is nothing more than the counter so this actually holds the value the current value of the timer and again it's all sixteen bits okay so what's going on your timer is going to count up from zero all the way up to whatever value is up in here it's in this auto reload register right so here's your time account right here's the value it's counting up it's counting up once it reaches that auto reload register value it's gonna go back to zero then it's gonna start counting up again go back to zero count up again go back to zero and you get it so it that's why you'd have to have a value if you have a zero here if you have no value here right it's just gonna you're gonna get a flat line output it's gonna be nothing because this you know what I mean it's gonna count up to zero go back to zero zero you know I mean so you have to put something here you can't have the zero this can be zero you can have a pre scalar of zero that just means it's not gonna divide your clock but you can't have an auto reload register of zero okay so yeah now there's different modes of counting and like I said before if you look on the datasheet here we go back to that control mr. 1 remember this direction bit bid for you can actually set it as an up counter which is what you see right here right this is what you see right here it's counting up and then it's overflowing back to 0 counting up so that's an up counter or if you set that bit to 1 it'll be a down counter and in that scenario it's it's the same thing but kind of I guess upside down so now starting at the value that you have in your AR register and it's counting down to 0 when it gets to 0 it shoots back up instantly obviously to the ARR register and then counts back to 0 up and then counts back to 0 okay so you can have it as a down counter also then there's a I guess I forgot to highlight these bits right here these bits right here you have edge align mode which is what we have right now which is the default they call it edge aligned mode and when you when you get into appeal PWM you'll see why they call it that and why they call these central line mode so in central line mode the counter counts up and then down instead of just down or up so in central line mode what you get is something that looks like this now your timer counter is gonna count from 0 all the way up to your value that you have set and it's gonna count down count up and then down instead of going up and then shooting straight to 0 no it's gonna count gradually down to 0 so it looks like this ok so let's see if we can observe all these behaviors in our in our actual code or actually I mean on our here honor where is it the accessport ok so right now I have my microcontroller it's running and I have the prescaler to anything trust me anything faster than this watch if I set it up to 100 I do this and we upload it it's happening so fast you it just you can't even tell what's going on because the frequency is just too fast so I have it counting up to 6,000 and it's all I did was I put that maximum value in the prescaler and I have the outer reload register to 6000 and I'm again in my while I'm doing is printing the timer count which is the current value of the counter so like I said it's gonna go up - it's gonna go take that away it's gonna go up to that value oops what did I do it's gonna go up to this value or gosh in my little file here got all messed up anyways it's gonna go up to that value and it's going to to go right back to zero so if you can observe that right here 2,000 3,000 4,000 5,000 it's getting up to 6,000 and then it's just going right back to 0 right after 6000 there you go see so again it's this kind of behavior now what we can do is go to the code and in this control register 1 right all we did was enable but we can add also this add this bit now which is right here now that's this is a zero currently but let's change that bit to a 1 so it'll count down right so then it'll have the opposite behavior so we can do time 1 control over just one and we're looking for this bit right there ok so now we have that bit activated and we'll get this this sort of behavior and did I upload it I'm not sure and there you go see it's counting down its gonna get down to zero and I'm gonna jump back up to 6,000 and it started counting down okay so now it can excuse me now let's go ahead and do the other not this right here would Center aligned and all we have to do for that is putting one into the dureth bit not this bit but deservers bit for for this section so this would be 0 this would be 1 for this section so just put a 1 there and we'll get center line mode and since it's counting up and down then this becomes irrelevant because you know it's gonna count up and down so it doesn't really matter what direction we put it so control register 1 with the CM Simas so we're gonna put our 1 into the the first bit oh no this is right here into the first bit which is bit zero and now we should see it count up to 6,000 and down instead of resetting back to zero so it's going down and now it's going up and then down and then up so now it has that this behavior that I showed you before what's going counting up and counting down counting up counting down so that in its most basic form is our counter and there's there's no PWM or anything here we're just setting up the counter and getting it going now in this video that's gonna follow right after this we're gonna jump from I'm gonna I'm gonna just cut this video off and then I'm gonna start making the video for a PWM so it's gonna leave off right where this one um and I mean it's gonna start off right with this one ends okay guys
Info
Channel: Eddie Amaya
Views: 34,726
Rating: undefined out of 5
Keywords: stm32, stm32f103c8, stm32 timers, timers, pwm, arduino, programming
Id: BtAi6-7Lnlw
Channel Id: undefined
Length: 24min 34sec (1474 seconds)
Published: Sat Oct 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.