Forums

MicroController control for switching regulator

Started by RogerN December 25, 2012
Another possibility I thought of was to replace the pot in the adjustable 
regulator circuit with a digital pot.



mike <ham789@netzero.net> wrote:

>On 12/25/2012 9:35 AM, John Larkin wrote: >> On Tue, 25 Dec 2012 21:55:40 +0800, Rheilly Phoull >> <rheilly@bigslong.com> wrote: >> >>> On 25/12/2012 8:05 PM, RogerN wrote: >>>> I'm wanting to use a microcontroller to control the output of a switching >>>> regulator, maybe something like a LM2576 simple switcher. I see the >>>> adjustability is on the feedback lead but what's the best way make it >>>> adjustable from a microcontroller. I'm guessing the feedback may be used in >>>> switching timing, ie, I guess I can't just use a DAC and get an output of >>>> 1.23V more than the DAC output? I want to monitor voltage and current and >>>> use a control algorithm in the microcontroller (Arduino at first for >>>> experimentation) for custom battery charging experimentation. >>>> >>>> RogerN >>>> >>>> >>> The ADC in the Arduino is not too flash, as in not being stable. You >>> will see the value "jitter" up and down. Previous advice about using >>> purpose designed devices would seem a better idea to me :-) >> >> Software lowpass filter the ADC samples. You'd want to do that even if >> the ADC was perfect. Any signal near a power switcher is going to be >> noisy. >> >> The most elegant way to do this job is to use the uP PWM to drive the >> switcher stage, and use the ADC to measure input voltage, output >> voltage, and current. > >Got any tips on how to do this? >At 20 kHz. you have 50microseconds to do two measurements, update >the displays, and decide whether and how to tweak the PWM. > >Even if you dedicate the whole processor to the task, you still >gotta get data in and out. A 50 millisecond key debounce timer >is an eternity when the output is shorted. >So you make everything interrupt driven...and it gets messy.
Its actually pretty simple with an ARM. Even at 300kHz you can measure the voltage across a MOSFET for cycle by cycle current limiting. Its one interrupt running the PWM controller. Updating displays etc is done in the main loop. You'll actually have 2 processes with different priorities running in parallel. -- Failure does not prove something is impossible, failure simply indicates you are not using the right tools... nico@nctdevpuntnl (punt=.) --------------------------------------------------------------
On Tuesday, December 25, 2012 7:13:12 PM UTC+1, mike wrote:
> On 12/25/2012 9:35 AM, John Larkin wrote: >=20 > > On Tue, 25 Dec 2012 21:55:40 +0800, Rheilly Phoull >=20 > > <rheilly@bigslong.com> wrote: >=20 > > >=20 > >> On 25/12/2012 8:05 PM, RogerN wrote: >=20 > >>> I'm wanting to use a microcontroller to control the output of a switc=
hing
>=20 > >>> regulator, maybe something like a LM2576 simple switcher. I see the >=20 > >>> adjustability is on the feedback lead but what's the best way make it >=20 > >>> adjustable from a microcontroller. I'm guessing the feedback may be =
used in
>=20 > >>> switching timing, ie, I guess I can't just use a DAC and get an outpu=
t of
>=20 > >>> 1.23V more than the DAC output? I want to monitor voltage and curren=
t and
>=20 > >>> use a control algorithm in the microcontroller (Arduino at first for >=20 > >>> experimentation) for custom battery charging experimentation. >=20 > >>> >=20 > >>> RogerN >=20 > >>> >=20 > >>> >=20 > >> The ADC in the Arduino is not too flash, as in not being stable. You >=20 > >> will see the value "jitter" up and down. Previous advice about using >=20 > >> purpose designed devices would seem a better idea to me :-) >=20 > > >=20 > > Software lowpass filter the ADC samples. You'd want to do that even if >=20 > > the ADC was perfect. Any signal near a power switcher is going to be >=20 > > noisy. >=20 > > >=20 > > The most elegant way to do this job is to use the uP PWM to drive the >=20 > > switcher stage, and use the ADC to measure input voltage, output >=20 > > voltage, and current. >=20 >=20 >=20 > Got any tips on how to do this? >=20 > At 20 kHz. you have 50microseconds to do two measurements, update >=20 > the displays, and decide whether and how to tweak the PWM. >=20 >=20 >=20 > Even if you dedicate the whole processor to the task, you still >=20 > gotta get data in and out. A 50 millisecond key debounce timer >=20 > is an eternity when the output is shorted. >=20 > So you make everything interrupt driven...and it gets messy. >=20 >=20 >=20 > For a 20kHz. PWM frequency, can you really get enough resolution? >=20 > Or do you dither the clock and the duty factor or???? >=20
I did it with a forward converter. The loop was slowed down by adding more capacity to the output, loop regula= tion was done by TL431 and opto to the primary side (error signal). The Err= or signal was sampled in a 10kHz interrupt, converter to duty cycle. I only= had 4 bits of different duty cycles (16), but the loop the automatically d= ithers around the FB voltage. I even tried for fun to go down to 4 different duty cycles, and due to the = capacitance on the output it was still stable with a lot of dithering Converter switching of 200kHz and 4MHz ARM clock. (only loading the process= or about 15%) Regards Klaus
On 12/25/2012 1:23 PM, John Larkin wrote:
> On Tue, 25 Dec 2012 10:13:12 -0800, mike<ham789@netzero.net> wrote: > >> On 12/25/2012 9:35 AM, John Larkin wrote: >>> On Tue, 25 Dec 2012 21:55:40 +0800, Rheilly Phoull >>> <rheilly@bigslong.com> wrote: >>> >>>> On 25/12/2012 8:05 PM, RogerN wrote: >>>>> I'm wanting to use a microcontroller to control the output of a switching >>>>> regulator, maybe something like a LM2576 simple switcher. I see the >>>>> adjustability is on the feedback lead but what's the best way make it >>>>> adjustable from a microcontroller. I'm guessing the feedback may be used in >>>>> switching timing, ie, I guess I can't just use a DAC and get an output of >>>>> 1.23V more than the DAC output? I want to monitor voltage and current and >>>>> use a control algorithm in the microcontroller (Arduino at first for >>>>> experimentation) for custom battery charging experimentation. >>>>> >>>>> RogerN >>>>> >>>>> >>>> The ADC in the Arduino is not too flash, as in not being stable. You >>>> will see the value "jitter" up and down. Previous advice about using >>>> purpose designed devices would seem a better idea to me :-) >>> >>> Software lowpass filter the ADC samples. You'd want to do that even if >>> the ADC was perfect. Any signal near a power switcher is going to be >>> noisy. >>> >>> The most elegant way to do this job is to use the uP PWM to drive the >>> switcher stage, and use the ADC to measure input voltage, output >>> voltage, and current. >> >> Got any tips on how to do this? > > The easy way is exponential smoothing, the equivalent of a 1st order > RC lowpass filter. > > Filt_Val = Filt_Val + (ADC_Val - Filt_Val) / K > > where, if you work in intgers, you can do the divide-by-K as a right > shift. > > Filt_Val = Filt_Val + ( (ADC_Val - Filt_Val)>> N ) > > It's all signed. N might be, say, 16 or 256 or something. > > The data has to be aligned such that you don't roll bits off the end > when you do the right shift. Using a 32-bit Filt_Val simplifies life; > the ADC sample should be up-shifted maybe 16 bits before the math is > done. > > If you do this in floats, it's simpler but slower; don't worry about > alignment at all. > > Filt_Val = Filt_Val + J * (ADC_Val - Filt_Val) > > since multiplies are faster than divs. J = 0.01 would give the filter > a time constant 100x the sample rate. > > >> At 20 kHz. you have 50microseconds to do two measurements, update >> the displays, and decide whether and how to tweak the PWM. > > If you do all the math as ints, that should be possible. We've done > more complex stuff, filtering and PID control and error checks at 100 > KHz on a cheap ARM. > > >> >> Even if you dedicate the whole processor to the task, you still >> gotta get data in and out. A 50 millisecond key debounce timer >> is an eternity when the output is shorted. >> So you make everything interrupt driven...and it gets messy. >> >> For a 20kHz. PWM frequency, can you really get enough resolution? >> Or do you dither the clock and the duty factor or???? > > If you don't have a real DAC, run the PWM as fast as it will go. > That's not the same rate at which you might run the filtering and > control algorithm. > > One trick is to filter the ADC data twice; a fast filter for the > control path and a slow one for display cosmetics. If you filter too > much in the control path, the loop can go unstable. A P+I controller > with low P may not need filtering at all. > >
Thanks for the input. You're using a faster machine. My PIC takes over 19 microseconds per A/D acquisition under optimal conditions...according to the datasheet.
On 12/25/2012 7:25 PM, mike wrote:
> On 12/25/2012 1:23 PM, John Larkin wrote: >> On Tue, 25 Dec 2012 10:13:12 -0800, mike<ham789@netzero.net> wrote: >> >>> On 12/25/2012 9:35 AM, John Larkin wrote: >>>> On Tue, 25 Dec 2012 21:55:40 +0800, Rheilly Phoull >>>> <rheilly@bigslong.com> wrote: >>>> >>>>> On 25/12/2012 8:05 PM, RogerN wrote: >>>>>> I'm wanting to use a microcontroller to control the output of a >>>>>> switching >>>>>> regulator, maybe something like a LM2576 simple switcher. I see the >>>>>> adjustability is on the feedback lead but what's the best way make it >>>>>> adjustable from a microcontroller. I'm guessing the feedback may >>>>>> be used in >>>>>> switching timing, ie, I guess I can't just use a DAC and get an >>>>>> output of >>>>>> 1.23V more than the DAC output? I want to monitor voltage and >>>>>> current and >>>>>> use a control algorithm in the microcontroller (Arduino at first for >>>>>> experimentation) for custom battery charging experimentation. >>>>>> >>>>>> RogerN >>>>>> >>>>>> >>>>> The ADC in the Arduino is not too flash, as in not being stable. You >>>>> will see the value "jitter" up and down. Previous advice about using >>>>> purpose designed devices would seem a better idea to me :-) >>>> >>>> Software lowpass filter the ADC samples. You'd want to do that even if >>>> the ADC was perfect. Any signal near a power switcher is going to be >>>> noisy. >>>> >>>> The most elegant way to do this job is to use the uP PWM to drive the >>>> switcher stage, and use the ADC to measure input voltage, output >>>> voltage, and current. >>> >>> Got any tips on how to do this? >> >> The easy way is exponential smoothing, the equivalent of a 1st order >> RC lowpass filter. >> >> Filt_Val = Filt_Val + (ADC_Val - Filt_Val) / K >> >> where, if you work in intgers, you can do the divide-by-K as a right >> shift. >> >> Filt_Val = Filt_Val + ( (ADC_Val - Filt_Val)>> N ) >> >> It's all signed. N might be, say, 16 or 256 or something. >> >> The data has to be aligned such that you don't roll bits off the end >> when you do the right shift. Using a 32-bit Filt_Val simplifies life; >> the ADC sample should be up-shifted maybe 16 bits before the math is >> done. >> >> If you do this in floats, it's simpler but slower; don't worry about >> alignment at all. >> >> Filt_Val = Filt_Val + J * (ADC_Val - Filt_Val) >> >> since multiplies are faster than divs. J = 0.01 would give the filter >> a time constant 100x the sample rate. >> >> >>> At 20 kHz. you have 50microseconds to do two measurements, update >>> the displays, and decide whether and how to tweak the PWM. >> >> If you do all the math as ints, that should be possible. We've done >> more complex stuff, filtering and PID control and error checks at 100 >> KHz on a cheap ARM. >> >> >>> >>> Even if you dedicate the whole processor to the task, you still >>> gotta get data in and out. A 50 millisecond key debounce timer >>> is an eternity when the output is shorted. >>> So you make everything interrupt driven...and it gets messy. >>> >>> For a 20kHz. PWM frequency, can you really get enough resolution? >>> Or do you dither the clock and the duty factor or???? >> >> If you don't have a real DAC, run the PWM as fast as it will go. >> That's not the same rate at which you might run the filtering and >> control algorithm. >> >> One trick is to filter the ADC data twice; a fast filter for the >> control path and a slow one for display cosmetics. If you filter too >> much in the control path, the loop can go unstable. A P+I controller >> with low P may not need filtering at all. >> >> > Thanks for the input. > You're using a faster machine. > My PIC takes over 19 microseconds per A/D acquisition under > optimal conditions...according to the datasheet.
And what do you do with those readings ? How much time does it take to do one calculation in a PID loop ? How much time to it take to decide to change the PWM ? How fast does it take to update the charge on a capacitor ? And about 20 other parameters need to be looked at. hamilton
On Tue, 25 Dec 2012 18:25:47 -0800, mike <ham789@netzero.net> wrote:

