先看下面的主程序:
/* Includes ------------------------------------------------------------------*/ #include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; u16 CCR1_Val = 15; u16 CCR2_Val = 0x5FFF; u16 CCR3_Val = 0x3FFF; u16 CCR4_Val = 0x1FFF;
/* Private function prototypes -----------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); /* Private functions ---------------------------------------------------------*/
/******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif
/* System Clocks Configuration ---------------------------------------------*/ RCC_Configuration();
/* GPIO Configuration ------------------------------------------------------*/ GPIO_Configuration();
/* NVIC Configuration ------------------------------------------------------*/ NVIC_Configuration();
/* TIM2 Configuration ------------------------------------------------------*/ /* TIM2CLK = 36 MHz, Prescaler = 0x0, TIM2 counter clock = 36 MHz */ /* CC1 update rate = TIM2 counter clock / (2* CCR1_Val) ~= 549 Hz (1.82 ms) */ /* CC2 update rate = TIM2 counter clock / (2* CCR2_Val) ~= 732 Hz (1.36 ms) */ /* CC3 update rate = TIM2 counter clock / (2* CCR3_Val) ~= 1098 Hz (0.91 ms) */ /* CC4 update rate = TIM2 counter clock / (2* CCR4_Val) ~= 2197 Hz (0.45 ms) */
TIM_DeInit(TIM2);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_OCStructInit(&TIM_OCInitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 359; //分频360 TIM_TimeBaseStructure.TIM_Prescaler = 99; //ARR=100 TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* Output Compare Toggle Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; TIM_OCInitStructure.TIM_Channel = TIM_Channel_1; TIM_OCInitStructure.TIM_Pulse = 15;// RCC= 15 TIM_OCInit(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
TIM_Cmd(TIM2, ENABLE);
while(1) { } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET) { }
/* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */ *(vu32 *)0x40022000 = 0x01;
/* PLLCLK = 8MHz * 9 = 72 MHz */ 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 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOB, ENABLE);
/* Enable TIM2 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); }
/******************************************************************************* * Function Name : GPIO_Configuration * Description : Configures the TIM2 Pins. * Input : None * Output : None * Return : None *******************************************************************************/ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA Configuration:TIM2 Channel1, 2, 3 and 4 in Output */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIOB->ODR=GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10; SysTick_CounterCmd(SysTick_Counter_Enable);
} /******************************************************************************* * Function Name : NVIC_Configuration * Description : Configures NVIC and Vector Table base location. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(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
/* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the USART1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);
}
#ifdef DEBUG /******************************************************************************* * Function Name : assert_failed * Description : Reports the name of the source file and the source line number * where the assert error has occurred. * Input : - file: pointer to the source file name * - line: assert error line source number * Output : None * Return : None *******************************************************************************/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */ while (1) { } }
再看中断程序: void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) { if(GPIOB->ODR & GPIO_Pin_8) GPIOB->BRR=GPIO_Pin_8; else GPIOB->BSRR=GPIO_Pin_8; capture = TIM_GetCapture1(TIM2); TIM_SetCompare1(TIM2, 15 + 1); TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); } } 现在我的问题是,如果我按以上配置程序,定时器没隔多久进一次中断函数TIM2_IRQHandler()? 按我的理解是我分频后的CK_PSC=360000Hz,ARR的值是360,就是说我的频率是1kHz,然后RCC的值是15,就是说当计数到16后会产生事件更新,也就是会进中断程序,然后GPIO_Pin_8会产生翻转。 按照我的理解,就是说我的程序会没隔160us会进中断,但是实际上我用示波器查看时却为500us进中断函数,然后GPIO_Pin_8翻转一次。 请问高手们,这个进定时器中断函数的时间是与哪些值相关联的呢?
|