How to use the Timer in Gated Mode

This is the 10th tutorial in the STM32 Timer series, and today we will be looking at another application of the timer in the slave mode, i.e. Using the Gated Mode.

In Gated mode, both the start and stop of the counter of the slave timer can be controlled using a trigger signal. The counter can be programmed to run while the trigger signal is either HIGH or LOW. Although the counter does not reset automatically.

I will use a button for the trigger signal. When the button will be pressed, the trigger signal will go LOW and the counter will start running. The timer will output a PWM signal at this point. When the button is released, the trigger signal will go HIGH, the counter will stop and so does the PWM signal.

We will also see how to automate the process without the need of using the button for the trigger. Basically we will use another timer to generate the trigger signal periodically, and this timer will act as the master timer.

Using button as the trigger

CubeMX Setup

Below is the configuration for the TIM1 in the Slave Gated Mode.

  • The Timer is configured in the Gated Mode with Trigger source being selected as the TI1FP1, basically the channel 1.
  • The channel 2 is configured to output a PWM signal.
  • The pin PA8 is the CH1 pin where we will connect the button to and the pin PA9 is the CH2 pin where the PWM will be generated.
  • The TIM1 is connected to APB2 BUS, which is running at 180MHz. So a PSC of 180 along with the ARR value of 1000 will bring down the clock to 1KHz.
  • This will be the Frequency of the PWM signal.
  • Also the Pulse value is set to 500, which is 50% of the ARR. Thus making the duty cycle 50%.
  • The trigger polarity is set to falling edge, therefore the counter will only operate as long as the signal is LOW.

The code

There is not much in the code. We will simply start the timer in the PWM mode.

HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);

Here I am using the timer 1 channel 2 to generate the PWM signal.


connection

As shown above, the button is connected between the GND and the pin PA8. The pin PA9 is connected to the Logic Analyzer, where we will see the output of both the button and the PWM.


Result

Below is the GIF showing the working.

As you can see above, whenever the trigger signal goes low we can see the PWM being generated.

Basically when the button is pressed, the pin PA8 is pulled to the ground. As soon as this trigger signal goes LOW, the counter resumes and the PWM is generated. And when the button is released, the pin PA8 is pulled back to high, therefore the trigger signal goes HIGH again. The counter stops at this point and since the counter is not running, the PWM signal also stops.

You can see the detailed working in the video at the end of this post.



Periodic Trigger Signal

We saw how to use the button as the trigger signal, now we will see how can we automate this process. I am going to use another timer to generate a trigger signal periodically.

Below is the picture from the MCU reference manual showing the Internal Trigger System in the F446RE.

As you can see above, the TIM1 is a slave to TIM2 and can be triggered by the ITR1 signal.


cubeMX setup

Below is the configuration for both the TIM1.

The only change I made here is that the trigger source has been changed to ITR1. Also note that the PA8 has been disabled now since the trigger source is set to internal trigger between the timers.

Below is the configuration for both the TIM2.

  • The TIM2 Channel 1 is configured to generate the PWM with no output. The output will be fed to the internal Trigger signal.
  • The TIM2 is connected to APB1 BUS, which is running at 90MHz. So a PSC of 90 along with the ARR value of 100000 will bring down the clock to 10Hz.
  • This will be the Frequency of the PWM signal.
  • Also the Pulse value is set to 50000, which is 50% of the ARR. Thus making the duty cycle 50%.
  • Basically the trigger signal will remain HIGH and LOW for equal amount of time, i.e. 50ms.
  • The Trigger Event is set to OC1REF, i.e. the output compare of channel 1.

The Code

  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);

Here we will start both the timers in PWM mode. The TIM1 channel 2 is connected to the Logic analyzer and the TIM2 channel 1 will provide the trigger signal.


Result

As you can see above, the PWM is being generated at fixed intervals.
The signal is disabled for 50ms and PWM is also being generated for 50ms.
The PWM frequency is 1KHz and the duty cycle is 50%.

Check out the Video Below




Info

You can help with the development by DONATING Below.
To download the project, click the DOWNLOAD button.

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
keyboard_arrow_up