The simplest timing configuration of stm32 basic timer

Publisher:CoboroLatest update time:2018-12-10 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

I have recently used the timing function of the timer. I learned that the timer function of stm32 is very powerful and has a lot of things. The datasheet also talks about it in a long paragraph. I will not go into details about it. Here I will only explain how to configure the simplest timing function configuration.


Environment description: MCU: stm32F4 series, using the general timer TIM3


The first thing to know is that TIM3 is mounted on the APB1 bus. For details, see STM32F417xx_datasheet-P.19. There is a block diagram called Device overview. Forget it, let's take a screenshot. 


Write the picture description here


What is the clock of APB1? It depends on how you configure it. Generally, it is in the SetSysClock() function. Here, I divide the system clock by 4. The system clock is 168MHz, so the clock on APB1 is 42MHz. Don't think that the clock of TIM3 is 42MHz, because APB1 to TIM3 has to go through a multiplier. This multiplier works only when the division coefficient of the previous system clock to APB1 is not 1. As we said before, APB1 is obtained by dividing the system clock by 4, so the multiplier is effective here, so the clock of TIM3 is the multiplication of APB1, that is, 84MHz.


OK, now that we have the input clock for the timer, we only need two parameters to configure the simple function of the timer: the prescaler and the auto-reload value.


Tout = ((arr+1)*(psc+1))/Tclk;


Where arr is the auto-reload value, psc is the pre-division value, and Tclk is the timer clock. 


For example, if I want a 1s timer, I can set: arr = 9999; psc = 8399; 


Here we should pay attention to the value range of arr and psc, the former is u32 and the latter is u16.


  NVIC_InitTypeDef NVIC_InitStructure;


  // TIM3 clock enable 

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);


  // Enable the TIM3 gloabal Interrupt 

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x3;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x3;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);


  TIM_TimeBaseStructure.TIM_Period          = arr;

  TIM_TimeBaseStructure.TIM_Prescaler       = psc;

  TIM_TimeBaseStructure.TIM_ClockDivision   = 0;

  TIM_TimeBaseStructure.TIM_CounterMode     = TIM_CounterMode_Up;

  //Upward counting mode is to count from 0 to arr. When counting to arr, a TIM_IT_Update interrupt is generated. There are other interrupts to choose from.


  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);


  /* TIM Interrupts enable */

  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);


  /* TIM3 enable counter */

  TIM_Cmd(TIM3, ENABLE);


In this way, the timer starts working, and then write the interrupt processing function.


void TIM3_IRQHandler(void)

{

    if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)

    {

        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);

        //your code

    }

}


At this point, the timing function of TIM3 is completed. According to the psc and arr values, we can set the desired timing time. 


Of course, this is just a very simple function of timing. There are many cool things. If there are any mistakes, please correct me!


Keywords:stm32 Reference address:The simplest timing configuration of stm32 basic timer

Previous article:Summary of the general timer interrupt experiment of STM32F107
Next article:STM32 learning general timer interrupt

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号