Sunday, December 23, 2012

Ferrite Transformer Turns Calculation for High-Frequency/SMPS Inverter


On different forums, I often find people asking for help in calculating the required turns for a ferrite transformer they are going to use in high-frequency/SMPS inverters. In a high-frequency/SMPS inverter, the ferrite transformer is used in the step-up/boost stage where the low voltage DC from the battery is stepped up to high voltage DC. In this situation, there are really only two choices when selecting topology – push-pull and full-bridge. For transformer design, the difference between a push-pull and a full-bridge transformer for same voltage and power will be that the push-pull transformer will require a center tap, meaning it will require twice the number of primary turns as the full-bridge transformer.

Calculation of required turns is actually quite simple and I’ll explain this here.

For explanation, I’ll use an example and go through the calculation process.

Let’s say the ferrite transformer will be used in a 250W inverter. The selected topology is push-pull. The power source is a 12V battery. Output voltage of the DC-DC converter stage will be 310V. Switching frequency is 50kHz. The selected core is ETD39. Remember that the output of the transformer will be high frequency AC (50kHz square wave in this case). When I refer to an output of high voltage DC (eg 310VDC mentioned above), this is the DC output obtained after rectification (using ultrafast recovery diodes configured as bridge rectifier) and filtration (using LC filter).

During operation, the battery voltage does not stay fixed at 12V. With high loads, the voltage will be less than 12V. With low loads and near-fully charged battery, the voltage may be higher than 13V. So, it must be kept in mind that the input voltage is not constant, but is variable. In inverters, the battery low-cut is usually set at 10.5V. So, we’ll take this as our lowest possible input voltage.

Vinmin = 10.5V

The formula for calculating the number of required primary turns is:

 
For our push-pull transformer, this will be one-half the required number of turns.
Npri means number of primary turns; Nsec means number of secondary turns; Naux means number of auxiliary turns and so on. But just N (with no subscript) refers to turns ratio.

For calculating the required number of primary turns using the formula, the parameters or variables that need to be considered are:

  • Vin(nom) – Nominal Input Voltage. We’ll take this as 12V. So, Vin(nom) = 12.
  • f – The operating switching frequency in Hertz. Since our switching frequency is 50kHz, f = 50000.
  • Bmax – Maximum flux density in Gauss. If you’re used to using Tesla or milliTesla (T or mT) for flux density, just remember that 1T = 104 Gauss. Bmax really depends on the design and the transformer cores being used. In my designs, I usually take Bmax to be in the range 1300G to 2000G. This will be acceptable for most transformer cores. In this example, let’s start with 1500G. So Bmax = 1500. Remember that too high a Bmax will cause the transformer to saturate. Too low a Bmax will be under utilizing the core.
  • Ac – Effective Cross-Sectional Area in cm2. You will get this information from the datasheets of the ferrite cores. Ac is also sometimes referred to as Ae. For ETD39, the effective cross-sectional area given in the datasheet/specification sheet (I’m referring to TDK E141. You can download it from here: www.tdk.co.jp/tefe02/e141.pdf  ), the effective cross-sectional area (in the specification sheet, it’s referred to as Ae but as I’ve said, it’s the same thing as Ac) is given as 125mm2. That is equal to 1.25cm2. So, Ac = 1.25 for ETD39.
 
So now, we’ve obtained the values of all required parameters for calculation Npri – the number of required primary turns.

Vin(nom) = 12                                        f = 50000                              Bmax = 1500                          Ac = 1.25

Plugging these values into the formula:



                        Npri = 3.2


We won’t be using fractional windings, so we’ll round off Npri to the nearest whole number, in this case, rounded down to 3 turns. Now, before we finalize this and select Npri = 3, we better make sure that Bmax is still within acceptable bounds. As we’ve decreased the number of turns from the calculated figure (down to 3.0 from 3.2), Bmax will increase. We now need to figure out just how much Bmax has increased and if that is still an acceptable value.

Vin(nom) = 12                 f = 50000                     Npri = 3                         Ac = 1.25




 
                                                                   Bmax = 1600


The new value of Bmax is well within acceptable bounds and so we can proceed with Npri = 3.

So, we now know that for the primary, our transformer will require 3 turns + 3 turns.

