PWM and the poke command

ccdubs

Member
Hi all,

I have the code below which configures 3 pwm pins to seperately output 1200, 1000, 830 Hz pulses. The only way I could get this to work was to use the Poke command. These PWM outputs are pulsed on and off and it appears I need to send the Poke command everytime I pulse? Is this correct, I would have thought that once the Poke was set, it would stay that way?

Code:
START:
	pwmout PWM1200_Tone, 103, 208 '1200/2 Hz
	pwmout PWM830_Tone, 150, 301 '830/2 Hz
	pwmout pwmdiv16, PWM1000_Tone, 124, 250 '1000/2 Hz
	
	'Poke the devisor registers
	'bit0=0 => 1/1, bit0=1 => 1/4, bit1=1 => 1/16 
	'Set divisor for B.0
	peeksfr $4A,b0
	bit0=0
	bit1=1
	pokesfr $4A,b0
	
	'Set divisor for B.5
	peeksfr $51,b0
	bit0=0	
	bit1=1
	pokesfr $51,b0
	
	pause 1000

	pwmout PWM830_Tone, OFF
	pwmout PWM1000_Tone, OFF
	pwmout PWM1200_Tone, OFF

	goto START
 

inglewoodpete

Senior Member
A couple of problems for me in understanding your hardware and software.

Which PICAXE are you using? You don't show your symbol definitions, so we can't guess either.

You are unlikely to be able to program 3 different frequencies on one PICAXE chip, depending on the model and the underlying silicon's capability.
 

ccdubs

Member
I am using a 28X2 chip. I can confirm that what I have done does work I'm just not sure if it is necessary to perform the POKESFR command everytime. The symbol declarations are now included below.

Code:
        symbol PWM1200_Tone = B.0
	symbol PWM830_Tone = B.5
	symbol PWM1000_Tone = C.2

START:
	pwmout PWM1200_Tone, 103, 208 '1200/2 Hz
	pwmout PWM830_Tone, 150, 301 '830/2 Hz
	pwmout pwmdiv16, PWM1000_Tone, 124, 250 '1000/2 Hz
	
	'Poke the devisor registers
	'bit0=0 => 1/1, bit0=1 => 1/4, bit1=1 => 1/16 
	'Set divisor for B.0
	peeksfr $4A,b0
	bit0=0
	bit1=1
	pokesfr $4A,b0
	
	'Set divisor for B.5
	peeksfr $51,b0
	bit0=0	
	bit1=1
	pokesfr $51,b0
	
	pause 1000

	pwmout PWM830_Tone, OFF
	pwmout PWM1000_Tone, OFF
	pwmout PWM1200_Tone, OFF

	pause 1000

	goto START
 

hippy

Technical Support
Staff member
On an X2 the latest divisor used affects all PWMOUT channels, so to use different divisors across PWMOUT channels it is necessary to poke the related SFR to set those. No poking is necessary if the divisors are the same, which would typically be the case for three motor drivers where the frequencies / divisors would be the same and only the duty would change between each.

The M2, which came later than the X2, have a slightly different firmware so such poking isn't necessary on those
 

ccdubs

Member
Thanks Hippy,

I'm still a little confused. Does this mean that I still need to poke the SFR everytime I start the PWM's? I'm assuming so because I can't make it work otherwise. I trust that there is no issue in doing this.
 

inglewoodpete

Senior Member
Thanks Hippy,

I'm still a little confused. Does this mean that I still need to poke the SFR everytime I start the PWM's? I'm assuming so because I can't make it work otherwise. I trust that there is no issue in doing this.
I think that is probably the case. There are ways of fixing the problem (obviously). It's hard to get my head around where the problem is: in the compiler or the chip's firmware. If it's in the firmware, due to the small audience, you would have to wait until Rev-Ed sees the need for a firmware revision. If the problem is in the PE, then they will probably fix it in due course.

My suggestion: ensure you have the latest version of the PE. Check the Revision.txt file (PE Revision) and also Firmware.txt (Firmware revision for each PICAXE chip version).
 

westaust55

Moderator
Thanks Hippy,

I'm still a little confused. Does this mean that I still need to poke the SFR everytime I start the PWM's? I'm assuming so because I can't make it work otherwise. I trust that there is no issue in doing this.
In short, yes.

As hippy has already mentioned, each time a PWMOUT command is used, the PICAXE firmware (or PE) will effectively set the divisor for all of the PWM channels to the current divisor value.
That value will be 1 if there is no pwmdiv parameter, 1/4 if the parameter is pwmdiv4, and 1/16 if the parameter is pwmdiv16.
 
Top