Skip to content

hpwm_fixed_mode

Anobium edited this page Oct 18, 2020 · 1 revision
HPWM Fixed Mode

Syntax:

    PWMOn               'only applies to CCP/PWM channel 1
    'or
    PWMOff

    PWMOn( channel )            'where the parameter can be any valid CCP/PWM channel, 1, 2, 3, 4 or 5
    'or
    PWMOff( channel )

    PWMOn( module_number , PWMModule)            'where the parameter can be any valid PWM channel 1 .. 9
    'or
    PWMOff( module_number , PWMModule)

Command Availability:

Only available on Microchip PIC microcontrollers with a CCP/PWM or PWM module.

See here HPWM CCP for the method to change PWM parameters dynamically or to use other CCP channels - this method support CCP1/PWM, CCP2/PWM, CCP3/PWM, CCP4/PWM and CCP5/PWM.

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.

These constants are required to set the parameters for the PWM. The frequency and the duty applies to all channels when using the method(s) or macro(s) shown above.

Constant Name Controls Default Value
PWM_Freq Specifies the output frequency of the PWM module in KHz. 38
PWM_Duty Sets the duty cycle of the PWM module output. Given as percentage. 50

For CCP/PWM modules are also supported using a call to a method or a macro, as follows:

Method/Macro Controls Default Value

PWMOn

No parameter enables CCP1/PWM only

No parameter

PWMOff

Disables CCP1/PWM only

 

PWMOn( channel )

Where the parameter is any valid CCP/PWM channel

channel can be 1, 2, 3, 4 or 5

PWMOff( channel )

Where the parameter is any valid CCP/PWM channel

channel can be 1, 2, 3, 4 or 5

PWMOn( module, PWMMODULE )

Where the parameter is any valid PWM module

module can be 1..9
See the example below for the constants to control fixed mode PWM using PWM modules.

PWMOff( channel, PWMMODULE )

Where the parameter is any valid CCP/PWM module

module can be 1..9

Fixed Mode PWM for PWM Modules.

To set the Fixed Mode PWM for PWM Modules you need to set a timer frequency, a PWM module cycle and the PWM model source clock.

The options for source clock are shown below. These are the PWM timers supported by the PWM modules, where nn is the frequency.

    PWM_Timer2_Freq `nn` or
    PWM_Timer4_Freq `nn` or
    PWM_Timer6_Freq `nn`.

The PWM module duty is set using PWM_`yy`_Duty xx' where `yy is between 1 and 9 and is a valid PWM module, and, xx is the Duty cycle for specific channels

    #define PWM_yy_Duty xx

The PMW module clock source us PWM_`zz`_Clock_Source tt. Where zz is channel and tt is the PWM clock source.

    #define PMW_zz_Clock_Source tt

You do not need to define all the timers and or all the channels, just define the constants you need.

The minimum is A timer with a frequency A PWM channel with a duty A PWM channel clock source

Example: For PWM channel 6 with a frequency of 38Khz with a duty of 50% with a clock source of timer 2, use

        #define PWM_Timer2_Freq 38
        #define PWM_7_Duty 50
        #define PMW_7_Clock_Source 6

Details of the constants with example parameters.

#define PWM_Timer2_Freq 20        'Set frequency in KHz, just change the number
#define PWM_Timer4_Freq 40        'Set frequency in KHz, just change the number
#define PWM_Timer6_Freq 60        'Set frequency in KHz, just change the number

Supported PWM modules, with example parameters.

#define PWM_1_Duty 10            'Set duty cycle as percentage 0-100%, just change the number
#define PMW_1_Clock_Source 2
#define PWM_2_Duty 20
#define PMW_2_Clock_Source 4
#define PWM_3_Duty 30
#define PMW_3_Clock_Source 6
#define PWM_4_Duty 40
#define PMW_4_Clock_Source 2
#define PWM_5_Duty 50
#define PMW_5_Clock_Source 6
#define PWM_6_Duty 60
#define PMW_6_Clock_Source 6
#define PWM_7_Duty 70
#define PMW_7_Clock_Source 4
#define PWM_8_Duty 80
#define PMW_8_Clock_Source 4
#define PWM_9_Duty 90
#define PMW_9_Clock_Source 6

Example #1:

