Author Topic: General Purpose Power Supply Design  (Read 204466 times)

0 Members and 1 Guest are viewing this topic.

Offline HackedFridgeMagnet

  • Super Contributor
  • ***
  • Posts: 2028
  • Country: au
Re: General Purpose Power Supply Design
« Reply #200 on: May 10, 2012, 04:25:52 am »
I am looking forward to making this design up Richard. Thanks for all your effort.
My current PSUs are not really up to scratch, just cheap kit supplies.
Are you going to layout the pcb, and specify hardware?
I really like the idea of very high resolution and good stability and just using my DMM to set the voltage accurately.
Obviously I like the preregulator too, but I would have thought this was fairly standard  these days.

Kudos to you too AHellene, I hope some of your code or ideas will be in the firmware, I would like to compare it against some of my AVR code, to look for improvements that I could include in mine.
 

Offline BravoV

  • Super Contributor
  • ***
  • Posts: 7547
  • Country: 00
  • +++ ATH1
Re: General Purpose Power Supply Design
« Reply #201 on: May 14, 2012, 07:24:03 am »
A bump for this fine thread, and really curious to see the next revision of linear front end part.  ;)

Offline heychris

  • Contributor
  • Posts: 16
Re: General Purpose Power Supply Design
« Reply #202 on: May 15, 2012, 04:37:34 pm »
I was wondering if anyone had thought about putting a couple USB charger ports on here.  I know that I have several USB wall warts I'd like to get rid of.  I know there needs to be a circuit to negotiate with chargers.  I've played with the TI tps2543.  http://www.ti.com/product/tps2543   It works quite well.
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #203 on: May 16, 2012, 12:10:40 am »
I will get back to posting some circuits today. The basic regulator board will not have a USB charger output, but there will be a separate isolated full featured control board, and that could have a USB charger.

The issue is that a USB charger outputs really need 5V at at least 1A to cope with modern phones and Kindles, and that is just not available on the basic regulator board.

Anyway, I just do not like USB outputs that are floating at the same potential as the power supply negative output. The whole idea of a floating output is you can connect it to anything - a 400V DC line, or a high voltage AC line, and I hate to think of a USB charging output at the same potential.

Richard.
 

Offline markus_b

  • Regular Contributor
  • *
  • Posts: 115
  • Country: ch
Re: General Purpose Power Supply Design
« Reply #204 on: May 16, 2012, 09:16:35 am »
I would leave the USB charging port out. While it can come handy at times, this is better served with a dedicated item. I have a four-port USB power supply at my desk for these needs. I also agree that separate potential for the lab power supply and USB devices are a good idea, other wise you are setting yourself a trap to break something down the road.
Markus

A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible.
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #205 on: May 16, 2012, 12:25:27 pm »
I would leave the USB charging port out. While it can come handy at times, this is better served with a dedicated item. I have a four-port USB power supply at my desk for these needs. I also agree that separate potential for the lab power supply and USB devices are a good idea, other wise you are setting yourself a trap to break something down the road.

I will probably leave it out. The best way to add a USB port that is isolated from everything else is to buy a $5 USB adapter, break it open and put the board inside the supply. Why not just leave the adapter as it is in its fully double insulated rated adapter enclosure.

If I add a USB port to the version with a full control panel that will probably be able to control several supplies, it will be because it is functioning in some way as a USB port (such as datalogging, or programmable sequencing) rather then just being a 5V supply output.

Richard.
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #206 on: May 16, 2012, 03:10:54 pm »
I will get back to the supply design.

Below I have the basic regulator, but the circuit will be changing a lot over the next few days as I add in the voltage and current reference components, the pre-regulator, high voltage mods and a few other enhancements.



I have settled on a PNP pass transistor, but instead of using a single PNP on a heatsink, I have gone for multiple 2N2907A transistors, or PZT2907A surface transistors on 1 square cm of PCB as a heatsink. The driver transistor is a 2N2222A or PZT2222A for surface mount use. The regulator board can have up to a 5V voltage drop at 1A without any heatsink. I am assuming the board will run at 75 deg C or less. A pre-regulator circuit will allow for the full 0 to 25V range at 1A.

