首页 > 代码库 > STM32定时器设置浅谈

STM32定时器设置浅谈

1、嵌套中断控制器的配置

void NVIC_Configuration()

{

     NVIC_InitTypeDef  NVIC_InitStructure;

     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//

 

  •         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 
  •         //选择TIM2的中断通道 
  •  
  •         NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;      
  •  
  •         //抢占式中断优先级设置为0 
  •  
  •         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  •  
  •        //响应式中断优先级设置为0 
  •  
  •         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  •  
  •         //使能中断 
  •  
  •         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  •         NVIC_Init(&NVIC_InitStructure);
  • voidTimer_Config(void) 
  • {  
  •  
  •     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);  
  •  
  •     TIM_DeInit(TIM2); 
  •  
  •     TIM_TimeBaseStructure.TIM_Period=2000-1;  //自动重装载寄存器的值 
  •  
  •     TIM_TimeBaseStructure.TIM_Prescaler=(36000-1);         //时钟预分频数 
  •  
  •     TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;  //采样分频 
  •  
  •      TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式 
  •     TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure); 
  •  
  •     TIM_ClearFlag(TIM2,TIM_FLAG_Update);               //清除溢出中断标志 
  •  
  •     TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE); 
  •  
  •      TIM_Cmd(TIM2,ENABLE);                              /开启时钟 
  •  

2、发生中断时间

发生中断时间 =(TIM_Prescaler+1)*(TIM_Period+1)/FLK

例如:发生中断时间 (2000-1+1)*(36000-1+1)/72000000 =1秒

 

STM32定时器设置浅谈