2013-01-16 08:18 AM
Ok, I'm trying to get a consistent 100 msec interrupt out of Timer2 to drive an external device in steps (but not using PWM). According to the manual, I have the regs set right
: ARR, prescaler, all loaded properly and the URS bit set in CR1 to make sure I only see update events. I'm upcounting, as well.Now, in theory I should only get my handler called when there's an overflow.. but what I'm seeing is that the instant I leave my ISR there's another INT pending! Now, I clear the update event bit on entry to the ISR so I see it go away, yet it comes back instantly.. no 100 msec wait.I'm sure my timebcase is correct and my oscillator (OC1) is set right, if I disable the update event INT I see the counter count up/reload properly...Is there anything I should be particularly aware of when setting TIM2 to do this kind of thing? I'm a bit puzzled at this stage, all pointers/manual pages/etc. appreciated. #stm32 #timer2013-01-16 08:50 AM
It's REALLY hard to critique code you don't post. You don't mention a part or specific pins.
This should be a workable 10 Hz (100 ms) interrupt example using TIM2 on an F4 (or other STM32 parts)// TOGGLETIM2 STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h'' /**************************************************************************************/ void RCC_Configuration(void) { /* --------------------------- System Clocks Configuration -----------------*/ /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); } /**************************************************************************************/ void TIM2_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Prescaler = ((SystemCoreClock / 10000) / 2) - 1; // 10 KHz timebase TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 10 Hz update TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); } /**************************************************************************************/ void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable TIM2 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); } /**************************************************************************************/ void TIM2_IRQHandler(void) { TIM_ClearITPendingBit(TIM2,TIM_IT_Update); STM_EVAL_LEDToggle(LED6); } /**************************************************************************************/ int main(void) { RCC_Configuration(); NVIC_Configuration(); STM_EVAL_LEDInit(LED6); STM_EVAL_LEDOn(LED6); TIM2_Configuration(); while(1); // Don't want to exit } /**************************************************************************************/2013-01-16 09:25 AM
Ooops! Sorry, serious lack of coffee error.. it's an STM32F10x CPU. My code was close, but I had shoved the value belonging in Prescaler into Period.. erm.. heh.
Thanks a TON for the code, you saved my sanity for the day! Now to debug the logic....ed..