You get a few things with this approach - the transistors can be very cheap particularly in quantity, and the transistors are operating at an optimal DC gain operating point at maximum output current.  There are specialized high gain PNP transistors from companies like Zetex that impressive performance, but the price is higher and the availability is lower. The bandwidth is good, and by eliminating an extra darlington-type gain stage, I get a much better stability margin. I think it will work well - better then my Mark I and Mark II designs.

The intended output is from 0V to 25V maximum for this configuration, but I will have a high voltage configuration that can go much higher without having to redesign the compensation circuit.

In the circuits I will be posting in the next few days, I will be giving three basic circuit choices:

1. Potentiometers for voltage and current control - meter modules for measuring voltage and current.
2. Basic microprocessor control with very basic single supply interface
3. Powerful controls on a separate isolated PCB that can drive several isolated regulator boards. Regulator boards have a simple microcontroller and talk to main controller via optocouplers.

I may also allow for paralleling modules for more then 1A output, and also master slave use for multiple tracking supplies.

I had done a fair bit of work on an isolated switching pre-regulator, but I am concerned about output noise, and the ease of making the transformers. I am not convinced I will go that way yet.

Linear pre-regulators are a simpler solution, and will definitely be an option.

More soon ....

Richard.

 

Offline markus_b

  • Regular Contributor
  • *
  • Posts: 115
  • Country: ch
Re: General Purpose Power Supply Design
« Reply #207 on: May 16, 2012, 03:58:09 pm »
Richard, I find your solution interesting.

But I'm not convinced that it is better/cheaper than Daves version with a single LT3080. The 2n2907A is $0.80, a LT3080 is $4 so the price is about the same. But I'd feel more comfortable with the TO220 LT3080 screwed to a heatsink dissipating 25W than the same spread over 9 TO-18 devices with their flimsy heat-sinks.

The 25 W is probably about the worst case when drawing a full amp at low voltage.
Markus

A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible.
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #208 on: May 17, 2012, 01:03:48 am »
Richard, I find your solution interesting.

But I'm not convinced that it is better/cheaper than Daves version with a single LT3080. The 2n2907A is $0.80, a LT3080 is $4 so the price is about the same. But I'd feel more comfortable with the TO220 LT3080 screwed to a heatsink dissipating 25W than the same spread over 9 TO-18 devices with their flimsy heat-sinks.

The 25 W is probably about the worst case when drawing a full amp at low voltage.

The 2N2907A is below 10c - if not you are being ripped off. In quantity, the prices get very low. The 2N2907A and the 2N2222A are exactly the kind of transistors it is great to stock in the lab anyway, so you can go for the 100 off prices without straining the wallet too much. I would use the TO92 devices, not the TO18 ones. The exact part number varies from manufacturer to manufacturer. The through hole version of the circuit relies on air cooling of the TO92 packaged transistors - no heatsink is used.  The PZT2907A and PZT2222A can dissipate over 1W, so they are pretty nice surface mount variations to have. I may have to increase the transistors to 10 for the through hole board.

My solution is fine, as long as I can limit the maximum dissipation on the board to 5W which amounts to about 3.6W peak in the transistors. My job is to see that the regulator board does not dissipate more then 5W continuous under any condition.

But why not just use Dave's LT3080? It is an extremely nice chip, but not perfect. Some of the problems are:
  • The supply current for the chip comes from the load, so there is a minimum load the chip needs to regulate. Dave's solution was to add a LM334 temperature dependent current source, but this has issues below 1V output, and the temperature dependency makes it hard to properly compensate for it in the current measurement circuit. I just like supplies that work properly down to zero volts - I do not design lab supplies that do not. It is easy to overcome this limitation by adding an extra resistive load to Dave's supply when workng at low voltages, but my choice is to avoid that.
  • The LT3080 only regulates to the output pin of the chip, not the output terminal of the power supply. If there is a total of 0.1 ohms resistance between the LT3080 and the output terminal (wiring resistance and maybe an output ON/OFF switch), then you get 0.1V regulation only as you go from 0 to 1A.
  • You cannot add a fuse on the LT3080 output as the resistance will wreck the regulation. This limits the ability to protect the supply from something like a SLA battery you accidentally connect in reverse. I can add a fuse to my board with no effect on the regulation and a fuse or breaker is the only protection against excessive high energy positive of negative voltages from a connected load.
  • The LT3080 has a reasonably high thermal resistance from junction to case which limits how much supply you can get in a small case. That is why Dave's original supply was going to be limited to about 10V maximum, and to boost that to 20V, he had to add a switching pre-regulator. As discussed in this thread, there are problems with the switching pre-regulator in that a specially crafted pulsed load can end up cooking my regulator board or in Dave's case, the LT3080.  The LT3080 is thermally protected right?  Well only you a point. In thermal limiting mode, the LT3080 chip is at 160 degC. The Absolute Maximum operating temperature of the chip is 150 deg C. Translated into english, that means Do Not Rely On Thermal Limiting for Normal Operational Modes. In a Lab power supply, difficult loads are a normal operating mode. The whole idea of a Lab supply is it is meant to be able to cope with difficult loads without failing. The thermal limiting of the LM317 chip has the same concerns as the LT3080
  • The LT3080 requires a reverse bias bypass diode across the LT3080 to prevent damage from voltages coming from the load. I like the rather unique property of the LM324 in that is can take over 30V on the input pins even when the LM324 is unpowered, so I can avoid the need for this diode. With the reverse diode solution, I do not like the fact that attaching a 12V SLA battery to the supply while it is off basically powers up the power supply. But this is my preference - not really an issue I want to get into a big discussion about. People choose different colours to paint their house - it does not mean that one colour is right and another colour is wrong - they are just different colours

