芯片:STM32F103 想产生1秒的定时,使用TIMER2 void TIM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // TIM_OCInitTypeDef TIM_OCInitStructure ; TIM_DeInit( TIM2);//复位TIM2定时器
/* TIM2 configuration */ TIM_TimeBaseStructure.TIM_Period = 1000 //最大计数值1000 TIM_TimeBaseStructure.TIM_Prescaler = 36000;//分频36000 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; // 时钟分割 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);
/* TIM2 enable counter [允许tim2计数]*/ TIM_Cmd(TIM2, ENABLE); }
/* PCLK1 = HCLK/2 [低速APB1时钟等于HCLK/2]*/ RCC_PCLK1Config(RCC_HCLK_Div2); 系统时钟是72MHZ,所以APB1时钟为36MHZ
希望能得到1秒的定时,但是实际上有4秒,不知道为什么? |