About Hardware PWM Code Optimisation
For compatibility all channels are supported by default. This is maintains backward compatibility.
To mimise the code, use the following to disable support for a specific Capture/Compare/PWM (CCP) module, timers or the PWM
module.
Setting a constant to FALSE will remove the support of the capability from the method and therefore will reduce the program size.
#define USE_HPWMCCP1 FALSE
#define USE_HPWMCCP2 FALSE
#define USE_HPWMCCP3 FALSE
#define USE_HPWMCCP4 FALSETo further mimise the code, use the following to disable support for a specific PWM channels. Only PWM channels 5, 6 and 7 are supported.
#define USE_HPWM3 FALSE
#define USE_HPWM4 FALSE
#define USE_HPWM5 FALSE
#define USE_HPWM6 FALSE
#define USE_HPWM7 FALSETo further mimise the code, use the following to disable support for a specific timers.
#define USE_HPWM_TIMER2 TRUE
#define USE_HPWM_TIMER4 TRUE
#define USE_HPWM_TIMER6 TRUEExample
This will save 335 bytes of program memory by removing support for CCP1, CCP2 and CCP4.
#chip 16f18855,32
#Config MCLRE_ON
UNLOCKPPS
RC2PPS = 0x0A 'RC2->CCP2:CCP2;
LOCKPPS
#define USE_HPWMCCP1 FALSE ' This is not used so optimise ' <<< the constants that select which CCP channels get compiled in
#define USE_HPWMCCP2 TRUE ' This is used so include in the compiled code
#define USE_HPWMCCP3 FALSE ' This is not used so optimise
#define USE_HPWMCCP4 FALSE ' This is not used so optimise
'Setting the port an output is VERY important... LED will not work if you do not set as an output.
dir portC.2 out ; CCP2
do forever
For Bright = 1 to 255
HPWM 2, 40, Bright
wait 10 ms
next
loopKey line: #define USE_HPWMCCP1 FALSE (and the matching lines for CCP3 and CCP4) — since only channel 2 is used by the HPWM 2, 40, Bright call below, disabling the other three channels' compiled-in support saves 335 bytes of program memory without changing behaviour.
See Also:
- HPWM CCP — the command whose channel support this page’s constants control
- HPWM 10 Bit — the 10-bit PWM module, which has its own USE_HPWMn constants
- HPWM 16 Bit — the 16-bit PWM module, which has its own USE_HPWM16_n constants