That list will do for the moment. The LT3080 is a brilliant chip, and Dave chose it for very good and sound reasons. My board is a different solution that will have its own positives and negatives.

Just to go back to the start, I was frustrated with the number of times people had to go to the LM317 IC to make a cheap power supply as it does not make a great lab supply. I initially wanted to see if I could come up with some kind of general purpose regulator board that could be used to make different supplies with different output ranges and current ranges without needing to design a new compensation circuit each time.

It looks like it just cannot be done - change a voltage divider or a regulator power transistor and you need to redesign the compensation. A really well designed power supply compensation circuit is very hard to do for any power supply as all lab power supplies relying on a feedback stabilization operate very close to instability. It is something impossible for beginners to get right, and it can be very time consuming to get right for experienced designers.

The reason why devices like the LM317 are so popular is they have built in compensation. Not perfect for all capacitive loads, but pretty good. The LT3080 also has excellent built in compensation.

There are IC's available that can drive an external MOSFET to make a linear supply, but I really do not like lab supplies using MOSFETS. The thing I just do not like is that if you switch a sudden overload on to a MOSFET supply, the Miller effect causes turn the MOSFET hard on causing  peak current in the 10s to 100s of Amps regardless of your current limit setting. This big current spike is something that can easily be eliminated in transistor-based supplies, but it is extremely hard to prevent in MOSFET based linear supplies. I have chosen to stick to transistor-based supply designs.

Inspired by some ideas in Dave's design - in particular the fact that the regulator was always adjusting for zero difference between the reference voltage in and the out voltage (no voltage dividers, so no loop gain change for different output voltage ranges) and the fact that the the supply to the reference circuit was allowed to change with the required output voltage - I started looking at a regulators board made from common parts that are extremely cheap to make a fully compensated module, and then work out how to add extra external parts to extend the range without affecting the regulation compensation.

Compensation ends up being much harder then looking at Bodes plots and getting the zero's and poles right. Things change for different currents, different voltages, different load capacitances, and even if you get stability with all those variations, then you have to look at conditional instability. If you tailor a load to force the regulating opamp into output slew rate limiting, you have changed the loop gain and opamp speed, and this can cause an instability that is not there with static loads. If you can force the opamp to saturate - more problems. Power supply compensation circuits can look simple when completed, but it can be a total nightmare finding that right solution. I bet there are many commercial lab supplies that are conditionally unstable and this is one of the things that separates the $1000+ quality designs from the $100 designs.

My board can be used directly to make a 0 - 22V 210mA supply or a 0-6V 800mA supply , and with an NPN darlington preregulator transistor, it could be extended to 0-25V at 1A. My latest idea would allow it to be extended further to almost anything -  in theory you could make a 0-1000V 1A supply. I will probably limit any designs to 60V maximum myself, but that will not be limited by my board.

I would be prepared to use my board to make an internal HV supply inside another project, but a HV lab supply is extremely dangerous - I really caution against the idea.

If I can get the module paralleling to work, then you can get 2, 3, 4 amps out and the modules will be cheap enough to make this economical.

