PWM enhancements in 5.07.06 (undocumented)


Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11635
Posted: 08:43am 09 Jan 2023      

The 5.07.06 release notes and manual document the ability to optionally invert the PWM channels but that was actually part of a much more powerful change to PWM that is included in 5.07.06 (get the very latest download from Geoff's site). This change is difficult to document and late breaking hence the omission in the current manual.

The PWM command is modified as follows:

PWM channel, frequency, [dutyA] [,dutyB] [,deferredstart]

The new parameter "deferredstart" when set to 1 prepares the PWM channels as before but does not start the output running. This is to avoid any undesirable startup artefacts

There is a new sub command

PWM SYNC [channel0offset] [,channel1offset] .......[channel7offset]

This initiates the PWM on channels where a deferred start was defined or just syncs exisitng running channels. However, the power comes in the ability to offset the channels one to another (defined as a percentage of the time period as per the duty cycle - can be a float)

In this thread  a method is described to create two signals with one starting after the other and ending before it.

With the new functionality this is easy without pokes or changing frequency.


setpin GP0,PWM
setpin GP2,PWM
PWM 0,10000,50,,1
PWM 1,10000,40,,1
PWM SYNC 0,5





The same method can create a quadrature clock


setpin GP0,PWM
setpin GP2,PWM
setpin GP4,PWM
setpin GP6,PWM
PWM 0,10000,25,,1
PWM 1,10000,25,,1
PWM 2,10000,25,,1
PWM 3,10000,25,,1
PWM SYNC 0,25,50,75


Unfortunately I don't have a 4 channel scope




The SYNC command can also be used when the channels are already running

e.g.


setpin GP0,PWM
setpin GP2,PWM
PWM 0,10000,50
PWM 1,10000,50
do
 for i=0 to 99
   PWM SYNC 0,i
   pause 50
 next
loop

Edited 2023-01-09 18:46 by matherp

Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5994
Posted: 08:56am 09 Jan 2023      

Hi Peter,

This works (only) on different PWM's. So you could potentially also use it to generate fading in game sounds. One PWM at 1kHz, the other at 998Hz. And use sync to start them simultaneous. And PWM STOP to stop them.

Martin may like this, he has some game sounds that take a lot of CPU power, much simpler this way.

Nice feature...

Volhout

P.S. In my particular switchmode power supply test, I used 100kHz up to 400kHz frequencies, and had very small time offsets. That would require fractions of a microsecond start delay. I guess in that application the "poke" still is needed.

matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11635
Posted: 10:03am 09 Jan 2023      

  Quote  P.S. In my particular switchmode power supply test, I used 100kHz up to 400kHz frequencies, and had very small time offsets. That would require fractions of a microsecond start delay. I guess in that application the "poke" still is needed.


In the margin you are limited by the number of clock pulses in the time period
@400KHz with 1CPU @ 378MHz you have 945 clock periods per cycle so half of that is 472 and therefore a "shorter" pulse must be 470. Either method hits exactly the same limitation.


setpin gp0,pwm
setpin gp2,pwm
pwm 0,400000,50,,1
pwm 1,400000,49.8,,1
pwm sync 0,0.1
pw


gives you a symmetrical pair of pulses with exactly 1 count offset (2.6ns) at each end when the CPU clock is 378MHz.

disco4now

Guru

Joined: 18/12/2014
Location: Australia
Posts: 1133
Posted: 11:04am 09 Jan 2023      

Hi Peter,
Can the duty cycle be changed while the PWMs are running and keep the same phase relationship.i.e is this valid

setpin GP0,PWM
setpin GP2,PWM
setpin GP4,PWM
setpin GP6,PWM
PWM 0,10000,25,,1
PWM 1,10000,25,,1
PWM 2,10000,25,,1
PWM 3,10000,25,,1
PWM SYNC 0,25,50,75

for x=25 to 50 step 5
PWM 0,10000,x
PWM 1,10000,x
PWM 2,10000,x
PWM 3,10000,x
pause 100
next x


matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 11635
Posted: 11:28am 09 Jan 2023      

yes

Forgot to mention you can use an offset of -1 to omit a channel from the synch

e.g.



setpin gp0,pwm
setpin gp1,pwm
pwm 0,5000,33,66
setpin gp2,pwm
setpin gp4,pwm
pwm 1,10000,25,,1
pwm 2,10000,25
pwm sync -1,0,25 ' syncs channels 1 and 2 but leaves 0 untouched

Edited 2023-01-10 01:47 by matherp

TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6546
Posted: 07:15pm 09 Jan 2023      

It would be nice if the same thing could be done for PLAY TONE...

Jim

karlelch

Guru

Joined: 30/10/2014
Location: Germany
Posts: 335
Posted: 12:37pm 10 Jan 2023      

This option to offset PWM channel starts may also be useful when starting multiple servos at once - that is, to spread to initial current draw over some small time window.

Thomas