今天调试STM32定时器的时候,初始化完定时器再打开,就发现它不停的延时-中断,可我只是想让它中断一次啊,以后我用它的时候再开启,请大家帮忙分析一下我的代码,谢谢! int main(void) { RCC_Config();//RCC NVIC_Init();//NVIC InitGPIO();//GPIO InitTimer();//定时器2 while(1){ } } void RCC_Config(void) { ErrorStatus HSEStartUpStatus; /* RCC system reset(for debug purpose) */ RCC_DeInit(); /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if (HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* ADCCLK = PCLK2/6 */ RCC_ADCCLKConfig(RCC_PCLK2_Div6); RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while (RCC_GetSYSCLKSource() != 0x08) {} } /* Enable GPIOA, GPIOB, GPIOC and AFIO clocks */ //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打开定时器时钟 } void NVIC_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Enable the TIM2 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;//从优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void InitGPIO(void) { //将外设能大部分都初始化成GPIO了,包括定时器的那个I/O ...... } void InitTimer(void) { //TIM_DeInit( TIM2);//复位TIM2定时器 /* TIM2 configuration */ TIM_TimeBaseStructure.TIM_Period = 0xffff; //最大计数值0xffff TIM_TimeBaseStructure.TIM_Prescaler = T2_Prescaler_Value;//分频0x36 TIM_TimeBaseStructure.TIM_ClockDivision = T2_ClockDivision_Value; // 时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数方向向上计数 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Clear TIM2 update pending flag[清除TIM2溢出中断标志] */ //TIM_ClearFlag(TIM2, TIM_FLAG_Update); /* Enable TIM2 Update interrupt [TIM2溢出中断允许]*/ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); /* Prescaler configuration */ //TIM_PrescalerConfig(TIM2, T2_Prescaler_Value, TIM_PSCReloadMode_Immediate); /* Enables the TIM2 Preload on ARR Register */ TIM_ARRPreloadConfig(TIM2, DISABLE); //不允许自动重载功能 /* TIM2 enable counter [允许tim2计数]*/ TIM_Cmd(TIM2, ENABLE); } void TIM2_IRQHandler(void)//中断服务程序 { TIM_ClearFlag(TIM2, TIM_FLAG_Update);//清中断溢出标记 ....... //干一些我希望它做的事情 } 现在的问题是,我希望它延时50ms后,进一次中断,但问题是,它以后就不停的延时50ms,进中断。。。。 我用硬件仿真,发现它的SR寄存器的值总是0x1f(但是我只开启的溢出中断啊,其它的都没有用到,应该是0x01才对啊),执行TIM_ClearFlag(TIM2, TIM_FLAG_Update)的时候,SR寄存器的值也没有变化,我想是不是仿真器的问题,因为它的延时都是正确的 请问大家,我的代码是不是有什么问题啊。谢谢啊