2.PWM驅(qū)動(dòng)
2.1進(jìn)入工程目錄,啟動(dòng) Env 控制臺(tái)
2.2pwm 驅(qū)動(dòng)使能
2.3保存配置,自動(dòng)生成mdk5的工程
2.4測(cè)試驅(qū)動(dòng)代碼
驅(qū)動(dòng)涉及的io口
在menuconfig中配置生成的宏
KConfig
2.5測(cè)試代碼
//-----------------------------pwm測(cè)試代碼 ---------------開始------------------
#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);
}
}
//導(dǎo)出函數(shù)到命令行
MSH_CMD_EXPORT(pwm_sample, channel7 sample);
//-----------------------------pwm測(cè)試代碼 ---------------結(jié)束------------------
2.6 pwm驅(qū)動(dòng)框架學(xué)習(xí)
實(shí)現(xiàn)pwm控制函數(shù)
在控制函數(shù)內(nèi)部根據(jù)命令的類型,編寫對(duì)應(yīng)的外設(shè)控制函數(shù)
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) //互補(bǔ)輸出打開
#define PWMN_CMD_DISABLE (RT_DEVICE_CTRL_BASE(PWM) + 5)
#define PWM_CMD_SET_PERIOD (RT_DEVICE_CTRL_BASE(PWM) + 6) //設(shè)置周期
#define PWM_CMD_SET_PULSE (RT_DEVICE_CTRL_BASE(PWM) + 7) //設(shè)置占空比
#define PWM_CMD_SET_DEAD_TIME (RT_DEVICE_CTRL_BASE(PWM) + 8) //設(shè)置死去時(shí)間
#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)
實(shí)現(xiàn)各個(gè)控制函數(shù)
/*
- rt_pwm_enable pwm使能函數(shù),打開pwm輸出
2. rt_pwm_disable 關(guān)閉pwm輸出
3. rt_pwm_set 設(shè)置pwm頻率和占空比函數(shù)
4. rt_pwm_set_period 設(shè)置pwm周期
5. rt_pwm_set_pulse 設(shè)置占空比
6. rt_pwm_set_dead_time 設(shè)置pwm死區(qū)時(shí)間
7. rt_pwm_set_phase 設(shè)置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);
填充注冊(cè)前的各個(gè)配置結(jié)構(gòu)體的參數(shù)
通道
頻率
占空比
死區(qū)時(shí)間
相位調(diào)整
互補(bǔ)輸出使能
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 死區(qū)時(shí)間設(shè)置 /
rt_uint32_t phase; /unit: degree, 0~360, which is the phase of pwm output,其為pwm輸出的相位, /
/*
RT_TRUE : 互補(bǔ)輸出
RT_FALSE : 正常輸出.
*/
rt_bool_t complementary;
};
注冊(cè)pwm驅(qū)動(dòng)
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); ```
-
控制器
+關(guān)注
關(guān)注
112文章
16389瀏覽量
178419 -
CMD命令
+關(guān)注
關(guān)注
0文章
28瀏覽量
8321 -
MCU控制
+關(guān)注
關(guān)注
0文章
48瀏覽量
6752 -
PWM驅(qū)動(dòng)
+關(guān)注
關(guān)注
0文章
28瀏覽量
1220 -
RTThread
+關(guān)注
關(guān)注
8文章
132瀏覽量
40903
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論