STM32 CubeMX使用问题

2019-07-21 03:37发布

大家平时做项目时用STM32 CubeMX这个工具多吗?我用这个工具自动生成的代码,定时器一直不工作,看了好半天代码也没发现有什么地方没配置好

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
13条回答
LearnerLi
2019-07-21 08:40
我确实发现这样一个错误,需要将HAL库中的函数TIM_CCxChannelCmd:
void TIM_CCxChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelState)
{
  uint32_t tmp = 0U;

  /* Check the parameters */
  assert_param(IS_TIM_CC1_INSTANCE(TIMx));
  assert_param(IS_TIM_CHANNELS(Channel));

  tmp = TIM_CCER_CC1E << Channel;

  /* Reset the CCxE Bit */
  TIMx->CCER &= ~tmp;

  /* Set or reset the CCxE Bit */
  TIMx->CCER |=  (uint32_t)(ChannelState << Channel);
}
修改为以下这样
void TIM_CCxChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelState)
{
    uint32_t tmp = 0U;

    /* Check the parameters */
    assert_param(IS_TIM_CC1_INSTANCE(TIMx));
assert_param(IS_TIM_CHANNELS(Channel));

    tmp = TIM_CCER_CC1E << (Channel - 1) * 4;

    /* Reset the CCxE Bit */
    TIMx->CCER &= ~tmp;

    /* Set or reset the CCxE Bit */
    TIMx->CCER |=  (uint32_t)(ChannelState << (Channel - 1) * 4);
}
并且需要在初始化后使用函数HAL_TIM_PWM_Start来开启定时器功能。

附:
CubeMX-V4.26.0
Firmware Package Version 1.6.1

你可以试试!

一周热门 更多>