2008-06-16 02:21 AM
Newbie Timer help
2011-05-17 03:28 AM
Hi there. I'm a newbie from a Microchip PIC16/18, dsPIC background and I need to learn the ST32 quickly for a new project.
I'm trying to setup Timer2 for a 1 second interrupt and flash an LED in the ISR. My code never enters the ISR, is there any example code out there that makes use of the library functions? Also, could someone please explain how I can calculate my input clock to the timer? I've got an external 8Mhz crystal with the PLL setup for 72MHz operation. Thanks [ This message was edited by: ahoek on 02-04-2008 12:45 ]2011-05-17 03:28 AM
/*************************************************************************
* Function Name : STM32_OnesecCounter * Description : Initialize the Timer2 channel-1. * TIM2CLK = 36 MHz, Prescaler = 35999, TIM2 counter clock = 1 KHz * TIM2_CH1 delay = Period/TIM2 counter clock = 1000 ms(1 sec) * Input : None * Output : none * Return : None ************************************************************************** void STM32_Onesec_Counter(void) { /* Peripherals InitStructure define */ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 1000; // auto reaload register TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* Prescaler configuration */ TIM_PrescalerConfig(TIM2, 35999, TIM_PSCReloadMode_Immediate); /* Output Compare Active Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing ; TIM_OCInitStructure.TIM_Channel = TIM_Channel_1; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInit(TIM2, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable); /* TIM IT enable */ TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE); }2011-05-17 03:28 AM
Thanks, that's pretty close to what I had, but my ISR still isn't firing.
Do I need to setup the interrupt register somewhere else as well? Here is the ISR inside stm32f10x_it.c: /******************************************************************************* * Function Name : TIM2_IRQHandler * Description : This function handles TIM2 global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void TIM2_IRQHandler(void) { GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET); // Turn LED on TIM_ClearITPendingBit(TIM2, TIM_IT_CC1); // Clear the interrupt flag }2011-05-17 03:28 AM
I haven't used the library for this - i wrote my own timer function however looking at their library have you remembered:
1 you need it enabled on the APB as well as in the timer RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 2 if you're triggering an ISR you need the NVIC setup: NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); 3 more a subnote when there is a presecaler for the apb (aka not div1) the timer is clocked at 2x the apb clock i have no idea if their library takes this into account or not. 4 Have you got the led pin set correctly to output push pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); 5 have you got the GPIO set on clocks enable too: RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE); 6 Easier way to toggle: //ODR is output Data Register IDR is input data register GPIOB->ODR ^= (1 << 9); //using xor2011-05-17 03:28 AM
Thanks guys, all working now.
Did not have the TMR2 Clock enabled. Also cleared the wrong interrupt flag in the ISR, should have been the Update flag -> TIM_ClearFlag(TIM2, TIM_FLAG_Update); [ This message was edited by: ahoek on 04-04-2008 09:31 ]2011-05-17 03:28 AM
any idea what is the different or how to use the different of the timer mode such as:
TIM_OCMode_Timing: TIM Output Compare Timing mode. TIM_OCMode_Active: TIM Output Compare Active mode. TIM_OCMode_Inactive: TIM Output Compare Inactive mode. TIM_OCMode_Toggle: TIM Output Compare Toggle mode. TIM_OCMode_PWM1: TIM Pulse Width Modulation mode1.