PWM library

The General-Purpose Timer module is available with a number of ARM MCUs. mikroC PRO for ARM provides a library which simplifies using of the PWM mode of General-Purpose Timer Module.

  Important :

Library Routines for Stellaris Cortex M3

Library Routines for Stellaris Cortex M4

Library Routines for ST

PWM_CCPx_Init

Prototype // for Stellaris MCUs with dedicated PORT functions :

unsigned int PWM_CCPx_Init(unsigned long freq_hz);

// for Stellaris MCUs with alternative PORT functions on GPIO pins :

unsigned int PWM_CCPx_Init(unsigned long freq_hz, const Module_Struct *module);

Description

Initializes the PWM module for Stellaris Cortex M3 MCUs.

Parameters
  • freq_hz: PWM frequency in Hz (refer to device datasheet for correct values in respect with Fosc).
  • module: appropriate module pinout, see the following table :
    CCP Module Pinouts for Stellaris Cortex M3
    CCP0 _GPIO_MODULE_CCP0_B0 _GPIO_MODULE_CCP0_B2 _GPIO_MODULE_CCP0_B5 _GPIO_MODULE_CCP0_C6 _GPIO_MODULE_CCP0_C7
    _GPIO_MODULE_CCP0_D3 _GPIO_MODULE_CCP0_D4 _GPIO_MODULE_CCP0_F4 _GPIO_MODULE_CCP0_J2 _GPIO_MODULE_CCP0_J7
    CCP1 _GPIO_MODULE_CCP1_A6 _GPIO_MODULE_CCP1_B1 _GPIO_MODULE_CCP1_B6 _GPIO_MODULE_CCP1_C4 _GPIO_MODULE_CCP1_C5
    _GPIO_MODULE_CCP1_D7 _GPIO_MODULE_CCP1_E3 _GPIO_MODULE_CCP1_F6 _GPIO_MODULE_CCP1_J6
    CCP2 _GPIO_MODULE_CCP2_B1 _GPIO_MODULE_CCP2_B5 _GPIO_MODULE_CCP2_C4 _GPIO_MODULE_CCP2_D1 _GPIO_MODULE_CCP2_D5 _GPIO_MODULE_CCP2_E1
    _GPIO_MODULE_CCP2_E2 _GPIO_MODULE_CCP2_E4 _GPIO_MODULE_CCP2_F5 _GPIO_MODULE_CCP2_J5
    CCP3 _GPIO_MODULE_CCP3_A7 _GPIO_MODULE_CCP3_B2 _GPIO_MODULE_CCP3_C5 _GPIO_MODULE_CCP3_C6 _GPIO_MODULE_CCP3_D4 _GPIO_MODULE_CCP3_E0
    _GPIO_MODULE_CCP3_E4 _GPIO_MODULE_CCP3_F1 _GPIO_MODULE_CCP3_G4
    CCP4 _GPIO_MODULE_CCP4_A7 _GPIO_MODULE_CCP4_C4 _GPIO_MODULE_CCP4_C7 _GPIO_MODULE_CCP4_D5 _GPIO_MODULE_CCP4_E2 _GPIO_MODULE_CCP4_F7 _GPIO_MODULE_CCP4_J4
    CCP5 _GPIO_MODULE_CCP5_B5 _GPIO_MODULE_CCP5_B6 _GPIO_MODULE_CCP5_C4 _GPIO_MODULE_CCP5_D2 _GPIO_MODULE_CCP5_E5 _GPIO_MODULE_CCP5_G7 _GPIO_MODULE_CCP5_G5
    CCP6 _GPIO_MODULE_CCP6_B5 _GPIO_MODULE_CCP6_D0 _GPIO_MODULE_CCP6_D2 _GPIO_MODULE_CCP6_E1 _GPIO_MODULE_CCP6_H0 _GPIO_MODULE_CCP6_J3
    CCP7 _GPIO_MODULE_CCP7_B6 _GPIO_MODULE_CCP7_D1 _GPIO_MODULE_CCP7_D3 _GPIO_MODULE_CCP7_E3 _GPIO_MODULE_CCP7_H1
Returns

This function returns calculated timer period.

Requires
  • MCU must have the HW PWM Module.
  • Prior to PWM module initialization user must set appropriate pins as output. Please, see GPIO_Config routine.
  • PWM library routines require you to specify the module you want to use. To select the desired PWM module, simply change the letter x in the routine prototype for a number from 0 to 7.
