mikroSDK Reference Manual

PWM Driver API Reference. More...

Functions list

void pwm_configure_default (pwm_config_t *config)
 Configure PWM config structure. More...
 
err_t pwm_open (pwm_t *obj, pwm_config_t *config)
 Open the PWM driver object on selected pin. More...
 
err_t pwm_set_freq (pwm_t *obj, uint32_t freq_hz)
 Set PWM frequency in Hertz. More...
 
err_t pwm_start (pwm_t *obj)
 Enable counter and start PWM module. More...
 
err_t pwm_set_duty (pwm_t *obj, float duty_ratio)
 Set PWM duty cycle in percentages. More...
 
err_t pwm_stop (pwm_t *obj)
 Stop PWM module. More...
 
void pwm_close (pwm_t *obj)
 Close PWM driver context object. More...
 

Detailed Description

The PWM driver provides standard PMM functionality including setting the duty cycle and frequency.

Function Documentation

◆ pwm_configure_default()

void pwm_configure_default ( pwm_config_t config)

Configures structure to default initialization values. Take into consideration that this is just structure variable initial values setting. Values need to be redefined by user.

Parameters
[in,out]configPWM driver configuration settings. See pwm_config_t structure definition for detailed explanation.

Default values:

Function Default value
PWM pin 0xFFFFFFFF (invalid pin)
Frequency in Hz 0 (Frequency set to 0)
Returns
Nothing.

Example

// PWM driver configuration structure.
pwm_config_t pwm_cfg;
// Fill structure with default values.

◆ pwm_open()

err_t pwm_open ( pwm_t obj,
pwm_config_t config 
)

Opens the PWM driver object on selected pins. Allocates memory and pin for specified object.

Parameters
[in,out]objPWM driver object. See pwm_t structure definition for detailed explanation.
[in]configPWM driver configuration settings. See pwm_config_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by pwm_err_t, which is size dependant on the architecture.
Precondition
Make sure that pwm_config_t structure has been adequately filled out. See pwm_configure_default definition for detailed explanation.
Note
This function needs to be called before other driver functions. It is recommended to check return value for error.
Warning
The following example describes how the function is used. Take into consideration that different hardware might not have the same pin. Make sure to accommodate pin name based on your hardware specifics.

Example

// PWM driver context structure.
pwm_t pwm;
// PWM driver configuration structure.
pwm_config_t pwm_cfg;
// Fill structure with default values.
// Fill structure with pin name.
pwm_cfg.pin = MIKROBUS_1_PWM;
// Fill structure with PWM frequency.
pwm_cfg.freq_hz = 5000;
// Open PWM driver.
pwm_open( &pwm, &pwm_cfg );

◆ pwm_set_freq()

err_t pwm_set_freq ( pwm_t obj,
uint32_t  freq_hz 
)

This function is used to set the PWM frequency, it stops PWM module and sets duty_cycle on 0. Take into consideration that the module will be re-initialized on the hardware level.

Parameters
[in]objPWM driver context object. See pwm_t structure definition for detailed explanation.
[in]freq_hzPWM frequency in HZ.
Returns
The function can return one of the values defined by pwm_err_t, which is size dependant on the architecture.
Precondition
Before calling this function, the user is expected to call pwm_open function.
Postcondition
This function stops PWM module and sets duty cycle on 0.
Note
This function should be called first after pwm_open for the PWM module to work. After calling this function, the user is expected to call pwm_start and pwm_set_duty.

Example

// PWM driver context structure.
pwm_t pwm;
// Fill structure with PWM frequency.
pwm_cfg.freq_hz = 5000;
// Set PWM frequency.
pwm_set_freq( &pwm, pwm_cfg.freq_hz );

◆ pwm_start()

err_t pwm_start ( pwm_t obj)

Initializes PWM module on hardware level, if not already initialized and starts PWM module.

Parameters
[in]objPWM driver object. See pwm_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by pwm_err_t, which is size dependant on the architecture.
Precondition
Before calling this function, the user is expected to set frequency by using pwm_set_freq function.

Example

// PWM driver context structure.
pwm_t pwm;
// Start PWM module.
pwm_start( &pwm );

◆ pwm_set_duty()

err_t pwm_set_duty ( pwm_t obj,
float  duty_ratio 
)

Set PWM duty cycle in percentages. The user should enter the duty_ratio in percentages. The duty_ratio value should be between 0 and 1, (where 0 represents 0% and 1 represents 100%). If the user sets value for duty_ratio to be less than 0, duty_ratio is automatically set to 0(0%), and If the user sets value for duty_ratio to be greater than 1, duty_ratio is automatically set to 1(100%).

Parameters
[in]objPWM driver object. See pwm_t structure definition for detailed explanation.
[in]duty_ratioPWM duty_ratio.
Returns
The function can return one of the values defined by pwm_err_t, which is size dependant on the architecture.
Precondition
This function should be called after the pwm_start function for the PWM to work.

Example

// PWM driver context structure.
pwm_t pwm;
// PWM duty ratio.
float duty_ratio = 0,5;
// Set PWM duty ratio.
pwm_set_duty( &pwm, duty_ratio );

◆ pwm_stop()

err_t pwm_stop ( pwm_t obj)

Disable output for specific PWM module.

Parameters
[in]objPWM driver object. See pwm_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by pwm_err_t, which is size dependant on the architecture.
Precondition
In order to stop PWM module user should first start PWM module. See pwm_start function definition for detailed explanation.
Note
The PWM Module needs to be initialized so that the stop can be done.

Example

// PWM driver context structure.
pwm_t pwm;
// Stop PWM module.
pwm_stop( &pwm );

◆ pwm_close()

void pwm_close ( pwm_t obj)

De-allocates hardware resources for specific driver object and de-initializes the module on a hardware level.

Parameters
[in,out]objPWM driver object. See pwm_t structure definition for detailed explanation.
Returns
Nothing.

Example

// PWM driver context structure.
pwm_t pwm;
// Close PWM driver.
pwm_close( &pwm );
pwm_close
void pwm_close(pwm_t *obj)
Close PWM driver context object.
pwm_open
err_t pwm_open(pwm_t *obj, pwm_config_t *config)
Open the PWM driver object on selected pin.
pwm_configure_default
void pwm_configure_default(pwm_config_t *config)
Configure PWM config structure.
pwm_config_t
PWM driver config structure, consisted of the following fields:
Definition: drv_pwm.h:82
pwm_config_t::freq_hz
uint32_t freq_hz
Definition: drv_pwm.h:85
pwm_config_t::pin
pin_name_t pin
Definition: drv_pwm.h:84
pwm_set_duty
err_t pwm_set_duty(pwm_t *obj, float duty_ratio)
Set PWM duty cycle in percentages.
pwm_set_freq
err_t pwm_set_freq(pwm_t *obj, uint32_t freq_hz)
Set PWM frequency in Hertz.
pwm_start
err_t pwm_start(pwm_t *obj)
Enable counter and start PWM module.
pwm_t
PWM driver context structure.
Definition: drv_pwm.h:95
pwm_stop
err_t pwm_stop(pwm_t *obj)
Stop PWM module.