Enable CCP1/PWM channel only. This is the legacy command.

    #chip 16f877a,20

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

    'Main code

    #define PWM_Freq 38      'Frequency of PWM in KHz
    #define PWM_Duty 50      'Duty cycle of PWM (%)

    PWMOn    'Will enable CCP1/PWM Only

    wait 10 s                'Wait 10 s

    PWMOff   'Will disable CCP1/PWM Only

    do
    loop

Example #2:

Enable any CCP/PWM channel using a call to a method.

    #chip 16f877a,20

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

    'Main code

    #define PWM_Freq 38      'Frequency of PWM in KHz
    #define PWM_Duty 50      'Duty cycle of PWM (%)

    PWMOn (2)    'Will enable any valid CCP/PWM channel

    wait 10 s                'Wait 10 s

    PWMOff (2)   'Will disable any valid CCP/PWM channel

    do
    loop

Example #3:*

Enable any PWM module using a PWM specific method.

    'A real simple and easy PWM setup for 8 and 10 bit PWM channels
        #chip 18f25k42, 16

        #startup InitPPS, 85

        Sub InitPPS

                'Module: PWM5
                RA0PPS = 0x000D    'PWM5 > RA0
                'Module: PWM6
                RA1PPS = 0x000E    'PWM6 > RA1
                'Module: PWM7
                RA2PPS = 0x000F    'PWM7 > RA2
                'Module: PWM8
                RA3PPS = 0x0010    'PWM8 > RA3

        End Sub

        'Template comment at the end of the config file
        dir porta Out
        dir portb Out
        dir portc Out

    'This is the setup section for fixed mode PWM

        'The only options are PWM_Timer2_Freq nn|PWM_Timer4_Freq nn|PWM_Timer6_Freq nn. These are the PWM timers
        'The PWM_yy_Duty xx' where yy is between 1 and 9 and is a valid PWM module, and, xx is the Duty cycle for specific channels
        'The PMW_zz_Clock_Source tt.  Where zz is channel and tt is the PWM clock source.
        'You do not need to define all the timers and channels, just define the constants you need.
        'The minimum is
        '   A timer with a frequency
        '   A PWM channel with a duty
        '   A PWM channel clock source
        '   For PWM channel 2 with a frequency of 38Khz with a duty of 50% with a clock source of timer 2, use
        '     #define PWM_Timer2_Freq 38
        '     #define PWM_7_Duty 50
        '     #define PMW_7_Clock_Source 2

        #define PWM_Timer2_Freq 20        'Set frequency in KHz, just change the number
        #define PWM_Timer4_Freq 40        'Set frequency in KHz, just change the number
        #define PWM_Timer6_Freq 60        'Set frequency in KHz, just change the number


      '    Supported PWM module but not by this specific microcontroller
      '
      '    #define PWM_1_Duty 10            'Set duty cycle as percentage 0-100%, just change the number
      '    #define PMW_1_Clock_Source 2
      '
      '    #define PWM_2_Duty 20
      '    #define PMW_2_Clock_Source 4
      '
      '    #define PWM_3_Duty 30
      '    #define PMW_3_Clock_Source 6
      '
      '    #define PWM_4_Duty 40
      '    #define PMW_4_Clock_Source 2

        #define PWM_5_Duty 50
        #define PMW_5_Clock_Source 6

        #define PWM_6_Duty 60
        #define PMW_6_Clock_Source 6

        #define PWM_7_Duty 70
        #define PMW_7_Clock_Source 4

        #define PWM_8_Duty 80
        #define PMW_8_Clock_Source 4

        '    Supported PWM module but not by this specific microcontroller
        '
        '    #define PWM_9_Duty 90
        '    #define PMW_9_Clock_Source 6


        '   Enable module 7
        HPWMOn ( 7, PWMModule )
        wait 2 s
        '   Disable channel 7
        HPWMOff ( 7, PWMModule)
        '    wait 2 s

        '   Enable others module
        HPWMOn ( 5, PWMModule )
        HPWMOn ( 6, PWMModule )
        HPWMOn ( 7, PWMModule )
        HPWMOn ( 8, PWMModule )

        '  Enable CCP/PWM channel 1  - uses constants FREQ and DUTY
        PWMOn

        '  Enable CCP/PWM channel 2
        PWMOn ( 2 )
        do
        loop

    End

For more help, see PWMOn and PWMOff or, for AVR see Fixed Mode PWM for AVR

Clone this wiki locally