Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
PWM
【英飞凌开发板模块评测任务大挑战】-2-PWM外设使用
发布于 2023-05-04 10:07:37 浏览:341
订阅该版
[tocm] # 2.PWM驱动 代码仓库地址:https://gitee.com/chejia12/rtt_-cy8-c624-alqalqi ## 2.1进入工程目录,启动 Env 控制台 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230504/7fb1e572d0a06a6389afd4fc08868d8b.png.webp) ## 2.2pwm 驱动使能 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230504/584e3b7f1903f783acca9c2e5aab4329.png.webp) ## 2.3保存配置,自动生成mdk5的工程 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230504/27a5df69c4a9f9c82d7c23244bd92a33.png) ## 2.4测试驱动代码 ### 驱动涉及的io口 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230504/1693d30d94a67f834e21ccc360ac8b59.png) ### 在menuconfig中配置生成的宏 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230504/8f03c0c0297c942fecdbf1e411db72e8.png.webp) ### KConfig ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230504/448bd812303f8124f7a36a18842b5fb6.png.webp) ## 2.5测试代码 ```c //-----------------------------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驱动框架学习 [RTT的I/O设备模型 ](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/device) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230504/fbeea3f1bcf16a2867080708094716d9.png.webp) 1. 实现pwm控制函数 在控制函数内部根据命令的类型,编写对应的外设控制函数 > rt_err_t (*control)(struct rt_device_pwm *device, int cmd, void *arg); 2. 命令的类型有 ```c #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) ``` 3. 实现各个控制函数 >/* > > 1. 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); 4. 填充注册前的各个配置结构体的参数 >1. 通道 >2. 频率 >3. 占空比 >4. 死区时间 >5. 相位调整 >6. 互补输出使能 > >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 1ns~4.29s:1Ghz~0.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; > }; 5. 注册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); ```
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
chejia12
这家伙很懒,什么也没写!
文章
6
回答
0
被采纳
0
关注TA
发私信
相关文章
1
玩iot camera笔记之3测试试用3路pwm
2
[已解决]PWM输出异常分析
3
给RT-Thread添加PWM驱动框架
4
关于rt-thread-3.1.0 pwm
5
关于rt-thread的PWM框架在stm32f103vf应用的疑问
6
rt-thread stm32 bsp adc pwm 外设适配好了吗
7
stm32f103ze 添加pwm 设备失败
8
【正点原子】潘多拉IoT-STM32L475开发板 用menuconfig 看不到PWM....
9
关于rtthread 4.0.0版本中pwm的初始化定时器寄存器读写问题
10
RT-Thread正点原子战舰V3使用PWM设备驱动没有输出
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
SFUD
msh
ulog
C++_cpp
MicroPython
本月问答贡献
三世执戟
5
个答案
1
次被采纳
KunYi
4
个答案
1
次被采纳
RTT_逍遥
3
个答案
1
次被采纳
xiaorui
1
个答案
1
次被采纳
JonasWen
1
个答案
1
次被采纳
本月文章贡献
出出啊
1
篇文章
3
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
3
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部