All these supplies will regulate fully down to 0V.

At the moment, it is really hard to find any kind of generalized design that allows you to make a supply with exactly the voltage and current specs you need. Hopefully, this design will mean that there is at least one option available.

Richard.
« Last Edit: May 17, 2012, 04:38:33 am by amspire »
 

Offline markus_b

  • Regular Contributor
  • *
  • Posts: 115
  • Country: ch
Re: General Purpose Power Supply Design
« Reply #209 on: May 17, 2012, 08:47:30 pm »
Richard,

You make an, excellent argument, way out of my league and experience to argue!

Personally I'm gunning for 30V/3Amps, sort of what a LT3083 can do. While it is a fine chip, it will have the same short comings as the LT3080 which you elaborated on.

A switching pre-stage will be an essential tool to get reasonable heat dissipation. The linear stage should only have to cope with 2V or so to keep the power losses down.

 I'm working on my own solution, using a 3080 for now, will show my hand when I'm confident not to be laughed out of the forum right away. :-).

Markus
Markus

A good scientist is a person with original ideas. A good engineer is a person who makes a design that works with as few original ideas as possible.
 

Offline BravoV

  • Super Contributor
  • ***
  • Posts: 7547
  • Country: 00
  • +++ ATH1
Re: General Purpose Power Supply Design
« Reply #210 on: May 17, 2012, 11:15:11 pm »
Richard, thanks for posting the update, I did capture in ltspice and it seems like the lowest current in CC mode is around 700ma even the VRef for the current was set below 700mV like below example.

Any idea why ? Glitches at ltspice ?

Also attached the ltspice for the below circuit.

« Last Edit: May 17, 2012, 11:21:11 pm by BravoV »
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #211 on: May 18, 2012, 12:12:49 am »
I did a few late changes without testing properly. I had reduced R10 to boost the drive current and I forgot to reduce R9. Make R9 330 ohms. Also add a 470 ohm from the output of U2 to ground.

When I finish my modifications, I have to go back and tweak the circuit a bit so there will be changes. I am sure I will change the current control a bit.  I will have to get my hands on a batch of PZT2907A and 2N2907A's to work out what gain I can expect for the range of 0.5V to 2V Vce. There are no specs for gain at low voltages, but it looks like gain of the 2N2907A is still very good at 1V.  The properties at about 1V will determine how much current I need through Q9, and after getting that right, I then have to tweak the compensation again.

It is one thing getting a design. it is another thing getting it to work with all the possible component variations.  This is a very much part time project, but I will try and get the bulk of the final circuit worked out over the next few days.

Richard.
 

Offline sorin

  • Frequent Contributor
  • **
  • Posts: 279
  • Country: de
Re: General Purpose Power Supply Design
« Reply #212 on: May 26, 2012, 12:43:20 pm »
What method you have used to calculate the value of PI regulator (R13,C1 ; R14,C2)???
thank you!
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #213 on: May 27, 2012, 12:11:22 am »
What method you have used to calculate the value of PI regulator (R13,C1 ; R14,C2)???
thank you!
I used LT Spice to open up the control loop ( a bit like a network analyzer) so I could look at the loop gain and phase. I then tried to adjust for a maximum phase shift of under 80 degrees from 100Hz to the unity gain point with no capacitors on the output (or less then 170 degrees with a huge lossless capacitor on the output). Many supply designs depend on a zero on the output caused by the esr of the load capacitance, and I am trying to avoid that so it will be stable with any capacitance.

To open up the loop, I add a resistor (perhaps 1Mohm) between R20 and the output, feed a signal source onto R20, and look at the output.

I will do something similar on the bench when I get to build a prototype, but I have been struggling to get time to work on this for the last couple of months. The bench work will be important as there is just not enough data in the datasheets to be able to do an accurate paper design.

It sounds like a fairly simple project and perhaps for a one off build, it would be. It is surprising how much work it takes for a really solid design that works with component variations and my design has always been more of a design exercise rather then a commercial product. Power supplies really do operate on the margins of stability which makes them much more work then you might expect. With a fixed load, it is not hard to get stability. With any load, it is hard.

Richard.
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #214 on: May 27, 2012, 01:35:30 am »
Richard you might want to consider something like a NJT4031NT1G.
150mVcesat @1A
 
