cancel
Showing results for 
Search instead for 
Did you mean: 

Looking for a STM32CUBMX example that blinks an LED on PA8 using Timer 1

Mike Burmeister
Associate II
Posted on June 20, 2017 at 18:31

Have an STM32415RG chip and would like an example using Timer1 blinking an LED on PA8 using either PWM or alternating.

#timers
12 REPLIES 12
Posted on June 21, 2017 at 15:48

Yes, right. HAL is good for quick setup but in case of any issue you need to dig deeper. LL is the next choice OR stay with the registry level programming digging in datasheet. It is always balance between 'easy to setup simple stuff' and optimization at the low level.

Posted on June 21, 2017 at 16:52

Can't this be cubic manifestation of the 'TIM1/TIM8 need MOE to be set' problem?

JW

Mike Burmeister
Associate II
Posted on June 23, 2017 at 13:50

Ok, now for the answer which doesn't seem easy.

First we want to pulse the LED 10 times a second or every 10th of a second so we can see it blink.

In the STM32CUB software we select RCC.HSE=Crystal as I am using a 1Bitsy board.  I click on PA8 and select TIM1_CH1. 

In TIM1 I select Clock source Internal Clock.  Channel 1 I select PWM Generation CH1.

In clock configuration tab I set the HCLK to 168 Mhz as that is what I have.  The causes a re-calculate of the inputs.  I then select HSE as it wanted to use HSI and re-calculate again.  Now I set APB2 to 16 to divide the internal clock by 16 to give me a 21Mhz clock for Timer 1.

On the configuration tab I select TIM1 and set my PSC=80 and ARR=26250 and set PWM Pulse value at 13125 for a 50% duty cycle for each pulse produced.  So 21,000,000 / 80 / 26250 = 10 pulses per.

Push the generate button and select my project name and set my toolchain to SW4STM32.  Open the main routine of the generated code and add the following line to start the TIM1 on its way.

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_TIM1_Init();

  /* USER CODE BEGIN 2 */

  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);

  /* USER CODE END 2 */

Build the code and load it onto the board and it blinks.

Well while it does what it was suppose to do it is actually not totally correct.  If you place a scope on the line you will see that it is not exactly 10 pulses per second.  Its more like 9.8 pulses per second.  So what gives.  Well if you read the timer manual you will see the formula as Update_event = TIM_CLK/((PSC + 1)*(ARR + 1)*(RCR + 1))

So really the PSC should be 79 and the ARR should be 26249 to account for the one clock cycle that is added to each value.

So you go into the STM32CUB software and pull up the timer and change those values.  Then hit the generate button and go over to SW4STM32 and hit the compile button.  Load the code onto the board and now the pulse is 10.

Now for the hard part.  I want to generate 1 pulse every 20 milliseconds that last between 1 and 2 milliseconds.  If you know anything about RC you will know that is what is needed to drive a servo.

Mike