 Hi, my name is Ali Shehseva and welcome to this breached digital ST digital PFC design webinar using STM32 Throughout the next hour or so we're going to talk about why we need power fact correction or PFC And then we're going to look at PFC operation at the block level And then we talk about each main building block power stage voltage loop current loop Voltage feed forward and so on and then we look at the digital implementation of our PFC and then we will do a complete design using breaches PLD design software. This is available free for all the SD customs and This software effectively designs almost everything for you including the sizing of the power stage components and the control loop coefficients in digital domain and then finally my colleague and friend Dr Michael Holworth is going to show you some fantastic demonstrations and some experimental results on how we get everything working on the setup that we actually use in our digital power and digital PFC workshops with SD So why do we need power factor correction? Basically because non-linear loads draw non-linear currents And you'll see shortly that this will cause an enormous amount of problems Unfortunately, almost all power electronic power converters fall in this category and that will result in large harmonic currents And a poor power factor is only a byproduct of these harmonics that we are effectively injecting into our system So non-soluble current drawn by these non-linear loads have many disadvantages They result in the deformation of the voltage waveform So those of you who have looked at the mains voltage waveform You'll see that it's actually rarely a sinusoid many times is quite Crop top triangular shape and that is because of all these harmonics that our various products are injecting on the line and They result in higher i-squared R losses on the utility side of the grid That is before the meter and that means that the power company cannot charge for it And of course, they are not terribly happy about this the charge in larger RMS currents And that means that they need to install thicker conductors Certain harmonics such as triplet harmonics can go down the neutral line and can be quite hazardous So in order to make sure that we don't face these Disadvantages there are international regulations such as in the 61,000 3-2 from let's say European Union that prohibit us from drawing Too much non-sinusoidal current and power factor correction is just a way that we use It's a method that we use in order to make sure that our current that we draw is not peaky and full of harmonics But instead it's sinusoidal like a resistor. So if you consider our system will look something like this now We have Our mains voltage 230 volts First we rectify it using a bridge rectifier and you see this rectified sine wave over here And then we put it in a in a whole bunch of bulk capacitors DC link capacitors and we end up with a loosely hardly regulated DC link voltage like so it's a DC voltage at the moment actually there's no regulation and then and then you have your appliance now If this appliance was just a resistor if it was just a heater for example Would we still need power factor correction and that is yes We would because the problem is not with this load being resistive The problem is over here the way we rectify bar sine wave and then we store the charge into this capacitor So let's have a look at this in a little bit more detail. I have got the setup here I've got a bridge rectifier I've got a capacitor and I've got a resistive load here now The green line is the capacitor voltage and the red line is a line voltage So the red line is here and the green line is here And you can see that as long as the red line the voltage on the line is lower than the capacitor voltage Right the current is coming out of the capacitor and is feeding our load Okay, but no current can go this way because the potential across the capacitor is bigger than the potential of this line You see the green line is higher than the red line now During this period from there for from there to here the capacitor is feeding the resistor When the capacitors feeding the resistor is getting discharged and you can see that because the green line is coming down This is the capacitor voltage going down as we feed our resistor Now what happens then is that at exactly this point here the line voltage to red line goes Above the capacitor voltage at that point you have got Massive current flowing down here from the line down to here in order to recharge the capacitor you can see that the capacitor Recharges and follows the shape of the line voltage, but more importantly look at what happens to the current a Massive burst of current from there to there Starts flowing into the capacitor to replenish it of the energy that it has been Supplying the load and this is the bit that is a problem Okay, so let us look at this in a little bit more detail here I've got my line voltage rectified right in an ideal world if you just had a resistor without the bridge Driven fires and without the capacitor a resistor would take a sinusoidal Current like so and we know that power is voltage times the current and therefore you can see that we deliver power during this entire period okay, so This is for a pure resistor. That's your line voltage. That is a line current with a pure resistor Okay, and we take this much time To deliver a certain amount of power Now because of the action of the bridge rectifier and the capacitor just like we saw because we only use this Period here to deliver the same amount of current What happens is that during this period and this period? We have to deliver exactly the same amount of power that we delivered during this entire cycle and Therefore the current has to be much bigger because we're only using a short period of the cycle to actually Feed our capacitor and of course that is a problem if the current flowing into the capacitor is this high Instead of a resistive load which would look like so then the conductors have to take into account that the peak of the current is higher RMS of the current is higher these sharp edges have got much more harmonics, which we know that we have got some problems with and That is why we need power factor correction So effectively all a power factor corrector does is that it forces us to draw a current during the entire cycle Like your resistor as opposed to let the current go wild and go massively high like so, okay So here is a real example of a real measurement here I have got my voltage and here you can clearly see The current that is flowing into the capacitor and it's only It's only taking this long in order to charge the capacitor so that we can deliver the power over the entire cycle So how does active power? Factor corrector operates. Well the first place a PFC stage between the diode bridge and The bulk capacitor we actually then can reduce the size of this bulk capacitor quite significantly and then We put a diagnostic converter afterwards. So our buck converter flyback converter, whatever it is Will then convert that note that the output of this will be around 400 volts in case of an active PFC But it's poorly regulated in terms of the voltage. So this is not a stiff hard pork for 400 Big about a 400 volt DC link, right? This is quite badly regulated But we don't care because the job of the power factor corrector is not to regulate the voltage The job of the power factor corrector is to make sure that the current over here is a sinusoid The job of the downstream converter is to regulate the voltage So so when we place our downstream converter This will provide us with a stiff well regulated voltage which our load requires Okay now As I mentioned earlier, it's important to note that the job of the PFC is not to control the voltage It's job is to control the current. Okay? Okay, so you get poorly regulated distorted voltage over here But beautiful sinusoidal current over here just like a resistor and then you get clean regulated isolated voltage over there Now the most common topology that we use For PFC is the boost topology and in this webinar We're going to talk about continuous conduction mode boost topology It is suitable for PFC applications because we can operate over the entire universal voltage rate. So we can go from 85 volts Which is the lowest voltage that we expect to get in the world In an ac line to 265 volts, which is the highest one that we expect to get Now input current is not switched as you can see That's a good advantage because the switch is here input card is got a massive inductor orient And of course, we know that inductors don't like big changes in current So the fact that we have got a inductor heat actually helps us the switch is on the low side So the voltage across the switch is not too high And we need a cheap gate drive and therefore it's a quite a suitable topology In the boost inductor the input current is the same as the The inductor current and that means that if I changed a duty of this I Can control the current. So if I change the duty of this I can control the input current and that means that I can control this in order to force the input current to be sinusoidal, so by varying a duty we shape the input current into what we want And of course in order to do that we need some control loops. So Effectively what we're doing is we are putting a rectified Sine wave that's our voltage across the input of our boost converter Then we control the duty of this Like so in a kind of a sinusoidal manner, right? And as we do that we force the shape of the current to be sinusoidal And that makes it look like a resistor and you can see if you actually zoom in you're turning a switch on So we switch off turning a switch on turning a switch off and by varying the duty You force it to go up and down for now. Let us ignore the voltage loop. So for now, let's say that we're not controlling the Output voltage at all Therefore we are free to do what we want with this switch and all we do We don't care what's happening here for now All we do is we vary this in order to ensure that this is in phase and Same shape as that. Effectively it makes the whole thing look like a resistor so We have got a current loop therefore in order to shape the current and later on we talk about the voltage loop Because we don't want the voltage to be completely uncontrolled We want to have some control on it and we'll talk about that a little bit later So for now if we just talk about the current We need to generate a demand duty and we do that from the input voltage We want our demand current that goes into the current loop control To be the same shape. So what we do typically is we take a potential divider We take this rectify sine wave and we say to the control loop You know what? I want my current to be exactly the same shape as this rectify sine wave So that goes into the control loop then we measure our current So this is the current shape that we want this is the current shape that we're getting We compare these two together and that gives us the error signal that gets that gives me the difference between the current that I want and the current that I'm getting and then that goes into a Compensator now in analog that is typically just a type 2 Compensator in digital that becomes a 2 pull to 0 We have discussed this in one of our previous videos and of course PLD will Calculate everything for you now for now all you need is the fact that this is a compensator that is going to compensate and ensure that this Actual current follows the demand current. So the current demand current Sorry, the current error goes into the PWM the pedium turns on the switch and the switch controls the input current And there we go our current loop is control is closed But we still don't have any control over the output voltage. Okay? As I mentioned earlier the PLD will design this loop for you And therefore we're not going to go through the detailed design of this within this shorter webinar In fact in our workshops, we do discuss how you calculate everything and how you control your current so We have closed the current loop which is the inner current loop over here And now we're gonna have to close the voltage loop because as I mentioned earlier up until now We've been ignoring the output voltage. We say, you know what? We don't care But we do we need to somehow have a little bit of control over it Okay. Now we take the output voltage We pass it through a potential divider and then we Compare it to a reference voltage typically what we ask for the output voltage to be around 400 volts usually 375 385 volts So this is the voltage that I'm getting I'll talk about these harmonics later So this is the voltage that I'm getting this is the voltage that I want 400 volts I compared to together. So this is the error and again that goes into another Control loop and this one is the voltage loop the voltage loop takes the error signal and Compensates it we call this VA out voltage amplifier output again That is a type 2 compensator in analog world again. That becomes a 2 port to zero in digital world again We have had a many videos in fact on these and how you design them I mean discuss them in a workshop for this short webinar We are going to use PLD and PLD will design everything for then what we will do is we will multiply this With that so effectively the output of this voltage loop is only scaling this demand current our demand current is a sinusoidal erectified sine wave and the voltage loop will just Increase or decrease its amplitude. So for example if The output suddenly starts drawing too much current this voltage here would fall down Because this is the negative feedback loop as this will fall down this VA out will go up So you multiply this current by bigger number and therefore you get bigger current and that's exactly what we want a negative feedback There we go, and that is what we're doing and we have closed both loops So now we've got an inner current loop and an outer voltage loop and that gives us a little bit of control over The output voltage, but because the main job is to control the current You don't want this loop to interfere the performance of that So typically the crossover frequency of this is 10 kilohertz very fast very quick transient response and You cannot make this fast because if you do then you mess this bit up And of course our job is to shape the current so that this one is extremely slow around seven to ten hertz Okay, and because it's such a slow loop you end up with having some second harmonic ripple now in the In Europe second harmonic of hunt of 50 hertz would be 100 Hertz and in United States and North America Canada, it will be around 120 here So here you've got let's say hundred Hertz of ripple now this causes us a problem because This is being used in order to generate the demand value of the current And of course if this has got second harmonic on it We end up demanding second harmonic and therefore one of the jobs of this voltage loop is to attenuate this 100 Hertz error signal because we don't want to add extra hundred Hertz on top of this It will it will reduce the performance of the whole Okay, again as I mentioned earlier PLD will design the loop for so let's have a look at the next building log And that is the voltage feet forward filter We have a problem with the way that we are doing this in that our demand current is Being generated from the input voltage Okay, now because you're trying to deliver constant power In a normal let's say DC DC converter if the vote if the line voltage Falls down the current has to go up in order to make sure that the amount of power that we deliver is the same Okay, however, look what happens here if the line voltage goes down the demand current also goes down And that is exactly the opposite of what we want If the line voltage goes down in a pfc to deliver the same power we have to force this To go up so that again we deliver the same amount of current and the part of the circuitry that does this is called the voltage feet forward filter again in the Workshops we go through a lot of details of how this operates and and how it actually works And how you design one for the sake of this short webinar We just use PLD and PLD will design everything for you. So As I mentioned earlier Ideally in our pfc we would like the input power to stay constant but this is not the case because of The fact that the current shape is being generated from the input voltage shape And of course if that goes down the current will also go so down and we use the Voltage feet forward filter. Okay. Now at block level then we have got a boost converter We have got some gain again You need to take into account within Your control loop you have got some gain Then you have rectified this now That goes into an ADC Okay, so we take Samples of this because we need to start doing all those calculations that I told you now here is the sampled version So you are sample sample sample sample sample sample sample and then you scale that was something called kv in then After you've scaled this you end up with a constant which is that scaling factor and a sine wave in Discrete time so it is sample sample sample sample sample and it's following the shape of the sign then This is the part that does the voltage feet forward section So you pass it through a digital low pass filter again WDS will design that for you You multiply it by 1.1 and we do that in the software. We've got all the software libraries That's that that will do all of this for you and then we have to square it And that is called the voltage feet forward calculations that you have to do right then we take our output voltage Remember, we've got a control loop on the output voltage again. We put that through an ADC and again, this was the original continuous time version This is the discrete time version so we've taken sample sample sample sample and so on that goes through another scaling factor We compare what we want with what we are getting that is the real voltage now It's sampled that is the demand voltage. That's inside of our code and the difference between these two is the voltage error As I mentioned earlier in the analog world, we put that through a Analog compensator here now we put it through a digital compensator and that is a two port to zero compensator PLD will calculate everything for you all the coefficients and so on and that gives you v a out Remember that was the scaling factor that just scaled the rectify sine wave for the For the demand current then finally you calculate this so you take this Value, there we go. That's here. You multiply it by v a out Right, so you scale it by v a out, right? Just like we do in our log world and then you divide it by the square of the RMS voltage And what you have done here by low pass filtering multiply by 1.1 and the squaring is actually you have created this input voltage squared and This is the voltage feed forward term. Okay, the output of this is The demand current I am out Okay, so now we have created something that is taking into account the shape of the shape that we want this Rectify sine wave we multiply it by the output of the voltage loop and we divide it by the voltage for Feed forward filter term and that is the demand current now. We have to measure the real current So here we measured a real current again. We have got a game term now this is the Current that we want this is the current that we getting if you remember we now have a current loop So you sample this You Scale it this is the current that you want. This is the current that you're getting now They are both in discrete time in digital domain You subtract one from the other and that gives you the current error and then that goes through the current loop Which is another to pull to zero again PLD will calculate all the coefficients for you again from our other videos You know, you will know exactly how these compensators work and then You scale it again and then finally you have got your PWM and that is the one that we feed into the Switch and therefore Everything is we've closed the loop So we have got the close the voltage loop We've closed the current loop and we have looked after voltage feed forward filter And this is a type of setup that we use in order to implement the The the in digital domain at digital PC So if for now we ignore all the scaling from previous lies You can see that we need to design one digital voltage controller. We have and that is just a Type-2 controller one digital current controller again, that is the PLD will design up for you. That's the current controller We have not designed them in analog. We do this in the workshop And For this for this case of this short seminar We have taken them all out and then we have designed one digital low pass filter and that is the voltage forward term So of course a steepie LD will calculate all of these for you and this is in fact available for you For free from this link over here Okay, now consider our filter in real life Now we have An absolute maximum peak voltage of 375 for universal voltage. We have scaled it down to 3.3 volts Okay, so what we haven't discussed so far is all the scaling. So we took 375 And we scaled it down with a potential divider So we need the scaling factor of one over 114 Right here for our potential divider. So everything that we discussed right is off by factor of one over 114 okay, and therefore There we go That's the gain of this potential divider in order to put everything back You have to anti-scale everything that you calculate. So in your code you have to multiply by 114 to negate this over here, right? Then let's look at the ADC. ADC takes a voltage from 0 to 3.3 And gives a number between 0 to 4 or 9 5. So It has a scaling factor of 4 or 9 5 divided by 3.3 So everything that we have done is off by this scaling factor So so to negate this you have to anti-scale it by reciprocal of this, which is 3.3 divided by 4 or 9 5 So 4 or 9 5 divided by 3.3 in your code you multiply by 3.3 divided by 4 or 9 5 The multiplication gives you one so scaling becomes one and therefore you anti-scale it effectively Okay, so that's the gain of the ADC And so on so that means that our result is wrong by this scaling factor and that scaling factor So what you have to do is anti-scale all of these okay Now let's talk about it. This is scaling a little bit more detail. So we started here. We have got G3 which is the scaling factor over here And we have got a scaling factor for the ADC which is 4 or 9 5 divided by 3.3 So what we have to do is anti-scale that by Exactly the opposite. Okay one over G3 times GADC So If we have a look here what the scaling factors we have we have one a scaling factor there one a scaling factor there One scaling factor there one scaling factor there Uh, where's the next one? Yeah one scaling factor over here One a scaling factor over here And you want a scaling factor over here and you see that you've got a whole bunch of scaling factors Again, we have written library functions that deals with all of these all you have to do is know what This resistor and that resistor is calculate again You put this in the library functions that are available for you and all the scalings will be looked after for you okay, so Again in the workshop we discuss exactly how you negate all of these but for the sake of this Short webinar All you have to do is when you look at the code samples that we give you and they're in fact named exactly the same When you see g2 when you see g3 all you have to do is work it out and type it into code and everything else will be calculated for This is some experimental results for you now then This is the rectified input voltage right This is the current the blue trace is the current The green trace is the input voltage This is the rectified input voltage and you can see that the current is following beautifully the the the voltage It's a bit noisier because we're actually switching we're forcing it to shape like to be to be shaped like a sinus So it if it were a Resistor obviously this would have been clean and then finally this is the output voltage That's around let's say 400 volts or this is the low voltage board by the in our workshops Because we cannot deal with 400 volts because it can hurt the attendees We have designed this special board specifically for our workshops whereby we have divided everything by 10 So you put in 23 volts sinusoid and you take out 40 volts dc And this is the workshop that we use on our in our workshop so that you can have some real hands-on experience You really code this you do calculate everything you do some real measurements on this board And there we go here is your output voltage and you can just see That is not quite as straight and that is because it has got that second harmonic superimposed on top of it And then if I look at it on my oscilloscope I looked at the harmonics These are the harmonics with the power factor switched on you can see now that the voltage and the current are both sinusoid And you can see that this is my switching frequency and after that the harmonics fall down quite significantly If you measure the if you decide to measure the loop You should really measure both the voltage loop and the current loop. We do that using Omicron's Body 100 we've got some great videos on how you measure the loops of of power factor characters On our on our website and this is a type of result that you'd get remember. I said that the The the voltage loop is usually very slow at around 6 Or 7 hertz 6 to 10 hertz and there we go We have here designed for 7 hertz and we've got 60 degrees of phase margin So it shows that the design using pld is actually this is a real measurement. It is giving us very good results Okay, so I am now going to switch over to st pld and I'm going to design one step by step all the way and After that I will pass on to michael who will show you the experimental results So let's go to st pld Okay, so when you open up st pld again, you can download this directly from our website You provide it with the The voltages that you want so the minimum voltage for universal voltage. That is usually 85 volts nominal voltage, let's say is 230 volts for the european union maximum voltage, let's say 275 volts Frequency in the european union that would be 50 hertz in north america that would be 60 hertz Of course en 61 000 3 dash 2 is a european union standard and therefore we go at 50 hertz Let's design it for 250 watts And let's use switching frequency of 200 kilohertz Then the output voltage i have been saying 400 volts is typically 375 to 385 let's stay with 385 volts and then hold up time is The period of time that you're going to assume that the line can go down And then come back up and we're saying that the hold up time is 20 milliseconds. So it's one cycle So in within this one cycle How low are you going to allow the output voltage to fall our output voltage on the normal conditions is 385 after 20 milliseconds of line going down I only Allow it to go down to 335 volts. I don't want it to go down any lower Okay, and that of course determines the size of the output capacitors. So by typing this number here You that the software will automatically calculate the Output capacitors for you and you can see here based on what you have given it If we allow 15 percent ripple current ripple It will allow it will It has calculated the inductance to be around 500 microhenry. So let's say 500 microhenry and then it is take it is Given us 278 millifarads of capacitance and that is based on this hold up time over here Okay, it also gives you the stresses on the components so that You know the current ripple on the capacitor and the the voltages and so on and therefore you can go and Buy the correct size of the capacitor and the inductor then In the case of our digital power supply Let us use let us say that we use a current transformer with a turns ratio of 1 to 100 And then the peak return current is around four amps and let us say that we are going to allow We would like a voltage to appear on our ADC pin This is the voltage across the burden resistor of the of the current sense and if our ADC is 3.3 volts I would usually go for around 2.5 to 2.8, but for simplicity, let's say that we want three volts And as soon as you type in what voltage you are going to allow on the pin of the ADC It calculates the exact size of the burden resistor. So look at that if I change that to 1 It says you need a burden resistor 24 if I change that to 3 It says you need a burden resistant resistor of 72 And what this means Is that at the peak of the current The voltage that will appear on the ADC pin that is sampling the current Is going to be three volts if you solder in a 72 ohm resistor And it's calculated the current gain for you if you remember from when I was talking about all the scaling and all the gains Current gain is one of those gains that you need. So you just go and put that in your code It's already working out to be 0.721 okay, then In the multiplier section, we're going to have to tell The software how much harmonics we are going to allow and of course that Represents whether we pass or fail now the more low you make this Harmonic the lower you make this harmonic there harder it is to Design the compensator you have to have a higher crossover frequency and so on and it makes life more difficult So ideally what you want to do is pick a Certain amount of harmonic that just allows you to pass the test With a little bit of margin right 0.75 third harmonic permitted as a result of the voltage feedforward is not a bad number um, then it works out the voltage feedforward gain if you remember the voltage feedforward was a Low pass filter you saw that on the block diagram So it is taking these values and it works out the cutoff frequency of the filter that you need to design Not only that it actually works out the coefficients of this digital filter And of course in the sample code that we provide for you You there's a section whereby you just need to paste these in and then you end up with a 10 hertz 10 hertz low pass filter. Let's say I was going to allow one and a half percent Of harmonics third harmonic due to the voltage feedforward filter Okay, then you can see that I can then get away with a filter with a higher crossover frequency Which is 15 hertz again. It has calculated the coefficients for me. All I have to do is copy to clipboard You go to your code. Michael will demonstrate this for you Um, at least the entire code and you just paste in these coefficients and there you have it You have the voltage feedforward filter Okay, then let us go to voltage loop. I'm here looking at the voltage loop I'm looking at the plant and the loop, but I only really need the look for this demonstration This is the loop. I have got the crossover frequency of seven kilohertz and I've got a phase margin of 60 degrees I've got very good gain margin and a very nice slope again And our other videos we just we describe what is a good body plot for a stable control loop I've here set this to seven hertz crossover frequency, but I can change it Let's say if I do 10 Or if I do 100, please don't go higher than 10, but you can see that as soon as I change It will redesign everything for me. It gives me new coefficients and also gives me a warning saying a Your crossover frequency for the voltage loop is too high. So let's stick with 10 60 degrees of phase margin and with 60 degrees of phase margin It is estimating that I'm going to end up with 3.5 percent of second harmonic on our output voltage now that might be a little bit too high. So let's Let's see if we can get away with seven Look how quickly how it calculates it. So if I have a crossover frequency of seven hertz, I end up with 1.7 percent of um Harmonics on my output 10 gives me three four point five percent Okay, if I go to stay with seven And of course that manifests itself on the input current as naught point eight five percent of third harmonic and It's calculated the poles and zeros and it has calculated the coefficients. I copy this And again in a sample code that we give you All you do you paste it and that is your voltage loop completed Finally current loop as I mentioned earlier on a current loop Needs to be much faster because the job of the Of the pfc is to shape the current and not control the output voltage. So this is my input current You typically want a high crossover frequency 10 kilohertz 50 degrees of phase margin again. Have a look at oh, you need to go to frequency response of the current loop I Look at the loop only Have a look at this if I do five kilohertz it automatically Redraws and recalculates all the coefficients. So let's stay with 10 kilohertz That will give you a relatively nice sine wave in time domain 50 degrees of phase margin 200 kilohertz is my sampling frequency which happens to be the same as my switching frequency And then The way we structure our code is that there is a total delay of half a sampling interval So if you use our code that has to stay at north point five if you use your own code and you've got different amount of delay Then Of course in the workshop we go through a lot of details on how you make sure that you get this bit right Otherwise you may lose a lot of phase margin And then finally based on this data it has calculated the Current loops coefficients Which I can go and I can paste again into my code again. Michael will demo the code for you. This is The coefficients of the current loop and Our design is effectively complete. So to summarize, uh, we discussed, um The uh, bedlam books of the, uh, pfc we discussed the setup in digital And then I have just now designed everything voltage feed forward voltage loop current loop and so on And now I pass you over to my colleague and friend Dr. Michael Holworth Who's going to put all of this into practice and show you how it will work in real life Show you the code and actually show you the experimental setup and give you some experimental results Thank you very much for watching. Uh, this is the end of my section and Michael will take over from that Okay, thanks ellie. So in this half of the webinar, we're going to look at some experimental results using our Uh, digital pfc start kit. So this is the start kit that we use in our workshops It is a, uh, low voltage digital pfc start kit Um, it's obviously we don't want people to hurt themselves in our workshop So effectively what we've done is divide everything by a factor of 10 Um, so the input voltage is 24 volts ac Output voltage is 40 volts dc Um, this is a ccm boost pfc and there are two phases so we can do interleaved ccm boost pfc Obviously you can see the resistive load here. We have a Roughly 16 watt load bank here Which you can control using the switches or using the MOSFETs And the st nuclear card just plugs directly into the starter kit. So it's a complete kit That allows us to do design development work at safe load voltages It's very useful because then if you're doing some development work, things won't pop dramatically So yes, that's the board we're using All we need to do now is look at how we implement the digital pfc in terms of code So what do we need to implement on our digital pfc today? Well, looking at this block diagram We have our boost power stage here And we're going to use the adc to sample several points We're going to be sampling our input voltage scaled down by a factor g3 Sampled by the adc and then that gets passed through our digital low pass filter It gets scaled by a factor of 1.1 and then squared to give to convert from average to rms and then rms squared The other The input voltage is also used as our v and peak sine omega t term So that's our to be our rectified sine wave shape And that is so that our current tracks our input voltage in a sinusoidal manner Um, we also sample our output voltage Scaled down by g4 sample using adc That gets compared with our reference and the error term is passed through our two pole two zero voltage loop controller Then these three inputs are used as inputs to our multiplier That which is this block here And the multiplier output is our demand value of current For that switching cycle So we also sample our current, of course And there are multiple ways of sampling the current you can sample the return current like we're doing this diagram Or you could sample the switch current with a some kind of current transducer in the One of the legs of the MOSFET So we sample the return current here scaled by g2 That gives us our sample current we compare that with our demand current, which is the output of the multiplier And that gets passed through our two pole two zero current loop compensator And then that gives us our new value of duty cycle Uh, so you'll notice here that there are several gains. So we've got g2 g3 and g4 And then we have Some blocks in our diagram So we have this gain here Which we call kva out and this ensures that The demand current value Is in the same units as a sampled current value So you can see our sampled current here is g80cg2 And the output of our multiplier our demand current also needs to be in the same units g80cg2 And we go through this in a lot of detail in the workshop, but Effectively We if you multiply the output of our voltage loop by this gain kva out, that's what we get Then we need to negate these games before we Set our pvm pwm To ensure we get the correct crossover frequency of our current loop So we do one over g80cg2 at the output of our current loop compensator And in our code we call that ki loop. So we'll look at where those are in our code shortly Okay, so now we need to configure the STM32 chip We're using the STM32 g474 device today And to do that We are using STM32 qmx Now the setup is very similar to the Setup we did in the digital power dcdc webinar a few weeks ago So we won't go through it in as much detail The only real difference is that we are sampling more channels Without adc in our dcdc webinar We just sampled the output voltage in this digital pfc webinar We're sampling as you can see here Uh our input voltage or output voltage Our return current and down here the Switch current so iq1 and iq2 for the two phases that we have on interleaved boost Um So there are more adc channels to configure So We are using adc1 to sample our input voltage and output voltage And adc2 to sample our currents So let's take a look at adc2.currents We're not actually using the return current in this example We are just using the switch current So we have configured injected conversion mode with two conversions First conversion rank one is channel three Which is this one here the the uh fat q1 Which is one of the phases And then the second conversion is Channel four, which is q2 the current in the in the other phase Okay, so we are doing a ccm boost pfc Which means that we are using average current mode control So we need to make sure that the current that we sample is the average current Um, so for ccm as you can imagine We turn the switch on and the current rises then we turn the switch off and the current force So if we set our adc to sample in the middle of our on time That would be the middle of the the rise of our current That will be equal to the average current for that switching cycle So to do this we use the high resolution timer So we're using high res timer trigger two event to trigger the sampling of our adc So let's take a look at our high res timer high res timer here adc triggers And trigger two event is enabled And there are two trigger sources timer a compare for timer b compare for So timer a is used to control switch q1 So the pwm of switch q1 And then timer b is used to control the pwm of switch q2 So we're using the compare register on timer a and b Comparished afford To trigger the adc to ensure that we sample in the middle of the on time So let's take a look at timer a So timer a again, we won't go through all the configuration. We did that in the dc-dc webinar. Please feel free to take a look at that But effectively let's go down to the output configuration ta1 drives the pwm of our switch q1 The set event is compare register one compare unit one And the reset event is compare unit three We also have an external event to shut down the pwm in the event of a current or Pause in the debugger which we'll talk about shortly And then we said we're using compare register four compare unit four to Take the adc sample So you'll note that in the code here we haven't actually set the compare values Sorry in in keeping max we've not actually set the compare values because we're gonna do that in the code We have some digital pfc library functions That manage the setup of the compare unit so that we get the the correct control Sampling at the correct point which we'll look at in a moment So that's a high-risk timer What we do now is just press generate code and then that will open our selected ide We're using embedded workbench from iar as our ide today Let's just take a look at the main.c file So we've got some header files There's main.h which sets up the GPIOs Defines the GPIOs and then we have our digital power library function and our breacher pfc utility functions We're actually combining these into a new library digital power library That will be released soon. So please subscribe our website for updates Then we have a board specific header file. Let's take a look at this And in this board specific header file, this is where we define the games. So G3 g So g2 g3 g4 that we saw in the block diagram are defined here as well as kv8 out and ki loop So then further down in our main.c file We have our coefficients. So these are the coefficients that ali generated using pld We copy and paste them into our code. So we have the vff filter coefficients For a voltage feed forward filter Our voltage loop coefficients with a cross area of 7 hertz 50 degrees And our current loop coefficients with a cross area of 10 kilohertz 50 degrees We've defined our switching frequency as 200 kilohertz And our voltage loop frequency as 6 kilohertz And we define our design output voltage Overvoltage protection level under voltage protection level, etc We are using a data structure called that we've created called pfc data float to hold all of the variables needed to implement our digital pfc So that will be all of the two quality zero controllers The soft start The runtime variables, etc are all stored in this data structure Which we have called we created an instance of called my pfc So then we have the interrupt search routines. We'll look at them in a moment. Let's just scroll down to our main function first So here we have in main. This is where the microcontroller jumps to at the beginning of execution We configure GPIOs, ADCs Comparator DACs, etc This is all auto-generated code From cuba max Anything Inside these user code begin tags Is the code that we've inserted So this is code that we've inserted here We're turning off switches because on our Um start to get here. We've got some MOSFET controlled load banks. So we just set them to zero to begin with Then we have our digital power or digital pfc library functions. So here's one of them This config is our pfc controller pfc zero on config float We pass it a pointer to our pfc structure We pass it the switching frequency voltage loop Frequency and the soft start time in milliseconds Then we configure our current loop using this pfc zero on our loop float init function Again pointed to structure current loop coefficients min max duty Then we have our vff filter init function Pointed to structure and our coefficients for our vff filter And then we configure our voltage loop. So pfc zero one v loop float init Pointed to structure desired output voltage Converted to adc value using this macro Under voltage protection level converted to adc value over voltage protection level voltage loop coefficients min and max outputs for our voltage loop Then we set our Set up our pdbm and adc using this initialization function here And we set the number of phases that we have in this example We're going to do only going to do single phase. So even though we have two phases on our board for interlude pfc In this particular example, we're just covering a single phase So let's just take a quick look at this pfc init pdbm adc function This is what we want to look at so we mentioned in cuba max So this is our pdbm And we said that we're using compare unit one to set the output high so Compare unit one here sets the output high We're using compare unit three here to set the output low And then we're going to use compare unit four to trigger our adc so This sets up the uh the various compare registers to achieve this And we'll look at how we update the duty based on this uh functionality In a moment when we look at isr, but effectively what we're doing is up down count mode We're we're modulating both edges of our pwm So let's look at the isrs So isrs are our interruption routine and we have multiple isrs in our digital pfc system The first isr we look at is our fast loop. So the fast loop controls are current It runs at our switching frequency And we're using high res timer a To implement our current loop. So high res timer a interrupt will occur Every switching cycle so at 200 kilohertz We first thing we do is set a pin high so we can record on our scope when we enter the isr We clear our interrupt flag and then we read the value of our current And we store that in our My pfc structure Then we run our current loop controller. So pfc zero one i loop float Pass it the pointed to the structure and which phase we want to run So in this case, we're running phase zero because there is only one phase That will execute the two-poly two zero controller and give us our new value of duty So now we read that so my pfc I loop to pull to zero Which phase zero M out gets the value of duty from our pfc structure And then we divide it by two Why do we divide it by two? Well as we saw a few moments ago We're modulating both the trailing and leading edge of our pwm Using an up-down count mode effectively. So we need to split our duty between the trailing and leading edges And We then and That with this duty mask That's exactly what it what it says. It's a mask So if this was all zero then obviously the duty would be zero The mask is set to zero in the case of a fault. So over voltage under voltage or Some other fault condition the mask would be set to zero And that would shut down our boost power stage So then we update the compare registers using this macro here. Let's just take a look at this And we said that we're using compare unit one to set the rising edge and compare unit three to set the falling edge so all centered around the Center of our switching cycle. So I loop ticks as our switching period divided by two is the middle of our switching period minus the Compared the duty value divided by two Would be the rising edge and then middle of period plus duty divided by two would be the falling edge here And that means that we can always sample at the middle of our cycle to achieve our average current Then we set the isr pin low To mark the end of our isr The next is our we want to look at is a slow loop. So the slow loop runs at six kilohertz And in stm cube mx we set up timer d to run at six kilohertz So again, we set a pin high so we can measure the duration check it's running We clear a flag We read our input voltage We read our output voltage saw those in our structure We have a pfc waveform record function here Which we use in our workshop to display the variables in in the ide effectively on a scope We'll look at that here though Then we set the soft start or soft starting or soft stopping And then we run our voltage loop. So pfc zero on v loop executes all of the voltage loop. So let's just take a look at this So pfc zero on v loop inside this function, we run the voltage shoot forward filter We get our v in rms squared value We execute the voltage loop to put a zero voltage loop Then we calculate our multiplier scaling factor Then we check for over voltage we check for under voltage And then we update the soft start accordingly And we also update our duty mask Etc So it's all contained within that one function And finally we just set the output pin low so that we can measure the duration Good. Okay. So let's just run the code and test So We are just programming the device. Yep. So we're up and running And Now we need to turn on ac So to do this we are Several several ways you can do this. So you could use a ac source if you have one Obviously set it to 24 volts For the low voltage board Or if you don't have an ac source you can get 232 20 volt to 24 volt ac adapters or in the us 120 to 24 volt ac adapters There's actually quite a few 24 volt ac devices out there like cctv cameras and Doorbells, etc. Christmas lights. So they're actually quite readily available But if you have an ac source you can use this as well. So this ac source has been set to 24 volts Uh zero dc and let's measure the power factor Yes, so let's turn that on Okay, and we just need to turn on the probes and set the time base Okay, so we have On the screen here, uh our input voltage is channel three so 24 volt ac input voltage and the change of trigger yellow trace channel one is the Scaled down and rectified input voltage So now we just need to turn on our board and run our code So first we'll turn on the board And this is without pfc running. So pfc is not running So what we see now is a peaky waveform Rich in harmonics, which is what we don't want. So we're trying to avoid This is the typical waveform that you see When you have a bridge rectifier followed by some volt capacitance. So the block capacitance is being charged Towards the crest of the input voltage sine wave when the bridge rectifiers is conducting We have the output voltage from channel four The boost pfc is not running at the moment. So we're getting 20 volts per division. So roughly 30 volts On the output So now we can run our code and see what happens. So i'm just going to press run here go back to the scope And then pfc is running so i'll just turn on some load And you can see our input current Channel two is now a very nice sinusoid in phase with the input voltage With From the looks of it very very few harmonics other than the fundamental So that's good our output voltage Channel four is 40 volts Which is what we want and let's just change the load to see if our voltage loop is working So input current drops our voltage is still 40 volts. So our voltage loop is doing what it should do And at very low load We're in dcm for part of the cycle. So This notch here is when we're going from dcm to ccm As we increase the load then the waveform gets much better So we're full load at the moment. Let's look at our power factor so Let me move this here We have the power factor reading here from our ac source and we're getting Vectorly one power factor one, which is what we want. That means there's very few harmonics Um the blue button on the nuclear board has been programmed to disable the pfc So let's just press the blue button and see what happens So Our input current is now very peaky rich in harmonics on our power factor has reduced to 0.7 so The pfc is definitely doing its job Uh getting good power factor and few harmonics So that's good release the blue button and pfc comes back We'll go back to a power factor of one Okay, so let's analyze this in a bit more detail. We can use the nice features of our syglent oscilloscope to look at the harmonics so if we Turn on the math function f1 here We want to do an fft on channel two and we need to just change the time base of it Yeah, let's do that and we'll change the horizontal so we start at um Let's say one hertz And then we end at let's say one kilohertz We can then um turn on The table to look at our harmonic peaks So this is one hertz. So this is 50 hertz our fundamental And then that will be 100 hertz and then This mark two here will be our third harmonic 150 hertz and we're getting About minus 55 56 Oh Oh, sorry, I think the the the markers are moving So at the moment, it's the third homework the the third harmonic this one here minus 34 Yeah, two and three are switching so minus 33 Okay, so let's just see what happens to this harmonic here 150 hertz third harmonic When we disable pfc Let's disable the pfc Well, all the harmonics shoot up And this one here Is now minus eight db So we've gone from minus 33 to minus eight. So that's 25 db a difference that's a significant difference So you can see obviously that our pfc is doing its job if I release this blue button all of the harmonics will reduce And we're going over to update there we go And this harmonic the third harmonic has reduced to minus 34 33 db there So a difference of 25 db. So a significant difference there good, so our so our pfc appears to be working reducing the harmonics You could then compare these against the standard 61 000-3-2 Depending on which class device you're using and check that you pass the limits I've I've tested this with the power analyzer. So I know this passes very well But you can also use your scopes fft function to do that for you good, okay, so Let's now just take a look at our Switching frequency. So I'll disable the fft I'll bring in the time base so we have our This is obviously at 5 milliseconds per division. So you see our 100 hertz rectified input voltage or 50 hertz input voltage And 50 hertz current there But obviously our our switching frequency is much higher than this. We're switching at 200 kilohertz So what we can do is just move some probes So I'm going to just remove the input voltage And connect The switch current probe and you can see the switch current is Sinus hurdle. That's good. That's what we want. Um, and then I'm going to move the Scaled input voltage to the pwm and change the trigger Now if I just go to something close and so find my seconds per division Obviously 200 kilohertz is our switching frequency. So you can see that our pwm Is changing sinusoidally as you'd expect to give us that sinusoidal input current So I'm just going to use a single trigger And there we go. We see the pwm at 200 kilohertz because we're five max seconds per division And the switch current you can see there a nice ramp on a step in ccm And we're taking our sample in the middle of the on time. So it's nice and clean. There's no there's no spikes there So that's good The other signal we want to look at is our voltage loop a voltage loop. We said as much is running much slower Six kilohertz. So I'm going to Run this and move the probe to The test pin v-loop so v-loop is The let me show you in the code Is the pin we set high here v-loop iosr We set that high when we enter the slow loop and then we set it low When we leave the slow loop so This is what we see um It's uh It takes takes a few married seconds to execute the slow loop because there's several two voltage zero controllers in there There's lots of conditional if statements checking under voltage over voltage, etc But we don't particularly care because it only Occurs once every six microseconds. So once every six kilohertz Um, but let's just check the frequency make sure we're running At six kilohertz. We use our curses here and we bring this across and Yep, we are running at six kilohertz. That's good And one thing to note is that the duration of our slow loop isr changes So it's you can see the training edge here moving And that's because the slow loop isr is a lower priority than the fast loop is always set that up in qmx Um, and that means that the slow loop will be interrupted several times many times in fact during its execution cycle And the microcontroller would jump from the slow loop lower priority into the fast loop to control the current And then when the current loop isr has finished execution It will return back into the slow loop to finish doing what it was doing So that means that the execution time will vary plus or minus some amount But again, that's fine because we still are executing our slow loop every six kilohertz Good so as you can see here we have a nice running pfc Um We are have a good power factor Um Power factor of one we have low harmonics. Uh, the voltage is well regulated uh using the voltage loop And effectively we have a very well designed pfc. We don't have time to measure the uh the control loops There are some other of our videos on youtube about measuring the voltage loop current loop There's a few uh interesting points you have to take note of when you measure the voltage loop and current loop for a pfc So please feel free to take a look at those videos Um As you can appreciate there's a lot of information around digital pfc. We've only shown you uh an introduction and we spend Days three days in our workshop going through all of this information and showing you how to design digital pfc So if you get the chance, uh, please do come along to our workshops when we run them again hopefully next year And we'll go through this in much much more detail All that reminds me to say is thank you very much for watching We hope to see you at one of our workshops in the future Please visit our website to find more of these resources available for you to download And don't forget to like and subscribe to our youtube channel. Thank you very much and see you again soon. Thank you