均基于72Mhz时钟频率
预分频数(TIM_Prescaler) | 计数值(TIM_Period) | 秒数 s |
---|---|---|
7200 | 10000 | 1 |
14400 | 10000 | 2 |
14400 | 50000 | 10 |
28800(7200*4) | 50000 | 20 |
2880(7200*0.4) | 50000 | 2 |
简单算一下就是以1为基准两者相乘为秒数,基准值 72000000/7200/10000 = 1 |
c#include "stm32f10x.h" // Device header
void Timer_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_InternalClockConfig(TIM2); //可有可无
TIM_TimeBaseInitTypeDef TIM_TimeBase;
TIM_TimeBase.TIM_ClockDivision = TIM_CKD_DIV1; //几分频
TIM_TimeBase.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBase.TIM_Period = 5000 - 1;
TIM_TimeBase.TIM_Prescaler = 2880 - 1;
TIM_TimeBase.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBase);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_Initstr;
NVIC_Initstr.NVIC_IRQChannel= TIM2_IRQn; //中断通道
NVIC_Initstr.NVIC_IRQChannelCmd= ENABLE; // 使能
NVIC_Initstr.NVIC_IRQChannelPreemptionPriority= 1;
NVIC_Initstr.NVIC_IRQChannelSubPriority= 2;
NVIC_Init(&NVIC_Initstr);
TIM_Cmd(TIM2, ENABLE);//启动定时器
}