Example
// for Stellaris MCUs with dedicated PORT functions :
GPIO_Config(&GPIO_PORTD_DATA_BITS, _GPIO_PINMASK_4, _GPIO_DIR_NO_CHANGE, _GPIO_CFG_ADV_CCP);
ratio = PWM_CCP0_Init(25000);                          
PWM_CCP0_Set_Duty(ratio/4,_PWM_INVERTED_ENABLE);
PWM_CCP0_Start();

// for Stellaris MCUs with alternative PORT functions on GPIO pins :
ratio = PWM_CCP0_Init(25000, &_GPIO_MODULE_CCP0_B5);                       
PWM_CCP0_Set_Duty(ratio/4,_PWM_INVERTED_ENABLE);
PWM_CCP0_Start();
Notes

Number of available PWM channels depends on MCU. Refer to MCU datasheet for details.

PWM_TnCCPx_Init

Prototype

unsigned int PWM_TnCCPx_Init(unsigned long freq_hz, const Module_Struct *module);

Description

Initializes the PWM module for Stellaris Cortex M4 MCUs.

Parameters
  • freq_hz: PWM frequency in Hz (refer to device datasheet for correct values in respect with Fosc).
  • module: appropriate module pinout, see the following table :
    CCP Module Pinouts for Stellaris Cortex M4
    CCP0 CCP1
    Timer0 _GPIO_MODULE_T0CCP0_F0_AHB _GPIO_MODULE_T0CCP1_F1_AHB
    _GPIO_MODULE_T0CCP0_B6_AHB _GPIO_MODULE_T0CCP1_B7_AHB
    _GPIO_MODULE_T0CCP0_L0_AHB _GPIO_MODULE_T0CCP1_L1_AHB
    Timer1 _GPIO_MODULE_T1CCP0_B4_AHB _GPIO_MODULE_T1CCP1_B5_AHB
    _GPIO_MODULE_T1CCP0_L2_AHB _GPIO_MODULE_T1CCP1_F3_AHB
    _GPIO_MODULE_T1CCP0_F2_AHB _GPIO_MODULE_T1CCP1_J1_AHB
    _GPIO_MODULE_T1CCP0_J0_AHB _GPIO_MODULE_T1CCP1_L3_AHB
    Timer2 _GPIO_MODULE_T2CCP0_B0_AHB _GPIO_MODULE_T2CCP1_B1_AHB
    _GPIO_MODULE_T2CCP0_F4_AHB _GPIO_MODULE_T2CCP1_F5_AHB
    _GPIO_MODULE_T2CCP0_J2_AHB _GPIO_MODULE_T2CCP1_J3_AHB
    _GPIO_MODULE_T2CCP0_L4_AHB _GPIO_MODULE_T2CCP1_L5_AHB
    Timer3 _GPIO_MODULE_T3CCP0_B2_AHB _GPIO_MODULE_T3CCP1_B3_AHB
    _GPIO_MODULE_T3CCP0_F6_AHB _GPIO_MODULE_T3CCP1_F7_AHB
    _GPIO_MODULE_T3CCP0_J4_AHB _GPIO_MODULE_T3CCP1_J5_AHB
    _GPIO_MODULE_T3CCP0_L6_AHB _GPIO_MODULE_T3CCP1_L7_AHB
    Timer4 _GPIO_MODULE_T4CCP0_C0_AHB _GPIO_MODULE_T4CCP1_C1_AHB
    _GPIO_MODULE_T4CCP0_G0_AHB _GPIO_MODULE_T4CCP1_G1_AHB
    _GPIO_MODULE_T4CCP0_M0_AHB _GPIO_MODULE_T4CCP1_M1_AHB
    _GPIO_MODULE_T4CCP0_P0_AHB
    Timer5 _GPIO_MODULE_T5CCP0_C2_AHB _GPIO_MODULE_T5CCP1_C3_AHB
    _GPIO_MODULE_T5CCP0_G2_AHB _GPIO_MODULE_T5CCP1_G3_AHB
    _GPIO_MODULE_T5CCP0_M2_AHB _GPIO_MODULE_T5CCP1_M3_AHB
    _GPIO_MODULE_T5CCP0_P2_AHB
Returns

This function returns calculated timer period.