In any design, if you need to adjust the values, you can easily do so. But always remember to check that Bmax is acceptable.

  • For example, if for construction difficulties, winding 3 turns + 3 turns becomes difficult, you may use 2 turns + 2 turns or 4 turns + 4 turns. Increasing number of turns won’t hurt – you’ll just be under utilizing the core. However, decreasing number of turns increases Bmax, so just recheck to make sure Bmax is okay. The range I’ve stated for Bmax (1300G to 2000G) is just an estimate. It will work for most cores. However, with many cores, you can go higher to decrease the number of turns. Going lower will just be under utilizing the core, but may sometimes be required if number of turns is too low. 
 
  • I’ve started off with a set Bmax and gone on to calculate Npri from there. You can also assign a value of Npri and then check if Bmax is okay. If not, you can then increase or decrease Npri as required and then check if Bmax is okay, and repeat this process until you get a satisfactory result. For example, you may have set Npri = 2 and calculated Bmax and decided that this was too high. So, you set Npri = 3 and calculated Bmax and decided it was okay. Or you may have started with Npri = 4 and calculated Bmax and decided that it was too low. So, you set Npri = 3 and calculated Bmax and decided it was okay.


Now it’s time to move on to the secondary. The output of our DC-DC converter is 310V. So, the transformer output must be 310V at all input voltages, from all the way up from 13.5V to all the way down to 10.5V. Naturally, feedback will be implemented to keep the output voltage fixed even with line and load variations – changes due to battery voltage change and also due to load change. So, some headroom must be left for feedback to work. So, we’ll design the transformer with secondary rated at 330V. Feedback will just adjust the voltage required by changing the duty cycle of the PWM control signals. Besides feedback, the headroom also compensates for some of the losses in the converter and thus compensates for the voltage drops at different stages – for example, in the MOSFETs, in the transformer itself, in the output rectifiers, output inductor, etc.

This means that the output must be capable of supplying 330V with input voltage equal to 10.5V and also input voltage equal to 13.5V. For the PWM controller, we’ll take maximum duty cycle to be 98%. The gap allows for dead-time.

At minimum input voltage (when Vin = Vinmin), duty cycle will be maximum. Thus duty cycle will be 98% when Vin = 10.5 = Vinmin. At maximum duty cycle = 98%, voltage to transformer = 0.98 * 10.5V = 10.29V.

So, voltage ratio (secondary : primary) = 330V : 10.29V = 32.1

Since voltage ratio (secondary : primary) = 32.1, turns ratio (secondary : primary) must also be 32.1 as turns ratio (secondary : primary) = voltage ratio (secondary : primary). Turns ratio is designated by N. So, in our case, N = 32.1 (I’ve taken N as the ratio secondary : primary).

Npri = 3

Nsec = N * Npri = 32.1 * 3 = 96.3

Round off to the nearest whole number. Nsec = 96.

Thus 96 turns are required for the secondary. With proper implementation of feedback, a constant 310VDC output will be obtained throughout the entire input voltage range of 10.5V to 13.5V.

Here, one thing to note is that even though I took 98% as the maximum duty cycle, maximum duty cycle in practice will be smaller since our transformer was calculated to provide 330V output. In the circuit, the output will be 310V, so the duty cycle will be even lower. However, the advantage here is that you can be certain that the output will not drop below 330V even with heavy loads since a large enough headroom is provided for feedback to kick in and maintain the output voltage even at high loads.

If any auxiliary windings are required, the required turns can be easily calculated. Let me show with an example. Let’s say we need an auxiliary winding to provide 19V. I know that the output 310V will be regulated, whatever the input voltage may be, within the range initially specified (Vinmin to Vinmax – 10.5V to 13.5V). So, the turns ratio for the auxiliary winding can be calculated with respect to the secondary winding. Let’s call this turns ratio (secondary : auxiliary) NA.

NA = Nsec / Naux = Vsec / (Vaux + Vd). Vd is the output diode forward drop. Let’s assume that in our application, a schottky rectifier with a Vd = 0.5V is used.

So, NA = 310V / 19.5V =15.9

Nsec / Naux = NA

Naux = Nsec / NA = 96 / 15.9 = 5.96

Let’s round off Naux to 6 and see what the output voltage is.

Vsec / (Vaux + Vd) = NA = Nsec / Naux = 96 / 6 =16.0

(Vaux + Vd) = Vsec / NA = 310V / 16.0 = 19.375V

Vaux = 19.375V – 0.5V = 18.875V (rounded off)

I would say that’s great for an auxiliary supply. If in your calculations you come to a voltage that is too far off the required target voltage and thus greater accuracy is required, take Vaux as something higher and use a voltage regulator.

For example, if in our previous example, instead of18.875V we had gotten 19.8V but needed more accuracy, we could've used 24V or thereabouts and used a voltage regulator to give 19V output.

