RT-Thread RTOS 1.2.0
An open source embedded real-time operating system
载入中...
搜索中...
未找到
+ PWM 的协作图:

结构体

struct  rt_pwm_configuration
 
struct  rt_pwm_ops
 
struct  rt_device_pwm
 

宏定义

#define PWM_CMD_ENABLE   (RT_DEVICE_CTRL_BASE(PWM) + 0)
 
#define PWM_CMD_DISABLE   (RT_DEVICE_CTRL_BASE(PWM) + 1)
 
#define PWM_CMD_SET   (RT_DEVICE_CTRL_BASE(PWM) + 2)
 
#define PWM_CMD_GET   (RT_DEVICE_CTRL_BASE(PWM) + 3)
 
#define PWMN_CMD_ENABLE   (RT_DEVICE_CTRL_BASE(PWM) + 4)
 
#define PWMN_CMD_DISABLE   (RT_DEVICE_CTRL_BASE(PWM) + 5)
 
#define PWM_CMD_SET_PERIOD   (RT_DEVICE_CTRL_BASE(PWM) + 6)
 
#define PWM_CMD_SET_PULSE   (RT_DEVICE_CTRL_BASE(PWM) + 7)
 
#define PWM_CMD_SET_DEAD_TIME   (RT_DEVICE_CTRL_BASE(PWM) + 8)
 
#define PWM_CMD_SET_PHASE   (RT_DEVICE_CTRL_BASE(PWM) + 9)
 
#define PWM_CMD_ENABLE_IRQ   (RT_DEVICE_CTRL_BASE(PWM) + 10)
 
#define PWM_CMD_DISABLE_IRQ   (RT_DEVICE_CTRL_BASE(PWM) + 11)
 

函数

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)
 

详细描述

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_device_t rt_device_find(const char *name)
#define rt_kprintf(...)
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.
#define MSH_CMD_EXPORT(...)
unsigned int rt_uint32_t
#define RT_NULL

宏定义说明

◆ PWM_CMD_ENABLE

#define PWM_CMD_ENABLE   (RT_DEVICE_CTRL_BASE(PWM) + 0)

在文件 dev_pwm.h88 行定义.

◆ PWM_CMD_DISABLE

#define PWM_CMD_DISABLE   (RT_DEVICE_CTRL_BASE(PWM) + 1)

在文件 dev_pwm.h89 行定义.

◆ PWM_CMD_SET

#define PWM_CMD_SET   (RT_DEVICE_CTRL_BASE(PWM) + 2)

在文件 dev_pwm.h90 行定义.

◆ PWM_CMD_GET

#define PWM_CMD_GET   (RT_DEVICE_CTRL_BASE(PWM) + 3)

在文件 dev_pwm.h91 行定义.

◆ PWMN_CMD_ENABLE

#define PWMN_CMD_ENABLE   (RT_DEVICE_CTRL_BASE(PWM) + 4)

在文件 dev_pwm.h92 行定义.

◆ PWMN_CMD_DISABLE

#define PWMN_CMD_DISABLE   (RT_DEVICE_CTRL_BASE(PWM) + 5)

在文件 dev_pwm.h93 行定义.

◆ PWM_CMD_SET_PERIOD

#define PWM_CMD_SET_PERIOD   (RT_DEVICE_CTRL_BASE(PWM) + 6)

在文件 dev_pwm.h94 行定义.

◆ PWM_CMD_SET_PULSE

#define PWM_CMD_SET_PULSE   (RT_DEVICE_CTRL_BASE(PWM) + 7)

在文件 dev_pwm.h95 行定义.

◆ PWM_CMD_SET_DEAD_TIME

#define PWM_CMD_SET_DEAD_TIME   (RT_DEVICE_CTRL_BASE(PWM) + 8)

在文件 dev_pwm.h96 行定义.

◆ PWM_CMD_SET_PHASE

#define PWM_CMD_SET_PHASE   (RT_DEVICE_CTRL_BASE(PWM) + 9)

在文件 dev_pwm.h97 行定义.

◆ PWM_CMD_ENABLE_IRQ

#define PWM_CMD_ENABLE_IRQ   (RT_DEVICE_CTRL_BASE(PWM) + 10)

在文件 dev_pwm.h98 行定义.

◆ PWM_CMD_DISABLE_IRQ

#define PWM_CMD_DISABLE_IRQ   (RT_DEVICE_CTRL_BASE(PWM) + 11)

在文件 dev_pwm.h99 行定义.

函数说明

◆ 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

参数
devicethe PWM device
namethe name of PWM device
opsthe operations of PWM device
user_datathe user data of PWM device
返回
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

参数
devicethe PWM device
channelthe channel of PWM
返回
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

参数
devicethe PWM device
channelthe channel of PWM
返回
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

参数
devicethe PWM device
channelthe channel of PWM
periodthe period of PWM
pulsethe pulse of PWM
返回
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

参数
devicethe PWM device
channelthe channel of PWM
periodthe period of PWM
返回
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

参数
devicethe PWM device
channelthe channel of PWM
pulsethe period of PWM
返回
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

参数
devicethe PWM device
channelthe channel of PWM
dead_timedead zone time
返回
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

参数
devicethe PWM device
channelthe channel of PWM
phasephase
返回
rt_err_t error code