PWM Motor Control Library
The PWM Motor Control module is available with a number of dsPIC30/33 MCUs. mikroBasic PRO for dsPIC30/33 and PIC24 provides a library which simplifies using the PWM Motor Control module.
Important :
- 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 Routines
PWMx_Mc_Init
| Prototype |
sub function PWMx_Mc_Init(dim freq_hz, pair_output_mode, enable_output_x, clock_prescale_output_postscale as word) as word |
|---|---|
| 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 |
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.
|
| 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: dim duty_50 as word ... duty_50 = PWM1_Mc_Init(5000, 1, $0F, 0) |
| Notes |
|
PWMx_Mc_Set_Duty
| Prototype |
sub procedure PWM1_Mc_Set_Duty(dim duty, channel as word)
' For dsPIC 33FJ MCUs that have PWM2 module : |
|---|---|
| Description |
The function changes PWM duty ratio. |
| Parameters |
|
| 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,$F,0) ... PWM1_Mc_Set_Duty(32767, 1) |
| Notes |
|
PWMx_Mc_Start
| Prototype |
sub procedure 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 |
|
PWMx_Mc_Stop
| Prototype |
sub procedure 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 |
|
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.
program PWM
dim pwm_period, current_duty as word
main:
ADPCFG = 0xFFFF ' initialize AN pins as digital
PORTB = 0
TRISB = 0 ' initialize portb as output
current_duty = 10
Delay_ms(1000)
pwm_period = PWM1_MC_Init(5000, 1, 0x01, 0) ' Pwm_Mc_Init returns calculated timer period.
PWM1_MC_Set_Duty(current_duty, 1)
PWM1_MC_Start()
while (TRUE) ' Endless loop
if (RB0_bit) then ' Button on RB0 pressed
Delay_ms(20)
Inc(current_duty) ' Increment current_duty
if (current_duty > pwm_period) then ' If we increase current_duty greater then possible pwm_period value
current_duty = 0 ' reset current_duty value to zero
end if
PWM1_MC_Set_Duty(current_duty, 1) ' Set newly acquired duty ratio
end if
if (RB1_bit) then ' Button on RB1 pressed
Delay_ms(20)
Dec(current_duty) ' Decrement current_duty
if (current_duty > pwm_period) then ' If we decrease current_duty greater then possible pwm_period value (overflow)
current_duty = pwm_period ' set current_duty to max possible value
end if
PWM1_MC_Set_Duty(current_duty, 1) ' Set newly acquired duty ratio
end if
Delay_ms(5) ' Slow down change pace a little
wend
end.
HW Connection

PWM Motor Control demonstration
What do you think about this topic ? Send us feedback!




