How to use HPWM for picaxe chips

hpwm_test.bas (2616Bytes)

This information is based on the chip that is used for the 20x2(PIC18F14K22) To get a better understanding of the modes, I suggest referencing this datasheet for further information on the chips features. IMages and info are referenced from both the picaxe 2 manual and the PIC datasheet for the above mentioned chip.

HPWM, what is it?

HPWM is a function that can be used to enable the hardware pwm (timer2 on the 20x2) of the newer picaxe chips. The pic chip provides what is called PWM enhanced mode which allows for generation of a pwm signal on up to 4 pins. It allows for specialized modes such as single, half bridge and full bridge(forward and reverse).

What is special about those modes?

Single mode -  allows you to use any or all of the pins to output a SINGLE pwm signal. This does NOT allow for individual control  of the pwm output for each of those pins. So you can not run different duty cycles for each of these pins. 

Half Bridge - This uses two pins and uses a variable called deadband. This is essentially a delay  for the switchover of 1 pin to the other otherwise sparks could fly and things could fry....as we try to, we want to avoid letting the smoke out of our processors. Half bridge mode is restricted to 2 of the four pins so you can't use the second set for another pwm out.

halfbrideEX.png

Full Bridge -  This uses 4 pins with 2 pins having active states depending on direction.

fullbridgeEX.png

PWM or HPWM which do I use?

Either one will work, but care has to be taken when using the hpwm as you need to set the mode that you are interested in.

Can I use both PWM and HPWM

On some of the picaxe chips you have more than 1 and in that case yes you can use both though you are restricted to specific pins in this case. With chips like the picaxe 20x2 you only have 1 pwm timer and this is shared with PWM and HPWM. 

What about the holy grail of multiple PWM outputs that can vary their duty cycles?

This holy grail is only on the 28and 40x2 and as mentioned above it's due to these chips having more than one pwm timer module.

What else should I know?

There was an issue with some of the early 20x2 chips that can be fixed in this post. I'm not sure of any other issues, but just in case, post em on the forum.

The latest manual appears to have more info on this function and was released sometime this month, grab the latest and see if it helps! v7.4 3/11

Do you have any example code to show me how this works?

Not at this time but I'm sure that any specific questions can be answered in this post. Ask away and lets see what we can figure out!

Questions, comments, corrections? Let me know and all add em to the OP.

Edit (3/20):

  • Added some example code that I've run in the simulator and appears to work. Have not tested with a built circiut, but I'd like to imagine it does just fine.
  • Changed title as it seemed a bit ambiguous.