1. Enable timer clock: RCC > apb1periphclockcmd()
For example:
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //Open timer 3 clock
- 1
- 2
2. Set the basic parameters of timer (counting mode, counting cycle, frequency division coefficient, etc.)
Note: ① the counting methods include Tim ﹣ counter mode ﹣ up ﹣ and Tim ﹣ counter mode ﹣ down ﹣ etc
② The Tim? Prescaler can be any number between 1 and 65535
③ Calculation of initial count value: (count period + 1) * (division coefficient + 1) / (timer frequency). Generally, timer frequency is obtained by default from AHB dichotomy followed by frequency multiplication, so it is still 72M
For example, to time 100ms, it can be written as: counting cycle = 999, frequency division coefficient = 7199, that is, (999 + 1) * (7199 + 1) / (72000000), because
Division factor / counter frequency |
That is, the time taken to count a number, so multiplying the total count by this time is the total time.
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down; //Count down TIM_TimeBaseInitStruct.TIM_Period = 4999; TIM_TimeBaseInitStruct.TIM_Prescaler = 7199; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
- 1
- 2
- 3
- 4
- 5
3. Timer interrupt configuration:
/*Timer interrupt parameter setting*/ TIM3->SR &= 0xFFFE; //Clear the update interrupt flag bit, otherwise there will be a problem of entering the interrupt service function just after the interrupt is configured TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //Configure for update interrupt NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
4. Enable timer:
TIM_Cmd(TIM3,ENABLE);
- 1
- 2
5. Write the interrupt service function:
void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3,TIM_FLAG_Update) != RESET) { TIM3->SR &= ~(0x01 << 0); //Clear interrupt flag bit flag_1s ++; flag_500ms ++; } if(flag_500ms >= 1) { flag_500ms = 0; PBout(5) = !PBout(5); } if(flag_1s >= 2) { flag_1s = 0; PDout(12) = !PDout(12); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
Attach test source:
#include "stm32f10x.h" #include "stm32f10x_rcc.h" #include "delay.h" unsigned char time_count = 0; unsigned char flag_500ms = 0; unsigned char flag_1s = 0; void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3,TIM_FLAG_Update) != RESET) { TIM3->SR &= ~(0x01 << 0); //Clear interrupt flag bit flag_1s ++; flag_500ms ++; } if(flag_500ms >= 1) { flag_500ms = 0; PBout(5) = !PBout(5); } if(flag_1s >= 2) { flag_1s = 0; PDout(12) = !PDout(12); } } int main(void) { unsigned char clear = clear; GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStruct; TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; NVIC_InitTypeDef NVIC_InitStruct; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Interrupt group settings delay_init(); /*LED Indicator initialization, used to mark the completion of all initialization*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5; GPIO_Init(GPIOB,&GPIO_InitStruct); GPIO_SetBits(GPIOB,GPIO_Pin_5); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_Init(GPIOD,&GPIO_InitStruct); GPIO_SetBits(GPIOB,GPIO_Pin_5); GPIO_SetBits(GPIOD,GPIO_Pin_12); /*Serial port initialization*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //Serial port 1 clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //GPIOA clock enable GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); /*Initialization of serial port parameters*/ USART_InitStruct.USART_BaudRate = 115200; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1,&USART_InitStruct); //USART1->SR = USART1->SR; USART_Cmd(USART1,ENABLE); /*Timer 3 basic parameter initialization*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //Open timer 3 clock TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down; //Count down TIM_TimeBaseInitStruct.TIM_Period = 4999; TIM_TimeBaseInitStruct.TIM_Prescaler = 7199; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct); /*Timer interrupt parameter setting*/ TIM3->SR &= 0xFFFE; //Clear the update interrupt flag bit, otherwise there will be a problem of entering the interrupt service function just after the interrupt is configured TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //Configure for update interrupt NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStruct); /*Initialization complete*/ GPIO_ResetBits(GPIOB,GPIO_Pin_5); GPIO_ResetBits(GPIOD,GPIO_Pin_12); TIM_Cmd(TIM3,ENABLE); while(1) { } }