http://canada.newark.com/on-semiconductor/njt4031nt1g/bipolar-transistor-npn-40v-sot/dp/57M9869?ref=lookahead

An NPN pass device would simplify trying to compensate for every conceivable load, and esr  combination. The other advantage is no wasted base current.

I don’t think I’ve seen a GP Lab PSU with a PNP or PFET pass device, mind you I haven’t seen hundreds so who knows.

For those who don’t understand why Richard is having difficulty here is an application note from NATIONAL now TI.

www.national.com/an/AN/AN-1148.pdf
Thanks. That is a great transistor at a great price.

If you go back to the start, I did start with a NPN pass transistor, but it doesn't work as well as a PNP for low dropout mode (this power supply is always working with a low dropout). Of course the configuration can be changed to make it more conventional, but I really like many aspects of this low dropout configuration with the regulation that requires the output is the same voltage as the reference voltage.

What I found is that the PNP pass works great, as long as I can drive the base directly and do not use a darlington configuration. The moment I go to a darlington configuration, the compensation becomes far more critical.  PNP pass designs are widespread in IC regulators and they do perform well if you are driving the base with a current source. With the low dropout, I think the input noise rejection performance at higher frequencies was much better then the NPN circuit.

There are a small number of very high gain transistors, but I decided to really aim at common, extremely cheap parts. Why? I just decided to do it that way. Every time you replace a pass transistor, you have to restabilize the loop. 2907A transistors will always be available from many sources.

Will a njt4031nt1g transistor always be easily available? I doubt it.  Can I substitute it with transistors from other manufacturers without a redesign? Probably not.

The really big problem is that the manufacturers have lost interested in development on new discrete transistors which is a pity. New transistors are being developed all the time - in IC's. I cannot think of the number of times I wish I could get a discrete transistor that matches commonly used IC transistor. The super-beta transistors, the schottky base transistors, the transistors with a 30V base breakdown voltage, super matched transistor pairs, extremely low geometry transistors that work well down in the nA currents, multiple emitter transistors and multiple collector transistors. The standard CA3046 transistor array used in hundreds (thousands?) of analog scope designs has even been slated as obsolete and the supplies are disappearing. The manufactures could make something far superior to the ancient CA3046 today, but they don't bother.

The message is clear - mass production designs use IC's. The need for these devices as discreet designs have too low a volume to be worth much new development.

Richard.
« Last Edit: May 27, 2012, 01:45:07 am by amspire »
 

Offline BravoV

  • Super Contributor
  • ***
  • Posts: 7547
  • Country: 00
  • +++ ATH1
Re: General Purpose Power Supply Design
« Reply #215 on: May 27, 2012, 09:04:37 am »
I don’t think I’ve seen a GP Lab PSU with a PNP or PFET pass device, mind you I haven’t seen hundreds so who knows.
Agilent E361XA dc linear bench psu series uses mosfets like this below, partly captured schematic on the linear pass section :



The complete circuit is in here -> http://www.physics.fsu.edu/users/Wahl/labmanuals/instruments/ps/AgilentE361xAManual.pdf

I'm not qualified to comment though, but I'm guessing this must be good enough that made Agilent used this technique. Really eager to hear from experts on this linear pass configuration.

For those who don’t understand why Richard is having difficulty here is an application note from NATIONAL now TI.

www.national.com/an/AN/AN-1148.pdf
Thanks, good reading.

« Last Edit: May 27, 2012, 01:31:43 pm by BravoV »
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #216 on: May 27, 2012, 02:39:57 pm »
Bravo,

The  HP/Agilent supplies are more conventional with enough extra supply voltage to drive NPN darlington pass transistors (in 3610A and 3611A) and MOSFETS (in 3612A). Definitely not a low dropout design, and as usual with HP, the custom transformer with a separate winding for the regulator circuit.

I still think transistors are superior to mosfets in terms of the current spike problems of mosfets when the output voltage drops suddenly. If I had to use mosfets, I would use mosfets, but if I have a choice, I use transistors.

The interesting thing is the manual shows three different voltage/current supply combinations all based on the same circuit and perhaps the same PCB. If you look, there are extensive component changes between the models, including all the compensation components.

That is the traditional problems of all supplies - change anything and the changes tend to cascade through the whole design. That is why it has been so hard finding a general purpose design.

Richard.
 

Offline BravoV

  • Super Contributor
  • ***
  • Posts: 7547
  • Country: 00
  • +++ ATH1