Requires
  • MCU must have the HW PWM Module.
  • Prior to PWM module initialization user must set appropriate pins as output. Please, see GPIO_Config routine.
  • PWM library routines require you to specify the module you want to use. To select the desired PWM module, simply change the letter x in the routine prototype for a number 0 or 1 to select desired PWM module and letter n from 0 to 5 depending on the used timer module.
Example
ratio = PWM_T0CCP0_Init(25000, &_GPIO_MODULE_T0CCP0_F0_AHB);                       
PWM_T0CCP0_Set_Duty(ratio/4,_PWM_INVERTED_ENABLE);
PWM_T0CCP0_Start();
Notes
  • Number of PWM and Timer modules differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • PWM_TIMn_Init

    Prototype

    unsigned int PWM_TIMn_Init(unsigned long freq_hz);

    Description

    Initializes the Timer module in PWM mode for ST MCUs.

    Parameters
    • freq_hz: PWM frequency in Hz (refer to device datasheet for correct values in respect with Fosc).
    Returns

    This function returns calculated timer period.

    Requires
    • MCU must support the Timer module in PWM mode.
    • PWM library routines require you to specify the Timer module you want to use. To select the desired Timer module, simply change the letter n in the routine prototype for a number from 0 to 17.
    Example
    ratio = PWM_TIM0_Init(25000);                       
    PWM_TIM0_Set_Duty(ratio/4,_PWM_INVERTED_ENABLE);
    PWM_TIM0_Start();
    
    Notes
    • Number of Timer modules differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

    PWM_CCPx_Set_Duty

    Prototype

    void PWM_CCPx_Set_Duty(unsigned int duty, char inverted);

    Description

    The function changes PWM duty ratio for Stellaris Cortex M3 MCUs.

    Parameters
    • duty: PWM duty ratio. Valid values: 0 to timer period returned by the PWM_CCPx_Init function.
    • inverted: inverted and non inverted PWM signals. Valid values :

      Inverted parameter
      Description Predefined library const
      Inverted PWM signal _PWM_INVERTED_ENABLE
      Non-inverted PWM signal _PWM_INVERTED_DISABLE
    Returns

    Nothing.

    Requires
    • MCU must have the HW PWM Module.
    • PWM module must be properly initialized. See PWM_CCPx_Init routine.
    • PWM library routines require you to specify the module you want to use. To select the desired PWM module, simply change the letter x in the routine prototype for a number from 0 to 7.
    Example
    // Set channel 1 duty ratio to 50%:
    unsigned int pwm_period1;
    ...
    PWM_CCP0_Set_Duty(pwm_period1/2, 1);
    
    Notes

    Number of available PWM channels depends on MCU. Refer to MCU datasheet for details.

    PWM_TnCCPx_Set_Duty

    Prototype

    procedure PWM_CCPx_Set_Duty(duty : word; inverted : byte);

    Description

    The function changes PWM duty ratio for Stellaris Cortex M4 MCUs.

    Parameters
    • duty: PWM duty ratio. Valid values: 0 to timer period returned by the PWM_CCPx_Init function.
    • inverted: inverted and non inverted PWM signals. Valid values :

      Inverted parameter
      Description Predefined library const
      Inverted PWM signal _PWM_INVERTED_ENABLE
      Non-inverted PWM signal _PWM_INVERTED_DISABLE
    Returns

    Nothing.

    Requires
    • MCU must have the HW PWM Module.
    • Prior to PWM module initialization user must set appropriate pins as output. Please, see GPIO_Config routine.
    • PWM module must be properly initialized. See PWM_TnCCPx_Init routine.
    • PWM library routines require you to specify the module you want to use. To select the desired PWM module, simply change the letter x in the routine prototype for a number 0 or 1 to select desired PWM module and letter n from 0 to 5 depending on the used timer module.
    Example
    // Set channel 1 duty ratio to 50%:
    unsigned int pwm_period1;
    ...
    PWM_T0CCP0_Set_Duty(pwm_period1/2, 1);
    
    Notes
  • Number of PWM and Timer modules differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • PWM_TIMn_Set_Duty

    Prototype

    void PWM_TIMn_Set_Duty(unsigned int duty, char inverted, char channel);

    Description

    The function changes duty ratio for Timer module in PWM mode for ST MCUs.

    Parameters
    • duty: PWM duty ratio. Valid values: 0 to timer period returned by the PWM_TIMn_Init function.
    • inverted: inverted and non inverted PWM signals. Valid values :
      Inverted parameter
      Description Predefined library const
      Inverted PWM signal _PWM_INVERTED
      Non-inverted PWM signal _PWM_NON_INVERTED
    • channel: desired PWM channel :
      Channel parameter
      Description Predefined library const
      Channel 1 _PWM_CHANNEL1
      Channel 2 _PWM_CHANNEL2
      Channel 3 _PWM_CHANNEL3
      Channel 4 _PWM_CHANNEL4
    Returns

    Nothing.

    Requires
    • MCU must support the Timer module in PWM mode.
    • PWM mode must be properly initialized. See PWM_TIMn_Init routine.
    • PWM library routines require you to specify the Timer module you want to use. To select the desired Timer module, simply change the letter n in the routine prototype for a number from 0 to 17.
    Example
    // Set channel 1 duty ratio to 50%:
    unsigned int pwm_period1;
    ...
    PWM_TIM0_Set_Duty(pwm_period1/2, _PWM_NON_INVERTED, _PWM_CHANNEL1);
    
    Notes

    Number of available Timer modules and PWM channels depends on MCU. Refer to MCU datasheet for details.

    PWM_CCPx_Start

    Prototype

    void PWM_CCPx_Start();

    Description

    Starts appropriate PWM module for Stellaris Cortex M3 MCUs.

    Parameters

    None.

    Returns

    Nothing.

    Requires
    • MCU must have the HW PWM Module.
    • PWM channel must be properly initialized. See PWM_CCPx_Init routine.
    • PWM library routines require you to specify the module you want to use. To select the desired PWM module, simply change the letter x in the routine prototype for a number from 0 to 7.
    Example
    // Start PWM module
    PWM_CCP0_Start();
    Notes

    Number of available PWM channels depends on MCU. Refer to MCU datasheet for details.

    PWM_TnCCPx_Start

    Prototype

    void PWM_TnCCPx_Start();

    Description

    Starts appropriate PWM module for Stellaris Cortex M4 MCUs.

    Parameters

    None.

    Returns

    Nothing.

    Requires
    • MCU must have the HW PWM Module.
    • Prior to PWM module initialization user must set appropriate pins as output. Please, see GPIO_Config routine.
    • PWM channel must be properly initialized. See PWM_TnCCPx_Init routine.
    • PWM library routines require you to specify the module you want to use. To select the desired PWM module, simply change the letter x in the routine prototype for a number 0 or 1 to select desired PWM module and letter n from 0 to 5 depending on the used timer module.
    Example
    // Start PWM module
    PWM_T0CCP0_Start();
    Notes
    • Number of PWM and Timer modules differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

    PWM_TIMn_Start

    Prototype

    void PWM_TIMn_Start(char channel, const module_Struct *module);

    Description

    Starts appropriate Timer module in PWM mode for ST MCUs.

    Parameters
    • channel: desired PWM channel :
      Channel parameter
      Description Predefined library const
      Channel 1 _PWM_CHANNEL1
      Channel 2 _PWM_CHANNEL2
      Channel 3 _PWM_CHANNEL3
      Channel 4 _PWM_CHANNEL4
    • module: appropriate module pinout, see the following table :
      PWM Module Pinouts for ST
      Channel 1 Channel 2 Channel 3 Channel 4
      Timer1 _GPIO_MODULE_TIM1_CH1_PA8 _GPIO_MODULE_TIM1_CH2_PA9 _GPIO_MODULE_TIM1_CH3_PA10 _GPIO_MODULE_TIM1_CH4_PA11
      _GPIO_MODULE_TIM1_CH1_PE9 _GPIO_MODULE_TIM1_CH2_PE11 _GPIO_MODULE_TIM1_CH3_PE13 _GPIO_MODULE_TIM1_CH4_PE14
      Timer2 _GPIO_MODULE_TIM2_CH1_PA0 _GPIO_MODULE_TIM2_CH2_PA1 _GPIO_MODULE_TIM2_CH3_PA2 _GPIO_MODULE_TIM2_CH4_PA3
      _GPIO_MODULE_TIM2_CH1_PA5 _GPIO_MODULE_TIM2_CH2_PB3 _GPIO_MODULE_TIM2_CH3_PB10 _GPIO_MODULE_TIM2_CH4_PB11
      Timer3 _GPIO_MODULE_TIM3_CH1_PA6 _GPIO_MODULE_TIM3_CH2_PA7 _GPIO_MODULE_TIM3_CH3_PB0 _GPIO_MODULE_TIM3_CH4_PB1
      _GPIO_MODULE_TIM3_CH1_PB4 _GPIO_MODULE_TIM3_CH2_PB5 _GPIO_MODULE_TIM3_CH3_PC8 _GPIO_MODULE_TIM3_CH4_PC9
      _GPIO_MODULE_TIM3_CH1_PC6 _GPIO_MODULE_TIM3_CH2_PC7
      Timer4 _GPIO_MODULE_TIM4_CH1_PB6 _GPIO_MODULE_TIM4_CH2_PB7 _GPIO_MODULE_TIM4_CH3_PB8 _GPIO_MODULE_TIM4_CH4_PB9
      _GPIO_MODULE_TIM4_CH1_PD12 _GPIO_MODULE_TIM4_CH2_PD13 _GPIO_MODULE_TIM4_CH3_PD14 _GPIO_MODULE_TIM4_CH4_PD15
      Timer5 _GPIO_MODULE_TIM5_CH1_PH10 _GPIO_MODULE_TIM5_CH2_PA1 _GPIO_MODULE_TIM5_CH3_PA2 _GPIO_MODULE_TIM5_CH4_PA3
      _GPIO_MODULE_TIM5_CH2_PH11 _GPIO_MODULE_TIM5_CH3_PH12 _GPIO_MODULE_TIM5_CH4_PI0
      Timer8 _GPIO_MODULE_TIM8_CH1_PC6 _GPIO_MODULE_TIM8_CH2_PC7 _GPIO_MODULE_TIM8_CH3_PC8 _GPIO_MODULE_TIM8_CH4_PC9
      _GPIO_MODULE_TIM8_CH1_PI5 _GPIO_MODULE_TIM8_CH2_PI6 _GPIO_MODULE_TIM8_CH3_PI7 _GPIO_MODULE_TIM8_CH4_PI2
      Timer9 _GPIO_MODULE_TIM9_CH1_PA2 _GPIO_MODULE_TIM9_CH2_PA3
      _GPIO_MODULE_TIM9_CH1_PE5 _GPIO_MODULE_TIM9_CH2_PE6
      Timer10 _GPIO_MODULE_TIM10_CH1_PB8
      _GPIO_MODULE_TIM10_CH1_PF6
      Timer11 _GPIO_MODULE_TIM11_CH1_PB9
      _GPIO_MODULE_TIM11_CH1_PF7
      Timer12 _GPIO_MODULE_TIM12_CH1_PB14 _GPIO_MODULE_TIM12_CH2_PB15
      _GPIO_MODULE_TIM12_CH1_PH6 _GPIO_MODULE_TIM12_CH2_PH9
      Timer13 _GPIO_MODULE_TIM13_CH1_PA6
      _GPIO_MODULE_TIM13_CH1_PF8
      Timer14 _GPIO_MODULE_TIM14_CH1_PA7
      _GPIO_MODULE_TIM14_CH1_PF9
      Timer15 _GPIO_MODULE_TIM15_CH1_PA2 _GPIO_MODULE_TIM15_CH2_PA3
      _GPIO_MODULE_TIM15_CH1_PB14 _GPIO_MODULE_TIM15_CH2_PB15
      Timer16 _GPIO_MODULE_TIM16_CH1_PB8
      _GPIO_MODULE_TIM16_CH1_PA6
      Timer17 _GPIO_MODULE_TIM17_CH1_PB9
      _GPIO_MODULE_TIM17_CH1_PA7
    Returns

    Nothing.

    Requires
    • MCU must support the Timer module in PWM mode.
    • PWM mode must be properly initialized. See PWM_TIMn_Init routine.
    • PWM library routines require you to specify the Timer module you want to use. To select the desired Timer module, simply change the letter n in the routine prototype for a number from 0 to 17.
    Example
    // Start PWM module
    PWM_TIM0_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
    Notes

    Number of available Timer modules and PWM channels depends on MCU. Refer to MCU datasheet for details.

    PWM_CCPx_Stop

    Prototype

    void PWM_CCPx_Stop();

    Description

    Stops appropriate PWM module for Stellaris Cortex M3 MCUs.

    Parameters

    None.

    Returns

    Nothing.

    Requires
    • MCU must have the HW PWM Module.
    • PWM channel must be properly initialized. See PWM_CCPx_Init routine.
    • PWM library routines require you to specify the module you want to use. To select the desired PWM module, simply change the letter x in the routine prototype for a number from 0 to 7.
    Example
    // Stop PWM module
    PWM_CCP0_Stop();
    Notes
    • Number of PWM modules differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

    PWM_TnCCPx_Stop

    Prototype

    void PWM_TnCCPx_Stop();

    Description

    Stops appropriate PWM module for Stellaris Cortex M4 MCUs.

    Parameters

    None.

    Returns

    Nothing.

    Requires
    • MCU must have the HW PWM Module.
    • Prior to PWM module initialization user must set appropriate pins as output. Please, see GPIO_Config routine.
    • PWM channel must be properly initialized. See PWM_TnCCPx_Init routine.
    • PWM library routines require you to specify the module you want to use. To select the desired PWM module, simply change the letter x in the routine prototype for a number 0 or 1 to select desired PWM module and letter n from 0 to 5 depending on the used timer module.
    Example
    // Stop PWM module
    PWM_T0CCP0_Stop();
    Notes
    • Number of PWM and Timer modules differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

    PWM_TIMn_Stop

    Prototype

    void PWM_TIMn_Stop(char channel);

    Description

    Stops appropriate Timer module in PWM mode for ST MCUs.

    Parameters
    • channel: desired PWM channel :
      Channel parameter
      Description Predefined library const
      Channel 1 _PWM_CHANNEL1
      Channel 2 _PWM_CHANNEL2
      Channel 3 _PWM_CHANNEL3
      Channel 4 _PWM_CHANNEL4
    Returns

    Nothing.

    Requires
    • MCU must support the Timer module in PWM mode.
    • PWM mode must be properly initialized. See PWM_TIMn_Init routine.
    • PWM library routines require you to specify the Timer module you want to use. To select the desired Timer module, simply change the letter n in the routine prototype for a number from 0 to 17.
    Example
    // Start PWM module
    PWM_TIM0_Stop(_PWM_CHANNEL1);
    Notes

    Number of available Timer modules and PWM channels depends on MCU. Refer to MCU datasheet for details.

    Library Example

    The example changes PWM duty ratio on channels 1 and 2 continuously. If LEDs are connected to channels 1 and 2, a gradual change of emitted light will be noticeable.

    Stellaris

    unsigned int current_duty, old_duty, current_duty1, old_duty1;
    unsigned int pwm_period1, pwm_period2;
    
    void InitMain() {
      GPIO_Digital_Input (&GPIO_PORTA, _GPIO_PINMASK_ALL); // configure GPIO_PORTA pins as input
    }
    
    void main() {
      InitMain();
      current_duty  = 100;                        // initial value for current_duty
      current_duty1 = 100;                        // initial value for current_duty1
    
      pwm_period1 = PWM_CCP0_Init(5000, &_GPIO_MODULE_CCP0_B0);
      pwm_period2 = PWM_CCP3_Init(5000, &_GPIO_MODULE_CCP3_B2);
    
      PWM_CCP0_Start();
      PWM_CCP3_Start();
    
      PWM_CCP0_Set_Duty(current_duty,  _PWM_INVERTED_DISABLE);  // Set current duty for PWM_CCP0
      PWM_CCP3_Set_Duty(current_duty1, _PWM_INVERTED_DISABLE);  // Set current duty for PWM_CCP3
    
      while (1) {                                // endless loop
        if (GPIO_PORTA_DATA.B0) {                // button on RA0 pressed
          Delay_ms(1);
          current_duty = current_duty + 5;       // increment current_duty
          if (current_duty > pwm_period1) {      // if we increase current_duty greater then possible pwm_period1 value
            current_duty = 0;                    // reset current_duty value to zero
          }
          PWM_CCP0_Set_Duty(current_duty,  _PWM_INVERTED_DISABLE); // set newly acquired duty ratio
         }
    
        if (GPIO_PORTA_DATA.B1) {                // button on RA1 pressed
          Delay_ms(1);
          current_duty = current_duty - 5;       // decrement current_duty
          if (current_duty > pwm_period1) {      // if we decrease current_duty greater then possible pwm_period1 value (overflow)
            current_duty = pwm_period1;          // set current_duty to max possible value
          }
          PWM_CCP0_Set_Duty(current_duty,  _PWM_INVERTED_DISABLE); // set newly acquired duty ratio
         }
    
        if (GPIO_PORTA_DATA.B2) {                // button on RA2 pressed
          Delay_ms(1);
          current_duty1 = current_duty1 + 5;     // increment current_duty
          if (current_duty1 > pwm_period2) {     // if we increase current_duty1 greater then possible pwm_period2 value
            current_duty1 = 0;                   // reset current_duty1 value to zero
          }
          PWM_CCP3_Set_Duty(current_duty1, _PWM_INVERTED_DISABLE);       // set newly acquired duty ratio
         }
    
        if (GPIO_PORTA_DATA.B3) {                // button on RA3 pressed
          Delay_ms(1);
          current_duty1 = current_duty1 - 5;     // decrement current_duty
          if (current_duty1 > pwm_period2) {     // if we decrease current_duty1 greater then possible pwm_period1 value (overflow)
            current_duty1 = pwm_period2;         // set current_duty to max possible value
          }
          PWM_CCP3_Set_Duty(current_duty1, _PWM_INVERTED_DISABLE);
         }
    
        Delay_ms(1);                             // slow down change pace a little
      }
    }
    

    STM32

    unsigned int current_duty, old_duty, current_duty1, old_duty1;
    unsigned int pwm_period1, pwm_period2;
    
    void InitMain() {
      GPIO_Digital_Input (&GPIOA_BASE, _GPIO_PINMASK_3 | _GPIO_PINMASK_4 | _GPIO_PINMASK_5 | _GPIO_PINMASK_6); // configure PORTA pins as input
    }
    
    void main() {
      InitMain();
      current_duty  = 100;                        // initial value for current_duty
      current_duty1 = 100;                        // initial value for current_duty1
    
      pwm_period1 = PWM_TIM1_Init(5000);
      pwm_period2 = PWM_TIM4_Init(5000);
    
      PWM_TIM1_Set_Duty(current_duty,  _PWM_NON_INVERTED, _PWM_CHANNEL1);  // Set current duty for PWM_TIM1
      PWM_TIM4_Set_Duty(current_duty1, _PWM_NON_INVERTED, _PWM_CHANNEL2);  // Set current duty for PWM_TIM4
    
      PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PE9);
      PWM_TIM4_Start(_PWM_CHANNEL2, &_GPIO_MODULE_TIM4_CH2_PD13);
    
      while (1) {                                // endless loop
        if (GPIOA_IDR.B3) {                // button on RA3 pressed
          Delay_ms(1);
          current_duty = current_duty + 5;       // increment current_duty
          if (current_duty > pwm_period1) {      // if we increase current_duty greater then possible pwm_period1 value
            current_duty = 0;                    // reset current_duty value to zero
          }
          PWM_TIM1_Set_Duty(current_duty,  _PWM_NON_INVERTED, _PWM_CHANNEL1); // set newly acquired duty ratio
         }
    
        if (GPIOA_IDR.B4) {                // button on RA4 pressed
          Delay_ms(1);
          current_duty = current_duty - 5;       // decrement current_duty
          if (current_duty > pwm_period1) {      // if we decrease current_duty greater then possible pwm_period1 value (overflow)
            current_duty = pwm_period1;          // set current_duty to max possible value
          }
          PWM_TIM1_Set_Duty(current_duty,  _PWM_NON_INVERTED, _PWM_CHANNEL1); // set newly acquired duty ratio
         }
    
        if (GPIOA_IDR.B5) {                // button on RA5 pressed
          Delay_ms(1);
          current_duty1 = current_duty1 + 5;     // increment current_duty
          if (current_duty1 > pwm_period2) {     // if we increase current_duty1 greater then possible pwm_period2 value
            current_duty1 = 0;                   // reset current_duty1 value to zero
          }
          PWM_TIM4_Set_Duty(current_duty1, _PWM_NON_INVERTED, _PWM_CHANNEL2);       // set newly acquired duty ratio
         }
    
        if (GPIOA_IDR.B6) {                // button on RA6 pressed
          Delay_ms(1);
          current_duty1 = current_duty1 - 5;     // decrement current_duty
          if (current_duty1 > pwm_period2) {     // if we decrease current_duty1 greater then possible pwm_period1 value (overflow)
            current_duty1 = pwm_period2;         // set current_duty to max possible value
          }
          PWM_TIM4_Set_Duty(current_duty1, _PWM_NON_INVERTED, _PWM_CHANNEL2);
         }
    
        Delay_ms(1);                             // slow down change pace a little
      }
    }
    
    Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
    What do you think about this topic ? Send us feedback!
    Want more examples and libraries? 
    Find them on LibStock - A place for the code