Name:
hpwm
Syntax:

HPWM mode, polarity, setting, period, duty

HPWM PWMDIV4, mode, polarity, setting, period, duty

HPWM PWMDIV16, mode, polarity, setting, period, duty

HPWM PWMDIV64, mode, polarity, setting, period, duty

HPWM OFF

Mode - is a variable/constant which specifies the hardware pwm mode

  pwmsingle 0
  pwmhalf 1
  pwmfull_f 2
  pwmfull_r 3

Polarity - is a variable/constant which specifies the active polarity (DCBA)

  pwmHHHH 0
  pwmLHLH 1
  pwmHLHL 2
  pwmLLLL 3

Setting - is a variable/constant which specifies a specific setting

  single mode bit mask %0000 to %1111 to dis/enable DCBA
  half mode dead band delay (value 0-127)
  full mode not used, enter 0 as default value

 

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 frequencey by 4, 16 or 64. This slows down the PWM. 64 is not supported by all parts.

Note that the 'PWMout Wizard' from the PICAXE>Wizards menu in the Programming Editor or AXEpad software can also be used to calculate hpwm frequencies. See the 'pwmout' command for more details about this wizard.

 

28 pin devices - the 28X1, 28X2, 28X2-3V support hpwm, the 28X2-5V does not.

40 pin devices - the 40X2, 40X2-5V and 40X2-3V parts support hpwm, the 40X1 does not.

This is a design restriction of the silicon within these particular chips.

Description:

Hardware PWM is an advanced method of motor control using PWM methods. It can use a number of outputs and modes, as defined by the PIC microcontroller's internal pwm hardware.

hpwm can be used instead of, not at the same time as, the pwmout command on 2 (28/40 pin). However pwmout on 1 can be used simultaneously if desired. The servo command cannot be used at the same time as the hpwm command.

hpwm gives access to the advanced pwm controller in the PIC microcontroller. It uses up to 4 pins, which are labelled here A,B,C,D for convenience.. Some of these pins normally 'default' to input status, in this case they will automatically be converted to outputs when the hpwm command is processed.

On 20 pin devices: On 14 pin devices:
A is input 5 (C.5) A is input 2 (C.2)
B is input 4 (C.4) B is input 1 (C.1)
C is input 3 (C.3) C is input 0 (C.0)
D is output 4 (B.4) D is output 5 (B.5)
On 28 pin devices: On 40 pin devices:
A is input 2 (C.2) A is portC 2 (C.2)
B is output 2 (B.2) B is input 5 (D.5)
C is output 1 (B.1) C is input 6 (D.6)
D is output 4 (B.4) D is input 7 (D.7)

Not all pins are used in all hpwm modes. Unused bits are left as normal i/o pins.

  single - A and/or B and/or C and/or D (each bit is selectable)
  half - A, B only
  full - A, B, C, D

The active polarity of each pair of pins can be selected by the polarity setting:

  pwm_HHHH A and C active high, B and D active high
  pwm_LHLH A and C active high, B and D active low
  pwm_HLHL A and C active low, B and D active high
  pwm_LLLL A and C active low, B and D active low

When using active high outputs, it is important to use a pull-down resistor from the PICAXE pin (A-D) to 0V. When using active-low outputs a pull-up resistor is essential. The purpose of the pull-up/down resistor is to hold the FET driver in the correct state whilst the PICAXE chip initialises upon power up. During this short initialisation period the drivers are not actively driven (ie they 'float') and so the resistor is essential to hold the FET in the required off condition.

 

Single Mode

Supported: 20X2, 28X1, 28X2, 28X2-3V, 40X2, 40X2-3V
Not Supported:  14M, 14M2, 20M2, 28X2-5V, 40X1, 40X2-5V

In single mode each pin works independently. It is therefore equivalent to a pwmout command. However more than one pin can be enabled at a time. Therefore this mode has two main uses:

1) To allow the equivalent of a 'pwmout' command on different outputs (than the pwmout command)

2) To allow pwmout on more than one pin (up to 4) at the same time. The pwm applied to each output is identical. This is often used to provide a brightness control on multiple LEDs or to control multiple motors.

To enable a single output simply set its corresponding bit to '1' (D-C-B-A) within the settings byte of the command e.g. to enable all 4 pins use %1111

 

Half Mode (all parts)

In half mode outputs A and B control a half bridge. C and D are not used. The PWM signal is output on pin A, while the complementary PWM signal is output on pin B. The dead band delay 'setting' value is a very important value, without a correct value a shoot-through current may destroy the half bridge setup. This delay prevents both outputs being active at the same time. The command delay value (0-127) gives a delay equivalent to (value x oscillator speed (e.g. 4MHz) / 4). The value depends on the switch on/off characteristics of the FET drivers used.

 

Full Mode (all parts)

In full bridge mode outputs A, B, C and D control a full bridge.

In forward mode A is driven to its active state whilst D is modulated. B and C are in their inactive state. In reverse mode C is driven to its active state whilst B is modulated. A and D are in their inactive state. In this mode a deadband delay is generally not required as only one output is modulated at one time. However there can be conditions (when near 100% duty cycle) where current shoot-through could occur. In this case it is recommended to either 1) switch off pwm before changing directions or 2) use a specialist FET driver that can switch the FET on quicker than it switches off (the opposite is normally true on non-specialist parts).

Applies To:
14M, 14M2, 20M2, 20X2, 28X1, 28X2, 40X2
See Also:
Related Create:
Share:
Print:

Start and change a hardware PWM signal

Enable a hardware PWM signal and then change its duty rate

Code Example:
init:	hpwm 0,0,%1111,150,100	; start pwm

main:	hpwmduty 150		; set pwm duty
	pause 1000		; pause 1 s
	hpwmduty 50		; set pwm duty
	pause 1000		; pause 1 s
	goto main		; loop back to start
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.