Pulse width modulation (PWM) is a method used to digitally encode analog signal levels and convert them into pulses. It can be used for motor control and backlight brightness adjustment.
## Overview
Pulse width modulation (PWM) is a technology that digitally encodes analog signal levels and converts them into pulses. It can be used for motor control and backlight brightness adjustment.
The PWM APIs provide a set of functions for operating a PWM device, including those for:
- Opening or closing a PWM device handle
The PWM APIs provide a set of functions for operating a PWM device, including those for:
- Obtaining and releasing a PWM device handle
- Setting the PWM period, signal ON-state time, and polarity
| duty | Time that a signal is in the ON state, in ns.|
| period | Time for a signal to complete an on-and-off cycle, in ns.|
| number | Number of square waves to generate. A positive value indicates the number of square waves to generate. The value <b>0</b> means to generate square waves repeatedly.|
| polarity | PWM signal polarity, which can be **PWM\_NORMAL\_POLARITY** or **PWM\_INVERTED\_POLARITY**.|
| status | PWM device status, which can be enabled or disabled.|
During the OS startup process, the driver management module loads the PWM driver based on the configuration file. Then, the PWM driver detects the PWM device and initializes the driver.
[Figure 1](#fig1_PWM_des) shows the general process of using the PWM module.
**Figure 1** Process of using the PWM module<aname="fig1_PWM_des"></a>
### PwmConfig Structure
**Table 1** PwmConfig structure
| Parameter| Description|
| -------- | -------- |
| duty | Time that a signal is in the ON state, in ns.|
| period | Time for a signal to complete an on-and-off cycle, in ns.|
| number | Number of square waves to generate.<br>- Positive value: indicates the number of square waves to generate.<br>- **0**: indicates that square waves are generated continuously.|
| polarity | PWM signal polarity, which can be positive or reverse.|
| status | PWM device status, which can be enabled or disabled.|
## Available APIs
**Table 2** PWM driver APIs
| Category| Description|
| -------- | -------- |
| Operating PWM handles| - **PwmOpen**: opens the device handle of a PWM device.<br>- **PwmClose**: closes the device handle of a PWM device.|
| Enabling or disabling PWM| - **PwmEnable**: enables a PWM device.<br>- **PwmDisable**: disables a PWM device.|
| Setting PWM| - **PwmSetPeriod**: sets the PWM period.<br>- **PwmSetDuty**: sets the signal ON-state time.<br>- **PwmSetPolarity**: sets the PWM signal polarity.|