PWM Motor Control Library

The PWM Motor Control module is available with a number of dsPIC30/33 MCUs. The mikroC PRO for dsPIC30/33 and PIC24 provides a library which simplifies using the PWM Motor Control module.

  Important :

Library Routines

PWMx_Mc_Init

Prototype

unsigned int PWMx_Mc_Init(unsigned int freq_hz, unsigned int pair_output_mode, unsigned int enable_output_x, unsigned int clock_prescale_output_postscale);

Description

Initializes the Motor Control PWM module with duty ratio 0. The function calculates timer period, writes it to the MCU's PTPER register and returns it as the function result.

Parameters
  • freq_hz: PWM frequency in Hz (refer to device datasheet for correct values in respect with Fosc)
  • pair_output_mode: output mode for output pin pairs: 1 = independent, 0 = complementary.
    If pair_output_mode.B0 is equal to 1 then PWM channels PWM1L and PWM1H will be independent,
    If pair_output_mode.B1 is equal to 0 then PWM channels PWM2L and PWM2H will be complementary,
  • ...
    If pair_output_mode.Bn is equal to 1 then PWM channels PWM(n+1)L and PWM(n+1)H will be independent,
    If pair_output_mode.Bn is equal to 0 then PWM channels PWM(n+1)L and PWM(n+1)H will be complementary.
  • enable_output_x: bits <7..0> are enabling corresponding PWM channels <PWM4H, PWM3H, PWM2H, PWM1H, PWM4L, PWM3L, PWM2L, PWM1L>.
    If bit value is equal to 0 then corresponding PWM channel is disabled (pin is standard I/O).
    If bit value is equal to 1 then corresponding PWM channel is enabled (pin is PWM output).
    For detalied explanation consult the "Motor Control PWM Module" section in device datasheet
  • clock_prescale_output_postscale: PWM clock prescaler/postscaler settings. Values <0..3> and <0..15> correspond to prescaler/postscaler <1:1, 1:4, 1:16, 1:64> and <1:1, 1:2, ..., 1:16>
Returns

Calculated timer period.

Requires

The dsPIC30/33 MCU must have the Motor Control PWM module.

Example
// Initializes the PWM1 module at 5KHz, complementary pin-pair output, output enabled on pins 4l..1l, no clock prescale and no clock postscale:
unsigned int duty_50;
...
duty_50 = PWM1_Mc_Init(5000, 1, 0x0F, 0);
Notes
  • Number of PWM modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • PWM library routines require you to specify the module you want to use. To use the desired PWM module, simply change the letter x in the routine prototype for a number from 1 to 2.

PWMx_Mc_Set_Duty

Prototype

void PWM1_Mc_Set_Duty(unsigned duty, unsigned channel);

// For dsPIC 33FJ MCUs that have PWM2 module :
void PWM2_Mc_Set_Duty(unsigned duty);

Description

The function changes PWM duty ratio.

Parameters
  • duty: PWM duty ratio. Valid values: 0 to timer period returned by the PWMx_Mc_Init function.
  • channel: number of PWM channel to change duty to.
Returns

Nothing.

Requires

The dsPIC30/33 MCU must have the Motor Control PWM module.

The PWM module needs to be initalized. See the PWMx_Mc_Init function.

Example
// Set duty ratio to 50% at channel 1:
PWM1_Mc_Init(5000,1,0xF,0);
...
PWM1_Mc_Set_Duty(32767, 1);
Notes
  • Number of PWM modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • PWM library routines require you to specify the module you want to use. To use the desired PWM module, simply change the letter x in the routine prototype for a number from 1 to 2.

PWMx_Mc_Start

Prototype

void PWMx_Mc_Start();

Description

Starts the Motor Control PWM module (channels initialized in the PWMx_Mc_Init function).

Parameters

None.

Returns

Nothing.

Requires

The dsPIC30/33 MCU must have the Motor Control PWM module.

The PWM module needs to be initalized. See the PWMx_Mc_Init function.

Example
// start the Motor Control PWM1 module
PWM1_Mc_Start();
Notes
  • Number of PWM modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • PWM library routines require you to specify the module you want to use. To use the desired PWM module, simply change the letter x in the routine prototype for a number from 1 to 2.

PWMx_Mc_Stop

Prototype

void PWMx_Mc_Stop();

Description

Stops the Motor Control PWM module.

Parameters

None.

Returns

Nothing.

Requires

The dsPIC30/33 MCU must have the Motor Control PWM module.

Example
// stop the Motor Control PWM1 module 
PWM1_Mc_Stop();
Notes
  • Number of PWM modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
  • PWM library routines require you to specify the module you want to use. To use the desired PWM module, simply change the letter x in the routine prototype for a number from 1 to 2.

Library Example

The example changes PWM duty ratio on channel 1 continually. If LED is connected to the channel 1, a gradual change of emitted light will be noticeable.

Copy Code To ClipboardCopy Code To Clipboard
unsigned int i;

unsigned int duty_50;

void main(){

      ADPCFG = 0xFFFF;                            // initialize AN pins as digital
      PORTB  = 0xAAAA;
      TRISB  = 0;                                 // initialize portb as output
      Delay_ms(1000);

      duty_50 = PWM1_MC_Init(5000, 0, 0x01, 0);   // Pwm_Mc_Init returns 50% of the duty
      PWM1_MC_Set_Duty(i = duty_50, 1);
      PWM1_MC_Start();

      do
      {
        i--;
        PWM1_MC_Set_Duty(i, 1);
        Delay_ms(10);
        if (i == 0)
          i = duty_50 * 2 - 1;                    // Let us not allow the overflow
        PORTB = i;
      }
      while(1);

}

HW Connection

PWM Motor Control demonstration

PWM Motor Control demonstration

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