So, there we have it. Our transformer has 3 turns + 3 turns for primary, 96 turns for secondary and 6 turns for auxiliary.

Here’s our transformer:

Calculating required number of turns for a transformer is actually a simple task and I hope that I could help you understand how to do this. I hope this tutorial helps you in your ferrite transformer designs. Do let me know your comments and feedback.


Saturday, December 22, 2012

Low-Side MOSFET Drive Circuits and Techniques - 7 Practical Circuits


In many circuits, it is necessary to use MOSFETs for switching. In many cases, the MOSFET drive signals are generated by microcontrollers. In other cases, they are generated by ICs – PWM controllers, timers or any IC in fact. However, MOSFETs cannot always just be connected to the drive signal and be expected to work properly. Due to the construction of the MOSFET, driving it is not the simplest of tasks, especially for beginners. There are many users who regularly ask for help on MOSFET drive related issues or problems on different blogs, websites and forums. So, here I will show some MOSFET drive techniques/methods for MOSFETs configured as low-side switches.

Before I head on to MOSFET drive, let me just tell you what a low-side switch is, in case you don't know. When the MOSFET (that you're using as a switch) sinks current, it is a low-side switch. The load will be between the drain and +V supply. The source will be connected to ground. Gate will be driven with respect to ground.

Let me start with N-channel Power MOSFETs. One thing to keep in mind is that these MOSFETs are fully turned on when the voltage at the gate with respect to the source (VGS) is > 8V. So while at voltages less than 8V, the MOSFET will be on (as long as VGS > VGS(threshold) ), the MOSFET won’t be fully turned on. It will operate in the linear region and there will be very high losses.

Now let’s go to Logic Level MOSFETs. These MOSFETs tend to have much lower VGS (threshold) and thus are usually fully turned on with VGS of 4V to 5V. One downside to Logic Level MOSFETs is that they usually have higher on-state resistances compared to similarly rated Power MOSFETs. So, this would mean higher conduction losses for the same load.

Another thing to remember is the gate capacitance. So, when the MOSFET is to be turned on or off, the gate must be driven high or low with sufficient current quickly enough so as to charge or discharge the gate capacitance quickly enough so that the MOSFET spends minimal time in the linear region and is quickly turned fully on or off. This is true especially for high speed switching when time period is small. However, in low frequency applications, this may not be a big problem as, even if the MOSFET spends some time in the linear region, it will spend the (remaining) majority time fully on and thus the small amount of time spent in the linear region will not cause much of a problem.

Let’s first consider the simplest situation: a MOSFET is driving a load and is periodically turned on and off to turn the load on and off respectively. How long do I mean by periodically? Well, it really depends on the application. But for this situation, the period between turning on and off may be very long or very short but will be at least half a second. A few examples:

  • The MOSFET could be driving a DC fan that will be turned on, kept on for 2 hours and then kept off for about an hour and turned on and off in few hour intervals.
  • The MOSFET is driving a DC lamp that will stay on for the entire night and will only be turned off in the morning and kept off during the entire day.
  • The MOSFET is driving a heating element through which current passes (the heater is on) for about 16 hours a day.
  • The MOSFET is driving an IR LED in a remote transmitter unit. The LED is turned on and off frequently – every few seconds.

So, the above examples should clarify what I meant as “periodically” in the preceding paragraph.
In such situations, MOSFET drive is simplest – apply a high enough voltage to the MOSFET gate and the MOSFET will be on; ground the MOSFET gate and the MOSFET will be off. Quick charging and discharging of the MOSFET gate capacitance are always desirable. But in this situation, the charging and discharging need not be very quick as frequency isn’t high.
Here are a few circuits that can easily be used.




 Fig. 1  (Click on image to enlarge it)


Fig. 2  (Click on image to enlarge it)


Fig. 3  (Click on image to enlarge it)


Fig. 4  (Click on image to enlarge it)


In each of these circuits Vin is the input drive signal to the MOSFET. +V_DRIVE is the voltage supply to drive the MOSFET. When the MOSFET is on, VGS = +V_DRIVE. When the MOSFET is off, VGS = 0. MOSFETs have a maximum rated VGS. For most MOSFETs, this is 20V. For some it is higher up to 30V. There are MOSFETs (usually Logic Level) which have a lower maximum rated VGS. It is always best to check the datasheet for the specific MOSFET being used.


Fig. 1 shows an inverting driver. Here, the MOSFET is turned on when Vin is low and turned off when Vin is high. When the MOSFET is on, the load is on and vice versa.

