Strange PWMOUT result

eddydde

Member
I am using the following lines of code as part of a loop to produce a burst of 3725Hz square wave.

pwmout pwmdiv4,3,66,134
pause 200
pwmout 3 , off

If I look at this burst of tone on a CRO, there is 6 'cycles' of approx 15kHz square wave before it settles down to produce the 3725Hz square wave!

Very strange. Could this possibly be a faulty 18X device or am I missing something?
 
Last edited:

Technical

Technical Support
Staff member
This is correct. The 'pwmdiv4' is a 'retrofit' option onto the 18X instruction set, which is actually parsed by the compiler as two commands

pwmout 3,66,134
poke to div4

This was the only way to add the divider options to older firmware devices. So you will have a very short interval of time at the wrong pulses between the processing of the two commands.
 

westaust55

Moderator
Totally untested by me, but would it be possible to pre-force the divide by 4 then set the rest of the PWMOUT parameters?

so the code:
Code:
pwmout pwmdiv4,3,66,134
pause 200
pwmout 3 , off
could then possibly become:
Code:
peek $12, b0       ; b0 or some other free variable
b0 = b0 OR $01   ; make the prescaller a "divide by 4" function
poke $12,b0         ; now tentatively the prescaller is set in advance
pwmout pwmdiv4, 3, 66, 134
whether it works would all depend on what else the PICAXE interpreter is doing behind the scenes
if the core/original PWMOUT command clears bits 0 and 1 of the prescaler then no improvement will occur.
 

Dippy

Moderator
How important is this characteristic?

If it is really significant maybe you can do the complete PWM setup by writing to PIC registers? Then you can set things in the order required so that no glitches occur.
i.e. roll your own command.
I've never tried this in PICAXE BASIC so don't know if it's possible.
Unfortunately, it will require you (or Westy) to have a read of a PIC Data Sheet.
 

eddydde

Member
Thanks Dippy, and Westy. As a newcomer to PICAXE your suggestion is way beyond my level of competency at this stage. The tone burst is just a small part of a larger program/project and until it develops more, I don't know if it is going to be a major problem or not, therefore probably not worth a lot of time or effort by anyone sorting out a complicated software work around at this stage.

Another option is via hardware and gate the 'good' part of the PWMOUT with an AND gate.
 

hippy

Ex-Staff (retired)
Pre-poking a PWMDIV4 won't work as the PWMOUT will reset that, but 'roll your own PWM' poking should work. The AND gate will also work, and a simple diode mix 'OR' if 'idle high' is acceptable.

Untested, but on those which support HPWM, it should be possible to configure HPWM, let the pulse frequency stabilise on one pin then a simple poke should allow that to be routed to a different HPWM pin.
 
Top