>On 12/25/2012 1:23 PM, John Larkin wrote: >> On Tue, 25 Dec 2012 10:13:12 -0800, mike<ham789@netzero.net> wrote: >> >>> On 12/25/2012 9:35 AM, John Larkin wrote: >>>> On Tue, 25 Dec 2012 21:55:40 +0800, Rheilly Phoull >>>> <rheilly@bigslong.com> wrote: >>>> >>>>> On 25/12/2012 8:05 PM, RogerN wrote: >>>>>> I'm wanting to use a microcontroller to control the output of a switching >>>>>> regulator, maybe something like a LM2576 simple switcher. I see the >>>>>> adjustability is on the feedback lead but what's the best way make it >>>>>> adjustable from a microcontroller. I'm guessing the feedback may be used in >>>>>> switching timing, ie, I guess I can't just use a DAC and get an output of >>>>>> 1.23V more than the DAC output? I want to monitor voltage and current and >>>>>> use a control algorithm in the microcontroller (Arduino at first for >>>>>> experimentation) for custom battery charging experimentation. >>>>>> >>>>>> RogerN >>>>>> >>>>>> >>>>> The ADC in the Arduino is not too flash, as in not being stable. You >>>>> will see the value "jitter" up and down. Previous advice about using >>>>> purpose designed devices would seem a better idea to me :-) >>>> >>>> Software lowpass filter the ADC samples. You'd want to do that even if >>>> the ADC was perfect. Any signal near a power switcher is going to be >>>> noisy. >>>> >>>> The most elegant way to do this job is to use the uP PWM to drive the >>>> switcher stage, and use the ADC to measure input voltage, output >>>> voltage, and current. >>> >>> Got any tips on how to do this? >> >> The easy way is exponential smoothing, the equivalent of a 1st order >> RC lowpass filter. >> >> Filt_Val = Filt_Val + (ADC_Val - Filt_Val) / K >> >> where, if you work in intgers, you can do the divide-by-K as a right >> shift. >> >> Filt_Val = Filt_Val + ( (ADC_Val - Filt_Val)>> N ) >> >> It's all signed. N might be, say, 16 or 256 or something. >> >> The data has to be aligned such that you don't roll bits off the end >> when you do the right shift. Using a 32-bit Filt_Val simplifies life; >> the ADC sample should be up-shifted maybe 16 bits before the math is >> done. >> >> If you do this in floats, it's simpler but slower; don't worry about >> alignment at all. >> >> Filt_Val = Filt_Val + J * (ADC_Val - Filt_Val) >> >> since multiplies are faster than divs. J = 0.01 would give the filter >> a time constant 100x the sample rate. >> >> >>> At 20 kHz. you have 50microseconds to do two measurements, update >>> the displays, and decide whether and how to tweak the PWM. >> >> If you do all the math as ints, that should be possible. We've done >> more complex stuff, filtering and PID control and error checks at 100 >> KHz on a cheap ARM. >> >> >>> >>> Even if you dedicate the whole processor to the task, you still >>> gotta get data in and out. A 50 millisecond key debounce timer >>> is an eternity when the output is shorted. >>> So you make everything interrupt driven...and it gets messy. >>> >>> For a 20kHz. PWM frequency, can you really get enough resolution? >>> Or do you dither the clock and the duty factor or???? >> >> If you don't have a real DAC, run the PWM as fast as it will go. >> That's not the same rate at which you might run the filtering and >> control algorithm. >> >> One trick is to filter the ADC data twice; a fast filter for the >> control path and a slow one for display cosmetics. If you filter too >> much in the control path, the loop can go unstable. A P+I controller >> with low P may not need filtering at all. >> >> >Thanks for the input. >You're using a faster machine.
Yup. The NXP ARMs are ferocious. We geared the CPU clock rate down to 50 MHz in the case I mentioned, so we computed for about 7 usec worst-case, with an ADC interrupt every 10. It's this one: http://www.highlandtechnology.com/DSS/V220DS.shtml There are 13 ARM processors on this board. 12 of them are galvanically isolated, one per 4-20 mA channel.
>My PIC takes over 19 microseconds per A/D acquisition under >optimal conditions...according to the datasheet.
If you have to trigger the ADC and wait for data, rearrange the loop to do the math while it's digitizing the next sample. Don't waste time waiting for the ADC.
On 2012-12-25, mike <ham789@netzero.net> wrote:

