RT-Thread RTOS
An open source embedded real-time operating system
|
PWM (Pulse Width Modulation) is a method of digitally encoding the level of an analog signal. The frequency of the square wave is used to encode the level of a specific analog signal by pulses of different frequencies. The output receives a series of pulses of equal magnitude and uses these pulses to replace the device with the desired waveform.
Above is a simple schematic diagram of PWM. Assuming that the timer works in a up-counter mode. When the count value is less than the threshold, it outputs a level state, such as a high level. When the count value is greater than the threshold, it outputs the opposite, such as a low level. When the count value reaches the maximum value, the counter recounts from 0 and returns to the original level state. The ratio of the high-level duration (pulse width) to the cycle time is the duty cycle, ranging from 0 to 100%. The high level of the above picture is just half of the cycle time, so the duty cycle is 50%.
One of the common PWM control scenarios is to adjust the brightness of a light or screen. The brightness can be adjusted through changing the duty cycle. The PWM does not adjust the light continuously, rather it constantly turns the screen on and off. When the light is turned on and off fast enough, the naked eye will always think that it is always bright. In the process of switching it on and off, the longer the light is off, the lower the brightness of the screen to the naked eye. Conversely, the longer the light is on, the brighter the screen will appear.
The application accesses the PWM device hardware through the PWM device management interface provided by RT-Thread. The related interfaces are as follows:
Function | Description |
---|---|
rt_device_find() | Find device handles based on the name of PWM device |
rt_pwm_set() | Set PWM period and pulse width |
rt_pwm_enable() | Enable PWM device |
rt_pwm_disable() | Disable the PWM device |
The application obtains the device handle based on the name of PWM device, which in turn can operate the PWM device. The function is as follows:
Parameter | Description |
---|---|
name | Device |
Return | —— |
Device handle | Found the corresponding device, will return the corresponding device handle |
RT_NULL | Device not found |
In general, the name of the PWM device registered to the system is pwm0, pwm1, etc. The usage examples are as follows:
Set the PWM period and duty cycle by using the following function:
Parameter | Description |
---|---|
device | PWM device handle |
channel | PWM channel |
period | PWM period (ns) |
pulse | PWM pulse width time (ns) |
Return | —— |
RT_EOK | successful |
-RT_EIO | device is null |
-RT_ENOSYS | Device operation method is null |
Other Errors | Execute failed |
The output frequency of the PWM is determined by the period. For example, the time of a period is 0.5ms (milliseconds), the period value is 500000ns (nanoseconds), the output frequency is 2KHz, the duty cycle is pulse / period
, and the pulse value cannot exceed period.
An example of use is as follows:
After setting the PWM period and pulse width, you can enable the PWM device by the following function:
Parameter | Description |
---|---|
device | PWM device handle |
channel | PWM channel |
Return | —— |
RT_EOK | Enable device successful |
-RT_ENOSYS | Device operation method is null |
Other Errors | Enable device failed |
An example of use is as follows:
Use the following function to turn off the corresponding channel of the PWM device.
Parameter | Description |
---|---|
device | PWM device handle |
channel | PWM channel |
Return | —— |
RT_EOK | Turn off device successful |
-RT_EIO | Device handle is null |
Other Errors | Turn off device failed |
An example of use is as follows:
To set the period and duty cycle of a channel of a PWM device, use the command pwm_set pwm1 1 500000 5000
. The first parameter is the command, the second parameter is the PWM device name, the third parameter is the PWM channel, and the fourth parameter is PWM period (ns), the fifth parameter is the pulse width (ns).
To enable a channel of the PWM device, use the command pwm_enable pwm1 1
. The first parameter is the command, the second parameter is the PWM device name, and the third parameter is the PWM channel.
To disable a channel of the PWM device, use the command pwm_disable pwm1 1
. The first parameter is the command, the second parameter is the PWM device name, and the third parameter is the PWM channel.
The following sample code is a PWM device usage sample . The main steps of the sample code are as follows: