mikroSDK Reference Manual

PWM Hardware Abstraction Layer API Reference. More...

Functions list

void hal_pwm_configure_default (hal_pwm_config_t *config)
 Configure PWM configuration structure.
 
err_t hal_pwm_open (handle_t *handle, bool hal_obj_open_state)
 Open the PWM HAL object on selected pin.
 
err_t hal_pwm_set_freq (handle_t *handle, hal_pwm_config_t *config)
 Set PWM frequency in Hertz.
 
err_t hal_pwm_start (handle_t *handle)
 Start PWM HAL context object.
 
err_t hal_pwm_set_duty (handle_t *handle, float duty_ratio)
 Set PWM duty cycle in percentages.
 
err_t hal_pwm_stop (handle_t *handle)
 Stop PWM module.
 
err_t hal_pwm_close (handle_t *handle)
 Close PWM HAL object.
 

Function Documentation

◆ hal_pwm_close()

err_t hal_pwm_close ( handle_t * handle)

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

Parameters
[in,out]handlePWM handle.
Returns
Nothing.

Example

hal_pwm_close( &hal_pwm->handle );
err_t hal_pwm_close(handle_t *handle)
Close PWM HAL object.

◆ hal_pwm_configure_default()

void hal_pwm_configure_default ( hal_pwm_config_t * config)

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

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

Default values:

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

Example

hal_pwm_config_t hal_pwm_cfg;
hal_pwm_configure_default( &hal_pwm_cfg );
void hal_pwm_configure_default(hal_pwm_config_t *config)
Configure PWM configuration structure.
PWM HAL config structure, consisted of the following fields:
Definition hal_pwm.h:92

◆ hal_pwm_open()

err_t hal_pwm_open ( handle_t * handle,
bool hal_obj_open_state )

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

Parameters
[in,out]handlePWM handle. See hal_pwm_t structure definition for detailed explanation.
[in]hal_obj_open_statePWM state, is it open or not.
Returns
The function can return one of the values defined by hal_pwm_err_t, which is size dependant on the architecture.
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

hal_pwm_t hal_pwm;
hal_pwm_config_t hal_pwm_cfg;
hal_pwm_configure_default( &hal_pwm_cfg );
hal_pwm_cfg.pin = MIKROBUS_1_PWM;
hal_pwm_cfg.freq_hz = 5000;
hal_pwm_open( &hal_pwm->handle, true );
err_t hal_pwm_open(handle_t *handle, bool hal_obj_open_state)
Open the PWM HAL object on selected pin.
uint32_t freq_hz
Definition hal_pwm.h:94
hal_pin_name_t pin
Definition hal_pwm.h:93
The PWM HAL context structure.
Definition hal_pwm.h:104
handle_t handle
Definition hal_pwm.h:105

◆ hal_pwm_set_duty()

err_t hal_pwm_set_duty ( handle_t * handle,
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]handlePWM handle.
[in]duty_ratioPWM duty_ratio.
Returns
The function can return one of the values defined by hal_pwm_err_t, which is size dependant on the architecture.
Precondition
This function should be called after the hal_pwm_start function for the PWM to work.

Example

hal_pwm_set_duty( &hal_pwm->handle, 0.5 );
err_t hal_pwm_set_duty(handle_t *handle, float duty_ratio)
Set PWM duty cycle in percentages.

◆ hal_pwm_set_freq()

err_t hal_pwm_set_freq ( handle_t * handle,
hal_pwm_config_t * config )

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]handlePWM handle. See hal_pwm_t structure definition for detailed explanation.
[in]configPWM configuration structure. See hal_pwm_config_t structure definition for detailed explanation.
Returns
The function can return one of the values defined by hal_pwm_err_t, which is size dependant on the architecture.
Precondition
Before calling this function, the user is expected to call hal_pwm_open
Postcondition
This function stops PWM module and sets duty cycle on 0.
Note
This function should be called first after hal_pwm_open for the PWM module to work. After calling this function, the user is expected to call hal_pwm_start and pwm_set_duty.

Example

hal_pwm_set_freq( &hal_pwm->handle, hal_pwm_cfg.freq_hz );
err_t hal_pwm_set_freq(handle_t *handle, hal_pwm_config_t *config)
Set PWM frequency in Hertz.

◆ hal_pwm_start()

err_t hal_pwm_start ( handle_t * handle)

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

Parameters
[in]handlePWM handle.
Returns
The function can return one of the values defined by hal_pwm_err_t, which is size dependant on the architecture.
Precondition
Before calling this function, the user is expected to set frequency by using hal_pwm_set_freq function.

Example

hal_pwm_start( &hal_pwm->handle );
err_t hal_pwm_start(handle_t *handle)
Start PWM HAL context object.

◆ hal_pwm_stop()

err_t hal_pwm_stop ( handle_t * handle)

Disable output for specific PWM module.

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

Example

hal_pwm_stop( &hal_pwm->handle );
err_t hal_pwm_stop(handle_t *handle)
Stop PWM module.