> Got any tips on how to do this? > At 20 kHz. you have 50microseconds to do two measurements, update > the displays, and decide whether and how to tweak the PWM.
you don't need to update the displays at 20Khz 2Hz would be plenty for update 40hz would be a sufficient refresh rate if you need to do that.
> Even if you dedicate the whole processor to the task, you still > gotta get data in and out. A 50 millisecond key debounce timer > is an eternity when the output is shorted.
> So you make everything interrupt driven...and it gets messy.
no, everything gets tidy when it's interrupt driven, the critical code is no longer at the mercy of the main loop, you can use float arithmetic and printf, and other slow code in the main loop without breaking anything. OTOH propeller chips wulti-core and thus ideal for people who don't like interrupts,
>> Or, the OP could use a DAC (or a PWM made into a DAC) to push the >> feedback node of the Simple Switcher.
yeah, that's the easy way. -- &#9858;&#9859; 100% natural --- news://freenews.netfront.net/ - complaints: news@netfront.net ---
On a sunny day (Tue, 25 Dec 2012 17:22:47 -0600) it happened "RogerN"
<regor@midwest.net> wrote in <PYmdnddIRJHLqEfNnZ2dnUVZ_qydnZ2d@earthlink.com>:

> >Thanks for the replies. > >So around 30 years ago in school we made an oscillator using an Op Amp, >IIRC, this was a comparator circuit with hysteresis and the output caused a >capacitor to charge/discharge. What if the same type of circuit was used in >a buck regulator so when the voltage reached a certain level, the output >would go low, when the output dropped below a lower level, the output would >go back high? Then an analog output could determine the comparator voltage. >That may not be perfect SMPS design but I would think it would be good >enough to charge batteries, probably need to switch off to take measurements >or at least filter the measurement. > >RogerN
I have been using the LM317 as switcher, pretty much does what you mentioned: http://www.circuitstoday.com/3a-switching-regulator The on/off current in the now oscillating LM317 switches a power transistor. It is still regulating ...
On 12/26/2012 1:46 AM, Jasen Betts wrote:
> On 2012-12-25, mike<ham789@netzero.net> wrote: > >> Got any tips on how to do this? >> At 20 kHz. you have 50microseconds to do two measurements, update >> the displays, and decide whether and how to tweak the PWM. > > you don't need to update the displays at 20Khz > 2Hz would be plenty for update 40hz would be a sufficient refresh rate > if you need to do that. > >> Even if you dedicate the whole processor to the task, you still >> gotta get data in and out. A 50 millisecond key debounce timer >> is an eternity when the output is shorted. > >> So you make everything interrupt driven...and it gets messy. > > no, everything gets tidy when it's interrupt driven, the critical code > is no longer at the mercy of the main loop, you can use float > arithmetic and printf, and other slow code in the main loop without > breaking anything.
I'm not saying that it can't be done. I'm saying that my PIC with 200ns instruction cycle and one level of interrupt and inaccessible stack and 20us A/D conversion time, might have some trouble shifting serial data out to a display while managing keyboard debounce, measuring two voltages, implementing a software filter or two and deciding whether the output got shorted and what to do about it all within 50 us. Overlapped functions work until you need overlapped interrupts and don't have 'em. So what if you miss a cycle...no big deal...except that your inductor has to handle more current without saturating and your transistors may have to be bigger and...and...and If you're testing how creative you can be at using the wrong tool for the job, this is as good as any. Throwing a multi-core processor at a general purpose adjustable power supply regulator may not be the optimum solution. Fixed voltage multiple supplies with simple short protection, maybe. I was too lazy to decipher the code, but I did study the hardware for Jan's solution. I convinced myself that it could be made to degrade gracefully even if you overloaded the processor with other functions. I was gonna build one for fun until I realized that I have programmable power supplies in the attic that I don't need.
> > OTOH propeller chips wulti-core and thus ideal for people who don't > like interrupts, > >>> Or, the OP could use a DAC (or a PWM made into a DAC) to push the >>> feedback node of the Simple Switcher. > > yeah, that's the easy way. >
On a sunny day (Wed, 26 Dec 2012 04:38:03 -0800) it happened mike
<ham789@netzero.net> wrote in <kber4n$vek$1@dont-email.me>:

