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.