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文章
16444瀏覽量
179314 -
CMD命令
+關注
關注
0文章
28瀏覽量
8345 -
MCU控制
+關注
關注
0文章
48瀏覽量
6785 -
PWM驅動
+關注
關注
0文章
28瀏覽量
1237 -
RTThread
+關注
關注
8文章
132瀏覽量
40990
發布評論請先 登錄
相關推薦
評論