Stm32 Timers in PWM mode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay cool so now we're gonna be talking about PWM so from the last video like I just said it's gonna we're starting off right where we ended with that video so we have our timer and it's doing this this sort of behavior it's counting up and then counting down and you can see it here on my output screen now it's counting up now it's gonna count down so it's doing this sort of behavior but for PWM you can use it in this mode too but I'm gonna go ahead and I'm gonna set it back to to the regular mode where it's just counting up that means this guy's gonna go away that means we're gonna leave the direction bit to default right here so we can have an up counter okay so here's how PWM is gonna work here's the base I'm gonna give you the basic of how it's gonna work and then we'll get more into the what the actual bits and things that we have to set right so we're gonna be using counting up okay and this is how it works remember there's four channels so I'm gonna be using channel 4 and time off on time or 4 so this right here is the capture compare register for and that is right here see these four these are the four different channels that can now put four different things for the same exact timer right and we're gonna be using this one okay this time or capture compare register for and that's because I'm using channel 4 and again channel 4 is going to output on port b9 timer for channel 4 ok again you can choose any time or any channel you want but that's just what I'm using here oops where's mine there it is ok nope not what I wanted so this is how this is going to work so as you recall from the previous video you're your counter is counting from zero up to whatever value you have here in the ARR register so what's going what happens is that you have this you have one of these for every channel so let's say for example we said channel 4 to eat compare register 2 equal 5 so what's going to happen is that our PWM output is going to be like this we're gonna have a high input so long as the timer counter this value right here is less than this value that we have here right so you can see right here we have this high output during the entire time that this value is less than this value once it becomes equal to that value our PWM signal goes down and we're gonna be down for the rest of the time int'l again timer counter which is now 0 at this point into timer counter is less than channel 4 value right there so and then so this is the PWM signal that you're going to get on your output pin so as you can see if I were to set channel 4 to something like 2 that's right around here that means we would be high up to here right up to here and then we would be low for this rest of the time so then we'd have basically a smaller duty cycle so as you can see that tells me that this controls my duty cycle okay because 0 is here and whatever value we put here whether even if it's 0 I mean whatever value this is it's it's always going to be for example if I put 0 here then we're just gonna get a flat line because timer counter and these they're gonna be equal and when they're equal the the signal drops right so we're gonna get this flat line if I set this counter register equal to the value of whatever is up here right at this register then what we're gonna get is of also a flat line but at the high value right up here and if I set this to the middle whatever the point is that this right here controls your duty cycle this says how how wide your high part is okay and this right here as you can see it controls your frequency this is how often this is happening right along with the prescaler obviously but for the most part you're not gonna be changing your prescaler all the time or you can it whoa both of these are gonna be fixed for the most part so this basically right here controls your the frequency of your PWM and then this register right here controls the duty cycle of the PWM now if you take a look at our data sheet that's exactly what they tell us if we go to functional description let's go to PWM mode so it tells us that we have two that the frequency is determined by the value of the ARR register like I just said it doesn't mention the prescaler but that becomes that's pretty obvious because well it just does according to the last video right and the duty cycle is determined by the value of the CC rx register where the the X corresponds to the channel that you're working on okay one thing about the channels before we get to this part which is also important if you look at your registers here's register 1 2 3 & 4 these correspond to your different channels so whatever whatever value you're going to put let's say you can have a PWM frequency with a duty cycle of 50% right and on channel 4 and then you can have another one on channel 3 with a different duty cycle they're all gonna have the same frequency because the frequency is determined by this and there's only one of these so all your channels are gonna have the same frequency but you can make them have different duty cycles because the duty cycle depends is dependent on this register and there's four of them one for every channel and every channel is bit is a different pin on your microcontroller so you can have four different PW aims same frequency but different duty cycles okay and this is the the registers for your four different channels now there's other things we have to set it's not that easy right it says PWM mode could be selected independently for one PWM Channel per output compare output by writing a 101 I mean 1 0 1 1 0 or a 1 1 1 into the ocm bits in the the specific register so now this is telling us that we can select either PWM mode 1 or PWM mode 2 by writing to this bit in this register so let's go to this register and look at that and I like to have two data sheets open because I don't want to lose this page so here's another copy of the datasheet where I'm going to go to that register okay so see CMR X right CCM rx and there's actually two of them amar one and Mar to know if you notice here it's a 16-bit register this half from from bit zero all the way up to here up to here here this is all one channel as you can see see how it says o c CC 1 OC 1 OC 1 PE OC 1 and 1 everything here's has a 1 in it ok I see one f IC 1 P so that channel 1 and then over here this side is identical to this side but this is all has the number 2 2 2 2 2 everything's with 2 so this is for your channel 2 so then we have this other register which is the same name but has a number 2 now this register has everything with a 3 and everything with the floor so obviously if we're working with channel 4 then this is what we're going to be setting okay these are the settings that we're going to be concerned with and there's not much description here if you go here for example the OC for M it says output compare mode okay but it doesn't tell us what what to set like what mode how can we set it because it it all describes it for the it only describes it for this first one it only describes it for 4 for channel 1 and you just it it's obvious that okay the same will apply to the rest of the channels the same settings because all the registers are identical so if we look at the description for channel 1 we go back here it says that we can select our PWM mode by looking at these bits the OC m OC XM so we're gonna be looking at OC 1 M to read info but in reality in our code we're gonna do OC 4 M so OC you want to M it tells us here that a whole bunch of other stuff but what we're concerned with is that we have this here PWM mode 1 and PWM mode 2 Jesus okay so PWM mode 1 tells us this it said in up counting channel 1 is active keyword active as long as the timer counter is less than the control register 1 so in PWM mode 1 it's basically what I described here this signal is active so as long as this value it's less than this value right that's all the data sheet is telling us for PWM mode one would I go that in up counting we're not counting that's what I just showed you channel one or we're gonna use channel 4 but that doesn't matter is active as long as the time or count is less than control register one we're not I mean capture comparator one in our case it's four or else it's inactive PWM o2 is the inverse of this in up counting right it's inactive so you see here it's gonna be inactive as long as that that count is less than the capture compare register so that's the only difference between PWM mode one and two and one you would have a behavior that looks exactly like this that's so long as this value is less than this value we have a high signal but in mode 2 it's the other way around that so long as this value is less than this value this signal will be done here and then for this part it would be up here so it's just gonna flip the wave upside down that's all that does okay and then let's go back here okay so we have that what else it says the user must enable the corresponding preload register by setting blah blah blah blah blah okay so it says we have to enable this register enable must enable the pretty little register by setting the ocx PE bit in the CCM rx bit okay so now let's look at this OC PE bits o CP e and we're gonna go to register 1 because that's where OC PE is bit 3 output compare preload register zero means that the preload register on the timer counter compare register 1 is disabled and that the timer control register 1 can be written at any time the new value is taken immediately if you set it to a 1 it means that it's going to preload right operation preservative Ella ok what this basically means is that where's the picture when you have a value here and let's say you have a value here right and all this stuff is happening if for some reason you changed this value if you have a preload register which is exactly what it sounds like you're gonna change this value right on your code you're gonna be like Oh ccr4 equals you know 10 so you change it now from 5 to 10 but in hardware it's not gonna write it to here even though that's what your code says in hardware it's gonna preload that value 10 somewhere in what they call shadow registers it's gonna preload it somewhere and it's only gonna put it into you into the actual register it's only gonna put it once we get this overflow event once it overflows then it'll do it so it won't allowed you to allow you to do it mid in in the middle of a counting operation here so it'll only do it during that event to your eyes you won't tell the difference where they did it here did it here did it here or if it did it here and for the most part it won't matter so we'll just follow the datasheet and we'll use that preload register because again we won't know the difference of where did it but that's all that means it sounds a little more complicated all that saying it says whatever value you're gonna put in here when you're changing it during an operation we're not gonna put it there right away we're gonna wait for you know for this to happen and then we'll put it and then your new value will be there so that's all that's that that says in that register all right so that's the OSI 1pe but remember we're gonna be using OSI for PE in the other register okay what else does the datasheet say huh and eventually the auto reload preload register by sitting RPE in the control register one so we're gonna set our PE in control register one so if we go to control register one which is right here and that's this bit bit 7 it says the AR our register is buffered and that's exactly what I did I just described instead of putting our value directly to here and into here the auto reload register is it's gonna do something where what the hell is this damn picture where it's gonna I don't know how to explain it it's gonna do this thing in sort of a buffer kind of thing and once this actually occurs then it's it all has to do with this when it's gonna actually do this so that's the buffer whatever values we're gonna have left here until that that specific overflow event occurs then it's gonna go ahead and do that and again the data sheet is very obscure about what all this stuff does or why you have to what you call it mmm why you have to set these things but at least that's what I can decipher from you know the auto reload the preload register at least that's what I can decipher from it ok so what else and I ended up changing my okay so once we do that it says as the pretty little registers are transferred to the shadow registers like I was saying before you get into these quote-unquote shadow registers right an update event occurs before starting the counter the user has to initialize so now they're saying that we have to initialize this ug bit into this EGR register and this EGR bit and that's that ug bit ug update generation you can go ahead and read that I'm not feel like reading that but anyways they're telling us we have to set this bit so let's go ahead and and I do believe that's it yeah okay so now another thing we can do is change the polarity of our timer if you go into this register right here where are we see see er okay so here's the polarities for the different channels as you can see for whatever whatever so channel one so we can have inverted or non inverted sort of signals by changing this order of polarity now remember when we read up here in PWM mode let me go to this datasheet now where did we do PWM mode right here on these was it the ocm remember PWM mode one in up counting channel one is active as long as basically what we said was this it'll be high as long as that this counter is less than this it's high well it's not really high it's active right active could mean high or low into whatever we want to be so in these bits right here notice how it doesn't say hi it says active and inactive that's why I highlighted these because you can actually change what active is you want active to be hi leave it as zero do you want active to be low leave it as a one so the funny thing is that you can actually get the behavior of PWM to mode by setting active to be low and then this will be low and then this inactive part will be high low high so in essence you can get PWM mode too by doing that so if you leave it to default you'll you get this sort of behavior which is the more I guess common way to use it as active high as most things define active as a high value and one of the last things we have to do is going to be actually to enable the actual so that's the polarity and this anyways the point is that it's going to go here and you see this bit seven no that's not it which is the it's a bit to no trying to find with the actual enable code is it isn't that because you actually have to enable the yeah for PE and they boo yeah I think I've covered just about everything actually and then obviously you also have to do the whole time or enable thing in control register one this right here because right anyway so let's go ahead and get to that that part of the code so now in this part of the code we are using our GPIO so in this car so in this part I like the other one we were just using this code you do have to enable your GPIO be I'm enabling GPIO a because I'm using that as my debug comport and you alternate function okay that's very important because if you don't do that then your your PIN will not be connected to the timer and then you also have to enable your timer for in my case which is on the APB one enable now you have to check what port what register your specific timer that you're using is on alright so if you use time before it is on the APB one bus now you also have to configure your pins your mode we're gonna go in here and look at this real quick because there's a little thing that it used to get me all the time but now I think I'm getting used to it where the hell is my GP oh okay so we're using I'm using pin 9 so it's this right here mode 9 so the mode you're gonna set to output at 50 megahertz right 1 1 so I have that right here so I'm setting a 1 in the zeroth bit of mo 9 and a 1 into the one bit of mode 9 and then I'm also setting the config the configuration is output mode we can his autant function push/pull so right here you need to have a 1 and a 0 right according to this right here alternate function push/pull so we need to have a 1 and a 0 now don't assume that these are 0 by default and then you just put a 1 here and then default 0 is already here no here is not a 0 by default this is the reset state this is the default value so your default value is 0 and 1 so if you just put a 1 here thinking there's already a 0 here you're gonna actually have 1 1 which is this it's not what you want so yeah you have to put a 1 here but you also have to go in and clear this one okay that's all one part that I used to mess me up a lot so here I put a 1 into that config 9 one bit and I cleared the zeroeth bit right in the control register higher for that now I'm gonna go ahead and paste the code that i have for this because remember i already did this okay so what I have here let me put this I'm gonna put this down here just so we can see all the numbers together so um given what our data sheet said I did enable that AR PE bit I went to the control register and here I'm enabling the actual timer and I'm also doing here for some odd reason which I should take these two because these two things actually I'm going to do them last cuz I don't want to enable the timer until I have all the settings in there so let's see what I'm doing here back to our datasheet so si si ER and this is timer which are timer time I see CEO hell are you here we go and what am i sending in here CC for ECC for e oh yeah oh yeah I think that's what I was looking for I couldn't find it the capture compare for so basically this right here bit 12 of this CC e our output the capture compare register enable racer so these are your capture compare registers right these are your different channels and this is the CC er which is capture compare enable register so this register actually enables these channels so we have to go in and able channel 4 which is this one so we go to this register and you go to here CC 4e which is the output enable so we're enabling the output on that fourth channel so we just so that's what I did in this line of code right here and then I did that ARP thing that has to do with the shadow registers or whatever the crap was um this um OC 4m thing sets it in to PWM mode right this is the control register so those two bits correspond to Jesus so this stuff right so I'm setting it the zero 1 to the 1 and the 2 bit the 1 and the 2 bit to set it in to PWM mode one and then I'm setting this OC for PE which means that are the preload enable which has to do again with that the whole thing about pre loading the value before it actually loads it here right so I'm enabling that oops where's my coat so that's what that does and then here's the prescaler alright so now let's see if our frequency calculations are correct so my this should go here so let's say we want a frequency of 1 kilohertz right and I know that my micro child is 72 megahertz and I'm going to divide it by the frequency that I want so I want one killer so I'm gonna kill her it's right there so that gives me 72 million divided by one killer heart gives me 72 thousand so I need to make it so that this and this give me 72,000 so what I can do is I wanna I want this auto reload register to be a easy number to do duty cycle because the duty cycle is nothing more than that register divided by the AR register right because where's this picture your auto reload register right from 0 to whatever number you want there your duty cycle depends on this range right so if you have from 0 let's say to a hundred let's say you put a hundred in your auto reload register right what's fifty percent of a hundred well it's fifty so if you want a duty cycle of fifty you put a fifty year right so that's what I'm saying when I say that I want an easy number here so that I can find the duty cycles easily if I put the number of 535 well and and I want a duty cycle of fifty percent well what's half of 535 off the top of my head I don't know that's that's kind of a hard number to find fifty percent of it right so that's what I mean I want an easy number here or I could do duty cycles where it's easy to find the duty cycle right so I hope you can see how this number relates to your duty cycle because ultimately you're gonna put a number here where we're gonna stop our PWM signal right so if I put a number here like a thousand and I want a 50 percent duty cycle that means I'm gonna count up to five hundred so five hundred that would be fifty percent so that's what I'm saying I want an easy number here so if I have to put according to my calculator seventy-two thousand what I'm going to do is set that IRR to one thousand and set this one to 72 so now when I do like these two multiplied together we'll give me 72,000 which when I divide that don't do the whole math thing I'll have that one kilohertz frequency and here we'll do this is what's to see I think that's 25 right so if I have 1,000 oops and I do 25 percent of it you get that value right there so we'll have here a duty cycle of 25% okay because this is 25% of that and we're counting from 0 up to that value so upload and upload and it right now doesn't matter I don't want to I'm not too concerned about the the output of the count right now what I want to see is the actual signal so we're gonna go here and so this is a logic analyzer I have hooked up so as you can see here if you look up there I can't put the mouse on it because it moves but as you can see up there it says a frequency of nine hundred and eighty five point five Hertz which is about one killer heart and this logic analyzer may or may not be that accurate so maybe in an oscilloscope would it be closer to one kilohertz but that's about one kilohertz and as you can see the duty cycle says twenty four point ninety eight percent which is about 25 percent so we'll just run that again there you go twenty four point nine eighty percent and nine eighty five point four Hertz maybe add one to this see if that does anything because eighty-four not we're doing this it's left foot nine nine nine I'm just trying to check because I know the actual okay we're getting a little closer because III know the actual formula where is it DMD is she I know here I didn't account for the in my formula that I wrote down for you guys are we pretty either here I didn't account for the if I could show you guys functional description I'm trying to show you guys I didn't account for the one in the [Music] nope I don't know where the hell it is and the prescaler in the prescaler thing here but you can you guys can figure that out Jesus telling you these data sheets are really something else I can see why a lot of people have a hard time with these things and to be honest I have a hard time too and that's why I study these things a lot and that's why I make these videos I really recommend you guys to make these videos for yourself because they say that one of the best ways to learn something is to try to teach it to someone else because it forces you to put it into your own words and to explain yourself to see if you really know what you're talking about and you don't even if you don't like your voice oh you don't you know you don't like the video whatever you don't have to publish it it's it's on YouTube you can make it private for yourself and you just you know when you forgot okay how is it that I do this you go back and you watch your own video and it's private and nobody else will see it but it forces you to to really say okay do I know what am i I'm doing can I explain it you know in a rational and incoherent way so that's what I try to do with these videos a lot of times some of these concepts are new to me and I try to explain them the best that I can and you know the only differences I do publish them but I only do publish them once I feel like I've gotten a good grasp on it so well that's gonna be it for this video guys so that that's your PWM so as you can see if we put this back to 1000 you can vary this will control your duty cycle so right now we have that little duty cycle 25% if you set this to 750 you have a completely different duty cycle of 75% and so you see you have that 75 percent duty cycle so yeah so that controls your your duty cycle this along with the prescaler will control your frequency and there you have it guys I hope you enjoyed that if you have any questions or comments please let me know and I do reply and I do listen and I try to explain everything the best that I can alright guys enjoy and let me know what else you want next I will do Wayne output compare an input capture mode for timers just not today because this already took so long later
Info
Channel: Eddie Amaya
Views: 34,318
Rating: undefined out of 5
Keywords: stm32, stm32f103, stm32f103c8, programming, embedded, arduino, pwm, timers, register
Id: qAZjdx71ePc
Channel Id: undefined
Length: 37min 44sec (2264 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.