I'm working on a project where I need the on-board LEDs to respond to PWM from the timers in order to make the LED pulsate over a given time period. I've been able to get the LED to pulsate (blue LED - LD6 mapped to PD15) however I'm having problems controlling the time of the pulse. I also have limited experience with this topic and all the code I've so far compiled has been through guess-work, online-examples and my limited understanding of the documentation.

My timer configurations for TIM1 and TIM4 are as follows:


Clock Source: Internal Clock
Prescaler: 1600
Counter Period: 500
NVIC: TIM1 update interrupt and TIM10 global interrupt


Clock source: Internal Clock
Channel 4: PWM Generation No Output
Prescaler: 8
Counter Period: 100
NVIC: TIM4 global interrupt

My understanding is that I've configured TIM1 to interrupt at 20 Hz or every 50 ms (I use this to change the PWM value) and TIM4 is configured to send PWM values to the LED at 20 kHz.

My code to change the PWM value is located in 'stm32f4xx_it.c':

  if (eDir == UP)
      if(ui8TimPulse >= PWM_MAX)
        eDir = DOWN;
        ui8TimPulse += PWM_MIN_CHANGE;
    if (ui8TimPulse <= PWM_MIN)
      eDir = UP;
      ui8TimPulse -= PWM_MIN_CHANGE;

I've defined the constants in main.h as:

  #define PWM_MIN 4
  #define PWM_MAX 100
  #define PWM_MIN_CHANGE 1

I also have code in TIM4 (stm32f4xx_it.c) to control the cycles (I think):

TIM4->CCR4 = (htim4.Init.Period * ui8TimPulse) / 100u;

My problem is that all the pulses seem to only last 1 second and they only seem to pulse towards a state of maximum brightness before seemingly dropping back to an OFF state and starting again. I've tried changing the constants (what I'm showing are the default values although I've played with them extensively) but nothing seems to stretch the time of the pulse out.

To achieve this, should I be working in my code to change the PWM_MAX value? or the TIM4->CCR4 equation? Or should this be configured in the timer configurations? Any feedback on this would be much appreciated.

  • \$\begingroup\$ what is the internal clock speed? an image of the clock tree settings would help. \$\endgroup\$ Jun 4, 2020 at 6:41
  • \$\begingroup\$ I believe internal clock speed is 16 MHz (datasheet says internal 16 MHz factory-trimmed RC). Does this mean I should I be changing the prescaler/counter-period values in my TIM4 config to modify how long the pulses last? \$\endgroup\$
    – Saturn
    Jun 4, 2020 at 11:26

1 Answer 1


this is the default clock tree of STM32F4 discovery board, initialized with STM32CubeMX (with or without choosing default peripherals settings). the default clock for timers is 12.5 MHz (depending on which bus timer is connected to, refer to datasheet, in you case, both are connected to APB1).

enter image description here

also, using interrupts for TIM4 to drive the LED is not a good practice. you should use PWM generation CH4 in CubeMX as the blue LED is connected to PD15, which is CH4 of TIM4 itself. (the PD15 ouput is used for another purpose in the board at startup settings, you have to click on the pin in the image in CubeMX choose reset state. then you can choose PWM generation CH4 in the TIM4 settings.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.