HPWM explanation

Hemi345

Senior Member
Could someone elaborate on what this means:
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 <a href="/commands/servo">servo</a> command cannot be used at the same time as the hpwm command.
What are "2" and "1" referring to?

I'm in the planning stages for a project to use PWM on 4 to 6 LEDs. The PWM would be used to change the RGB LEDs color and status lights. I want the capability to PWM them for dimming and appearance of 'pulsing' separately, if possible, so I'm looking at using either a 20M2 or 20X2. I'd go with the 20X2 with 4 of the LEDs on the HPWM-capable pins and the other two on the regular PWM pins if using them simultaneously is possible. I understand that the 20M2 can't operate HPWM in single mode so in that case I'd just PWMOUT for 4 LEDs and maybe blink the other two or something. I don't have any spare 20M2/X2 to test with and not finding a definitive answer simulating some test code in PE6.

Thanks
 

hippy

Technical Support
Staff member
It always helps to post a link to the source quote or where it was found. I am guessing it's from the online HPWM command description -

http://www.picaxe.com/BASIC-Commands/Advanced-IO-Interfacing/hpwm

That does seem to be somewhat mangled, and also in the PDF manual. The link for 'servo' was easy enough to fix but will have to figure out what the text is meant to say. My guess would be there is a missing "pin", "channel" or a port identifier.

HPWM does not give four individual PWM channels so it is not really suitable for controlling four separate LED channels.
 

Hemi345

Senior Member
Yes, I forgot to post the link to the online documentation of HPWM you linked to. I believe the description of HPWM needs to be reviewed and rewritten.
HPWM does not give four individual PWM channels so it is not really suitable for controlling four separate LED channels.
seems to contradict the way I interprete what it initially says about "single mode":
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)
then...
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
confusing!
 

inglewoodpete

Senior Member
hPWM is really intended to be used for motor control as a full bridge or two half-bridge PWM sources. The reason is that the duty value is linked to that on other pin/s, although inverted. I investigated this some time ago, when trying to maximise PWM outputs for driving RGB LEDs with a 14M2.

For a detailed description on how the hPWM is performed in the chip's silicon, refer to the appropriate Microchip datasheet for the PICAXE you intend to use. Links to the Microchip datasheets can be found on this (PICAXE) page.
 

hippy

Technical Support
Staff member
This is a diagram I created of how the 28X2 PWM / HPWM single mode configuration is -

Code:
.--------.
| PWM #1 |--------------------------------> B.0
`--------'
.--------.
| PWM #2 |--------------------------------> B.5
`--------'
.--------.
| PWM #3 |--------------------------------> C.1
`--------'
.--------.
| PWM #4 |---.----------------------o 
`--------'   |   .---.               \o---> C.2
             |   |   |---> HPWMA ---o
             |___|   |---> HPWMB ---------> B.2
                 |   |---> HPWMC ---------> B.1
                 |   |---> HPWMD ---------> B.4
                 `---'
HPWM is not a PWM generator in its own right, but a router for PWM#4. PWMA-PWMD can be individually enabled, allowing up to four things to be separately controlled, but what is output on PWMA-PWMD comes from the same PWM#4 source.

It is like a car with a single engine and a clever selector which can drive just the front wheels, back wheels, both front and back together, or none. Each set of wheels is separately controlled, front and back can be driven or not, but both sets of wheels cannot be driven at the same time at different speeds.

Also, when HPWM mode is not used, C.2 is driven by PWM#4. Once HPWM is enabled, PWM#4 (PWMOUT C.2) is no longer available because PWM#4 is allocated to HPWM use.

Hence the description that not all of the PWMOUT channels are available when HPWM is used; N x PWMOUT, or N-1 x PWMOUT + HPWM.
 

Hemi345

Senior Member
Thank you for the responses. Hippy, your description should replace what is in the manual/online documentation as it's a great explanation of how it works. :)
 

stan74

Senior Member
I also think the pwm explanation is confusing.Can 1 pin run at 100 Hz and another at 38 KHz?
 

stan74

Senior Member
I've been asking about pwm. Seems the 28X2 uses C.0 and C.1 which run at the same period and duty say 38KHz and B.0 and B.5 which are independent and could each control the inhibit pins of a L293D dual h-bridge for 2 motor pwm.
I don't know why this doesn't work.

init:
pwmout C.1, 52, 105 ;38 KHz 50%
pwmout pwmdiv16, B.0,124,249 ; 1KHz 50%
pwmout pwmdiv16, B.5,124,249 ; 1KHz 50%

main:
pwmduty C.1,105
pwmduty B.0,249
pwmduty B.5,249
 

hippy

Technical Support
Staff member

nbw

Senior Member
This is a diagram I created of how the 28X2 PWM / HPWM single mode configuration is -

Code:
.--------.
| PWM #1 |--------------------------------> B.0
`--------'
.--------.
| PWM #2 |--------------------------------> B.5
`--------'
.--------.
| PWM #3 |--------------------------------> C.1
`--------'
.--------.
| PWM #4 |---.----------------------o
`--------'   |   .---.               \o---> C.2
             |   |   |---> HPWMA ---o
             |___|   |---> HPWMB ---------> B.2
                 |   |---> HPWMC ---------> B.1
                 |   |---> HPWMD ---------> B.4
                 `---'
HPWM is not a PWM generator in its own right, but a router for PWM#4. PWMA-PWMD can be individually enabled, allowing up to four things to be separately controlled, but what is output on PWMA-PWMD comes from the same PWM#4 source.

It is like a car with a single engine and a clever selector which can drive just the front wheels, back wheels, both front and back together, or none. Each set of wheels is separately controlled, front and back can be driven or not, but both sets of wheels cannot be driven at the same time at different speeds.

Also, when HPWM mode is not used, C.2 is driven by PWM#4. Once HPWM is enabled, PWM#4 (PWMOUT C.2) is no longer available because PWM#4 is allocated to HPWM use.

Hence the description that not all of the PWMOUT channels are available when HPWM is used; N x PWMOUT, or N-1 x PWMOUT + HPWM.
Thought I'd drop in as I'm making a copper jellyfish with four banks of LEDs that need PWMing... and all I have in my toolbox is a 20X2, a 20M2, and some 08M2s. THe four banks operate LEDs on the tentacles, and I want them to pulse at different rates, rather than all fading up and down together.

Sounds like my cheeky get-out-of-jail with the four x HPWM on the 20x2 isn't going to work... a bit wasteful, but I guess I could use one 08m2 per bank??
 
Top