【HAL库每天一例】系列例程从今天开始持续更新。。。。。
我们将坚持每天至少发布一个基于YS-F1Pro开发板的HAL库例程,
该系列例程将带领大家从零开始使用HAL库,后面会持续添加模块应用例程。
同样的,我们还程序发布基于HAL库的指导文档和视频教程,欢迎持续关注,并提出改进意见。
例程下载:
资料包括程序、相关说明资料以及软件使用截图
百度云盘:https://pan.baidu.com/s/1slN8rIt 密码:u6m1 360云盘:https://yunpan.cn/OcPiRp3wEcA92u密码 cfb6
(硬石YS-F1Pro开发板HAL库例程持续更新1. 软件设计之基本裸机例程(HAL库版本)YSF1_HAL-067. tiM-六步PWM)
/**
******************************************************************************
* 硬石YS-F1Pro开发板例程功能说明
*
* 例程名称: YSF1_HAL-067. TIM-六步PWM
*
******************************************************************************
* 说明:
* 本例程配套硬石STM32开发板YS-F1Pro使用。
*
* 淘宝:
* 论坛:硬石电子社区
* 版权归硬石嵌入式开发团队所有,请勿商用。
******************************************************************************
*/
【1】例程简介
【2】跳线帽情况
【3】操作及现象
/******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/
bsp_AdvancedTIM.h文件内容
- #ifndef __ADVANCED_TIM_H__
- #define __ADVANCED_TIM_H__
- /* 包含头文件 ----------------------------------------------------------------*/
- #include "stm32f1xx_hal.h"
- /* 类型定义 ------------------------------------------------------------------*/
- /* 宏定义 --------------------------------------------------------------------*/
- #define ADVANCED_TIMx TIM1
- #define ADVANCED_TIM_RCC_CLK_ENABLE() __HAL_RCC_TIM1_CLK_ENABLE()
- #define ADVANCED_TIM_RCC_CLK_DISABLE() __HAL_RCC_TIM1_CLK_DISABLE()
- #define ADVANCED_TIM_GPIO_RCC_CLK_ENABLE() {__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();}
- #define ADVANCED_TIM_CH1_PORT GPIOA
- #define ADVANCED_TIM_CH1_PIN GPIO_PIN_8
- #define ADVANCED_TIM_CH2_PORT GPIOA
- #define ADVANCED_TIM_CH2_PIN GPIO_PIN_9
- #define ADVANCED_TIM_CH3_PORT GPIOA
- #define ADVANCED_TIM_CH3_PIN GPIO_PIN_10
- #define ADVANCED_TIM_CH1N_PORT GPIOB
- #define ADVANCED_TIM_CH1N_PIN GPIO_PIN_13
- #define ADVANCED_TIM_CH2N_PORT GPIOB
- #define ADVANCED_TIM_CH2N_PIN GPIO_PIN_14
- #define ADVANCED_TIM_CH3N_PORT GPIOB
- #define ADVANCED_TIM_CH3N_PIN GPIO_PIN_15
- #define ADVANCED_TIM_BKIN_PORT GPIOB
- #define ADVANCED_TIM_BKIN_PIN GPIO_PIN_12
- // 定义定时器预分频,定时器实际时钟频率为:72MHz/(ADVANCED_TIMx_PRESCALER+1)
- #define ADVANCED_TIM_PRESCALER 0 // 实际时钟频率为:72MHz
- // 定义定时器周期,当定时器开始计数到ADVANCED_TIMx_PERIOD值并且重复计数寄存器为0时更新定时器并生成对应事件和中断
- #define ADVANCED_TIM_PERIOD 2047 // 定时器产生中断频率为:1MHz/1000=1KHz,即1ms定时周期
- // 定义高级定时器重复计数寄存器值,
- #define ADVANCED_TIM_REPETITIONCOUNTER 0
- // 最终定时器频率计算为: 72MHz/(ADVANCED_TIMx_PRESCALER+1/(ADVANCED_TIM_REPETITIONCOUNTER+1)/ADVANCED_TIMx_PERIOD
- // 比如需要产生20ms周期定时,可以设置为: 72MHz/(359+1)/(9+1)/400=50Hz,即20ms周期
- // 这里设置 ADVANCED_TIMx_PRESCALER=359;ADVANCED_TIM_REPETITIONCOUNTER=9;ADVANCED_TIMx_PERIOD=400;
- /* 扩展变量 ------------------------------------------------------------------*/
- extern TIM_HandleTypeDef htimx;
- extern TIM_OC_InitTypeDef sPWMConfig1, sPWMConfig2, sPWMConfig3;
- /* 函数声明 ------------------------------------------------------------------*/
- void ADVANCED_TIMx_Init(void);
- #endif /* __ADVANCED_TIM_H__ */
- /******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/
复制代码
bsp_AdvancedTIM.c文件内容
- /**
- ******************************************************************************
- * 文件名程: bsp_AdvancedTIM.c
- * 作 者: 硬石嵌入式开发团队
- * 版 本: V1.0
- * 编写日期: 2015-10-04
- * 功 能: 高级定时器TIM1 & TIM8底层驱动程序
- ******************************************************************************
- * 说明:
- * 本例程配套硬石stm32开发板YS-F1Pro使用。
- *
- * 淘宝:
- * 论坛:http://www.ing10bbs.com
- * 版权归硬石嵌入式开发团队所有,请勿商用。
- ******************************************************************************
- */
- /* 包含头文件 ----------------------------------------------------------------*/
- #include "AdvancedTIM/bsp_AdvancedTIM.h"
- /* 私有类型定义 --------------------------------------------------------------*/
- /* 私有宏定义 ----------------------------------------------------------------*/
- /* 私有变量 ------------------------------------------------------------------*/
- TIM_HandleTypeDef htimx;
- /* Timer Output Compare Configuration Structure declaration */
- TIM_OC_InitTypeDef sPWMConfig1, sPWMConfig2, sPWMConfig3;
- /* 扩展变量 ------------------------------------------------------------------*/
- /* 私有函数原形 --------------------------------------------------------------*/
- /* 函数体 --------------------------------------------------------------------*/
- /**
- * 函数功能: 定时器硬件初始化配置
- * 输入参数: htim:定时器句柄类型指针
- * 返 回 值: 无
- * 说 明: 该函数被ADVANCED_TIMx_Init函数调用
- */
- void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- if(htim->Instance==ADVANCED_TIMx)
- {
- /* 定时器通道功能引脚端口时钟使能 */
- ADVANCED_TIM_GPIO_RCC_CLK_ENABLE();
-
- /* 定时器通道1功能引脚IO初始化 */
- GPIO_InitStruct.Pin = ADVANCED_TIM_CH1_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(ADVANCED_TIM_CH1_PORT, &GPIO_InitStruct);
- /* 定时器通道1互补通道功能引脚IO初始化 */
- GPIO_InitStruct.Pin = ADVANCED_TIM_CH1N_PIN;
- HAL_GPIO_Init(ADVANCED_TIM_CH1N_PORT, &GPIO_InitStruct);
- /* 定时器通道2功能引脚IO初始化 */
- GPIO_InitStruct.Pin = ADVANCED_TIM_CH2_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(ADVANCED_TIM_CH2_PORT, &GPIO_InitStruct);
- /* 定时器通道2互补通道功能引脚IO初始化 */
- GPIO_InitStruct.Pin = ADVANCED_TIM_CH2N_PIN;
- HAL_GPIO_Init(ADVANCED_TIM_CH2N_PORT, &GPIO_InitStruct);
-
- /* 定时器通道3功能引脚IO初始化 */
- GPIO_InitStruct.Pin = ADVANCED_TIM_CH3_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(ADVANCED_TIM_CH3_PORT, &GPIO_InitStruct);
- /* 定时器通道3互补通道功能引脚IO初始化 */
- GPIO_InitStruct.Pin = ADVANCED_TIM_CH3N_PIN;
- HAL_GPIO_Init(ADVANCED_TIM_CH3N_PORT, &GPIO_InitStruct);
-
- /* 定时器通道4功能引脚IO初始化 */
- GPIO_InitStruct.Pin = ADVANCED_TIM_BKIN_PIN;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(ADVANCED_TIM_BKIN_PORT, &GPIO_InitStruct);
- }
- }
- /**
- * 函数功能: 基本定时器初始化
- * 输入参数: 无
- * 返 回 值: 无
- * 说 明: 无
- */
- void ADVANCED_TIMx_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig;
- TIM_MasterConfigTypeDef sMasterConfig;
- TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
- htimx.Instance = ADVANCED_TIMx;
- htimx.Init.Prescaler = ADVANCED_TIM_PRESCALER;
- htimx.Init.CounterMode = TIM_COUNTERMODE_UP;
- htimx.Init.Period = ADVANCED_TIM_PERIOD;
- htimx.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;
- htimx.Init.RepetitionCounter = ADVANCED_TIM_REPETITIONCOUNTER;
- HAL_TIM_Base_Init(&htimx);
- HAL_TIM_OC_Init(&htimx);
-
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- HAL_TIM_ConfigClockSource(&htimx, &sClockSourceConfig);
-
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- HAL_TIMEx_MasterConfigSynchronization(&htimx, &sMasterConfig);
-
- sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE;
- sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE;
- sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
- sBreakDeadTimeConfig.DeadTime = 1;
- sBreakDeadTimeConfig.BreakState = TIM_BREAK_ENABLE;
- sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
- sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
- HAL_TIMEx_ConfigBreakDeadTime(&htimx, &sBreakDeadTimeConfig);
- sPWMConfig1.OCMode = TIM_OCMODE_TIMING;
- sPWMConfig1.Pulse = 2047;
- sPWMConfig1.OCPolarity = TIM_OCPOLARITY_HIGH;
- sPWMConfig1.OCNPolarity = TIM_OCNPOLARITY_HIGH;
- sPWMConfig1.OCFastMode = TIM_OCFAST_DISABLE;
- sPWMConfig1.OCIdleState = TIM_OCIDLESTATE_SET;
- sPWMConfig1.OCNIdleState = TIM_OCNIDLESTATE_SET;
- HAL_TIM_PWM_ConfigChannel(&htimx, &sPWMConfig1, TIM_CHANNEL_1);
- sPWMConfig2=sPWMConfig1;
- sPWMConfig2.Pulse = 2047;
- HAL_TIM_PWM_ConfigChannel(&htimx, &sPWMConfig2, TIM_CHANNEL_2);
- sPWMConfig3=sPWMConfig2;
- sPWMConfig3.Pulse = 2047;
- HAL_TIM_PWM_ConfigChannel(&htimx, &sPWMConfig3, TIM_CHANNEL_3);
- HAL_TIM_MspPostInit(&htimx);
-
- /* Enable the TIM1 global Interrupt & set priority */
- HAL_NVIC_SetPriority(TIM1_TRG_COM_IRQn, 0, 1);
- HAL_NVIC_EnableIRQ(TIM1_TRG_COM_IRQn);
- /*##-4- Configure the commutation event: software event ####################*/
- HAL_TIMEx_ConfigCommutationEvent_IT(&htimx, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);
- }
- /**
- * 函数功能: 基本定时器硬件初始化配置
- * 输入参数: htim_base:基本定时器句柄类型指针
- * 返 回 值: 无
- * 说 明: 该函数被HAL库内部调用
- */
- void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
- {
- if(htim_base->Instance==ADVANCED_TIMx)
- {
- /* 基本定时器外设时钟使能 */
- ADVANCED_TIM_RCC_CLK_ENABLE();
- }
- }
- /**
- * 函数功能: 基本定时器硬件反初始化配置
- * 输入参数: htim_base:基本定时器句柄类型指针
- * 返 回 值: 无
- * 说 明: 该函数被HAL库内部调用
- */
- void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
- {
- if(htim_base->Instance==ADVANCED_TIMx)
- {
- /* 基本定时器外设时钟禁用 */
- ADVANCED_TIM_RCC_CLK_DISABLE();
- }
- }
- /******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/
复制代码
0
|
|
|
|