2013-11-18 08:43 AM
I have a Timer Problem . I want that my timer every 1ms makes an interrupt .
I testes it here if (TIM2_Tick >= 1000) but my LED was alway on then I tryied if (TIM2_Tick >= 1000000) and it was about 1 sec (30 blinks in 34 sec) My system_clk is 72Mhz . where is my mistake void Config_Timer(TIM_TimeBaseInitTypeDef *TIM_TimeBaseStructure, NVIC_InitTypeDef *NVIC_InitStructure) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Enable the Timer2 Interrupt */ NVIC_InitStructure->NVIC_IRQChannel = TIM2_IRQn; // configure the Timer2 interrupts NVIC_InitStructure->NVIC_IRQChannelPreemptionPriority = 0; // sets the priority group of the TIMER2 interrupts NVIC_InitStructure->NVIC_IRQChannelSubPriority = 1; // set the subpriority inside the group NVIC_InitStructure->NVIC_IRQChannelCmd = ENABLE; // TIMER2 interrupts are globally enabled NVIC_Init(NVIC_InitStructure); /* Configure Timer which is every 1 ms active*/ TIM_DeInit(TIM2); TIM_TimeBaseStructInit(TIM_TimeBaseStructure); /* Time base configuration */ TIM_TimeBaseStructure->TIM_Prescaler = 360 - 1; // 72 MHz / 360 = 200 khz TIM_TimeBaseStructure->TIM_Period = 200 - 1; // (1/200khz) * 200 = 1 ms; TIM_TimeBaseStructure->TIM_ClockDivision = 0; TIM_TimeBaseStructure->TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, TIM_TimeBaseStructure); /* Enable TIM2 Update Interrupt */ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); /* Enable TIM2 */ TIM_Cmd(TIM2,ENABLE); } void TIM2_IRQHandler(void) { static int TIM2_Tick = 0; TIM2_Tick++; if (TIM2_Tick >= 1000) // 1 Second { if(check == 0) { STM_EVAL_LEDOn(LED1); check=1; } else { STM_EVAL_LEDOff(LED1); check =0; } TIM2_Tick = 0; } } I set check=0 in my main once2013-11-18 09:00 AM
You don't clear the interrupt, the routine will keep re-entering.
2013-11-19 02:06 AM
You mean
TIM_ClearITPendingBit(
TIM2,
TIM_IT_Update)
;
in my TIM2_IRQHandler ? I put it inside and now the LED is blinking by the condition if (TIM2_Tick >= 1000) frequent but not like a second . More about 10 times in 19 sec2013-11-19 04:33 AM
I put it inside and now the LED is blinking by the condition if (TIM2_Tick >= 1000) frequent but not like a second. More about 10 times in 19 sec.
But you understand that if you toggle a pin you effectively half the frequency, right?2013-11-19 05:32 AM
mhh not really maybe you can tell me my mistake.
I thought that every 1ms my TIM2_IRQHandler is called. Right ? Because of the if if (TIM2_Tick >= 1000) it enters only in when my tick is over 1000. right ? I thought when I toogle my LED It will not take about 0,5 s right ?2013-11-19 06:52 AM
For a PERIOD of 1000 ms, the duty for the HIGH and LOW will be 500 ms each, NOT 1000