Fig. 2, Fig. 3 and Fig. 4 show non-inverting drivers where the MOSFET is turned on when Vin is high and the MOSFET is turned off when Vin is low.

In Fig. 2, the MOSFET turns off almost instantaneously when Vin is zero. This is because when Vin is zero, Q3 is off and Q5 is on. Q5 pulls the gate low, discharging the gate capacitance. However, the MOSFET turn-on is not instantaneous. When Vin is high, Q3 is on and Q5 is off. So, current flows through R14 and charges the MOSFET gate. Here turn-on time is dictated by R14. However, when I say long turn-on time, I am talking in the order of tens or hundreds of microseconds to milliseconds at max. Thus this circuit can easily be used in any of the situations depicted in the four examples above or any similar situation. This driver cannot be used when switching is done in the order of microseconds where both turn-on and turn-off must be very quick – almost instantaneous.

In Fig. 3, the MOSFET turns on almost instantaneously when Vin is one. When Vin is one, Q6 is on and so Q8 is on and thus VGS = +VDRIVE and so the MOSFET turns on. However, when Vin is zero (and so Q6 and Q8 are off), the MOSFET gate discharges through R18. So, gate discharge is slow and turn-off time is dictated by R18. However, this “slow” time is in the same order as mentioned above and so can be used in any of the situations depicted in the four examples above or any similar situation. Similarly, this driver cannot be used when switching is done in the order of microseconds where both turn-on and turn-off must be very quick – almost instantaneous.

In Fig. 4, both turn-on and turn-off are very quick. This driver can be used for higher frequencies than can the other three.

If the MOSFET is a Power MOSFET, +V_DRIVE should be at least 8V. A voltage commonly used is 12V. +V_DRIVE should be less than VGS(max) as specified in the MOSFET datasheet. Some “headroom” should be used. So, if VGS(max) is specified as 20V in the MOSFET datasheet, it is good practice to use lower than 18V. 12V and 15V are commonly used for Power MOSFET gate drive. For Logic Level MOSFETs, 5V is most common because the reason a Logic Level MOSFET is usually used instead of a Power MOSFET is that the Logic Level MOSFET can be driven from 5V. In the above shown circuits, the MOSFET can be either a Logic Level MOSFET or a Power MOSFET. +V_DRIVE must be selected accordingly.

Each of the above four circuits requires only a minimum voltage of 0.7V to fully turn on the MOSFET (+V_DRIVE must be selected properly). Each of the above circuits can also be used to drive either Logic Level MOSFETs or Power MOSFETs.

Here are a few more circuits that I’ll discuss below.




Fig. 5  (Click on image to enlarge it)

Fig. 6  (Click on image to enlarge it)


 
Fig. 5 and Fig. 6 both show non-inverting drivers.

The driver in Fig. 5 has the advantage that, while being simple, it can both quickly turn on and turn off the MOSFET. In the drivers in Fig. 1, Fig. 2, Fig.3 and Fig. 4, when the MOSFET is on, VGS = +V_DRIVE. The MOSFET is turned no when Vin is at least 0.7V. So, that means that when Vin is higher than 0.7V, VGS = +V_DRIVE. So, for example, if +V_DRIVE is 12V, VGS will be +12V, when Vin = 3.3V, 5V, 9V and so on.

However, in the drivers shown in Fig. 5 and Fig. 6, when the MOSFET is on, VGS = Vin – 0.7V. So, that means that in Fig. 5, even if +V_DRIVE is +12V, when the MOSFET is on, VGS will only be equal to Vin – 0.7V and not +V_DRIVE. So, for Fig. 5 and Fig. 6, if +V_DRIVE is +12V and Vin = +5V, VGS will equal +4.3V when the MOSFET is on.

That is the disadvantage to these circuits – that the MOSFET VGS will depend on the magnitude of Vin. However, there are situations where these disadvantages do not matter.

If the MOSFET is a Logic Level MOSFET that is to be driven by a microcontroller, then the driver in Fig. 5 can be used quite easily. When Vin = +5V (typical output of a microcontroller), the MOSFET will be turned on and VGS = +4.3V. Remember that +4.3V is enough to fully turn on the MOSFET. When Vin = 0, VGS = 0 and the MOSFET will be off.

You may, then, ask why a driver is needed. You may simply drive the microcontroller directly from the microcontroller. And well, you can. For low speed applications. So, in the four situations previously described, the microcontroller can directly drive the Logic Level MOSFET. The problem lies where the microcontroller output voltage will drop when supplying the current needed to quickly turn the MOSFET on and off when a large current will be required. So, the MOSFET may not fully turn on. In that case, the driver shown in Fig. 5 can be used to ensure that the MOSFET can be driven with sufficient current.