Re: General Purpose Power Supply Design
« Reply #217 on: June 02, 2012, 05:54:05 am »
Richard, about finding the "right" compensation component's value, how hard is that ? Say I have a nice high power PNP with high beta even at high current instead of using multiple 2907s.

Will through trial & error with different compensation values possible ? Any guideline how to find them ? or its way too complicated for a hobbyist level ?
« Last Edit: June 02, 2012, 05:58:06 am by BravoV »
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11849
  • Country: us
Re: General Purpose Power Supply Design
« Reply #218 on: June 02, 2012, 06:15:54 am »
Richard, about finding the "right" compensation component's value, how hard is that ? Say I have a nice high power PNP with high beta even at high current instead of using multiple 2907s.

Will through trial & error with different compensation values possible ? Any guideline how to find them ? or its way too complicated for a hobbyist level ?

Broadly speaking I imagine this is difficult. This meaning of this magic "compensation" word is sometimes lost or not understood with overuse or use without definition. I'm in that boat. My understanding is that it means compensating for an unstable loop gain at certain frequencies.

However, stability is not the only goal. Achieving a loop that is stable under all circumstances is easy. They key thing is to make it stable while also responding fast enough to changes to keep things under control (supply changes or load changes, but especially load changes). If the circuit responds too slowly it ceases to be useful and so mere stability is not sufficient for success.

It follows that the design becomes something of a balancing act, sitting on a knife edge. Too far one way and it responds too slowly. Too far the other way and it is unstable. Given the nature of this balancing act trial and error is going to be difficult. You really need some analytical tools in your toolbox combined with some practical experiments to test the results.
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #219 on: June 02, 2012, 06:22:09 am »
Richard, about finding the "right" compensation component's value, how hard is that ? Say I have a nice high power PNP with high beta even at high current instead of using multiple 2907s.

Will through trial & error with different compensation values possible ? Any guideline how to find them ? or its way too complicated for a hobbyist level ?
The moment you change the transistors, you need to recompensate, and many of the PNP medium power transistors are slower and much harder to get a good broad compensation. I chose the 2907A as they really behaved very well. Trial and error s not always great, as it is really easy to get a conditional stability - it is stable until the right combination of load parameters, and then it is unstable.  Particularly this is an issue with loads that include low ESR electrolytics across the supply.
 

Offline fmaimon

  • Supporter
  • ****
  • Posts: 165
  • Country: br
Re: General Purpose Power Supply Design
« Reply #220 on: June 02, 2012, 10:23:06 pm »
Here is the C code with the assembler the compiler generated:

Code: [Select]
ISR(TIMER2_OVF_vect) {

  OCR2A = (byte) (pwm_accum / 0x01000000L ) ;  // 16 cycles
  pwm_accum &= 0x00FFFFFFL ; // 7 cycles
  pwm_accum +=  value ;  // 11 cycles
}

Is that all the code needed for the 24 bit PWM? You se the value you want in the main loop and thats all?
 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #221 on: June 02, 2012, 11:09:10 pm »
Here is the C code with the assembler the compiler generated:

Code: [Select]
ISR(TIMER2_OVF_vect) {

  OCR2A = (byte) (pwm_accum / 0x01000000L ) ;  // 16 cycles
  pwm_accum &= 0x00FFFFFFL ; // 7 cycles
  pwm_accum +=  value ;  // 11 cycles
}

Is that all the code needed for the 24 bit PWM? You se the value you want in the main loop and thats all?
Other then the setup code for the PWM and the interrupts, yes - that is all the code you need.

You have to make sure that no other interrupt routine is able to stop this interrupt from running and completing before the next PWM interrupt is due.

So either other interrupts have to be very short, or other interrupts have to re-enable the PWM interrupt in their first line of code so they will not block the pwm interrupt.

If the "value" is being changed regularly in the main loop, you may need some extra code to ensure that all bytes of "value" are written before the PWM interrupt is called. There are several ways to do this, but one way would be for the PWM to maintain a buffered copy of "value", and the PWM would look for a flag to indicate that a new "value" is available. This would mean an extra "if" statement is added to the interrupt routine.

If value is not changed regularly, you can probably live with any slight error in the output for one PWM cycle as value is changing. The effect will be hard to notice anyway and it is probably not worth worrying about.

