PWMOUT pin, period, duty cycles
PWMOUT PWMDIV4,pin, period, duty cycles
PWMOUT PWMDIV16, pin, period, duty cycles
PWMOUT PWMDIV64, pin, period, duty cycles
PWMOUT pin, OFF
Pin - is a variable/constant which specifies the i/o pin to use. Note that the pwmout pin is not always a default output pin.
Period - is a variable/constant (0-255) which sets the PWM period (period is the length of 1 on/off cycle i.e. the total mark:space time).
Duty - is a variable/constant (0-1023) which sets the PWM duty cycle. (duty cycle is the mark or 'on time' )
The PWMDIV keyword is used to divide the frequency by 4, 16 or 64. This slows down the PWM. PWMDIV64 is only supported by the M2 devices.
Generate a continuous pwm output using the microcontroller's internal pwm module. also see the HPWM command, which can produce the equivalent of pwmout on different output pins. This command is different to most other BASIC commands in that the pwmout runs continuously (in the background) until another pwmout command is sent. Therefore it can be used, for instance, to continuously drive a motor at varying speeds. To stop pwmout issue a 'pwmout pin, off' (=pwmout pin,0,0) command.
The PWM period = (period + 1) x 4 x resonator speed (resonator speed for 4MHz = 1/4000000).
The PWM duty cycle = (duty) x resonator speed
Note that the period and duty values are linked by the above equations. If you wish to maintain a 50:50 mark-space ratio whilst increasing the period, you must also increase the duty cycle value appropriately. A change in resonator will change the formula. NB: If you wish to know the frequency, PWM frequency = 1 / (the PWM period).
In many cases you may want to use these equations to setup a duty cycle at a known frequency = e.g. 50% at 10 kHz. The Programming Editor software contains a wizard to automatically calculate the period and duty cycle values for you in this situation. Select the PICAXE>Wizards>pwmout menu to use this wizard.
As the pwmout command uses the internal pwm module of the microcontroller there are certain restrictions to its use:
1) The command only works on certain pins.
2) Duty cycle is a 10 bit value (0 to 1023). The maximum duty cycle value must not be set greater than 4x the period, as the mark 'on time' would then be longer than the total PWM period (see equations above)! Setting above this value will cause erratic behaviour.
3) The pwmout module uses a single timer for both the C.1/C.2 pins on 28/40 pin devices. Therefore when using PWMOUT on both these pins the period will be the same for both pins (however different duty cycles are possible).
4) The servo command cannot generally be used at the same time as the pwmout command. On older PICAXE parts the same internal timer (timer2) is used for both pwmout and servo, so these commands cannot be used at the same time. However some newer parts have additional dedicated internal timers that allow pwmout and servo to work together. This applies to these pwmout channels:
14M2 B.2, B.4 (C.0, C.2 share the servo timer) 18M2 B.3, B.6 20M2 B.1, C.2 (C.3, C.5 share the servo timer) 28X2 B.0, B.5 (C.1, C.2 share the servo timer)
6) pwmout 1 can be used at the same time as hpwm (see 3 above)
7) pwmout 2 cannot be used as the same time as hpwm
8) pwmout is dependant on the clock frequency. On some X1/X2 timing sensitive commands, such as readtemp, the command automatically drops to the internal 4MHz resonator to ensure timing accuracy. This will cause the background pwm to change, so pwm should be stopped during these commands.
9) using a 'pwmdiv' divider on the 28X2 and 40X2 parts causes other active pwmout timers to reset to 'divide by one'. In some situations this may produce an outcome other than expected unless additional steps are taken. Further discussion of this issue can be found here.
To stop pwmout on a pin it is necessary to issue a 'pwmout pin, off' command. Note that this stops all pwm channels sharing that timer (e.g. both C.1 and C.2 will stop together on a 28X2 part). To just stop one channel use 'pwmduty pin, 0' The pwmout command initialises the pin for pwm operation and starts the internal timers. As each pwmout command always resets the internal timer, the pwmduty command is recommended when rapidly changing the duty (i.e. use an initial pwmout command and then use pwmduty commands after that) . When driving a FET, a pull-down resistor between the PICAXE pin and 0V is essential. The purpose of the pull-down resistor is to hold the FET in the correct 'low' state whilst the PICAXE chip initialises upon power up. During this short initialisation period the pwmout pins are not actively driven (ie they 'float') and so the resistor is essential to hold the FET in the off condition.
Start and change a PWM signal
Enable a PWM signal and then change its duty rate
Submit Your Own Code!
You must be logged in to submit code examples. Login now.