PWM Remappable Library


mikroC PRO for PIC provides a library which simplifies usage of the PWM Remappable Module. This module is available for the MCUs with remappable pins, please check appropriate datasheet.


  Important :

Library Routines

PWMx_Remappable_Init

Prototype

void PWMx_Remappable_Init(long freq);

Returns

Nothing.

Description

Initializes the PWM module with duty ratio 0. Parameter freq is a desired PWM frequency in Hz (refer to device data sheet for correct values in respect with Fosc).

This routine needs to be called before using other functions from PWM Remappable Library.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired PWM module, simply change the letter x in the prototype for the appropriate module number.
Requires

MCU must have Remappable CCP module.

  Note :
  • Calculation of the PWM frequency value is carried out by the compiler, as it would produce a relatively large code if performed on the library level.
    Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.
Example

Initialize PWM module at 5KHz:

PWM4_Remappable_Init(5000);

PWMx_Remappable_Init_Advanced

Prototype

void PWMx_Remappable_Init_Advanced(unsigned long PWM_Freq, unsigned int pwm_conf, unsigned int clock_conf);

Returns

Nothing.

Description

Initializes the PWM module with custom parameters.

  • PWM_Freq: desired PWM frequency in Hz (refer to device data sheet for correct values in respect with Fosc).
  • pwm_conf: PWM module configuration. Valid value is derived by OR-ing the following constants :
    PWM mode of operation
    Value Description
    _PWM_MODE_STANDARD Standard PWM mode.
    _PWM_MODE_SET_ON_MATCH Set On Match mode.
    _PWM_MODE_TOGGLE_ON_MATCH Toggle On Match mode.
    _PWM_MODE_CENTER_ALIGN Center Align mode.

    PWM output polarity
    Value Description
    _PWM_OUT_ACTIVE_LOW PWM output active state is low.
    _PWM_OUT_ACTIVE_HIGH PWM output active state is high.

  • clock_conf:
    Clock prescaler
    Value Description
    _PWM_PRESCALER_DIV_1 No prescaler.
    _PWM_PRESCALER_DIV_2 Divide clock source by 2.
    _PWM_PRESCALER_DIV_4 Divide clock source by 4.
    _PWM_PRESCALER_DIV_8 Divide clock source by 8.
    _PWM_PRESCALER_DIV_16 Divide clock source by 16.
    _PWM_PRESCALER_DIV_32 Divide clock source by 23.
    _PWM_PRESCALER_DIV_64 Divide clock source by 64.
    _PWM_PRESCALER_DIV_128 Divide clock source by 128.

    Clock source
    Value Description
    _PWM_CLOCK_FOSC Oscillator determined by FOSC bits setting in Configuration words.
    _PWM_CLOCK_HFINTOSC High-frequency internal oscillator.
    _PWM_CLOCK_LFINTOSC Low-frequency internal oscillator.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired PWM module, simply change the letter x in the prototype for the appropriate module number.
  • This routine is avaliable only for the PIC16F1574/75/78/79 MCUs.
Requires

MCU must have Remappable CCP module.

  Note :
  • Calculation of the PWM frequency value is carried out by the compiler, as it would produce a relatively large code if performed on the library level.
    Therefore, compiler needs to know the value of the parameter in the compile time. That is why this parameter needs to be a constant, and not a variable.
Example
PWM1_Remappable_Init(5000, _PWM_MODE_STANDARD, _PWM_PRESCALER_DIV_128);

PWMx_Remappable_Set_Phase

Prototype

void PWMx_Remappable_Set_Phase(unsigned int new_phase);

Returns

Nothing.

Description

Sets the new PWM phase.

  • new_phase: .
  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired PWM module, simply change the letter x in the prototype for the appropriate module number.
  • This routine is avaliable only for the PIC16F1574/75/78/79 MCUs.
Requires
  • MCU must have Remappable CCP module.
  • PWMx_Remappable_Init must be called before using this routine.
Example
PWMx_Remappable_Set_Phase(192);

PWMx_Remappable_Set_Duty

Prototype

void PWMx_Remappable_Set_Duty(unsigned short duty_ratio);

Returns

Nothing.

Description

Sets 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.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired PWM module, simply change the letter x in the prototype for the appropriate module number.
Requires
  • MCU must have Remappable CCP module.
  • PWMx_Remappable_Init must be called before using this routine.
Example

Set duty ratio to 75%:

PWM4_Remappable_Set_Duty(192);

PWMx_Remappable_Start

Prototype

void PWMx_Remappable_Start(void);

Returns

Nothing.

Description

Starts PWM.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired PWM module, simply change the letter x in the prototype for the appropriate module number.
Requires
  • MCU must have Remappable CCP module.
  • PWMx_Remappable_Init must be called before using this routine.
Example
PWM4_Remappable_Start();

PWMx_Remappable_Stop

Prototype

void PWMx_Remappable_Stop(void);

Returns

Nothing.

Description

Stops PWM.

  Note :
  • Before using this library, make sure that Peripheral Pin Select Library is checked in the Library Manager, and that appropriate pins were mapped.
  • To select the desired PWM module, simply change the letter x in the prototype for the appropriate module number.
Requires
  • MCU must have Remappable CCP module.
  • PWMx_Remappable_Init must be called before using this routine.
  • PWMx_Remappable_Start should be called before using this routine, otherwise it will have no effect as the PWM module is not running.
Example
PWM4_Remappable_Stop();
Copyright (c) 2002-2019 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