The driver in Fig. 6 can be used when the control chip can only source current but cannot sink current. One example is the SG3524 PWM controller configured to source current. The 0.7V drop is not a problem since the outputs of the SG3524, when high, go up to near the supply voltage. So, even after the 0.7V drop, the voltage is sufficient to fully turn the MOSFET on.

The driver in Fig. 5 is commonly used to drive a MOSFET from a PWM controller. Usually the outputs of PWM controllers go all the way up to the supply voltage (usually 12V, 15V, etc). So, this driver can easily be used, because even after the 0.7V drop, the voltage is high enough to fully turn on a MOSFET. The transistors can also supply high currents. This type of driver based on such a combination of transistors is called a totem-pole driver.

Till now, I’ve only talked about low frequency applications. Now let’s move on to high frequency applications.

One thing that must be considered in high frequency applications is the MOSFET drive current. When frequency is high, time period is low. So, the MOSFET must be turned on and off in a short time to avoid high losses. The turn-on and turn-off times must be as short as possible to ensure that the MOSFET will be in the linear region for minimum time.

Thus it is natural that to minimize gate charge time, current must be maximized. So, the quicker the gate must be charged or discharged, the higher the charge or discharge current should be. The drivers in Fig.  4, Fig. 5 and Fig. 6 can be used for high-frequency MOSFET drive.

I’ve covered some discrete drivers here based on transistors. However, I always recommend using dedicated driver chips for driving Power MOSFETs in high-frequency applications. Low-side drivers are easily available at low prices. One widely available low-cost low-side driver (that I often use) is the TC427.

The circuit using a TC427 is very easy. The supply voltage has to be provided to the supply pins. Then, all you need to do is provide the input signal and just drive the MOSFET from the TC427 output. The TC427 has 2 drivers. So you can use the TC427 to drive 2 MOSFETs from 2 different input signals. Here is the circuit diagram:

 Fig. 7


 
