2.PWM驱动
2.1进入工程目录,启动 Env 控制台
2.2pwm 驱动使能
2.3保存配置,自动生成mdk5的工程
2.4测试驱动代码
驱动涉及的io口
在menuconfig中配置生成的宏
KConfig
2.5测试代码
//-----------------------------pwm测试代码 ---------------开始------------------
#define PWM_DEV_NAME "pwm0"
#define PWM_DEV_CHANNEL 0
struct rt_device_pwm *pwm_dev;
static int pwm_sample(int argc, char *argv[])
{
rt_uint32_t period, pulse, dir;
period = 1 * 1000 * 1000;
dir = 1;
pulse = 0;
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;
}
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
rt_kprintf("Now PWM[%s] Channel[%d] Period[%d] Pulse[%d]n", PWM_DEV_NAME, PWM_DEV_CHANNEL, period, pulse);
while (1)
{
rt_thread_mdelay(50);
if (dir)
{
pulse += 100000;
}
else
{
pulse -= 100000;
}
if (pulse >= period)
{
dir = 0;
}
if (0 == pulse)
{
dir = 1;
}
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
}
}
//导出函数到命令行
MSH_CMD_EXPORT(pwm_sample, channel7 sample);
//-----------------------------pwm测试代码 ---------------结束------------------
2.6 pwm驱动框架学习
实现pwm控制函数
在控制函数内部根据命令的类型,编写对应的外设控制函数
rt_err_t (control)(struct rt_device_pwm device, int cmd, void *arg);
命令的类型有
#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_pwm_enable pwm使能函数,打开pwm输出
2. rt_pwm_disable 关闭pwm输出
3. rt_pwm_set 设置pwm频率和占空比函数
4. rt_pwm_set_period 设置pwm周期
5. rt_pwm_set_pulse 设置占空比
6. rt_pwm_set_dead_time 设置pwm死区时间
7. rt_pwm_set_phase 设置pwm的输出相位
*/
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);
填充注册前的各个配置结构体的参数
通道
频率
占空比
死区时间
相位调整
互补输出使能
struct rt_pwm_configuration
{
rt_uint32_t channel; / 0 ~ n or 0 ~ -n, which depends on specific MCU requirements这取决于特定的MCU要求 /
rt_uint32_t period; / unit:ns 1ns4.29s:1Ghz0.23h 频率 /
rt_uint32_t pulse; / unit:ns (pulse<=period)占空比 /
rt_uint32_t dead_time; / unit:ns 死区时间设置 /
rt_uint32_t phase; /unit: degree, 0~360, which is the phase of pwm output,其为pwm输出的相位, /
/*
RT_TRUE : 互补输出
RT_FALSE : 正常输出.
*/
rt_bool_t complementary;
};
注册pwm驱动
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); ```
-
控制器
+关注
关注
112文章
16350浏览量
177947 -
CMD命令
+关注
关注
0文章
28浏览量
8306 -
MCU控制
+关注
关注
0文章
48浏览量
6734 -
PWM驱动
+关注
关注
0文章
28浏览量
1202 -
RTThread
+关注
关注
8文章
132浏览量
40869
发布评论请先 登录
相关推荐
评论