 So, next class, today's class, we will look at this microcontroller interfacing, specifically the PWM interface. So, last class we have already seen some kind of a basic philosophies of this interfacing. Now, we will go little bit more specific into the aspects of somewhat hardware kind of a aspects at more like a philosophical level in not really talking of all the details of the things, but to get you the concept like how do we think through like the way things can be implemented in microcontroller, that is the kind of idea that will slowly evolve. So, let's begin with that, so as you all know this PWM central idea is pulse fit modulation just to kind of flash this thing to make sure like your recollecting memory that PWM has this kind of a feature, it has some kind of a time period and some duty which is one can change that in operation. So, this is how like you see basic PWM signal, now we want to generate such signal and what is the way one can think of in microcontroller to generate such kind of signals. So, there are mainly two parameters here for PWM, one is frequency and other is a duty. So, the frequency parameter usually is fixed parameter and the duty is what changes continuously during operation like you want more power to be delivered or depend upon what your control input is or control computation is, then you will change this PWM duty cycle and you can see here the signals for different duty cycles, how they look like. So, this is a zero level here and this is one level here and 75 percent of the time this would like on time is 75 percent of the total cycle time and then that is how like you drive some kind of average voltage into the application, so that is what would happen. Now, we already have talked about this, why we need PWM and like all these details, you know like there are advantages because it is a digital signal and there are no noise effect that would come, we just reiterating some of these things that we have already seen. Now, the question is how this PWM can be generated by microcontroller, so we start off with microcontroller clock, so PWM signal is some kind of a periodic signal, so clock is the best thing to kind of begin with. So, the clock's frequency is if you scale it down you will get a PWM signal or you will get some kind of a frequency controlled signal of the clock or frequency controlled clock or clock at PWM frequency like that. But would that have that is that sufficient, so say for example, you start off with the system clock and scale it down, see here you can see that this is the system clock coming and I have here four kind of a clock pulses covered in one cycle, so I just put some kind of a scaling factor of four and I get this PWM clock signal, so this is one of the ways to kind of produce the clocks or produce some signals which are at a lesser frequency than the clock frequency, you use some kind of a scaling factor, now how that happens at hardware and all we are not getting into details. Now the question is once you have this some kind of a clock signal for PWM, then how do you kind of get a duty cycle from that, so that is what is can you think about like just a minute I will pause here and you think about it and then like you proceed you can pause here and think about it, suppose you have this PWM clock signal which is at running at a frequency which is some kind of a clock frequency for PWM interface, this is not really PWM frequency, so we want to kind of get two things, one is PWM signal and PWM frequency out of this PWM clock, so PWM clock will typically be running at very high kind of a frequency, so system clock for example in T by is 80 megahertz, so PWM clock will be you know scaled down may be 5 factor of 4 or 6 or 32 or some factor ok, so that will generate PWM clock, now using that clock we want to kind of get our PWM frequency and some other duty cycle, can you think of a way to do that, so because if we just scale this clock further down and like get PWM frequency clock signal that will have a fixed duty that will have like 50 percent like a duty cycle, so scaling may not be a way directly, because scaling will kind of give you only 50 percent duty cycle value, so let us proceed with and see now what is that we can do, so here you can see that you know this is now PWM clock signal, so it is like already scaled kind of a clock signal, so I have drawn this scale in a different kind of a manner here, so our clock signal was looking like this, but now our PWM clock is looking like this, so time scaling is done here to kind of illustrate the aspect, so now you start off here with the counter which is say counting pulses, so it counts 1, 2, 3 like that it counts and it counts with say 10 pulses and then like you do something, you say that is your period for PWM, so the PWM signal period will be like something like a 10 counts of the clock here and then further if you say okay my duty will be out of this 10 pulses like duty will be 3 pulses, so then I can get like a 30 percent duty cycle kind of a value, so I need these two numbers once we have a PWM clock, one is like you know the value which will be corresponding like you know so this is somewhat the kind of a scaling factor you can see, I count this number, this means number of cycles to generate one PWM cycle, okay so 10 PWM clock cycles will give one PWM cycle and out of that three counts or three cycles will correspond to a particular duty, okay so here now since I'm using this number 10 here my resolution of PWM, can you think like what is the resolution that is happening here, the resolution here you can see that I can have only one clock cycle as a duty, as a minimum count for the duty and that will correspond to out of the 10 1 that means it is like a 10 percent resolution, so I cannot say I want PWM signal of 5 percent duty cycle, I cannot produce with this particular kind of a thing for then what I need to do is I'll need to kind of increase this factor, okay so this is a very important understanding of basics that okay look if you want higher resolution what you are supposed to do, so if you want okay so given some kind of a scenario and we say that in this scenario I want like no best possible resolution, okay what should I do, so I need to kind of then have this PWM clock frequency a much higher value, okay so if it is higher value then I'll have a lot of numbers, okay because PWM frequency of PWM is fixed given by the application, so to have more cycles in the same kind of a period of the PWM frequency I'll need a clock, PWM clock frequency to be higher, okay so like that one can start thinking, okay this is a basic kind of an idea, then you can have additional control that okay you have left or right just buy these signals or the signal is delayed by some with respect to some other signals and things like that, okay then counter can be only counting down or counting up or all those kind of things can be possible so this is counter loaded with 10, now this 10 can be counted up or counted down and then depend upon that something will happen but basic concepts is basically these for getting the PWM signal produced in the system, okay so for programming if you go into now a little bit more programming details like there are two methods, so again we have talked about this philosophically in general but with respect to Tiva also you can have a direct programming by controlled registers, okay this XCPA 100 code we write based on like the registers, so you fill in these registers with some values, okay and then like something will get executed, so there are these two types of registers that we have seen, control registers and working registers for each interface there will be some control registers which need to be initiated or for configuration they are just initiated once and then working registers are maybe getting continuously updated by say for example in the PWM case, PWM frequency is fixed so it is a part of that control register setting and then working register is PDOM duty which is changing as per the control computation every sampling instance, okay so that will be like a working register and then second philosophy of method of having the programming done is using API or library functions that are already defined or already given by this Tivaware and so you have seen that the Tivaware library has a lot of these functions written, the header files some variables are defined and then some functions are written as functionalities can be directly used, okay so there is a manual which gives you the details of how these functions are and which is already posted to model, okay so some of these functions are also defined in the ROM of a Tiva for execution which is going to be very fast, okay because then like no then the compiler will not have to fetch these functions from your hard disk and then compile and dump this entire compilation to Tiva memory, you will save some memory if these functions from the ROM are directly because they are already there in the read-only memory part of the Tiva board, so if you are starting using those functions then you don't need to kind of download these functions again during compilation and put them onto the Tiva board or memory of the microcontroller, so we will focus on the second method but we'll get into some kind of insights into how this module particularly in Tiva is configured or like no working, so you can understand we'll not get into control and working registers in detail but just get a kind of a feel for how this is really done in Tiva, so if you see the datasheet you'll see that okay it has two PWM modules PWM 0 and PWM 1 correspondingly there'll be pins which are named and each module will have four generators, okay generating two independent PWM signals, so how many total PWM signals will be there? 16 PWM signals can be generated there, okay so two modules four generators and each generator generates two signals, so eight signals is like you know what will be generated by one module and you have two modules, like each module has four generators, so you have a 16 PWM signal that can be generated and although like I must caution you that okay all 16s may not be available for the Tiva launchpad that we are we have been working with because only some few signals will be available for programming different interfaces because otherwise there will be too many pinouts that have to be done on the board and that will be too cumbersome for the board, so it has like some kind of a bare minimum base minimum functionalities that they have provided out, although the chip that is there existing on the board will have all this capacity but the hardware pins that are gotten out from the board will not be all the pins, okay will be some selected pins have been out, okay so then each PWM generator has has all these kind of features okay so we can read through this and see like if these things can make sense, okay so how do you kind of make sense of these things, okay so you to make sense of like know some of this you know write up or functionality it is you need to refer to the block diagram, okay this is a block diagram of this you see this PWM generator block, okay so this is a block diagram for this PWM generator block and in that there are these different different kind of components here, there is a timer, there is a comparator and there is something called signal generator and then then dead band generator and then you will get your PWM signals out for this particular generator block, so each module you remember like has four such generator blocks, we are just looking at one generator block which will generate these two PWM signals A and B, okay and input it will take as a PWM block, so by seeing this diagram you be you should think you know what you can do what you cannot do, can you think about that a possible moment and think about okay what you can can do and what you cannot do, so one can see that the PWM clock is same for one particular PWM generator block or for that matter this clock may be going in other blocks also in the same way, okay so you cannot have independent clock for this PWM A and B signals, okay the PWM clock will be same for both the signals or for that matter all the generators, okay so like that one can start kind of like not concluding some things, then there is some kind of a timer which will do some further kind of a timing for this clock and then then that will comparator which will see how this things work, so the signals will go typically like know this is the flow of the signals and it will start with the timer and then timer the signals will go to comparator or some timer signals will also go to the signal generator and then from comparator you have these two outputs coming compare A compare B and they use some kind of a timing part here also and please this PWM A and PWM B signals and then those signals are further processed by this dead band generator block and here this is a kind of a signal flow is what one can understand, so like that you can think about and you can have your own conclusions drawn some of these conclusions I have written in the next slide, okay I need to get this pointer again here so you can read through this some of these conclusions, okay so PWM signals can have independent frequency or PWM A and B signals that are coming they would have a if the count is specific to the channel so you need to check whether that is a count for the specific channel same or same count is given for both channels something like that you check that data and you will be able to kind of see okay oh this PWM signal frequencies can be changed by changing the count for a specific channel, okay so these are some functional description that will be given so these are like not directly taken from the data sheet and you can see whether these things make sense to you, okay you can read through so this is just to kind of give you some feel of your typical interspace will have this kind of description and how do you make sense of this description to see okay how things are working inside internally, okay say for example if you are directly focused on the APIs and functions you know you may or may not need to know so many details, okay you will be able to kind of program even without knowing these details, okay but we need to know some of these to do little finer programming, okay so once you have some things going you want like you know little more finacy of control over signals that are generated then like you need to understand this and then see okay what is possible what is not possible so if you understand this for example there is this modes of timer which has count down timer mode or count up and down timer mode, okay so in count down mode it will only count down and then at the end of the count then okay or whatever this count becomes zero it will again the load the same value and again like you know start counting from that to zero, okay so these kind of things is one kind of a mode of operation or timer other mode will start up with counting up and then again in count down again count up from zero and then again count down like that it will start so how do you how do they use this further, okay then it will have some some more kind of a details that are given then there is a direction signal for counting up and down so this this this signal will be useful typically when we are doing count up and down mode so that you know which is the direction getting going ahead with so that that signal will be coming out of the block so if you see there will be some direction signal coming out of the counter block somewhere yeah see this is the direction signal okay from the timer okay so like that you can look at the diagrams with respect to the description that that is given so let us now get into a little bit more kind of a meaning of some of these things in terms of how things happen inside the timer this here is like you know this is a like no functional description for different different blocks now we are going through details okay and first is a timer block and timer block has these two modes of operation and then there is a comparator block which will keep like you know whenever this count happens to be the value which is given in the comparator block then it will produce some kind of a pulse that is what is the function of comparator block this will be more clear when we see the actually the the figure of this so we are looking at these two blocks timer and comparator they are they are typically working in sync with each other so timer keeps on like you know giving the the count which is kept on reducing as for every PWM block cycle okay so PWM block cycle takes and the counter reduces by one okay next stick counter reduces by other other value like that it keeps on kind of decreasing the successively the the value of the count and when the count becomes equal to say this this PWM compare a signal then some kind of a pulse will be produced on this compare a output and like same thing will happen with this compare b output what is the signal that will look like on these these lines is that what we we will look now okay can you think typically I suppose I am having a down timer then like if I say when the countdown value becomes equal to this this particular value PWM CMPA then I get some small like no clock pulse on the on this compare a signal otherwise this compare a signal is going to be zero given this description can you think and plot for example like you know how these signals are going to look like that is the exercise you can do pause and do a little bit and then move on to the next slide okay so this slide now actually gives you this data so this is a value that is loaded some value which is loaded in the counter and it starts counting it down every clock cycle one countdown will happen okay and then it goes to zero and again it starts the loading the same value this is like a countdown mode only okay you are not counting up any time only counting loading the fresh value and again counting down loading fresh value and again counting down okay so similar kind of a thing happens when we go for the count up and down where he is here now you will have a count up first you reach the load value then like we start counting down these signals are like going to look like that now whenever this compare a and compare b values are hit by the during the countdown process of course for that compare a compare b values have to be lesser than this whatever the value you have loaded in the counter okay if it is more than that I mean there will be some error that will be popped out or it may consider the highest value here so we don't know what is that must be like we need to see through the details of the data sheet to kind of see what happens in under such kind of scenarios now what happens when this compare a signal is reached by the counter the cmp a on that signal you'll get this little pulse clock pulse and same thing happens with compare b signal okay on the compare b signal you get this little clock pulse and now this clock pulses are used further for processing and generating how that happens we'll see that but you can see that similar kind of thing happens with the PWM countdown so so wherever this this counter hits on the up count it will generate pulse and on the down count also it will generate pulse on this cmp a cmp b signals these are the kind of signals that will be getting generated and then this is a direction counter so this is a direction counting up is positive and in counting down is zero so that that's how like you know these these things work inside now you are using this further in this block which is signal generator block okay so that these signals are coming into signal generator block and this signal generator is using like you know some of these and converting that into this PWM a and b signals here okay so how that happens you can see here okay so maybe you can pause and think and see okay what can be done say for example if you have this kind of pulses coming okay how can I kind of generate PWM out of these pulses so say for example this load corresponds to like some kind of a frequency okay that could be my PWM frequency and then this compare a value whatever I'm writing that can kind of give you some kind of a handle over what should be my PWM duty cycle things like that so one can think about that kind of a logic to do the signal generation in the signal generator block okay so you can see here what is happening now in Tiva so you can see in signal generator like whenever this compare b is reached like b signal that pulse now is getting converted into into a trigger which will make this PWM b signal go high okay so this value is low this signal goes high okay and when this this same value is reached on the down count then again this value is made low okay so so that's how like this compare a compare b signals work and and produce PWM b signal in the signal generator block okay so those little pulses that you that are produced by the comparator block are used in some way to create some kind of a logic circuit which will give this PWM signal b out in such a way that moment this value is compared matches count value matches PWM b signal is like made high same thing for PWM signal a whenever the value matches from zero the PWM a signal will be turned to one and then whenever on the down count the value matches this signal will be turned to zero okay this is a logic is used now you do an exercise now same logic if it is happening for for for the sawtooth waveform which was like you know countdown signal only so this was a countdown kind of a signal only then what is going to happen so you can think about that what kind of a logic needs to be there such that like you know this compare a value will will be somewhat changing the changing this binary signal from zero to one or one to zero or whatever so you think about that and like you think what kind of logic can be there okay so we'll leave it right now I'm not kind of going to explain you that but using the same extending the same concept one can think about okay why what will happen to this PWM and b signals in the countdown mode alone okay and that's what probably we'll be using for our PWM generation okay uh so that is what uh is is that we need to think about and then there are these additional facilities that okay dead band generator look suppose you want to kind of um have some dead band introduced in the in the signal okay at the at the start or falling age of the PWM signal then how do you recruit that normally if this is uh not enabled this signal directly comes as output PWM and PWM b signals okay so for one generator you get now like on this complete flow from timer to comparator to signal generator and then your your actual signal out so this particular part will not get into more details here there are some some things which will probably we may not be needing to use but you you can kind of go through the uh data sheet and like know a little bit kind of understand how this control and the interrupts in in PWM one the fault conditions also can be can be displayed so so you can go and look at that those details if if at all are interested more into that but we may not need that PWM interrupt kind of a mode of operation of PWM okay what we want to kind of do is generate these PWM signals and be able to control the duty cycle uh continuously so that we can deliver in the actuator in mechatron systems right amount of energy that is needed okay uh so this is a dead band a little more details about you can go through so if it is disabled then like no you don't have this uh shifting of the signals happening but uh if there is an enabled and then they depend upon the value that is given in some registers like you will have some delay produced in in PWM k prime signal which is the actual output of the PWM okay so so this signals will get delayed a little bit so this is like this part is called a dead band okay you can have a rising edge delay or you can have a falling edge delay okay uh so so this this this much understanding along with some kind of a particular knowledge of some registers so this register knowledge will be given in this uh if you see this data sheet okay in the data sheet uh you can get all the gory details about say registers say for some examples of these registers from the block you can take and uh from this previous block diagram and then search for those like no names you'll get these register details okay let us see this master control register something so so a lot of like no many many different registers will be there and each registers all the details will be given okay so so this is like a register map all the registers okay so you can go through these registers and like no make some familiarity okay uh if you directly want to use this registration program great you you can do that uh thinking okay all the course if i understand all the registers uh now that i understand his main philosophy if i further understand the registered details i'll be able to uh you know control this uh register based programming or i'll be able to generate this PWM signal at the registered kind of a level okay this is how uh we will uh go about programming for the register level but so so we'll we'll do the programming mainly in apis to begin with so later we may not need also to get into the register level programming but uh maybe as a part of some academic exercise we can take some simple thing to to program by using the register level details okay so that uh we have that kind of a grip over the the programming uh of this mic but okay uh so far now for uh typical typically by use for using this api or library for programming we'll need to do something uh okay some process need to be followed or some steps need to be followed okay so we'll we'll look at those steps now okay this other block details i'll leave it to you to kind of go through and uh you know understand if at all situation or needs comes in your project this may be useful so uh so these are the typical steps that that are uh there for the programming with the library functions so you need to make sure that okay your these header files are are there uh in the library okay so in driver library you'll have pwm.h and pwm.c files they'll be there and they'll initialize a lot of variables that are needed for this so so the register level programming will you'll need to know some of the base addresses and the the values of the hardware register addresses which will be already uh put in this some of this header and library files then you don't need to know those uh detailed uh names and description so you just need to know these are some of these functions how they work and like know things will be fine so um yeah so this function description is given in the in the other file that is given to you okay the all these functions are are given their their description is given maybe we'll see if we can like know figure out one of the such files okay so let's see here the tba functions here okay so say here i have this pulse width modulation only okay so this pulse width modulation if you if you see this base they have all api functions given and then programming example also is given okay so you can use that to to get started with this so you see this driver is contained in this uh below dot c and below dot h file okay so uh there are a lot of functions this function is directly uh reading those function details you may not get to this uh uh procedure so for procedural aspect like okay what functions to be used and like when or what is the sequence of using some functions uh that is a little bit tricky to figure out but uh you may be able to figure out based on the uh the the block diagram understanding and okay i need to first initialize this first initialize that and like then these then do that okay or you can see see the uh directly here the the the examples that are given programming examples that are given okay that will kind of give you a better understanding okay so uh uh here i'm i'm just giving you uh more uh some kind of a detailed steps for the for the programming this pwm interface so uh you need to initialize the hardware so clock frequency needs to be set and enable some some pwm model that are to be used and enable the port corresponding to the target pwm model so these are kind of steps now i'll not give you like an entire thing uh here so you need to figure out based on some of the steps that are listed here and uh the understanding that is given in the pwm so it's a little bit of a skill or art to master like seeing uh appropriate information at appropriate place see if you start writing the data sheet or like you know like the entire chapter on pwm things will become too uh lengthy procedure and you may not need a lot of such things so once you are given a task that we have specified in the in the um in our assignment two where you want to run the motor by using pwm so to get to that task you need to generate pwm signals on on two signals okay two pins actually and one pin will be connected to uh one of the motor inputs and other pin will be connected to other motor inputs there are two motor uh digital inputs that are to be given for the motor driver so the motor driver data sheet also will be uploaded uh on the thing i'll so these are different different uh things the important part here is like no you need to configure uh this pwm module in the there are many different kind of aspects of configuring so configuring the clock so we saw that block diagram needs a clock so this this is done by this command then uh you you want to uh you know this pin size as this i was discussing in the couple of last classes also same pin is is used for many different uh purposes in diva so in diva or many microcontroller system for that matter so you need to kind of specify that okay oh look i want now to use to use this pin as uh for the pwm kind of a generation so i i say you say is a pin as a part of gpio but i want to use it for pwm purpose that you need to specify somewhere so that is where like you know you need to make the pins as uh pin type as like no pwm pin so once you configure this as a pwm pin i mean you typically will not use this as uh input or pin again in that application because your hardware is connected to this pin now okay so you cannot say okay well i'll i'll now make it uh you know use in the same program once sometime it is used as a pwm pin and sometimes it is used as a digital that may not happen because your your hardware is fixed there okay so uh so you need to kind of make a choice of which pwm models you need to use or given the you know number of pinouts and what all application needs you need to kind of make a choice of which pwm model you need to use and which kind of a pin you need to configure as pwm pin so that those decisions are fairly simple you will be able to see the pin maps and from that maps you'll be able to kind of match your application needs versus uh module uh which pins are available okay then uh you need to configure pin for channels different channels and then uh uh so pwm generator configuration so now you know what is pwm generator okay in the block so in in that you need to configure that pwm generator so it will typically have whether this uh this will have a count up or count down those kind of things will be somewhere specified um oh uh forgive me about this this is q pwm model i'll change that this is not qi model this pwm all these are the functions for pwm models set the period of the pwm then like uh you you enable finally the pwm so so like that you'll have these different functionalities which are regarding the configuration of model and then you need to have some functionalities which are regarding some output of the pwm okay so like that you you set send the pwm period please you set somewhere pwm duty and and get your program going so so i'll leave you with this kind of understanding and you can see now the example program and other things to to figure out how things are really working what are what are are these each of these functionalities might be doing play around with the parameters and see how is how they are affecting your pwm generation signal and all those kind of things you can play around and do it yourself okay so uh that's where we'll stop here now and um then uh what so further we are going to kind of uh we may look at a little bit like this a little more details about your quadrature encoder interface as well because these are the two primarily important interfaces for us to use from tiva system so we'll we'll do this small kind of a presentation for quadrature encoder interfaces as well and then we'll start off with the modeling part of the the thing and while modeling we'll start exploring like you know how these models really work in practice so that is our like no future direction to go so here we'll start basic getting basic understanding about how to run the motor once we are starting running the motor then we'll have understanding about uh and how to read the motor position by using encoders so now we'll get really really high fidelity uh position output from the from the position sensor now we want to use this for further control okay so this is how like you know we we'll proceed so before we're going into control we'll uh like you know start sensing this position and use it for some some uh identification of parameters of the system particularly we'll look at uh friction identification in the motor system okay so this is how like this future direction is what we will we'll go okay so uh we'll proceed like that and like I'll stop here for now