# Some progress, and a refresh of a lesson-learned

A project log for Low-Cost Programmable Power Supply

Chinese "LM2596 DC/DC buck converter with voltmeter" + "some wires" + "Forth" = "programmable power supply"

Thomas 02/05/2017 at 12:406 Comments

In the last log I listed some ways for controlling the output voltage of the LM2596 with a limited modification of the CN2596-2 DC/DC module, which I found challenging. The challenge was accepted and won by @K.C. Lee: it's easiest to add an offset to the ground of the feedback voltage divider!

The idea is to create an offset voltage between 0 and 1.2V and thus change the behavior of the feedback. This way the LM2596 "native" feedback loop retains at least some of the closed loop control, and an overlay PI control loop can do the rest.

The offset voltage can be produced with a PWM signal and a simple RC, but it took me some time to figure out how to configure PC3 (TIM1_CC3) as a PWM output (hint: it's a bit more complicated than configuring TIM2 for PWM, and there is a red herring in the manual).

With the help of a small Forth program the concept can be tested:

```file
: iTim1 ( n -- )
tim1_arrh 2c!
1 tim1_ccer2 c!
\$80 tim1_bkr c!
\$60 tim1_ccmr3 c!
1 tim1_cr1 c! ;
: pwm ( n -- )
tim1_ccr3h 2c! ;
: vOut ( -- )
4 adc! adc@ 100 184 */ . ;
' vOut bg !
1000 iTim1
300 pwm
hand
```

iTim1 initializes TIMER1 with the reload value "n", and configures TIM1_CC3 as a PWM output.

pwm sets the compare value to n.

vOut reads Ain4, scales it by a factor of 1/1.84 using */ for 32/16 bit arithmetics, and displays the output voltage in units of 100mV.

The value is shown on the LED display because it's set as the background task with ' and bg !. The timer is then initialized to 16kHz, and the PWM duty cycle is set to 30%.

Please note that the whole program is just 13 lines, 51 words, or 215 characters long. It compiles to 93 bytes of code, and it doesn't require any variables since all intermediate values are passed through the Data Stack of the Forth VM.

I rectified the PWM signal from PC3 with a simple RC filter, and connected it to the voltage divider. It worked nicely, until I decided to enable the LM2596. When I then saw a negative voltage of about -13V at the capacitor I immediately knew that I should have spent more time drawing the schematics of the board, and less hacking the software. PC3 seems to be retired now.

Until I find a way to replace the µC on the board (or the new boards arrive), I'll be using a STM8S003F3P6 breakout board, with a pair of 1N4148 diodes for protection, which I should have done from the start.

A lesson-learned re-learned :-)

EDIT: a second examination of the board shows a normal PWM output. My breadboard wiring must have fooled me. I corrected my error, and also fitted two 1N4148 to my experimenting DC/DC board.

## Discussions

K.C. Lee wrote 02/05/2017 at 13:49 point

FYI on RC filtering: https://hackaday.io/project/4993-dual-channel-battery-chargeranalyzer/log/16159-pwm-part-1
tl;dr higher order RC filters have lower ripples.
BTW my integrator would work better (than with the LM317) as the initial high voltage actually force the output to 0V.  To increase/decrease voltages, I simply use a CPU delay loop (less than 10us) to set a GPIO to 0 or 1 with the PID value once every update at 30 times a second.

Are you sure? yes | no

Thomas wrote 02/05/2017 at 16:32 point

Thanks for the link!

I'd like to keep the PWM frequency high, and the RC filter impedance low compared to the feedback voltage divider. It would be easier with a buffer, but then I could as well use your integrator solution.

Unfortunately, using an op-amp (e.g. for the integrator) would require a lot more wiring, or an additional small PCB (which would defy the purpose).

Are you sure? yes | no

K.C. Lee wrote 02/05/2017 at 16:37 point

A transistor emitter follower as a buffer, perhaps?  Or use the DC resistance of the RC filter (i.e. sum of the resistors) to replace the lower branch of the voltage divider and not worry about low impedance?

In theory, you could use a lower resolution PWM as trade-off for a higher frequency and let the PID loop dither/modulate the DC offset.

In my charger, I use the PWM to control a discrete analog PWM so I can keep the both the resolution and switching frequency high.

Are you sure? yes | no

Thomas wrote 02/05/2017 at 16:44 point

Yes, that might work. There is still the problem of stripping some of the solder stop mask, and cut some of the ground fill into pads with a Dremel tool. Let's experiment a bit.

Synchronizing Timer1 with the LM2596 oscillator would be an alternative approach, but now with the filter in place the DC/DC ripple would be too weak to get sync from it.

Are you sure? yes | no

K.C. Lee wrote 02/05/2017 at 16:50 point

Kapton tape works really well to cover up existing track to make place for barnicles.

Are you sure? yes | no

Thomas wrote 02/05/2017 at 16:52 point

I had something like the pads in the following link in mind: http://www.janson-soft.de/seminare/dh7uaf/ugly/ugly.htm

Are you sure? yes | no