Calculation of the timing time of the general timer of the STM32 microcontroller

Publisher:PeacefulWarriorLatest update time:2018-03-20 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    There are many types of timers in STM32, which are divided into 2 advanced controller timers, 4 ordinary timers, 2 basic timers, 2 watchdog timers, and 1 system tick timer SysTICk according to their functions.

    The key to the timer is the calculation of the timing time. For example, when using a timer to control the switch of a relay, it needs to be delayed for a period of time before closing or opening, and the timer timing is indispensable at this time.

    General timer timing calculation. Basic implementation of 1 second interrupt:

 

    The input clock of the general timer module is divided to obtain the clock of the counter, which is represented by CK_CNT. The coefficient of the pre-divider is: TIMx_PSC. When TIMx_PSC=0, it means no division, when =1, it means 2 division. And so on.

    The formula is: CK_CNT=fclk_PSC/(PSC[15:0]+1), where the maximum value of PSC is 65535.

    The second step is to set the count value of the TIM5 counter. The TIM5 counter uses CK_CNT as the clock to count. When the count reaches the set value, an interrupt is generated.

    (1/counting clock after frequency division)*counting value=set time. Take 1 second as an example

    (1/(72MHz/7200))*10000=1s

    Initialize an important structure of the general timer TIM_TimeBaseInitTypeDef

     

  • typedef struct   

  • {   

  • u16 TIM_Period; //Count value automatically loaded, 

  • u16 TIM_Prescaler; //Division value, when it is 0, it means no frequency division, so it needs to be subtracted by 1. 

  • u8 TIM_CLOCkDivision; //Set clock division, 

  • u16 TIM_CounterMode; //Up, down, etc. counting modes 

  • } TIM_TimeBaseInitTypeDef; 

     

    Calling a function after initializing the structure

    void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef * TIM_TimeBaseInitStruct) Initializes the time base unit of the TIMx timer.

    Secondly, the interrupt pending bit must be cleared, and the function void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, u16 TIM_IT) does this work. TIM_IT: TIM interrupt pending bit to be checked.


Keywords:STM32 Reference address:Calculation of the timing time of the general timer of the STM32 microcontroller

Previous article:What is the RTC calibration function of the STM32F microcontroller?
Next article:Application of USB interface in smartphone base speaker based on Cortex-M0 processor

Recommended ReadingLatest update time:2024-11-15 11:12

Understanding the STM32 startup file
        In the process of embedded application development, due to the use of C language programming, the execution process of the underlying registers of the machine is rarely involved. Generally, the code is written directly in the main function. It seems that main has become the starting point for granted, although
[Microcontroller]
Ultrasonic ranging based on stm32
First, let's look at the module diagram  You can find it by searching on Taobao. It is also very simple to use. First, look at the points that need to be noted in the data sheet.  1 Basic parameters   In actual tests, the maximum measurement distance of more than three meters is still possible. I did not tes
[Microcontroller]
Ultrasonic ranging based on stm32
Solve the burning problem: Jlink's three-wire SWD method connected to the STM32 chip cannot be recognized
I found this article online that solved the problem that j-flash can only be connected but not burned. I hope that friends who encounter the same problem can use the same method to solve it: Some time ago, I made a board, and the debugging interface only had three ports, SWD, SWCLK, and GND. When I used Jlink's SWD
[Microcontroller]
Solve the burning problem: Jlink's three-wire SWD method connected to the STM32 chip cannot be recognized
STM32 TFT-LCD liquid crystal
TFT-LCD is a thin film transistor liquid crystal display. Its full English name is: Thin Film Transistor-Liquid Crystal Display. TFT-LCD is different from the simple matrix of passive TN-LCD and STN-LCD. It has a thin film transistor (TFT) on each pixel of the liquid crystal display screen, which can effectively overco
[Microcontroller]
STM32 TFT-LCD liquid crystal
STM32 external crystal oscillator provides common system clock settings after frequency multiplication
void RCC_Configuration(void) { RCC_DeInit(); //Reset the peripheral RCC register to the default value (i.e. default value)   RCC_HSEConfig(RCC_HSE_ON); //Turn on the external crystal oscillator   if(RCC_WaitForHSEStartUp() == SUCCESS) //Judge whether the oscillation is successful {         RCC_HCLKConf
[Microcontroller]
Use ST-Link Utility to remove STM32 chip read and write protection
Problem: Unable to download code using ISP/J-Link/ST-Link, etc., prompting that the chip is write-protected; prompting that read protection is displayed when reading chip information. Cause: Usually the option bytes have been modified.  Solution: Here, use ST-Link Utility to modify the option bytes. Use ST-Li
[Microcontroller]
Use ST-Link Utility to remove STM32 chip read and write protection
STM32 study notes 2——GPIO lighting
After learning how to build a project, you must be eager to use your own development board to show your skills. Don't worry, take your time. When learning C language, the first thing you compile must be the eternal classic code, yes, that is Hello World - simple, clear, and you can see the phenomenon intuitively. Ther
[Microcontroller]
STM32 study notes 2——GPIO lighting
Detailed explanation of ucosii transplantation on stm32 2
    In the detailed explanation 1, the CM3 kernel knowledge needed for transplantation was mainly discussed. This article will talk about the principle and code composition of ucosii. The most classic learning material for ucosii is Teacher Shao Beibei's >. I think this book is enough for learning ucosii, because he ex
[Microcontroller]
Latest Microcontroller Articles
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号