初级会员

- 积分
- 125
- 金钱
- 125
- 注册时间
- 2017-3-28
- 在线时间
- 38 小时
|
2金钱
1、cube配置,生成代码

2、修改kconfig文件

3、env配置PWM1 CH1

4、修改驱动
  
5、代码例程
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#define PWM_DEV_NAME "pwm1" /* PWM设备名称 */
#define PWM_DEV_CHANNEL 1 /* PWM通道 */
#define THREAD_PRIORITY 25 /* 线程优先级 */
#define THREAD_STACK_SIZE 512 /* 线程栈大小 */
#define THREAD_TIMESLICE 5 /* 线程时间片大小 */
static rt_thread_t tid1 = RT_NULL; /* 线程句柄 */
struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */
static rt_uint32_t period = 500000; /* 周期为0.5ms,单位为纳秒ns */
static rt_uint32_t pulse = 250000; /* PWM脉冲宽度值的增减方向 */
/* 线程 pwm_entry 的入口函数 */
static void pwm_entry(void *parameter)
{
rt_uint32_t count = 0;
while (1)
{
rt_thread_mdelay(50);
/* step 2、设置 PWM 周期和脉冲宽度,输出特定的波形 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse++);
}
/* step 3、如果不再使用该通道,可以关闭 PWM 通道的输出 */
// rt_pwm_disable(pwm_dev, PWM_DEV_CHANNEL);
}
static int pwm_test(int argc, char *argv[])
{
/* step 1.1、查找 PWM 设备 */
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;
}
/* step 1.2、设置 PWM 周期和脉冲宽度默认值 */
rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
/* step 1.3、使能 PWM 设备的输出通道 */
rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
/* 创建线程,名称是 pwm_thread ,入口是 pwm_entry*/
tid1 = rt_thread_create("pwm_thread",
pwm_entry,
RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY,
THREAD_TIMESLICE);
/* 如果获得线程控制块,启动这个线程 */
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
return RT_EOK;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(pwm_test, pwm sample);
6、实验结果
发送list_device 查询有PWM1驱动
发送pwm_test 找到PWM 但设置输出无反应

|
|