This section introduces the Pulse-Width Modulation(PWM) APIs including terms and acronyms, supported features, software architecture, details on how to use this driver, PWM function groups, enums, structures and functions.
More...
This section introduces the Pulse-Width Modulation(PWM) APIs including terms and acronyms, supported features, software architecture, details on how to use this driver, PWM function groups, enums, structures and functions.
Terms and acronyms
| Terms | Details |
| PWM | Pulse-Width Modulation. PWM is a modulation technique used to encode a message into a pulsing signal. For more information, please refer to PWM in Wikipedia . |
Supported features
- Support polling mode.
The PWM generates the fixed waveform at the specified frequency and duty. The application can query current frequency and duty of the polling mode. The PWM does not support interrupt mode.
Software architecture of PWM
- PWM polling mode architecture.
Call hal_pwm_init() function to initialize the PWM source clock, then call hal_pwm_set_frequency() function to set the PWM frequency and get the total counter of the hardware. Total counter is PWM counter internal value at specified frequency. The duty cycle is calculated as the product of application's duty ratio and total counter. Duty ratio is the ratio of duty counter over the total counter.
Call hal_pwm_set_duty_cycle() function to set the PWM duty cycle. Call hal_pwm_start() function to trigger PWM execution.
Call hal_pwm_get_frequency() and hal_pwm_get_duty_cycle() functions if the current frequency and duty cycle need to be retrieved.
Call hal_pwm_get_running_status() function to get the PWM status, either busy or idle. Call hal_pwm_stop() function to stop the PWM execution.
Polling mode architecture is similar to the polling mode architecture in HAL overview. See HAL Driver Model for polling mode architecture.
How to use this driver
- Use PWM with polling mode.
How to calculate PWM frequency and duty cycle at fixed souce clock. Assuming the crystal(XTAL) oscillator runs at 40MHz. and the applicaton needs a waveform at 2kHz frequency and 50% duty ratio.The calculations of division clock, total count and duty cycle are based on the formuals as shown below.
- total_count = source_clock/frequency
- duty_ cycle = (total_count*duty_ratio)/100
- PWM_ON duration = duty_cycle
- PWM_OFF duration = total_count-PWM_ON duration
- Set PWM_ON duration to PWM_PARAM register 0~15 bit
- Set PWM_OFF duration to PWM_PARAM register 16~31 bit
This function gets the current duty cycle of the PWM.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [out] | *duty_cycle | is PWM hardware duty cycle, which is calculated as a product of application's duty ratio and hardware 's total count. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_set_duty_cycle()
This function gets current frequency of the PWM, the unit of frequency is Hz.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [out] | frequency | is PWM output frequency. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_set_frequency()
This function gets the current status of PWM.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [out] | running_status | is PWM busy or idle status, For details about this parameter, please refer to hal_pwm_running_status_t . |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
This function initializes the PWM hardware source clock.
- Parameters
-
| [in] | source_clock | is the PWM source clock. For more details about the parameter, please refer to hal_pwm_source_clock_t. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- Note
- There are some limitations about the minimum frequency that we can set if we choose the related source clock list in hal_pwm_source_clock_t, and the caculation formula is as follows:
The minimum frequency = ( the chosen source clock) / 0XFFFF
For example:
if we choose the clock of HAL_PWM_CLOCK_32KHZ, the minimum frequency that we can set is 0.5 Hz;
if we choose the clock of HAL_PWM_CLOCK_2MHZ, the minimum frequency that we can set is 30 Hz;
if we choose the clock of HAL_PWM_CLOCK_40MHZ, the minimum frequency that we can set is 610 Hz;
- Warning
- For this chip, all the PWM channel is using the same clock once this function has been called.
- See also
- hal_pwm_deinit()
This function sets the PWM duty cycle.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [in] | duty_cycle | is the PWM hardware duty cycle, which is calculated as a product of application's duty ratio and hardware 's total count. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_get_duty_cycle()
This function sets the PWM frequency and retrieves total count of the PWM hardware at the specified frequency.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
| [in] | frequency | is the PWM output frequency. |
| [out] | total_count | is PWM hardware total count, the value of this parameter varies based on the given PWM frequency. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
This function starts the PWM execution.
- Parameters
-
| [in] | pwm_channel | is the PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_stop()
This function stops the PWM execution.
- Parameters
-
| [in] | pwm_channel | is PWM channel number. For more details about the parameter, please refer to hal_pwm_channel_t. |
- Returns
- To indicate whether this function call is successful or not. If the return value is HAL_PWM_STATUS_OK, the operation completed successfully. If the return value is HAL_PWM_STATUS_INVALID_PARAMETER, a wrong parameter is given. The parameter needs to be verified.
- Example
- For a sample code, please refer to How to use this driver.
- See also
- hal_pwm_start()