HPWM 10 Bit

Syntax:

    HPWM channel, frequency, duty cycle, timer [, resolution]

Command Availability:

Only available on Microchip PIC microcontrollers with the 10-bit PWM module.

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

Explanation:

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 1, 2, 3, 4, 5, 6 or 7. These corresponds to the HPWM3, HPWM4, HPWM5, HPWM6 or HPWM7 respectively.
The 10-bit PWM channel MUST be supported by the microcontroller. Check the microcontroller specific datasheet for the available channel.

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 1023 where 1023 is 100% duty cycle.    This should be a WORD value.    Note: Byte values are supported as a Byte value is factorised to a Word value. To use a Byte value and to ensure the 10-bit resolution you should cast the parameter as a Word, [WORD]byte_value or [WORD]constant_value

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

Optional resolution specifies the desired resolution to be used. These can be either 255 or 1023. The rational of this optional parameter is to support the duty cycle with a BYTE or a WORD range. If you call the method with a WORD the resolution will be set to 1023.



Notes:

PWM channels 1 and 2 are disable by default.  You must enable using the constants USE_HPWMn where n is the PWM channel you want to enable.  You can disable any PWM channel by setting the appropiatge change to FALSE.

On some microcontrollers you may need to set the port.pin as an output for PWM to operated as desired.

        #define USE_HPWM1 TRUE
        #define USE_HPWM2 TRUE



Example 1:

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

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

    'Generated by PIC PPS Tool for Great Cow Basic
    '
    'Template comment at the start of the config file
    '
    #startup InitPPS, 85

    Sub InitPPS

            'Module: PWM6
            RA2PPS = 0x000E    'PWM6OUT > RA2

    End Sub
    'Template comment at the end of the config file


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

    dim Bright as word

    'Main code
    do
        'Turn up brightness over the range
        For Bright = 0 to 1023
            HPWM 6, 40, Bright, 2
            wait 10 ms
        next
        'Turn down brightness over the range
        For Bright = 1023 to 0
            HPWM 6, 40, Bright, 2
            wait 10 ms
        next
    loop

Example 2:

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

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

    'Generated by PIC PPS Tool for Great Cow Basic
    '
    'Template comment at the start of the config file
    '
    #startup InitPPS, 85

    Sub InitPPS

            'Module: PWM3
            RA2PPS = 0x000E    'PWM3OUT > RA2

    End Sub
    'Template comment at the end of the config file


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

    dim Bright as word

    'Main code
    do
        'Turn up brightness over the range
        For Bright = 0 to 1023
            HPWM 3, 40, Bright, 2
            wait 10 ms
        next
        'Turn down brightness over the range
        For Bright = 1023 to 0
            HPWM 3, 40, Bright, 2
            wait 10 ms
        next
    loop

For more help, see PWMOff