As you can see, the circuit is very simple. +V is the supply voltage. MOSFET drive voltage swings up to about VDD - 0.025V when MOSFET is to be driven high and down to +0.025V (can be assumed to be zero as this is way below MOSFET VGS(threshold) when MOSFET is to be driven low.

Download the TC427 datasheet from here:

There are numerous ways to drive a MOSFET in a non-isolated low-side configuration. I’ve shown a few circuits using transistors and one using a dedicated driver.

You can find loads of driver chips and the datasheets should show the circuit diagrams. Here I have only covered low-side non-isolated drive. I shall soon make another tutorial where I cover high-side drive and isolated drive.

I hope that this tutorial has helped you. Let me know your comments and feedback.

Learning SMPS the hard way

SMPS is the toughest section in Power Electronics. Tremendous knowledge is required for choosing active and passive parts for use in SMPS circuits. Special parts are often required specifically for use in SMPS. Many components that can be used for low frequency applications in Power Electronics may fail if used in SMPS circuits.

When I started learning Power electronics in 2008, I placed emphasis on SMPS. Regarding SMPS, nothing was available here. I had to collect and gain everything piecemeal- knowledge, literature, active and passive parts, testing equipment etc. The main drawback was the lack of knowledge in this field. In my country, SMPS is not taught in detail in any engineering university. So, teachers are not readily available. Additionally, relevant books were also not available.

So, my father chalked out a deliberate and detailed plan for me to learn SMPS:

Detailed Plan for learning SMPS at home

Step 1   – Collecting knowledge by collecting required books from abroad.
Step 2   – Gathering SMPS related knowledge from internet.
Step 3   – Collecting test equipment.
Step 4   – Collecting minimum required parts.
Step 5   – Learning and collecting different types of Ferrite Cores and knowing in detail regarding winding of Ferrite Cores in different Topologies.
Step 6   – Learning how to design PCBs using design software and making the PCBs.
Step 7   – Collecting and practicing SMPS in hardware and in simulation software.
Step 8   – Testing the test circuits on verroboard.
Step 9   – Assembling the circuits on PCB and testing in detail.
Step 10 – Finding drawbacks and making necessary corrections.

Accordingly, I collected numerous SMPS-related books.

The first book with which I started to learn SMPS was “Power Electronics Demystified” by Chandra Shekhar Roy, which was collected from Kolkata, India. For beginners it is a very good book.

These are some of the important books I collected:

  • Power Supply Cookbook -- Marty Brown.
  • Practical Switching Power Supply Design -- Marty Brown.
  • Power Sources and Supplies -- Marty Brown editor.
  • Switching Power Supply Design -- Abraham I Pressmen.
  • High Frequency Switching Power Supplies-Theory and Design -- George Chryssis.
  • Demystifying Switching Power Supplies -- Raymond A. Mack,Jr.
  • Practical Design of Power Supplies -- Ron Lenk.
  • Regulated Power Supplies -- Irving M. Gottleb.
  • Switching Converters Medium and High-power -- Dorin O Neacsu.
  • Principles and Elements of Power electronics -- Barry William
  • Switch mode Power Converters Design & Analysis -- Keng C Wu.
  • Switch mode Power Supply Handbook -- Keith H. Billings.
  • Power Electronics Handbook – Industrial Electronics Series -- Edited by Timothy L. Skvarinina of Purdue University.
  • Power Mosfets –Theory and Application -- Duncan A Grant and John Gower.
  • Digital Power Electronics and Applications -- Fang Lin Luo, Hong Ye, Muhammad Rashid.
  • Switching Power Supplies A to Z -- Sanjaya Maniktala

Most of the test equipment including oscilloscope were collected from the  local market. For initial test circuits, good quality parts were collected. However one very annoying thing was that in the test circuits capacitors occasionally leaked and got destroyed. With a lot of effort, I found that in high frequency circuits, low ESR capacitors are to be used - many of the widely available regular capacitors cannot be used as they have rather high ESR. Now there was a problem. How would I know which capacitors have low ESR and which don't? I procured an ESR meter from Portugal through my aunt who lives in Vienna, Austria. With the help of the ESR meter, low ESR capacitors were collected from local market. Same thing happened with regular rectifier diodes also and I came to know ultimately that for high frequency SMPS circuits, ultrafast or schottky diodes needed to be used instead of the regular rectifier diodes (because of the reverse recovery time).


Next problem was with Ferrite core. In local market, required ferrite cores were not available. After quite a lot of searching, some ETD39 type ferrite cores made by Chinese manufacturers were found. These were available as spares for flyback transformers used in Chinese non-brand television sets available in local market. I collected some, unwound them and split them into 2 halves. I rewound the bobbin by hand to use in test circuits. Initially I faced problems due to the large air gap these cores had (intentionally provided as they were meant for use in flyback topology) and could not be used in an optimum way in other topologies where large air gap is not to be used. Problem was also there regarding use of required size of wire in different frequencies and those problems could be overcome by gaining knowledge regarding the phenomenon of “Skin Effect” in high frequency circuits. With the passage of time, I gathered knowledge regarding different type of cores – like EE, EC, EI, ETD, Toroidal – and their applications in different SMPS topologies and winding speciality. I collected some from China with the help of local suppliers.

To collect the required active and passive parts for SMPS circuits, one has to have detailed knowledge regarding SMPS. I had to work extremely hard to gain sufficient pertinent knowledge, for hundreds of hours had to be spent reading books, surfing net, going through forum contents and reading datasheets, application notes, design tips, etc. After gaining the minimum knowledge required, it became easier to collect required parts for test circuits from home and abroad. One of my aunts lives in USA and I have collected lots of parts from USA through her.

I could not manage any useful software for SMPS hardware simulation. But for making PCB, I learnt and used PROTEUS ARES software. Since I required only a few pieces for each test circuit, professional PCB makers were usually unwilling to make the PCBs. Being in a real dilemma, using the internet, I learnt how to make PCBs and started making them at home with Ferric Chloride, Sticker paper, Laser printer and Iron.

Almost everything was sorted out but one crucial thing remained. Professional transformer winders of the local market were not familiar with Ferrite Core winding and hence I personally had to wind by hand all the transformers for different Topologies with different sizes of wire and also with Litz wire (self-made) in the initial test circuits.

In the first 2 years, for learning about the different stages and sorting out different problems at the different stages of the test circuits, I made almost 300 circuits. Though almost all of these circuits were unsuccessful for different drawbacks, I learnt valuable practical lessons from those failures. By correcting those drawbacks and learning lessons from those, I am now confident and by applying the gathered knowledge of SMPS with microcontroller, I started building some successful circuits of practical value.

I love reading SMPS books like reading novels. Making SMPS circuits is like playing games to me.

Learning SMPS is really difficult but one can learn it with passion, patience, hard work, dedication and relentless effort.

If any beginner in SMPS finds anything useful for his pursuit of learning, my effort of narrating my difficult journey in SMPS world will be worth the trouble.

---------------------------------------------------------------------------------------------------------------------------------------------------

I edited this article from the article I had previously posted on www.edaboard.com on 17-02-12 - http://www.edaboard.com/entry1444.html .

Sunday, December 9, 2012

Modalities of Using the PIC (16F877A) CCP Module - Compare Section


The CCP module is one of the most important modules in the PIC microcontroller. While being extremely important, it is not very difficult to use. Here I will show you how to use the compare section of the CCP (Compare, Capture, Pulse Width Modulation) module of the PIC16F877A. You can use this concept for any PIC with a CCP/ECCP module

There are 2 modules associated with the compare section – the CCP module which we’ll use for the compare function, and the Timer 1 module. The function of the compare module is to compare (obviously, as the name suggests) the value of the CCPR1 register against the Timer 1 value. The CCPR1 register is actually composed of two 8-bit registers that together form a 16-bit register. The high 8-bits – the high byte – make up the CCPR1H register and the low 8-bits – the low byte – make up the CCPR1L register.

For example, if the value of CCPR1H is 30 and the value of CCPR1L is 47, what is the value of the 16-bit CCPR1 register?
Solution:
CCPR1H = 30 = 0x1E
CCPR1L = 47 = 0x2F
So, the high byte = 0x1E. The low byte = 0x2F.
CCPR1 = 0x1E2F = 7727

Like CCPR1, TMR1 is a 16-bit register composed of two 8-bit registers – TMR1H and TMR1L. If TMR1H = 30 and TMR1L = 47, TMR1 = 7727.

Back to the function of the compare mode. The 16-bit value of CCPR1 is compared against the 16-bit value of TMR1. RC2 pin is associated with the CCP module. Upon match of the CCPR1 register against TMR1 (when CCPR1 is equal to TMR1), one of the following can happen to RC2 (RC2 is the pin associated with the CCP module) depending on the setting of the CCP1CON register:
          • RC2 is driven high
          • RC2 is driven low
          • RC2 is unaffected

If RC2 is to be used, TRISC2 must be cleared to make RC2 a digital output.

Upon match of CCPR1 and TMR1 (when CCPR1 is equal to TMR1), CCP1IF (bit 2 of PIR1) is set. If the CCP interrupt is enabled, a CCP interrupt is generated. To enable the CCP interrupt, CCP1IE (bit 2 of PIE1) has to be set.

Another interesting setting of the CCP module is that it can be set up so that upon compare match of CCPR1 and TMR1, the following happen:
        • CCP1IF is set
        • CCP interrupt is generated if CCP1IE is set
        • RC2 is unaffected
        • Timer 1 is reset (TMR1 = 0)
        • If the ADC module is enabled, an A/D conversion is started

What happens when there is a compare match of CCPR1 and TMR1 depends on the setting of the CCP1CON register. Here is the CCP1CON register:



• When CCP1CON = 8 (0x08), CCP1 module is configured for compare mode and is set up so that upon a compare match of CCPR1 and TMR1, RC2 is driven high.

• When CCP1CON = 9 (0x09), CCP1 module is configured for compare mode and is set up so that upon a compare match of CCPR1 and TMR1, RC2 is driven low.

• When CCP1CON = 10 (0x0A), CCP1 module is configured for compare mode and is set up so that upon a compare match of CCPR1 and TMR1, RC2 is unaffected.

• When CCP1CON = 11 (0x0B), CCP1 module is configured for compare mode and is set up so that upon a compare match of CCPR1 and TMR1, RC2 is unaffected, TMR1 is reset and an A/D conversion is started if the ADC module is enabled.

Remember that in all compare modes, whenever there is a compare match of CCPR1 and TMR1, CCP1IF is set.




---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Now let’s take a look at a code example to make things clear. Let’s say we’re running a PIC16F877A with a crystal oscillator of frequency 20MHz. Let’s use the compare mode to generate a 50Hz output with 50% duty cycle.

Solution:
Tcy = (1/20000000)*4*106 us = 0.2us

Since one TMR1 increment takes 1 instruction cycle (with prescaler 1:1), one TMR1 overflow takes 65536 instruction cycles. That’s 65536 * 0.2us = 13107.2us = 13.1072ms. For 50Hz, time period = 20ms. So with 50% duty cycle, on time is 10ms.

The number of instruction cycles in 10ms is equal to (10000/0.2) = 50000.

When TMR1 = 50000 after counting up from 0, 10ms will have elapsed. So, we’ll assign 50000 to CCPR1 so that a compare match occurs every 10ms. We’ll use the CCP setting for trigger special event where TMR1 is reset upon compare match.

CCPR1 = 50000 = 0xC350
Therefore, CCPR1H = 0xC3 and CCPR1L = 0x50

For our required mode of operation, CCP1CON = 11. As CCP interrupt will be used, CCP1IE must be set. GIE and PEIE also have to be set.

We’ll use RC0 as our output pin. Every 10ms, the state of RC0 must be toggled. We’ll do the toggling in the interrupt service routine (ISR). We chose to use RC0. Since in our mode of operation, RC2 is unaffected, we can use any pin really and I arbitrarily chose to use RC0. If any of the pins that are multiplexed to the ADC module or the comparator module is to be used, remember to disable the analog circuitry and use that pin as digital output.


The code should be quite easy to understand. Here it is:




//Programmer: Syed Tahmid Mahbub
//Target Microcontroller: PIC16F877A
//Compiler: mikroC PRO for PIC
//Using Compare Section of PIC CCP Module - using PIC16F877A
void interrupt(){
     if (CCP1IF_bit == 1){
        RC0_bit = ~ RC0_bit; //Toggle RC0
        CCP1IF_bit = 0;
     }
}

void main() {
     PORTC = 0;
     TRISC = 0;
     CCP1CON = 11;
     CCP1IE_bit = 1;
     GIE_bit = 1;
     PEIE_bit = 1;
     CCPR1H = 0xC3;
     CCPR1L = 0x50;
     //CCPR1 = 0xC350 = 50000
     T1CON = 0x01; //Prescaler 1:1, start Timer 1
     while(1){
     //Do whatever else is required
     }
}


Here is the output waveform:




 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Now let’s take a look at another code example. Let’s say we’re running a PIC16F877A with a crystal oscillator of frequency 20MHz. Let’s use the compare mode to generate a 50Hz output with 75% duty cycle.

Solution:
Tcy = 0.2us

In the previous example, as duty cycle was 50%, the simple thing to do was to toggle the output pin. Here we cannot do that as duty cycle is 75%.

This time, we’ll use RC2 as the output pin.
Time period = 20ms. With 75% duty cycle, on time is 15ms. Off time is 5ms.

Number of instruction cycles for 5ms = (5000/0.2) = 25000. Number of instruction cycles for 15ms = 75000. This is beyond the maximum for TMR1. So, we’ll utilize the TMR1 prescaler. We’ll use a prescaler 1:2.
So, for 5ms, number of TMR1 increments = 12500. For 15ms, number of TMR1 increments = 37500.

We’ll change the CCP1CON setting from 8 to 9 to 8 to set the output high to low to high upon consecutive compare matches. We’ll need to alternately change CCPR1 between 12500 and 37500 as required.

12500 = 0x30D4
37500 = 0x927C

In the 2 modes of operation we chose to use, TMR1 is not cleared/reset upon compare match. So, in the interrupt, we must clear TMR1 manually to start TMR1 counting from 0.

If you understand how this code works, you should be clear about the compare module by now.
Here is the code:
  
//Programmer: Syed Tahmid Mahbub
//Target Microcontroller: PIC16F877A
//Compiler: mikroC PRO for PIC
//Using Compare Section of PIC CCP Module - using PIC16F877A
void interrupt(){
     if (CCP1IF_bit == 1){
        TMR1H = 0;
        TMR1L = 0;
        //TMR1 must be cleared manually
        if (CCP1CON == 8){
           CCP1CON = 9;
           CCPR1H = 0x92;
           CCPR1L = 0x7C;
           //CCPR1 = 37500 --> 15ms
        }
        else{
             CCP1CON = 8;
             CCPR1H = 0x30;
             CCPR1L = 0xD4;
             //CCPR1 = 12500 --> 5ms
        }
        CCP1IF_bit = 0;
     }
}

void main() {
     TRISC = 0;
     PORTC = 4;
     CCP1CON = 9; //Clear RC2 on match
     CCPR1H = 0x92;
     CCPR1L = 0x7C;
     //CCPR1 = 37500 --> 15ms
     CCP1IE_bit = 1;
     GIE_bit = 1;
     PEIE_bit = 1;
     T1CON = 0x11; //Prescaler 1:2, start Timer 1
     while(1){
     //Do whatever else is required
     }
}




Here is the output waveform:




The compare section of the CCP module is very useful for timing-dependent applications. As shown above, you can use it for PWM at frequencies that are too low for the PWM section of the CCP module.

If clearly understood, it is very easy to use and I hope I could make the compare mode of the CCP module clear. Your comments and feedback are welcome.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can download this entire tutorial in PDF format from 4shared or RapidShare: