RT-Thread RTOS
An open source embedded real-time operating system
+ Collaboration diagram for PWM:

Data Structures

struct  rt_pwm_configuration
 
struct  rt_pwm_ops
 
struct  rt_device_pwm
 

Functions

rt_err_t rt_device_pwm_register (struct rt_device_pwm *device, const char *name, const struct rt_pwm_ops *ops, const void *user_data)
 
rt_err_t rt_pwm_enable (struct rt_device_pwm *device, int channel)
 
rt_err_t rt_pwm_disable (struct rt_device_pwm *device, int channel)
 
rt_err_t rt_pwm_set (struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
 
rt_err_t rt_pwm_set_period (struct rt_device_pwm *device, int channel, rt_uint32_t period)
 
rt_err_t rt_pwm_set_pulse (struct rt_device_pwm *device, int channel, rt_uint32_t pulse)
 
rt_err_t rt_pwm_set_dead_time (struct rt_device_pwm *device, int channel, rt_uint32_t dead_time)
 
rt_err_t rt_pwm_set_phase (struct rt_device_pwm *device, int channel, rt_uint32_t phase)
 

Detailed Description

PWM driver api.

Example

#include <rtthread.h>
#include <rtdevice.h>
#define PWM_DEV_NAME "pwm3" // PWM设备名称
#define PWM_DEV_CHANNEL 4 // PWM通道
struct rt_device_pwm *pwm_dev; // PWM设备句柄
static int pwm_led_sample(int argc, char *argv[])
{
rt_uint32_t period, pulse, dir;
period = 500000; // 周期为0.5ms,单位为纳秒ns
dir = 1; // PWM脉冲宽度值的增减方向
pulse = 0; // PWM脉冲宽度值,单位为纳秒ns
// 查找设备
pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
if (pwm_dev == RT_NULL)
{
rt_kprintf("pwm sample run failed! can't find %s device!\n", PWM_DEV_NAME);
return -RT_ERROR;
}
// 设置PWM周期和脉冲宽度默认值
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
// 使能设备
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
while (1)
{
if (dir)
{
pulse += 5000; // 从0值开始每次增加5000ns
}
else
{
pulse -= 5000; // 从最大值开始每次减少5000ns
}
if (pulse >= period)
{
dir = 0;
}
if (0 == pulse)
{
dir = 1;
}
// 设置PWM周期和脉冲宽度
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
}
}
MSH_CMD_EXPORT(pwm_led_sample, pwm sample);
rt_err_t rt_pwm_enable(struct rt_device_pwm *device, int channel)
enable the PWM channel
rt_err_t rt_pwm_set(struct rt_device_pwm *device, int channel, rt_uint32_t period, rt_uint32_t pulse)
set the PWM channel
rt_err_t rt_thread_mdelay(rt_int32_t ms)
This function will let current thread delay for some milliseconds.
Definition: thread.c:754
#define MSH_CMD_EXPORT(...)
Definition: finsh.h:142
PWM device.
Definition: dev_pwm.h:131

Function Documentation

◆ rt_device_pwm_register()

rt_err_t rt_device_pwm_register ( struct rt_device_pwm device,
const char *  name,
const struct rt_pwm_ops ops,
const void *  user_data 
)

register a PWM device

Parameters
devicethe PWM device
namethe name of PWM device
opsthe operations of PWM device
user_datathe user data of PWM device
Returns
rt_err_t error code

◆ rt_pwm_enable()

rt_err_t rt_pwm_enable ( struct rt_device_pwm device,
int  channel 
)

enable the PWM channel

Parameters
devicethe PWM device
channelthe channel of PWM
Returns
rt_err_t error code

◆ rt_pwm_disable()

rt_err_t rt_pwm_disable ( struct rt_device_pwm device,
int  channel 
)

disable the PWM channel

Parameters
devicethe PWM device
channelthe channel of PWM
Returns
rt_err_t error code

◆ rt_pwm_set()

rt_err_t rt_pwm_set ( struct rt_device_pwm device,
int  channel,
rt_uint32_t  period,
rt_uint32_t  pulse 
)

set the PWM channel

Parameters
devicethe PWM device
channelthe channel of PWM
periodthe period of PWM
pulsethe pulse of PWM
Returns
rt_err_t error code

◆ rt_pwm_set_period()

rt_err_t rt_pwm_set_period ( struct rt_device_pwm device,
int  channel,
rt_uint32_t  period 
)

set the PWM channel period

Parameters
devicethe PWM device
channelthe channel of PWM
periodthe period of PWM
Returns
rt_err_t error code

◆ rt_pwm_set_pulse()

rt_err_t rt_pwm_set_pulse ( struct rt_device_pwm device,
int  channel,
rt_uint32_t  pulse 
)

set the PWM channel pulse

Parameters
devicethe PWM device
channelthe channel of PWM
pulsethe period of PWM
Returns
rt_err_t error code

◆ rt_pwm_set_dead_time()

rt_err_t rt_pwm_set_dead_time ( struct rt_device_pwm device,
int  channel,
rt_uint32_t  dead_time 
)

set the dead zone time of PWM

Parameters
devicethe PWM device
channelthe channel of PWM
dead_timedead zone time
Returns
rt_err_t error code

◆ rt_pwm_set_phase()

rt_err_t rt_pwm_set_phase ( struct rt_device_pwm device,
int  channel,
rt_uint32_t  phase 
)

set the phase of PWM

Parameters
devicethe PWM device
channelthe channel of PWM
phasephase
Returns
rt_err_t error code