STM32CubeMX Series|Timer Interrupt

Timer interrupt

1. Introduction to Timer Interrupt

The timer function of STM32 is very powerful, including advanced timers (TIM1 and TIM8), general timers (TIM2~TIM5) and basic timers (TIM6 and TIM7); this experiment mainly introduces general timers with moderate difficulty, general timers It is a 16-bit auto-loading counter driven by a programmable prescaler. It is suitable for many occasions, including measuring the pulse length of the input signal (input capture) or generating output waveforms (output comparison and PWM). Using the timer prescaler and the RCC clock controller prescaler, the pulse length and waveform period can be adjusted from several microseconds to several milliseconds. Each timer is completely independent and does not share any resources with each other.
Common TIMx (TIM2, TIM3, TIM4 and TIM5) timer functions include:

  • 16-bit up, down, up/down auto load counter
  • 16-bit programmable (can be modified in real time) prescaler, the division coefficient of the counter clock frequency is any value between 1 and 65536
  • 4 independent channels:
    ─ Input capture
    ─ Output comparison
    ─ PWM generation (edge ​​or center alignment mode)
    ─ Single pulse mode output
  • Use external signal to control timer and timer interconnection synchronization circuit
  • Interrupt/DMA is generated when the following events occur:
    ─ Update: Counter overflow/downflow, counter initialization (by software or internal/external trigger)
    ─ Trigger event (counter start, stop, initialization or count by internal/external trigger)
    ─ Input capture
    -output comparison
  • Supports incremental (quadrature) encoder and Hall sensor circuits for positioning
  • Trigger input as external clock or cycle current management

General purpose timer block diagram

Insert picture description here
Timer time base part

Insert picture description here

2. Hardware design

This experiment uses the interrupt of TIM3 to control the on and off of D1, which requires hardware resources

  • D1 and D2 indicator lights
  • Timer TIM3

Insert picture description here

3. Software design

3.1 STM32CubeMX settings
  • RCC set external HSE, clock set to 72MHz; TIM3 clock mounted on APB1 Time Clocks is 72MHz
  • PC0/PC1 is set to GPIO push-pull output mode, pull-up, high-speed, and the default output level is high
  • Activate the TIM3 timer, select the internal clock as the clock source, set the PSC prescaler to 7200-1, count up, and set the auto reload value (ARR) to 10000-1. Activate the TIM3 timer interrupt in the NVIC setting; according to the formula It can be calculated: the counter clock CK_CNT = 72M/7200 = 10000Hz, the timer interruption time is ARR/10000 = 1s

Insert picture description here

  • Enter the project name, select the project path (no Chinese), select MDK-ARM V5; check Generated periphera initialization as a pair of'.c/.h' files per IP; click GENERATE CODE to generate the project code
3.2 MDK-ARM programming
  • You can see the initialization function of the timer in the tim.c file
void MX_TIM3_Init(void){
    
    
  TIM_ClockConfigTypeDef sClockSourceConfig = {
    
    0};
  TIM_MasterConfigTypeDef sMasterConfig = {
    
    0};

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 7200-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 10000-1;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK){
    
    
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK){
    
    
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK){
    
    
    Error_Handler();
  }
}

Find the weak symbol cycle to run the callback function prototype, and customize the callback function in tim.c
__weak void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
    
    
	if(htim == &htim3){
    
    
		HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_0);	//LED1状态每1s翻转一次
	}
}
  • Write relevant code in the main function, and make LED2 flip every 500ms in while
int main(void){
    
    
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start_IT(&htim3);		//启动定时器中断模式计数
  /* USER CODE END 2 */
  while (1){
    
    
    HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_1);	//LED2状态每500ms翻转一次
	HAL_Delay(500);
  }
}

4. Download verification

After the compilation is correct, download to the development board, you can see that the state of LED1 flips once every 1s, and the state of LED2 flips once every 500ms.

Guess you like

Origin blog.csdn.net/Chuangke_Andy/article/details/108590136
Recommended