2010-11-29 12:31 AM
Timer interrupt Question
2011-05-17 05:16 AM
Hi,
You need to configure the time base, enable update interrupt and configure the NVIC. Add /* TIM update IT enable */ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); Remove from configuration: /******** satrt *******/ /* Output Compare Timing Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 3000; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); /* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE); /******** end *******/ /* !!!!!!!!!!!!!!!!!!!!! Don't remove this!!!!!!!!!!!!!!!!!!!!!!!!!!! */ /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); In the timer interrupt, as you said you need to clear the interrupt change void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); capture = TIM_GetCapture1(TIM2); TIM_SetCompare1(TIM2, capture + 3000); // Main Clock Timer Timer_mSec++; if(Timer_mSec == 1000) { Timer_mSec = 0; Timer_Sec++; if(Timer_Sec == 60) { Timer_Sec = 0; Timer_Min++; } } } TestTime++; } by void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { // Main Clock Timer Timer_mSec++; if(Timer_mSec == 1000) { Timer_mSec = 0; Timer_Sec++; if(Timer_Sec == 60) { Timer_Sec = 0; Timer_Min++; } TIM_ClearITPendingBit(TIM2, TIM_IT_Update); } } Remark: TIM_OC1Init() configures Output Compare channel: pwm1, pwm22011-05-17 05:16 AM
Third question:
Is this the best way to go for a simple time based interrupt routine ??
Well define ''best''. What period do you want, is it continuous or variable, etc. SysTick is pretty simple. The RTC can go sub-second with the appropriate scale. The TIMx timers can be a lot simpler than you have presented if you just want a constant periodic interrupt ticker.
2011-05-17 05:16 AM
For the VL Discovery a simple implementation with blinking LEDs
void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); /* Whatever */ STM32vldiscovery_LEDToggle(LED3); } } int main(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Initialise LEDs LD3&LD4, both off */ STM32vldiscovery_LEDInit(LED3); STM32vldiscovery_LEDInit(LED4); STM32vldiscovery_LEDOff(LED3); STM32vldiscovery_LEDOff(LED4); /* Enable the TIM2 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* VL clocks at 24 MHz, change to 72 - 1 for a 72 MHz system /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 MHz down to 1 KHz (1 ms) TIM_TimeBaseStructure.TIM_Prescaler = 24 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock) TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); while (1); }2011-05-17 05:16 AM
i am using the SysTick already, so i have learned how to use the SysTick.
But now i trying to master the ''Normal'' Timer interrupt. So right now i trying to find a simple interrupt based on the TIM timers. I have checked and tested some of the timer example. but i did not find any one that is a simple interrupt based timer. Lets say. a timer that go´s into interrupt every 1ms I haven´t tested code yet,