Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.
Article directory
- Preface
- 1. What is the structure of STM32 timer?
-
- 1. Structure of 51 timer
-
- 1.1 How to implement the 1s timing function?
- 2. Structure of stm32 timer
-
- 2.1 General timer
- 2. Usage steps
-
- 1. Turn on the clock
- 2.Initialize timer
- 3. Interrupt configuration (grouping, priority)
-
- 3.1 Configure interrupt sources
- 3.2 Configure interrupt priority
- 3.3 Start the timer
- 3.4 Interrupt service function
- 3.5 Determine the interrupt source and clear the interrupt source
- 3. Reference initialization function
-
- 3.1Write it as a peripheral initialization function to facilitate the main function call
- 3.2 Interrupt function
- 3.3 Time, minutes and seconds when interrupt service function is added
- 4. Test verification
-
- 4.1 Clock enable view
- 4.2 Update interrupt status bit
- 4.3 Other status and configuration
- 5. Expansion
- Summarize
Foreword
Tips: You can add the general content to be recorded in this article here:
What is the use of timer? Precise delay, PWM, make a clock: year, month, day, hours, minutes and seconds. The most basic thing about a clock is how to get 1s?
Tips: The following is the text of this article, the following cases are for reference
1. What is the structure of STM32 timer?
1. Structure of 51 timer
1.1 How to implement the 1s timing function?
- Function when selected
- Load initial value
- Configure timer interrupt
- Count inside the interrupt. How many times to count depends on timing needs.
2. Structure of stm32 timer
2.1 Universal timer
Some chips do not have a basic timer.
Models with basic timer
- Clock source RCC
- The controller should be enabled
- crossover
- Initial value of loading count
- interrupt
2. Usage steps
1. Turn on the clock
The code is as follows (example):
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
2.Initialize timer
The code is as follows (example):
//TIM_TimeBaseInitStruct.TIM_ClockDivision= TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Period=50000-1; TIM_TimeBaseInitStruct.TIM_Prescaler= 72-1; //TIM_TimeBaseInitStruct.TIM_RepetitionCounter= TIM_TimeBaseInit(TIM6, & amp;TIM_TimeBaseInitStruct);
3. Interrupt configuration (grouping, priority)
3.1 Configure interrupt source
timer6 only has overflow interrupt
TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
3.2 Configure interrupt priority
NVIC_InitStruct.NVIC_IRQChannel=TIM6_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd= ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0; //0-3 NVIC_InitStruct.NVIC_IRQChannelSubPriority= 1; //0-3 NVIC_Init( & amp;NVIC_InitStruct);
3.3 Start timer
After GPIO is released, each peripheral must be turned on individually.
TIM_Cmd(TIM6, ENABLE); //Enable
3.4 Interrupt service function
Find the interrupt vector table in the startup file, and then complete the interrupt service function
void TIM6_IRQHandler() {<!-- --> if(TIM_GetITStatus(TIM6, TIM_IT_Update)) \t\t {<!-- --> \t TIM_ClearITPendingBit(TIM6, TIM_IT_Update); num + + ; if(num==20) {<!-- --> num=0; //led negation \t\t } } }
3.5 Determine the interrupt source and clear the interrupt source
3. Reference initialization function
3.1 is written as a peripheral initialization function to facilitate main function calling
void timer6() {<!-- --> /* \t 1. Turn on APB1 clock 72MHz 2. Configure timer 6 TimeInit() 72 pre-allocated, 0-65535 1000000us/50000us =CNT=20 timing time/interrupt overflow time = count value 3. Interrupt configuration grouping, priority. Enable interrupt source 4. Interrupt service function count value, 1us, invert an LED after counting 20 times */ TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; \t RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); \t //TIM_TimeBaseInitStruct.TIM_ClockDivision= TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Period=50000-1; TIM_TimeBaseInitStruct.TIM_Prescaler= 72-1; //TIM_TimeBaseInitStruct.TIM_RepetitionCounter= \t TIM_TimeBaseInit(TIM6, & amp;TIM_TimeBaseInitStruct); //Enable interrupts: TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE); \t TIM_Cmd(TIM6, ENABLE); //Enable \t NVIC_InitStruct.NVIC_IRQChannel=TIM6_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd= ENABLE; NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0; //0-3 NVIC_InitStruct.NVIC_IRQChannelSubPriority= 1; //0-3 NVIC_Init( & amp;NVIC_InitStruct); \t \t }
3.2 Interrupt function
void TIM6_IRQHandler() {<!-- --> if(TIM_GetITStatus(TIM6, TIM_IT_Update)) \t\t {<!-- --> \t TIM_ClearITPendingBit(TIM6, TIM_IT_Update); num + + ; if(num==20) {<!-- --> num=0; //led negation \t\t } } }
3.3 Interrupt service function added time, minutes and seconds
void TIM6_IRQHandler() {<!-- --> if(TIM_GetITStatus(TIM6, TIM_IT_Update)) \t\t {<!-- --> \t TIM_ClearITPendingBit(TIM6, TIM_IT_Update); num + + ; if(num==20) {<!-- --> num=0; //led negation GPIOA->ODR^=0x0100; // XOR, the same is 0, different is 1 //Take 0 to XOR, it turns out to be 0, still 0; turns out to be 1, still 1. can remain unchanged //Take 1 to XOR, it turns out to be 0, it turns into 1; it turned out to be 0, it turns into 1. have the opposite effect second + + ; one_second_flag=1; if(second==60) {<!-- --> second=0; minute + + ; if(minute==60) {<!-- --> minute=0; hour + + ; if(hour==24) {<!-- --> hour=0; } } } \t\t } } }
4. Test verification
Set a breakpoint in the interrupt service function
4.1 Clock enable view
4.2 Update interrupt status bit
4.3 Other status and configuration
5. Expansion
Serial port: clock
- Get hours, minutes and seconds through serial port
- Modify time through serial port
Summary
In simple terms, from the 51 microcontroller timing structure to the stm32 timing structure, it explains how STM32 realizes the principle of timing 1s and implements it in the project.