Skip to main content
Visitor II
November 20, 2024
Solved

TIM2 not working on STM32F407VGT6

  • November 20, 2024
  • 1 reply
  • 577 views

I am working on a project where the end goal is that TIM8 is used to capture an incoming PWM signal on PC6.  I want TIM2 to trigger from TIM8 and generate a single pulse on PB11. I have TIM8 working, capturing both rising and falling edges properly.  However, after many attempts, I am not getting anything on TIM2. 

I decided to check the output itself, and PB11 can be set high or low just fine, set as open drain.  This rules out any hardware/PCB issues with the pin.  Next I tried running a continuous PWM on TIM2 and the pin stays high.

So I stepped through the debugger, and I noticed that TIM2 registers aren't getting set.  I tried manually setting them in the debugger and they immediately revert back to 0.

//This code works
TIM_HandleTypeDef HeaterTimer;

HeaterTimer.Instance = TIM4;
HeaterTimer.Init.Period = 0xFFFF;
HeaterTimer.Init.CounterMode = TIM_COUNTERMODE_UP;
HeaterTimer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HeaterTimer.Init.RepetitionCounter = 0;
HeaterTimer.Init.Prescaler = 11;
HeaterTimer.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HAL_TIM_PWM_Init (&HeaterTimer);

//This code does not work
TIM_HandleTypeDef HeaterTimer;
HeaterTimer.Instance = TIM2;
HeaterTimer.Init.Period = 0xFFFF;
HeaterTimer.Init.CounterMode = TIM_COUNTERMODE_UP;
HeaterTimer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HeaterTimer.Init.RepetitionCounter = 0;
HeaterTimer.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
HeaterTimer.Init.Prescaler = 11;
HAL_TIM_PWM_Init (&HeaterTimer);

I can use the debugger to manually change TIM4->CR1 to 0 or 1 and it works fine.
When I try to manually change TIM2->CR1 from 0 or 1, I get the following message:
Downloading 4 bytes @ address 0x40000000 - Verify failed (expected 0x01 read 0x00 @ 0x40000000)

What's wrong with TIM2?
    This topic has been closed for replies.
    Best answer by JPittenger

    I was missing this line:
    __HAL_RCC_TIM2_CLK_ENABLE();
    That fixed everything!

    1 reply

    JPittengerAuthorAnswer
    Visitor II
    November 20, 2024

    I was missing this line:
    __HAL_RCC_TIM2_CLK_ENABLE();
    That fixed everything!