STM32 Introduction - General Timer Configuration

Publisher:声慢慢Latest update time:2018-12-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Benefits of Universal Timer

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



TIM3 clock enable

TIM3 is mounted under APB1, so we enable TIM3 through the clock enable function under the APB1 bus. The function called is:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //Clock enable



Initialize timer parameters, set auto-reload value, frequency division factor, and counting mode.

In the library function, the timer initialization parameters are implemented through the initialization function TIM_TimeBaseInit:

voidTIM_TimeBaseInit(TIM_TypeDef*TIMx,TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);

The first parameter is to determine which timer it is, which is relatively easy to understand. The second parameter is the timer initialization parameter structure pointer, the structure type is TIM_TimeBaseInitTypeDef

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Period = 5000; //Set the automatic reload count period value

TIM_TimeBaseStructure.TIM_Prescaler =7199; //Parameter TIM_Prescaler is used to set the frequency division coefficient

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Set the clock division factor

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Parameter TIM_CounterMode is used to set the counting mode

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);



Setting TIM3_DIER enables update interrupts.

Because we want to use the update interrupt of TIM3, the corresponding bit of the register can enable the update interrupt. In the library function, the timer interrupt enable is implemented through the TIM_ITConfig function:

void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState);

The first parameter is to select the timer number, which is easy to understand. The value is TIM1~TIM17.

The second parameter is very critical. It is used to indicate the type of timer interrupt we enable. There are many types of timer interrupts, including update interrupt TIM_IT_Update, trigger interrupt TIM_IT_Trigger, and input capture interrupt, etc.

The third parameter is very simple, which is whether to disable or enable.



TIM3 interrupt priority setting.

After the timer interrupt is enabled, in order to generate an interrupt, it is necessary to set the NVIC related registers.

Interrupt priority.



Enable TIM3.

It is not enough to just configure the timer. If the timer is not enabled, it will not work. We need to enable the timer after configuration by setting the CEN bit of TIM3_CR1. In the firmware library, the function to enable the timer is implemented by the TIM_Cmd function:

void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)



Write an interrupt service function.



Function to read the value of the interrupt status register to determine the interrupt type

ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t)

The function is to determine whether the interrupt type TIM_IT of the timer TIMx has an interrupt if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET){}



Function to clear interrupt flag in firmware library

void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT)

The function is to clear the interrupt TIM_IT flag of timer TIMx. TIM_ClearITPendingBit(TIM3, TIM_IT_Update);



Keywords:STM32 Reference address:STM32 Introduction - General Timer Configuration

Previous article:Introduction to the enumeration process of STM32USB
Next article:stm32 system tick timer use

Recommended ReadingLatest update time:2024-11-15 13:55

STM32 startup process and vector table
1. First, let's take a look at the binary file of a typical STM32 program: Note the first 2 "words" of the start address, 0x2000870, 0x08000635. 2. In the project file, enter debug mode and click "RET" to reset It is found that MSP is the first 32-bit content of the program file, and PC is the next 32-bit content.
[Microcontroller]
Simple analysis of STM32 SysTick precise delay
When designing software, some have strict timing requirements, but simple delay functions cannot accurately delay --- STM32 has a system tick clock which is a good configuration --- ----The first part is---writing the initial value to the tick clock register, that is, decrementing according to the system frequency----
[Microcontroller]
AT sends variable SMS in stm32
/******************** (C) COPYRIGHT 2015 **************************  * File name: main.c  * Description: Usage: p is the content of the SMS to be sent  * center is the SMS center of the mobile phone card  * tel is the mobile phone number to be sent  * The serial port will print the contents of AT operation *
[Microcontroller]
Detailed explanation of STM32 DMA usage
The DMA part I used is relatively simple. Of course, maybe this is a new thing, and I can't use its complex functions for the time being. The following is a question and answer format to express my thoughts. What is DMA used for?        Direct memory access is used to provide high-speed data transfer between periphera
[Microcontroller]
Detailed explanation of STM32 DMA usage
Difference between STM32 core reset and system reset and program implementation
1. Preface Some systems allow reset, but have special requirements for peripherals: a certain IO state cannot be changed due to reset, a certain timer counter cannot be changed, etc. For example: System A controls the power of system B through an IO, and the power of system B is turned on only when this IO is set high
[Microcontroller]
STM32 IAP APP startup process
Recently, due to project requirements, we need to implement the online upgrade function of STM32, namely IAP. I would like to share with you the learning experience and specific implementation of IAP in the past few days. I hope it will be helpful to those who also implement IAP. At the end of the article, a compresse
[Microcontroller]
STM32 IAP APP startup process
STM32 JTAG pin multiplexing settings
Prelude First, let's copy the definition of JTAG and SW interface.  JTAG: JTAG (Joint Test Action Group) is an international standard test protocol, mainly used for internal chip testing. Most advanced devices now support JTAG protocol, such as DSP, FPGA devices, etc. The standard JTAG interface is 4 lines: TMS, TCK,
[Microcontroller]
STM32 JTAG pin multiplexing settings
Tick-tick clock operation to understand STM32 library operation
  The library function operation of STM32 brings many conveniences to design developers. Developers do not need to fully understand the internal registers and hardware mechanisms of STM32. As long as they have a basic understanding of C language, they can complete the development of the microcontroller, which shortens
[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号