PWM again

manie

Senior Member
The manual gives this rather confusing explanation for PWM calculations and after 5 months Picaxing I do not get it yet:

The PWM period = (period + 1) x 4 x resonator speed
(resonator speed for 4MHz = 1/4000000)
The PWM duty cycle = (duty) x resonator speed

What is meant by: (period+1) = what units is "period" in ?
Is the "resonator speed" in uSec/nSec/mSec or a number ?

If I know this I'll be able to do Set Freq command and lower PWM frequencies.

Thanks for your help
Manie
 
Last edited:

manuka

Senior Member
Rather that explain it straight off- are you using the editor PWM setup wizard? This rolls suitable PWM code for you, AND thereby also allows you to see the significance of each variable. Near magical! Stan
 

manie

Senior Member
Stan yes, I use that but only down to 3.9KHz pwm freq. Now I want to run the chip at say 125KHz, in order to produce a nice SLOW pwm freq and hopefully put LESS STRESS into the mosfets by switching less (per second...). Now I'm lost as I don't know the basic units to do a very BASIC calculation. I like to get to know the real nitty gritty, that way I'm free to explore and test better and I'm not tied to the pre-set wizzard...
 

Dippy

Moderator
"Now I want to run the chip at say 125KHz"
- are you talking about an oscillator speed of 125kHz for the PICAXE? (i.e. 'cock' sped)

If you POKE the right places you can get the PWM down to <250Hz with a 4MHz PICAXE.
So, with a clock of 125kHz you could have under 8 Hz.
 

hippy

Ex-Staff (retired)
Now I want to run the chip at say 125KHz, in order to produce a nice SLOW pwm freq and hopefully put LESS STRESS into the mosfets by switching less (per second...).
That 125kHz is a faster PWM frequency, more switching per seconds.

The best way to understand PWM operation internally is with reference to the PICmicro datasheet, but in pseudo form ( how it works, not how the chip actually works ) ...

PWM is generated from a free running counter clocked at Fosc/4 ( clocked once per 1us at 4MHz ). When the counter exceeds 'period' it is reset to zero and starts counting up again. This gives the frequency, the rate it resets to zero.
 

BeanieBots

Moderator
Once you've worked it out for 4Mhz using the wizard or long-hand, changing the chip clock speed will only affect the frequency and not the duty.
ie, halve the clock and you halve the PWM frequency but the duty will be the same.
 

manie

Senior Member
EC thanks, but that still assumes 4MHz chip freq. I need to go A LOT slower, like down to 125KHz chip freq and +- 300-500Hz pwm freq. Sorry to sound stupid but the calcs are not clear to me...

125Khz = 1,000,000/125,000 = chip period of 8 uSec. Is this then 32x more than calc at 4MHz ? At 0-255 steps then = 0.031 uSec/step. So: pwm x,100,x = 3(.13) uSec period ? and at 200 = 6(.27) uSec ? and at 255 = 8 uSec ?

300HZ pwm equates to 3,333 uSec pulse period correct ? How to calc it all is my problem... SORRY ......
Manie
 

Dippy

Moderator
Well found ec.
Gosh, the same subject two years ago?
Time flies doesn't it.

A neatened up description would be handy in the Manual.. or even the PWM Wizard.
 

hippy

Ex-Staff (retired)
Dippy, yes at 125KHz oscillator (internal) clock freq. The PWM freq. I want at 300Hz.
Sorry, I hadn't understood the clock issue earlier.

Reducing the PICAXE internal clock speed reduces the PWMOUT frequency, halve the clock speed, halve the PWM frequency, so if 32kHz PWM at 4MHz, at 2MHz PICAXE it becomes 16kHz PWM, at 125kHz PICAXE it becomes 1kHz PWM.

You can work that the other way; 300Hz PWM at 125kHz PICAXE is 9600Hz PWM at 4MHz PICAXE.

The values for 4MHz can be put into the PWMOUT Wizard, so "PwmOut 2 , 103, 208" should give ~300Hz square wave when the PICAXE is under-clocked at 125kHz.
 

eclectic

Moderator
Superslow PWMOUT

I think these are the earliest references.
http://www.picaxeforum.co.uk/showthread.php?t=2233&highlight=under+clocking

http://www.picaxeforum.co.uk/showthread.php?t=5872&highlight=jeremy+pwm*


If you're using a 28X1 /40X1 then you can use the
Setfreq k and m commands. (See manual 2)

Please try this, and watch the 'scope results.
You'll need a “hard reset” afterwards.
Man 1 p.41 (v6.7)

Code:
#picaxe 08M
;alter Picaxe and pwmout pin for your chip

main:
setfreq m4
pwmout 2,249,500
; ~4000 Hz "normal" pwmout

pause 5000

poke $12,6
; freq / 16 = ~250Hz

pause 5000

poke $8F , %00100000
;freq / 16 again ~15.6Hz
pause 500

goto main

;replace poke $8F, %00100000 with
; setfreq k250 for the X1 series

; don't forget the hard reset!!!!!!!
; Manual 1 (v6.7) page 41

'%00000000 = 31 kHz ~19bps
'%00010000 = 125 kHz 75bps
'%00100000 = 250 kHz 150bps
'%00110000 = 500 kHz 300bps
'%01000000 = 1 MHz 600bps
'%01010000 = 2 MHz 1200bps
'%01100000 = 4 MHz 2400bps
'%01110000 = 8 MHz 4800bps
 

manie

Senior Member
Thanks guys, that will keep me busy tommorrow... will digest it then. Tonight I must do some more work on the Xbee project.
 
Top