HPWM 10 Bit


    HPWM channel, frequency, duty cycle, timer

Command Availability:

Only available on Microchip PIC microcontrollers with the PWM module.

For the Capture/Compare/PWM (CCP) module, see here HPWM CCP


This command sets up the hardware PWM module of the Microchip PIC microcontroller to generate a PWM waveform of the given frequency and duty cycle. Once this command is called, the PWM will be emitted until PWMOff is called.

channel is 5, 6 or 7. These corresponds to the HPWM5, HPWM6 or HPWM7 respectively.

frequency sets the frequency of the PWM output. It is measured in KHz. The maximum value allowed is 255 KHz. The minimum value varies depending on the clock speed. 1 KHz is the minimum on chips 16 MHz or under and 2 Khz is the lowest possible on 20 MHz chips. In situations that do not require a specific PWM frequency, the PWM frequency should equal approximately 1 five-hundredth the clock speed of the microcontroller (ie 40 Khz on a 20 MHz chip, 16 KHz on an 8 MHz chip). This gives the best duty cycle resolution possible.

duty cycle specifies the desired duty cycle of the PWM signal, and ranges from 0 to 255 where 255 is 100% duty cycle.

timer specifies the desired to be used. These can be timer 2, 4 or 6.


    'This program will alter the brightness of an LED using
    'hardware PWM.

    'Select chip model and speed
    #chip 16F18855, 32

    'Set the PWM pin to output mode
    DIR PORTA.2 out

    'Main code
        'Turn up brightness over 2.5 seconds
        For Bright = 1 to 255
            HPWM 6, 40, Bright, 2
            wait 10 ms
        'Turn down brightness over 2.5 seconds
        For Bright = 255 to 1
            HPWM 6, 40, Bright, 2
            wait 10 ms

For more help, see PWMOff