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
1 ACCEPTED SOLUTION

Accepted Solutions
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

View solution in original post

12 REPLIES 12
Posted on June 20, 2017 at 18:40

HAL or CubeMX?

Wouldn't CubeMX just need to you bang in some GPIO and TIM settings, click on a few pins, and add a few lines of code?

May be check YouTube videos for any STM32 family part?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Mike Burmeister
Associate II
Posted on June 20, 2017 at 19:34

Just looking for the project file from cub mx to see why my example does not work.  It should be as simple as click the timer and set the pin to output.  LED does not blink though.

Posted on June 20, 2017 at 22:35

Just took nucleo-F411, 84MHz clock, default Cube project:.

1. Enable Timer2 (in my case this one could drive the onboard LED connected to PA5)

0690X00000607OyQAI.png

2. Configure Timer 2

0690X00000607R3QAI.png

Since I have 84MHz clock I used 8400 as prescaler, Period of 10000, 5000 for pulse.

3. Generate the code and add line to start the Timer:

/* USER CODE BEGIN WHILE */

    HAL_TIM_PWM_Start(&htim2,TIM_CHANNEL_1);

  while (1)

  {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }

  /* USER CODE END 3 */
Posted on June 20, 2017 at 22:35

It works fine with the latest Cube 4.21.0

Posted on June 20, 2017 at 22:44

Here you have project file:

 
john doe
Lead
Posted on June 21, 2017 at 04:21

you could also do it without pwm by using the overflow callback

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

    if (htim->Instance == TIM1)

    {

       HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_8);

    }

}

and start the timer with     HAL_TIM_Base_Start_IT(tim_baseHandle);

Mike Burmeister
Associate II
Posted on June 21, 2017 at 13:04

Ok, Using Timer 2 is cheating.  Timer one only has Output Compare as an option which I set to Toggle mode and 32767 so it's 50% duty cycle.

The LED is either on or off but does not flash.

Using an interrupt is nice to know but also cheating as the unit advertises it can control this pin with no additional code.

At least I know my code is on the right track.

Posted on June 21, 2017 at 14:59

Not sure what you mean by 'cheating'. You wanted either PWM or toggle solution in your original email..

Mike Burmeister
Associate II
Posted on June 21, 2017 at 15:12

Right, I wanted a solution using Timer 1 driving PA8 to an on off state.  I guess the example provide were helpful in determining what might be wrong with my example.

My impression is that the STM32CUB software is easy to use and builds most of the code needed to initialize the hardware.  Was thinking this would be easy for anyone to build the example I was looking for in just minutes and I am the only person having problems using the tool.

Many people hate using the HAL as it is not well documented and confusing to use.  Problems happen and are sometime hidden in the HAL making it harder to debug issues with the code.

I think I am closer now to getting this simple example working but thought this should be a piece of cake to do.