2011-06-30 01:25 AM
I m trying to use TIM2 as a general purpose timer to generate an interrupt at every 10mS.
void Timer2_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period = 10; TIM_TimeBaseStructure.TIM_Prescaler = 36000; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE); /* TIM IT disable */ TIM_Cmd(TIM2, DISABLE); /* TIM2 disable counter */ } void RCC_Configuration(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); // Enable HSE while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); FLASH_SetLatency(FLASH_Latency_0); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); // PCLK2 = HCLK RCC_PCLK1Config(RCC_HCLK_Div2); // PCLK1 = HCLK/2 RCC_PREDIV1Config(RCC_PREDIV1_Source_HSE, RCC_PREDIV1_Div1); RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_3); RCC_PLLCmd(ENABLE); // Enable PLL while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(RCC_GetSYSCLKSource() != 0x08); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 , ENABLE); RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2 | RCC_APB1Periph_USART3 | RCC_APB1Periph_TIM2, ENABLE); } I have enabled the timer interrupt also. The above routine doesnt generate an interupt exactly at 10mS. My external crystal connected is 8MHz. And the SYSCLK is configured as PLLCLK which is operating at 24MHz. I am not clear how the TIM_PERIOD and TIM_Prescaler are calculated. As the above example is provided by the STM FAE. And what is procedure to set a timer as GP Timer?2011-07-04 06:32 AM
Any error in the above posted code?
Nothing that looks particularly fatal. You might want to make your counter variables volatile. And do a >= comparison on both in case the serial output causes you to miss an interrupt/count. You need to attach a complete/freestanding project that will actually compile and run.