问题发生过程
使用CubeMX配置管脚,时钟后生成代码,由于要使用串口1和同时驱动四个舵机,所以这么设置,串口1重映射的管脚已经用作其他功能,所以只能用PA9PA10做串口1的复用管脚
然后初始化定时器 占空比预设好
HAL_TIM_Base_Start(&htim1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
然后示波器查看PB14 B15无输出,PA8,PA11有输出。
在cubemx中将通道换回普通通道,暂时禁用串口时,通道2通道3(PA9 PA10)可以正常输出PWM,但是其反相通道CH2N CH3N(PB14 PB15)无输出
解决问题的过程
首先猜想是重定向IO的问题,于是查看数据手册中的管脚默认复用功能以及重定向配置:
可以看出,使用PB14 PB15在TIM1中是不需要设置重定向寄存器的
删除掉
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
后PB14 PB15 PA8可以正常输出pwm但是PA11不能输出PWM,这和我需要四通道都输出要求不符合.
单独使用开启函数开启通道2,通道3时无反应
查看参考手册中PWM产生相关的寄存器和其解释
于是定位到库函数中HAL_TIM_PWM_Start函数的位置
HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
{
uint32_t tmpsmcr;
/* Check the parameters */
assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
/* Enable the Capture compare channel */
TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
{
/* Enable the main output */
__HAL_TIM_MOE_ENABLE(htim);
}
/* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
{
__HAL_TIM_ENABLE(htim);
}
/* Return function status */
return HAL_OK;
}
函数体中只发现了
TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1
查看这个函数的结构
/**
* @brief Enables or disables the TIM Capture Compare Channel x.
* @param TIMx to select the TIM peripheral
* @param Channel specifies the TIM Channel
* This parameter can be one of the following values:
* @arg TIM_CHANNEL_1: TIM Channel 1
* @arg TIM_CHANNEL_2: TIM Channel 2
* @arg TIM_CHANNEL_3: TIM Channel 3
* @arg TIM_CHANNEL_4: TIM Channel 4
* @param ChannelState specifies the TIM Channel CCxE bit new state.
* This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
* @retval None
*/
void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
{
uint32_t tmp;
/* Check the parameters */
assert_param(IS_TIM_CC1_INSTANCE(TIMx));
assert_param(IS_TIM_CHANNELS(Channel));
tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
/* Reset the CCxE Bit */
TIMx->CCER &= ~tmp;
/* Set or reset the CCxE Bit */
TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
}
定位TIM_CCx_ENABLE
#define TIM_CCx_ENABLE 0x00000001U /*!< Input or output channel is enabled */
#define TIM_CCx_DISABLE 0x00000000U /*!< Input or output channel is disabled */
#define TIM_CCxN_ENABLE 0x00000004U /*!< Complementary output channel is enabled */
#define TIM_CCxN_DISABLE 0x00000000U /*!< Complementary output channel is enabled */
可以发现这个函数修改的是通道启停的寄存器
再定位到参考手册中对TIMx_CCERx寄存器的描述
给对应的通道使能打钩可以在示波器上观察到波形输出和消失
于是在main函数中写下
TIM_CCxChannelCmd(htim1.Instance, TIM_CHANNEL_2, TIM_CCxN_ENABLE);
TIM_CCxChannelCmd(htim1.Instance, TIM_CHANNEL_3, TIM_CCxN_ENABLE);
烧录之后 PB14,PB15脚正常输出
PA8,PB14,PB15,PA11四个脚可以正常驱动舵机了
问题发生过程
使用CubeMX配置管脚,时钟后生成代码,由于要使用串口1和同时驱动四个舵机,所以这么设置,串口1重映射的管脚已经用作其他功能,所以只能用PA9PA10做串口1的复用管脚
然后初始化定时器 占空比预设好
HAL_TIM_Base_Start(&htim1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
然后示波器查看PB14 B15无输出,PA8,PA11有输出。
在cubemx中将通道换回普通通道,暂时禁用串口时,通道2通道3(PA9 PA10)可以正常输出PWM,但是其反相通道CH2N CH3N(PB14 PB15)无输出
解决问题的过程
首先猜想是重定向IO的问题,于是查看数据手册中的管脚默认复用功能以及重定向配置:
可以看出,使用PB14 PB15在TIM1中是不需要设置重定向寄存器的
删除掉
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
后PB14 PB15 PA8可以正常输出pwm但是PA11不能输出PWM,这和我需要四通道都输出要求不符合.
单独使用开启函数开启通道2,通道3时无反应
查看参考手册中PWM产生相关的寄存器和其解释
于是定位到库函数中HAL_TIM_PWM_Start函数的位置
HAL_StatusTypeDef HAL_TIM_PWM_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
{
uint32_t tmpsmcr;
/* Check the parameters */
assert_param(IS_TIM_CCX_INSTANCE(htim->Instance, Channel));
/* Enable the Capture compare channel */
TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
if (IS_TIM_BREAK_INSTANCE(htim->Instance) != RESET)
{
/* Enable the main output */
__HAL_TIM_MOE_ENABLE(htim);
}
/* Enable the Peripheral, except in trigger mode where enable is automatically done with trigger */
tmpsmcr = htim->Instance->SMCR & TIM_SMCR_SMS;
if (!IS_TIM_SLAVEMODE_TRIGGER_ENABLED(tmpsmcr))
{
__HAL_TIM_ENABLE(htim);
}
/* Return function status */
return HAL_OK;
}
函数体中只发现了
TIM_CCxChannelCmd(htim->Instance, Channel, TIM_CCx_ENABLE);
1
查看这个函数的结构
/**
* @brief Enables or disables the TIM Capture Compare Channel x.
* @param TIMx to select the TIM peripheral
* @param Channel specifies the TIM Channel
* This parameter can be one of the following values:
* @arg TIM_CHANNEL_1: TIM Channel 1
* @arg TIM_CHANNEL_2: TIM Channel 2
* @arg TIM_CHANNEL_3: TIM Channel 3
* @arg TIM_CHANNEL_4: TIM Channel 4
* @param ChannelState specifies the TIM Channel CCxE bit new state.
* This parameter can be: TIM_CCx_ENABLE or TIM_CCx_DISABLE.
* @retval None
*/
void TIM_CCxChannelCmd(TIM_TypeDef *TIMx, uint32_t Channel, uint32_t ChannelState)
{
uint32_t tmp;
/* Check the parameters */
assert_param(IS_TIM_CC1_INSTANCE(TIMx));
assert_param(IS_TIM_CHANNELS(Channel));
tmp = TIM_CCER_CC1E << (Channel & 0x1FU); /* 0x1FU = 31 bits max shift */
/* Reset the CCxE Bit */
TIMx->CCER &= ~tmp;
/* Set or reset the CCxE Bit */
TIMx->CCER |= (uint32_t)(ChannelState << (Channel & 0x1FU)); /* 0x1FU = 31 bits max shift */
}
定位TIM_CCx_ENABLE
#define TIM_CCx_ENABLE 0x00000001U /*!< Input or output channel is enabled */
#define TIM_CCx_DISABLE 0x00000000U /*!< Input or output channel is disabled */
#define TIM_CCxN_ENABLE 0x00000004U /*!< Complementary output channel is enabled */
#define TIM_CCxN_DISABLE 0x00000000U /*!< Complementary output channel is enabled */
可以发现这个函数修改的是通道启停的寄存器
再定位到参考手册中对TIMx_CCERx寄存器的描述
给对应的通道使能打钩可以在示波器上观察到波形输出和消失
于是在main函数中写下
TIM_CCxChannelCmd(htim1.Instance, TIM_CHANNEL_2, TIM_CCxN_ENABLE);
TIM_CCxChannelCmd(htim1.Instance, TIM_CHANNEL_3, TIM_CCxN_ENABLE);
烧录之后 PB14,PB15脚正常输出
PA8,PB14,PB15,PA11四个脚可以正常驱动舵机了
举报