Mikroelektronika MIKROE-724 Scheda Tecnica
mikroBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
433
PWM_Init
PWM_Set_Duty
Prototype
sub function PWM_Init(dim freq_hz as longint, dim enable_channel_x, timer_
prescale, use_timer_x as word) as word
‘ 30F1010 and dsPIC33FJ06GS101/102/202 prototype
sub function PWM_Init(dim freq_hz as longint, dim enable_channel_x, timer_
prescale) as word
Description Initializes the PWM module with duty ratio 0.
Parameters -
Parameters -
freq_hz
: PWM frequency in Hz (refer to device datasheet for correct values in respect with Fosc)
-
enable_channel_x
: number of PWM channel to be initialized. Refer to MCU’s datasheet for
available PWM channels
-
timer_prescale
: timer prescaler parameter. Valid values:
1, 8, 64,
and
256
-
use_timer_x
: timer to be used with the PWM module. Valid values:
2
(Timer2) and
3
(Timer3)
Returns
-
0xFFFF
- if timer settings are not valid
- otherwise returns calculated timer period
Requires
MCU must have the HW PWM Module.
Example
‘ Initializes the PWM module at 5KHz, channel 1, no clock prescale, timer2
:
dim pwm_period1 as word
...
pwm_period1 = PWM_Init(5000, 1, 0, 2)
Notes
Number of available PWM channels depends on MCU. Refer to MCU datasheet for details.
Prototype
sub procedure PWM_Set_Duty(dim duty, channel as word)
Description The function changes PWM duty ratio.
Parameters -
Parameters -
duty
: PWM duty ratio. Valid values:
0
to timer period returned by the PWM_Init function.
-
channel
: number of PWM channel to change duty to.
Returns
Nothing.
Requires
MCU must have the HW PWM Module.
PWM channel must be properly initialized. See PWM_Init routine.
Example
‘ Set channel 1 duty ratio to 50%:
dim pwm_period1 as word
...
PWM_Set_Duty(pwm_period1 div 2, 1)
Notes
Number of available PWM channels depends on MCU. Refer to MCU datasheet for details.