时钟配置结构体
typedef struct
{
uint32_t ClockSource; / *TIM时钟源 * /
uint32_t ClockPolarity; / *TIM时钟极性 * /
uint32_t ClockPrescaler; / *TIM时钟预分频器 * /
uint32_t ClockFilter; / *TIM时钟过滤器 该参数可以是Min_Data = 0x0和Max_Data = 0xF * /
} TIM_ClockConfigTypeDef;
/***TIM从站配置结构定义*/
typedef struct {
uint32_t SlaveMode; / *从模式选择 该参数可以是的值TIM_Slave_Mode * /
uint32_t InputTrigger; /*输入触发源 此参数可以是的值TIM_Trigger_Selection * /
uint32_t TriggerPolarity; /*输入触发极性 此参数可以是的值TIM_Trigger_Polarity * /
uint32_t TriggerPrescaler; /*输入触发预分频器 此参数可以是的值TIM_Trigger_Prescaler * /
uint32_t TriggerFilter; /*输入触发器过滤器 该参数可以是Min_Data = 0x0和Max_Data = 0xF * /
} TIM_SlaveConfigTypeDef;
这里将定时器几种运行模式相关的结构体放在一起对比,一般就是有以下五种定时器模式初始化结构体。
几个参数的简单说明:
(1) IC1Polarity
可以取值为: TIM_ICPOLARITY_RISING、TIM_ICPOLARITY_FALLING、TIM_ICPOLARITY_BOTHEDGE
(2) IC1Selection
可以取值为:TIM_ICSELECTION_DIRECTTI /*!< TIM Input 1, 2, 3 or 4 is selected to be
connected to IC1, IC2, IC3 or IC4, respectively */
TIM_ICSELECTION_INDIRECTTI /*!< TIM Input 1, 2, 3 or 4 is selected to be
connected to IC2, IC1, IC4 or IC3, respectively */
TIM_ICSELECTION_TRC /*!< TIM Input 1, 2, 3 or 4 is selected to be connected to TRC */
(3) IC1Prescaler
可以取值为: TIM_ICPSC_DIV1 /*!< Capture performed each time an edge is detected on the capture input */
TIM_ICPSC_DIV2 /*!< Capture performed once every 2 events */
TIM_ICPSC_DIV3 /*!< Capture performed once every 3 events */
TIM_ICPSC_DIV4 /*!< Capture performed once every 4 events */
第一种结构体用的较多,并作为定时器句柄的一部分用来初始化定时器。
第五种结构体在编码器模式下,用于初始化编码器。
中间三种模式被用来初始化相应类型的模式,函数如下所示:
HAL_StatusTypeDef HAL_TIM_OC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel);
HAL_StatusTypeDef HAL_TIM_PWM_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OC_InitTypeDef* sConfig, uint32_t Channel);
HAL_StatusTypeDef HAL_TIM_IC_ConfigChannel(TIM_HandleTypeDef *htim, TIM_IC_InitTypeDef* sConfig, uint32_t Channel);
HAL_StatusTypeDef HAL_TIM_OnePulse_ConfigChannel(TIM_HandleTypeDef *htim, TIM_OnePulse_InitTypeDef* sConfig, uint32_t OutputChannel, uint32_t InputChannel);
下面是一个测试例子,将定时器的各种模式分别设置了一个,观看程序的初始化过程。
四个通道分别设置了输出比较模式,PWM模式,强制有效,输入捕获模式以及单脉冲模式。
生成的相应代码如下:
static void MX_TIM1_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig;
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC;
TIM_IC_InitTypeDef sConfigIC;
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;
/*定时器的基本初始化**************************************/
htim1.Instance = TIM1;
htim1.Init.Prescaler = 0;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/*时钟配置***********************************************/
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_OC_Init(&htim1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_IC_Init(&htim1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
if (HAL_TIM_OnePulse_Init(&htim1, TIM_OPMODE_SINGLE) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/*主从模式设置*********************************************/
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/*输入、输出通道模式设置******************************************/
sConfigOC.OCMode = TIM_OCMODE_TIMING;
sConfigOC.Pulse = 100;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
/*(1)输出比较模式通道设置*************************************/
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/*(2)PWM输出模式通道设置************************************************/
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 200;
if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/*(3)强制输出模式通道设置*********************************************/
sConfigOC.OCMode = TIM_OCMODE_FORCED_ACTIVE;
sConfigOC.Pulse = 300;
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/*(4)输入捕获模式的 通道设置****************************************/
sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
sConfigIC.ICFilter = 0;
if (HAL_TIM_IC_ConfigChannel(&htim1, &sConfigIC, TIM_CHANNEL_4) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/*刹车与死区时间配置*******************************************/
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_TIM_MspPostInit(&htim1);
}