PWM Library

CMO module is available with a number of AVR MCUs. mikroC PRO for AVR provides library which simplifies using PWM HW Module.

  Important :

Library Routines

PWMx_Init

Prototype

void PWMx_Init(unsigned short wave_mode, unsigned short prescaler, unsigned short inverted, unsigned short duty);

Returns

Nothing.

Description

Initializes the PWM module.

Parameters :

  • wave_mode: desired PWM mode (Phase Correct or Fast PWM depending on the chosen MCU).

    Value Description
    _PWM1_FAST_MODE Fast mode for PWM1
    _PWM1_FAST_MODE_OCRA Fast mode for PWM1
    _PWM1_PHASE_CORRECT_MODE Phase Correct mode for PWM1
    _PWM1_PHASE_CORRECT_MODE_OCRA Phase Correct mode for PWM1
    _PWM2_FAST_MODE Fast mode for PWM2
    _PWM2_PHASE_CORRECT_MODE Phase Correct mode for PWM2

  • prescaler: prescale value N = 1,8,64,256 or 1024 (some modules support 32 and 128, but for this you will need to check the datasheet for the desired MCU).

    Value Description
    _PWM1_PRESCALER_1 Sets prescaler value to 1 (No prescaling).
    _PWM1_PRESCALER_8 Sets prescaler value to 8.
    _PWM1_PRESCALER_32 Sets prescaler value to 32.
    This value is not available for every MCU, please consult appropriate datasheet.
    _PWM1_PRESCALER_64 Sets prescaler value to 64.
    _PWM1_PRESCALER_128 Sets prescaler value to 128.
    This value is not available for every MCU, please consult appropriate datasheet.
    _PWM1_PRESCALER_256 Sets prescaler value to 256.
    _PWM1_PRESCALER_1024 Sets prescaler value to 1024.
    _PWM2_PRESCALER_1 Sets prescaler value to 1 for the second PWM module.
    _PWM2_PRESCALER_8 Sets prescaler value to 8 for the second PWM module.
    _PWM2_PRESCALER_32 Sets prescaler value to 32 for the second PWM module.
    This value is not available for every MCU, please consult appropriate datasheet.
    _PWM2_PRESCALER_64 Sets prescaler value to 64 for the second PWM module.
    _PWM2_PRESCALER_128 Sets prescaler value to 128 for the second PWM module.
    This value is not available for every MCU, please consult appropriate datasheet.
    _PWM2_PRESCALER_256 Sets prescaler value to 256 for the second PWM module.
    _PWM2_PRESCALER_1024 Sets prescaler value to 1024 for the second PWM module.

  • inverted: inverted or non inverted PWM signal.

    Value Description
    _PWM1_INVERTED PWM1 inverted mode
    _PWM1_NON_INVERTED PWM1 noninverted mode
    _PWM2_INVERTED PWM2 inverted mode
    _PWM2_NON_INVERTED PWM2 noninverted mode

  • duty: sets duty ratio from 0 to 255.

PWMx_Init must be called before using other functions from PWM Library.

Requires

You need a CMO on the given MCU (that supports PWM). Before calling this routine you must set the output pin for the PWM (according to the datasheet).

Example

Initialize PWM module:

PWM1_Init(_PWM1_FAST_MODE, _PWM1_PRESCALER_8, _PWM1_NON_INVERTED, 127);

PWM_xn_Init

Prototype

unsigned int PWM_xn_Init(unsigned long int freq_hz, unsigned short wave_mode);

Returns

Calculated timer period (value written in PER register) for XMEGA family of MCUs.

Description

Initializes the PWM module. Change the xn in the routine prototype with C0, C1, D0, D1, E0, E1, F0 or F1 (MCU dependent).

Parameters :

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

    Value Description
    _PWM_SINGLE_SLOPE Single Slope Mode
    _PWM_DUAL_SLOPE Dual Slope Mode

Requires

You need a CMO on the given MCU (that supports PWM). Before calling this routine you must set the output pin for the PWM (according to the datasheet).

Example

Initialize PWM module:

PWM_C0_Init(1000, _PWM_SINGLE_SLOPE);

PWMx_Set_Duty

Prototype

void PWMx_Set_Duty(unsigned short duty);

// for ATtiny441/481

