编辑
2023-07-28
单片机杂谈
00
请注意,本文编写于 544 天前,最后修改于 542 天前,其中某些信息可能已经过时。

均基于72Mhz时钟频率

预分频数(TIM_Prescaler)计数值(TIM_Period)秒数 s
7200100001
14400100002
144005000010
28800(7200*4)5000020
2880(7200*0.4)500002
简单算一下就是以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);//启动定时器 }