LESSON 05: Timer with STM32F1

posted in: Microcontroller | 0

1. About Timer

Timer is an integrated peripheral in most microcontrollers, providing users with many useful applications such as accurately determining a time interval, measuring – counting input pulses, controlling output waveforms , PWM pulse hashing, etc.

STM32f103C8 has all 7 timers but it includes 1 systick timer, 2 watchdog timer. So there are only 4 timers left for functions such as interrupt, timer base, PWM, Encoder, Input capture…. include:

  • Timer 1: Is it the Advanced – control Timer or the advanced control Timer with the most functions?
  • 3 sets of common Timer are Timer 2,3,4 which have similar functions and are independent of each other

In this article we will configure Timer 2 mode Time-base unit to change the led state on the Blue Pill Board.

The description of this function is as follows:

– Timer-Base unit mode is the main mode of Timer including counting modes: up, down or both up and down

– Clock pulse is divided by the prescaler to get the appropriate time to count 1 time

The management registers include:

  • Counter Register (TIMx_CNT): stores the count value
  • Prescaler Register (TIMx_PSC): stores the dividing value from the base frequency for the Timer to generate the appropriate frequency.
  • Auto-Reload Registor(TIMx_ARR): stores the target value count up or count down

– The Auto-Reload register will be loaded before the Timer operates, and can be loaded while the Timer is active, before an update event occurs (UEV).

– The counting mode will be active when the CEN bit of the TIMx_CR1 register is enabled

Prescaler is the frequency divider of the timer. This divider has a maximum value of 16 bits corresponding to a value of 65535.

Auto Reload value is the counter value to load into the timer. This counter value is set to a maximum of 16bit corresponding to a value of 65535. From the above parameters, we draw the final calculation formula that is:

fTIMER= fsystem/[(PSC+1)(Period+1)]
  • Ftimer : is the final value of the problem, the unit is hz.
  • fsystem : system clock frequency divided by timer used, unit is hz.
  • PSC : input value for timer frequency divider. The maximum is 65535.
  • Period : the counter value to be loaded into the timer. The maximum is 65535.

2. Programming with ST’s standard library

In this tutorial, we will use timer 2 to blink Led with 500ms . period

  • Timer 2 configuration program and overflow interrupt
void TIM2_Configuraion(void)
  /* khai bao bien*/
  TIM_TimeBaseInitTypeDef		TIM_TimeBaseStructure;
  TIM_OCInitTypeDef			TIM_OCInitStructure; 
   NVIC_InitTypeDef                     NVIC_InitStructure;
 /*Cap clock cho time 2*/
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
 /* cau hinh timer base */
  TIM_TimeBaseStructure.TIM_Prescaler = 7199;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseStructure.TIM_Period = 4999;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
 /* cho phep TIM2 hoat dong */
 /* Cau hình ngat NVIC */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn ;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

PSC = 7199, Period = 4999, the clock provided for TIMER 2 is 72Mhz. Calculating according to the formula, we will get the overflow interrupt time of 500ms.

  • GPIO configuration program
void GPIO_configuration(void)
  GPIO_InitTypeDef  GPIO_InitStructure; 		
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);	    		
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;			//LED0-->PC.13 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 	 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;	 
  GPIO_Init(GPIOC, &GPIO_InitStructure);	
  GPIO_ResetBits( GPIOC, GPIO_Pin_13);
  • Interrupt service routine in file stm32f10x_it.c
void TIM2_IRQHandler(void)

		if(ledstatus == 0)
			GPIO_WriteBit(GPIOC, GPIO_Pin_13, 1);
                        ledstatus = 1;
			GPIO_WriteBit(GPIOC, GPIO_Pin_13, 0);
                        ledstatus = 0;

                 /* Xoa co tran */
  • Program in function main
int main(void)
  while (1)

Sample code link

Leave a Reply

Your email address will not be published.