void PWMx_Set_Duty(unsigned short duty, channel);

Returns

Nothing.

Description

Changes PWM duty ratio. Parameter duty takes values from 0 to 255, where 0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*255)/100.

Parameters :

  • duty: sets duty ratio from 0 to 255.
  • channel: set desired channel (only for ATtiny441/481). Valid values :
    Value Description
    _PWM1_CH_A Channel A
    _PWM1_CH_B Channel B
Requires

PWM module must to be initialised (PWMx_Init) before using this function.

Example
PWM1_Set_Duty(192);

PWM_xn_Set_Duty

Prototype

void PWM_xn_Set_Duty(unsigned int duty_ratio, char inverted, char channel);

Returns

Nothing.

Description

Changes PWM duty ratio. Parameter duty takes values from 0 to 255, where 0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*255)/100.

Parameters :

  • duty_ratio: PWM duty ratio. Valid values: 0 to timer period returned by the PWM_xn_Init function.
  • inverted: inverted or non inverted PWM signal.

    Value Description
    _PWM_NON_INVERTED Non Inverted Mode
    _PWM_INVERTED Inverted Mode

  • channel: PWM channel. Values :

    Value Description
    _CCA_CHANNEL CCA channel
    _CCB_CHANNEL CCB channel
    _CCC_CHANNEL CCC (this channel not available from PWM_x1_Set_Duty.
    _CCD_CHANNEL CCD (this channel not available from PWM_x1_Set_Duty.

Requires

PWM module must to be initialised (PWM_xn_Init) before using this function.

Example
PWM_C0_Set_Duty(192,_PWM_NON_INVERTED,_CCC_CHANNEL);

PWMx_Start

Prototype

void PWMx_Start();

Returns

Nothing.

Description

Starts PWM.

Requires

MCU must have CMO module to use this library. PWMx_Init must be called before using this routine.

Example
PWM1_Start();

PWM_xn_Start

Prototype

void PWM_xn_Start(char channel);

Returns

Nothing.

Description

Starts PWM.

Parameters :

  • channel: Starts PWM at requested channel. Values :

    Value Description
    _CCA_CHANNEL CCA channel
    _CCB_CHANNEL CCB channel
    _CCD_CHANNEL CCC (this channel not available from PWM_x1_Start.
    _CCD_CHANNEL CCD (this channel not available from PWM_x1_Start.

Requires

MCU must have CMO module to use this library.

PWM module must to be initialised (PWM_xn_Init) before using this function.

Example
PWM_C0_Start(_CCD_CHANNEL);

PWMx_Stop

Prototype

void PWMx_Stop();

Returns

Nothing.

Description

Stops the PWM.

Requires

MCU must have CMO module to use this library. PWMx_Init and PWMx_Start must be called before
using this routine using this routine, otherwise it will have no effect as the PWM module is not running.

Example
PWM1_Stop();

PWM1_Enable

Prototype

void PWM1_Enable(unsigned short inverted, channel);

Returns

Nothing.

Description

Enables the PWM on desired channel.

Parameters :

  • inverted: PWM mode. Values :
    Value Description
    _PWM1_INVERTED PWM inverted mode
    _PWM1_NON_INVERTED PWM non-inverted mode
  • channel: set desired channel. Valid values :
    Value Description
    _PWM1_CH_A Channel A
    _PWM1_CH_B Channel B
Requires

MCU must have CMO module to use this library. PWMx_Init must be called before
using this routine otherwise it will have no effect as the PWM module is not running.

This routine is valid only for ATtiny441/481 MCUs.

Example
PWM1_Enable(_PWM1_NON_INVERTED, _PWM1_CH_A);

PWMxA_Enable

Prototype

void PWMxA_Enable(unsigned short inverted);

Returns

Nothing.

Description

Enables the PWM on channel A.

Parameters :

  • inverted: PWM mode. Values :

    Value Description
    _PWM1_INVERTED PWM1 module inverted mode
    _PWM1_NON_INVERTED PWM1 module non-inverted mode
    _PWM2_INVERTED PWM2 module inverted mode
    _PWM2_NON_INVERTED PWM2 module non-inverted mode

Requires

MCU must have CMO module to use this library. PWMx_Init must be called before
using this routine otherwise it will have no effect as the PWM module is not running.

Example
PWM1A_Enable(_PWM1_NON_INVERTED);

PWMxB_Enable

Prototype

void PWMxB_Enable(unsigned short inverted, unsigned short modifyTop);

Returns

Nothing.

Description

Enables the PWM on channel B.

Parameters :

  • inverted: PWM mode. Values :

    Value Description
    _PWM1_INVERTED PWM1 module inverted mode
    _PWM1_NON_INVERTED PWM1 module non-inverted mode
    _PWM2_INVERTED PWM2 module inverted mode
    _PWM2_NON_INVERTED PWM2 module non-inverted mode

  • modifyTop: counter value. Values :
    Value Description
    _PWM1B_TOP_VAL_OCRA TOP value stored in the OCR0A Register (when using this value, channel A cannot be used).
    _PWM1B_TOP_VAL_MAX Maximal TOP value (255).
    _PWM2B_TOP_VAL_OCRA TOP value stored in the OCR0A Register (when using this value, channel A cannot be used).
    _PWM2B_TOP_VAL_MAX Maximal TOP value (255).
Requires

MCU must have CMO module to use this library. PWMx_Init must be called before
using this routine otherwise it will have no effect as the PWM module is not running.

Example
PWM1B_Enable(_PWM1_NON_INVERTED, _PWM1B_TOP_VAL_MAX);

PWM1_Disable

Prototype

void PWM1_Disable(unsigned short channel);

Returns

Nothing.

Description

Disables the PWM on desired channel .
Note that the timer counter will not be stopped by this function.

Parameters :

  • channel: set desired channel. Valid values :
    Value Description
    _PWM1_CH_A Channel A
    _PWM1_CH_B Channel B
Requires

MCU must have CMO module to use this library. PWMx_Init must be called before
using this routine, otherwise it will have no effect as the PWM module is not running.

This routine is valid only for ATtiny441/481 MCUs.

Example
PWM1_Disable(_PWM1_CH_A);

PWMxA_Disable

Prototype

void PWMxA_Disable();

Returns

Nothing.

Description

Disables the PWM on channel A.
Note that the timer counter will not be stopped by this function.

Requires

MCU must have CMO module to use this library. PWMx_Init must be called before
using this routine, otherwise it will have no effect as the PWM module is not running.

Example
PWM1A_Disable();

PWMxB_Disable

Prototype

void PWMxB_Disable();

Returns

Nothing.

Description

Disables the PWM on channel B.
Note that the timer counter will not be stopped by this function.

Requires

MCU must have CMO module to use this library. PWMx_Init must be called before
using this routine, otherwise it will have no effect as the PWM module is not running.

Example
PWM1B_Disable();

PWMxA_Set_Duty

Prototype

void PWMxA_Set_Duty(unsigned short duty);

Returns

Nothing.

Description

Changes PWM duty ratio on channel A. Parameter duty takes values from 0 to 255, where 0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*255)/100.

Parameters :

  • duty: sets duty ratio from 0 to 255.
Requires

MCU must have CMO module to use this library. PWMx_Init and PWMx_Start must be called before
using this routine using this routine, otherwise it will have no effect as the PWM module is not running.

Example
PWM1A_Set_Duty(192);

PWMxB_Set_Duty

Prototype

void PWMxB_Set_Duty(unsigned short duty);

Returns

Nothing.

Description

Changes PWM duty ratio on channel B. Parameter duty takes values from 0 to 255, where 0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*255)/100.

Parameters :

  • duty: sets duty ratio from 0 to 255.
Requires

MCU must have CMO module to use this library. PWMx_Init and PWMx_Start must be called before
using this routine using this routine, otherwise it will have no effect as the PWM module is not running.

Example
PWM1B_Set_Duty(192);

PWM1_Set_Output

Prototype

void PWM1_Set_Output(unsigned short chOutputSelectA, chOutputSelectB);

Returns

Nothing.

Description

This routine sets TOCCn outputs for channels A and B. Valid only for ATtiny441/481 MCUs.

Parameters :

  • chOutputSelectA: TOCCn output for channel A. Valid values :
    Value Description
    _PWM1_CH_A_OUT_TOCC1 Sets TOCC1 output for channel A
    _PWM1_CH_A_OUT_TOCC3 Sets TOCC3 output for channel A
    _PWM1_CH_A_OUT_TOCC5 Sets TOCC5 output for channel A
    _PWM1_CH_A_OUT_TOCC7 Sets TOCC7 output for channel A
    _PWM1_CH_A_OUT_NONE Channel A will have no output.

  • chOutputSelectB: TOCCn output for channel B. Valid values :
    Value Description
    _PWM1_CH_B_OUT_TOCC0 Sets TOCC0 output for channel B
    _PWM1_CH_B_OUT_TOCC2 Sets TOCC2 output for channel B
    _PWM1_CH_B_OUT_TOCC4 Sets TOCC4 output for channel B
    _PWM1_CH_B_OUT_TOCC6 Sets TOCC6 output for channel B
    _PWM1_CH_B_OUT_NONE Channel B will have no output.
Requires

MCU must have CMO module to use this library. PWMx_Init must be called before
using this routine, otherwise it will have no effect as the PWM module is not running.

Example
PWM1_Set_Output(_PWM1_CH_A_OUT_TOCC1, _PWM1_CH_B_OUT_NONE);

PWM_xn_Stop

Prototype

void PWM_xn_Stop(char channel);

Returns

Nothing.

Description

Stops the PWM.

Parameters :

  • channel: Stops PWM at requested channel. Values :

    Value Description
    _CCA_CHANNEL CCA channel
    _CCB_CHANNEL CCB channel
    _CCC_CHANNEL CCC channel (this channel not available from PWM_x1_Stop.
    _CCD_CHANNEL CCD channel (this channel not available from PWM_x1_Stop.

Requires

MCU must have CMO module to use this library. PWM_xn_Init and PWM_xn_Start must be called before
using this routine otherwise it will have no effect as the PWM module is not running.

Example
PWM_C0_Stop(_CCC_CHANNEL);

Library Example

The example changes PWM duty ratio on pin PB3 continually. If LED is connected to PB3, you can observe the gradual change of emitted light.

Copy Code To ClipboardCopy Code To Clipboard
char current_duty;
char current_duty1;

void main(){

  DDB0_bit = 0;                   // Set PORTB pin 0 as input
  DDB1_bit = 0;                   // Set PORTB pin 1 as input

  DDC0_bit = 0;                   // Set PORTC pin 0 as input
  DDC1_bit = 0;                   // Set PORTC pin 1 as input

  current_duty  = 32;             // initial value for current_duty
  current_duty1 = 32;             // initial value for current_duty

  DDB3_bit = 1;                   // Set PORTB pin 3 as output pin for the PWM (according to datasheet)
  DDD7_bit = 1;                   // Set PORTD pin 7 as output pin for the PWM (according to datasheet)
  
  PWM1_Init(_PWM1_FAST_MODE, _PWM1_PRESCALER_8, _PWM1_NON_INVERTED, current_duty);
  PWM2_Init(_PWM2_FAST_MODE, _PWM2_PRESCALER_8, _PWM2_NON_INVERTED, current_duty1);

  do {
    if (PINB0_bit) {                            // Detect if PORTB pin 0 is pressed
      Delay_ms(40);                             // Small delay to avoid deboucing effect
      current_duty++;                           // Increment duty ratio
      PWM1_Set_Duty(current_duty);               // Set incremented duty
    }
    else
      if (PINB1_bit) {                          // Detect if PORTB pin 1 is pressed
        Delay_ms(40);                           // Small delay to avoid deboucing effect
        current_duty--;                         // Decrement duty ratio
        PWM1_Set_Duty(current_duty);             // Set decremented duty ratio
      }
      else
        if (PINC0_bit) {                        // Detect if PORTC pin 0 is pressed
          Delay_ms(40);                         // Small delay to avoid deboucing effect
          current_duty1++;                      // Increment duty ratio
          PWM2_Set_Duty(current_duty1);         // Set incremented duty
        }
        else
          if (PINC1_bit) {                      // Detect if PORTC pin 1 is pressed
            Delay_ms(40);                       // Small delay to avoid deboucing effect
            current_duty1--;                    // Decrement duty ratio
            PWM2_Set_Duty(current_duty1);       // Set decremented duty ratio
          }

  } while(1);                                   // Endless loop

}

HW Connection

PWM demonstration

PWM demonstration

Copyright (c) 2002-2017 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