定时器初始化配置
1 void TIM3_Configuration(void)//1MS 2 { 3 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 4 5 TIM_DeInit(TIM3);//开一下时钟再关闭 6 /* 设置TIM2CLK 为 72MHZ */ 7 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE); 8 9 10 /* 自动重装载寄存器周期的值(计数值) */ 11 TIM_TimeBaseStructure.TIM_Period=1000-1; 12 13 /* 累计 TIM_Period个频率后产生一个更新或者中断 */ 14 TIM_TimeBaseStructure.TIM_Prescaler= 71; 15 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;/* 时钟预分频数为72 */ 16 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; 17 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 18 19 TIM_ClearFlag(TIM3, TIM_FLAG_Update); 20 TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); 21 TIM_ARRPreloadConfig(TIM3, ENABLE); 22 TIM_Cmd(TIM3, ENABLE); 23 24 }
NVIC配置
void TIM3_NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
正常情况下是先配置好定时器参数设置,再配置NVIC中断-》使能中断。
时间: 2024-10-08 01:32:44