User manual

Table Of Contents
mikroC PRO for PIC32
MikroElektronika
401
PWM_Init
Prototype
unsigned int PWM_Init(unsigned long freq_hz, unsigned int enable_channel_x,
unsigned int timer_prescale, unsigned int use_timer_x);
Description Initializes the PWM module with duty ratio 0.
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
:
unsigned int pwm_period1;
...
pwm_period1 = PWM_Init(5000, 1, 0, 2);
Notes Number of available PWM channels depends on MCU. Refer to MCU datasheet for details.
PWM_Init_Advanced
Prototype
unsigned int PWM_Init_Advanced(unsigned long freq_hz, unsigned long Fpb_
kHz, unsigned int enable_channel_x, unsigned int timer_prescale, unsigned
int use_timer_x);
Description Initializes the PWM module with duty ratio 0.
Parameters - freq_hz: PWM frequency in Hz
- Fpb_kHz: Peripheral Bus Clock frequency in kHz.
- 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
:
unsigned int pwm_period1;
...
pwm_period1 = PWM_Init(5000, 1, 0, 2);
Notes Number of available PWM channels depends on MCU. Refer to MCU datasheet for details.