>On 12/26/2012 1:46 AM, Jasen Betts wrote: >> On 2012-12-25, mike<ham789@netzero.net> wrote: >> >>> Got any tips on how to do this? >>> At 20 kHz. you have 50microseconds to do two measurements, update >>> the displays, and decide whether and how to tweak the PWM. >> >> you don't need to update the displays at 20Khz >> 2Hz would be plenty for update 40hz would be a sufficient refresh rate >> if you need to do that. >> >>> Even if you dedicate the whole processor to the task, you still >>> gotta get data in and out. A 50 millisecond key debounce timer >>> is an eternity when the output is shorted. >> >>> So you make everything interrupt driven...and it gets messy. >> >> no, everything gets tidy when it's interrupt driven, the critical code >> is no longer at the mercy of the main loop, you can use float >> arithmetic and printf, and other slow code in the main loop without >> breaking anything. > >I'm not saying that it can't be done. >I'm saying that my PIC with 200ns instruction cycle and one level >of interrupt and inaccessible stack and 20us A/D conversion time, >might have some trouble shifting serial data out to a display >while managing keyboard debounce, measuring two voltages, >implementing a software filter or two and deciding whether the output got >shorted and what to do about it all within 50 us.
LOL http://panteltje.com/panteltje/pic/pwr_pic/
>Overlapped functions work until you need overlapped interrupts >and don't have 'em. >So what if you miss a cycle...no big deal...except that your inductor >has to handle more current without saturating and your transistors >may have to be bigger and...and...and
Mant PICs have hardware comparators that also control the PWM by hardware. Thats is what I use here, among other things such as interrupts. This code also has a real time clock, but I left it out in the LCD display for clarity, and space IIRC.