Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
STM32H7
PWM
keil+env在STM32H743上实现pwm呼吸灯
发布于 2023-06-04 22:31:57 浏览:724
订阅该版
一、工具 keil+Env+STM32CubeMX 二、在Env中勾选本工程需要的pkgs 在RT-Thread Components--->Device Drivers下勾选 Using hardware timer device drivers和Using PWM device drivers   勾选完成后保存、退出更新,再返回此界面查看是否已经更新好(勾好) 最后使用 scons --target=mdk5生成新的工程 会发现工程中多出了一个关键性的文件drv_pwm.c  三、修改配置文件 在 rtconfig.h 中可以查看 RT_USING_HWTIMER 和 RT_USING_PWM 的设备驱动宏定义。  stm32h7xx_hal_conf.h 头文件查看是否已宏定义HAL_TIM_MODULE_ENABLED 和 HAL_PWR_MODULE_ENABLED,如果没有自动配置则手动取消相关注释打开并保存  在board.h 增加 #define BSP_USING_PWM3 和 #define BSP_USING_PWM3_CH4 宏定义,然后保存(会在drv_pwm.c中使用)  为什么要选择TIM3的第4通道呢?因为此io(PB1)在正点原子stm32H743上连接了DS0(红灯),方便我们看实验 接下来,查看drv_pwm.c中TIM设备硬件驱动代码 可以看到,PWM3已经被启用  四、使用STM32CubeMX生成代码 drv_pwm.c的static rt_err_t stm32_hw_pwm_init(struct stm32_pwm *device) 中调用了 HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim) 这个函数,这样就需要通过STM32Cube配置自动生成相关驱动代码。  首先,找到工程下--->board--->CubeMX_Config文件夹下的CubeMX_Config.ioc文件  用管理员方式打开STM32CubeMX,再载入此ioc文件  配置TIM3 PWM CH4通道(即PB1)     修改完成后,点击右上角GENERATE CODE,可能会下载许多文件,如果下载失败,有可能是没有用管理员方式运行,也有可能是连接未建立 可以这样检查一下   等下载完成后,可以看到Src文件夹多了一些文件,并且这些文件已经加入了工程,其中核心文件就是stm32h7xx_hal_msp.c,里面包含了前文提到的HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim) 这个函数   这里可以看一下其中我们用到的重要函数: 打开TIM3时钟(初始化) ```c /** * @brief TIM_Base MSP Initialization * This function configures the hardware resources used in this example * @param htim_base: TIM_Base handle pointer * @retval None */ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_TIM3_CLK_ENABLE(); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM3_MspInit 1 */ } } ``` io初始化(drv_pwm.c中使用)  ```c void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(htim->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspPostInit 0 */ /* USER CODE END TIM3_MspPostInit 0 */ __HAL_RCC_GPIOB_CLK_ENABLE(); /**TIM3 GPIO Configuration PB1 ------> TIM3_CH4 */ GPIO_InitStruct.Pin = GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF2_TIM3; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN TIM3_MspPostInit 1 */ /* USER CODE END TIM3_MspPostInit 1 */ } } ``` 解除配置(释放资源) ```c /** * @brief TIM_Base MSP De-Initialization * This function freeze the hardware resources used in this example * @param htim_base: TIM_Base handle pointer * @retval None */ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspDeInit 0 */ /* USER CODE END TIM3_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM3_CLK_DISABLE(); /* USER CODE BEGIN TIM3_MspDeInit 1 */ /* USER CODE END TIM3_MspDeInit 1 */ } } ``` 四、跑pwm 到RT-Thread API参考手册官网找到pwm_led.c例程(https://www.rt-thread.org/document/api/pwm_led_sample_8c-example.html) 直接全部复制,在之前的工程下建立pwm_led.c,复制代码后保存,把文件加入工程,查看是否有警告(按理说现在没有了)  但是编译后出错(小问题,而且按理说不应该出现该问题)  Error_Handler函数没有实体,所以到STM32CubeMX生成的文件(main.c)中找到该函数并重新加入(main.h)文件中,再编译后无误。   下载到开发版,使用串口调用pwm_led_sample命令后,红灯变为了呼吸灯。
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
疯洋CrazySheep
这家伙很懒,什么也没写!
文章
2
回答
4
被采纳
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
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
10
个答案
3
次被采纳
xiaorui
3
个答案
2
次被采纳
winfeng
2
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
KunYi
8
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
lizimu
2
篇文章
9
次点赞
swet123
1
篇文章
4
次点赞
Days
1
篇文章
4
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部