Working with STM32 and Timers: Counting external clock

In this guide, we shall use external clock mode of timer in STM32 to count the external events and toggle the LED. (2 presses to turn on and 1 press to turn off).

In this guide, we shall cover the following:

  • What is ETR mode and it’s application.
  • GPIO Configuration.
  • Timer configuration.
  • Code.
  • Results.

1. What is ETR Mode:

In STM32 microcontrollers, ETR stands for External Trigger. ETR mode in STM32 timers refers to a mode where the timer’s counting can be triggered or controlled by an external signal applied to a dedicated input pin.

Here’s how ETR mode typically works:

  1. External Trigger Input (ETR): The ETR mode allows the timer to be triggered by an external signal applied to a dedicated pin, often labeled as ETR or TRGI (Trigger Input). This external signal can be generated by another device or module in the system.
  2. Trigger Source and Edge: You can configure the timer to trigger on a specific edge of the external signal (e.g., rising edge, falling edge, or both). This configuration allows flexibility in synchronizing the timer with external events.
  3. Application: ETR mode is commonly used in applications where precise timing or synchronization is required, such as in motor control, industrial automation, or communication systems. For example, the timer can be triggered by an external sensor or an event from another subsystem.

In other word, it allow the timer to use external clock source rather than the internal one.

2. GPIO Configuration:

Before we configure the GPIO, we need to find which pin is the ETR pin of STM32 Timer 2.

From the datasheet of STM32F407:

We can find the following informations:

  • PA0 is the ETR for timer 2.
  • The alternate function number is 0x01

We also find that TIM2_CH2 is connected to PA2 which shall be used to toggle the LED.

Hence, we can configure the PA0 and PA1 using the following steps:

  • Enable clock access to GPIOA.
  • Set PA0 and PA1 as alternate function.
  • Set which alternate function to be used:
	/*GPIO Setup*/
	#define TIM2_AF 0x01

	RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN;

	GPIOA->MODER|=GPIO_MODER_MODE0_1|GPIO_MODER_MODE1_1;
	GPIOA->MODER&=~(GPIO_MODER_MODE0_0|GPIO_MODER_MODE1_0);

	GPIOA->AFR[0]|=(TIM2_AF<<GPIO_AFRL_AFSEL0_Pos)|(TIM2_AF<<GPIO_AFRL_AFSEL1_Pos);

Thats all for timer configuration.

3. Timer Configuration:

First enable clock access to TIM2 as following:

	RCC->APB1ENR|=RCC_APB1ENR_TIM2EN;

Set the maximum value to be counted is two: (ARR register is the maximum value to be counted)

	TIM2->ARR=0x02;

Since STM32F407-Discovery board features push-button connected to PA0 with pull-down resistor, we need to set the detection to be falling edge as following:

From slave mode control register, set External trigger polarity to be inverted:

	TIM2->SMCR|=TIM_SMCR_ETP;

Set the external trigger filter to be maximum:

	TIM2->SMCR|=TIM_SMCR_ETF;

Enable External Clock as following:

	TIM2->SMCR|=TIM_SMCR_ECE;

Next, set CH2 to be PWM Mode2 as following:

	TIM2->CCMR1|=TIM_CCMR1_OC2M;

Enable CH2:

TIM2->CCER|=TIM_CCER_CC2E;

Set the CCR2 value to 2:

	TIM2->CCR2=0x02;

Finally, enable the timer:

	TIM2->CR1|=TIM_CR1_CEN;

4. Code:

#include "stm32f4xx.h"



void TIM2_ETR_Init(void);


int main(void)
{
	TIM2_ETR_Init();
	while(1)
	{

	}
}


void TIM2_ETR_Init(void)
{

	/*GPIO Setup*/
	#define TIM2_AF 0x01

	RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN;

	GPIOA->MODER|=GPIO_MODER_MODE0_1|GPIO_MODER_MODE1_1;
	GPIOA->MODER&=~(GPIO_MODER_MODE0_0|GPIO_MODER_MODE1_0);

	GPIOA->AFR[0]|=(TIM2_AF<<GPIO_AFRL_AFSEL0_Pos)|(TIM2_AF<<GPIO_AFRL_AFSEL1_Pos);


	/*Timer setup*/

	RCC->APB1ENR|=RCC_APB1ENR_TIM2EN;

	TIM2->ARR=0x02;

	TIM2->SMCR|=TIM_SMCR_ETP;

	TIM2->SMCR|=TIM_SMCR_ETF;

	TIM2->SMCR|=TIM_SMCR_ECE;

	TIM2->CCMR1|=TIM_CCMR1_OC2M;

	TIM2->CCER|=TIM_CCER_CC2E;

	TIM2->CCR2=0x02;

	TIM2->CR1|=TIM_CR1_CEN;


}

Notice how the while loop is empty since this is hardware level.

5. Results:

https://youtu.be/6jqdk_hXWuM

Note: connect a jumper wire from PA1 to PD13 to get the LED working.

Happy coding 🙂

Add Comment

Your email address will not be published. Required fields are marked *