Richard.
« Last Edit: June 02, 2012, 11:52:06 pm by amspire »
 

Offline m12lrpv

  • Regular Contributor
  • *
  • Posts: 175
  • Country: au
Re: General Purpose Power Supply Design
« Reply #222 on: June 03, 2012, 12:19:13 am »
Is that all the code needed for the 24 bit PWM? You se the value you want in the main loop and thats all?
Here's a full test sketch I put together for a 12 bit implementation of this (I didn't need 24 bit).

I've already thanked Richard for developing this. The results are nothing short of brilliant.

Code: [Select]
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <digitalWriteFast.h>

LiquidCrystal_I2C lcd(I2C_ADDR,16,2);

unsigned int VoltageValue = 0;
unsigned int pwm_Voltage_accum;

void setup()
{
  lcd.init();                      // initialize the lcd
 
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("Hello, world!");
 
  pinMode( 13, OUTPUT );
  digitalWrite( 13, LOW );

  pinMode(A0, INPUT);

  SetupTimer2();

  VoltageValue = 4000;  // pin3

  digitalWrite( 4, HIGH );

}

void loop()
{
  int i, j;
  String TempString;
  int sensorValue = analogRead(A0);   
  sensorValue = analogRead(A0);   
  sensorValue = sensorValue + analogRead(A0);   
  sensorValue = sensorValue /2;
  lcd.clear();

  lcd.setCursor(0, 0);
  TempString = "A0 = ";
  TempString += sensorValue;
  TempString += "  ";
  lcd.print(TempString);


  delay(2000);
}

void SetupTimer2() {
  // Disable interrupts while setting registers
  cli();

  TCCR2A = 0xA3;
  VoltageValue = 0L ;
  pwm_Voltage_accum = VoltageValue ;
  TCCR2B = 0x01;  //1 gives no prescale 
  TIMSK2 = 0x01;   // Enable interrupt when Timer reaches OCRA
  sei();

}
ISR(TIMER2_OVF_vect) {
 
  //OCR2B = (byte) (pwm_Voltage_accum / 0x010L ) ;
  OCR2B = (byte) (pwm_Voltage_accum >> 4 ) ;  //Reduce from 12 bit to 8 bit
  pwm_Voltage_accum &= 0x00F ;
  pwm_Voltage_accum +=  VoltageValue ;
  if( pwm_Voltage_accum > 0xFFF ) pwm_Voltage_accum = 0xFFF;
}

 

Offline amspireTopic starter

  • Super Contributor
  • ***
  • Posts: 3802
  • Country: au
Re: General Purpose Power Supply Design
« Reply #223 on: June 03, 2012, 01:11:17 am »
I've already thanked Richard for developing this. The results are nothing short of brilliant.

Don't forget to thank George (A Hellene) for all the research and work he did in developing this PWM code.

By the way, if you are using  this PWM code, you have to check for the use of delayMicroseconds() in other libraries. This is the No1 source of errors for this routine on the Arduino.

delayMicroseconds() disables interrupts for the period of the delay. The Arduino LiquidCrystal library includes some very long delays involving delayMicroseconds(), and in my test code, I made a new local copy of LiquidCrystal.cpp and  LiquidCrystal.h (that I renamed) and I changed all the delayMicroseconds() calls (excluding calls to delayMicroseconds(1) - I do not change them) to calls to this new delay function:

Code: [Select]
void delayMicroseconds2(long delaytime) {
  long j;
  for (j = 0L; j <= delaytime/2L; j++)delayMicroseconds(1);
}

Not as accurate, but it allows for other interrupts to be serviced during the delay. This was just a quick hack to fix the problem, but it is not the best solution.

There is a better discussion of the issue at:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1248706486

It is possible to drive a LCD with no lost PWM interrupts.

Richard.
« Last Edit: June 03, 2012, 01:19:49 am by amspire »
 

Offline bingo600

  • Super Contributor
  • ***
  • Posts: 1987
  • Country: dk
Re: General Purpose Power Supply Design
« Reply #224 on: June 03, 2012, 05:58:53 pm »
Regarding delays on the AVR
This is still the best (most accurate delay lib i have seen) for an AVR , if not using timers.

http://www.avrfreaks.net/index.php?module=Freaks%20Academy&func=viewItem&item_id=665&item_type=project

/Bingo

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf