Question
TIM2 Interrupt problem
Posted on June 18, 2013 at 11:25
I write this code witch turn on the 4 leds of the stm32 when pushing the bouttan and after it wait for some time and turn off the 4 leds.
I checked the code without the delay it work fine but whith the delay the program is blocked in the delay function witch mean that the timer_2 interruption don't work :\ if someone can tell me what did I do wrong or what did I miss ? //------------------the code---------------------- #include ''stm32f4xx.h'' #include ''stm32f4xx_gpio.h'' #include ''stm32f4xx_rcc.h'' #include ''stm32f4xx_tim.h'' uint32_t n_ms=0; void TIM2_IRQHandler(void) { TIM2->SR &= ~TIM_SR_UIF; // clear IRQ flag in TIM2 n_ms++; } void delay(uint32_t h){ n_ms=0; while(n_ms < h); } void init_timer2(void){ TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV4; TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Period = 0; TIM_TimeBaseInitStruct.TIM_Prescaler = 0xFFFF; //--------------------------------------------------------- TIM_DeInit(TIM2); TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct); TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); } void init_input_output_pins(void){ //Setting port_D RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitTypeDef GPIO_InitStructure_D; GPIO_InitStructure_D.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure_D.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure_D.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure_D.GPIO_PuPd = GPIO_OType_PP; GPIO_InitStructure_D.GPIO_OType = GPIO_PuPd_NOPULL; //Setting port_A RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure_A; GPIO_InitStructure_A.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure_A.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure_A.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure_A.GPIO_PuPd = GPIO_OType_PP; GPIO_InitStructure_A.GPIO_OType = GPIO_PuPd_DOWN; // GPIO_Init(GPIOA, &GPIO_InitStructure_A); GPIO_Init(GPIOD, &GPIO_InitStructure_D); } void main (void){ SystemInit(); init_timer2(); init_input_output_pins(); while (1) { if (GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)){ //turn on leds GPIO_WriteBit(GPIOD,GPIO_Pin_12,1); GPIO_WriteBit(GPIOD,GPIO_Pin_13,1); GPIO_WriteBit(GPIOD,GPIO_Pin_14,1); GPIO_WriteBit(GPIOD,GPIO_Pin_15,1); //wait some time based on timer interruption delay(100); } else //turn off leds GPIO_WriteBit(GPIOD,GPIO_Pin_12,0); GPIO_WriteBit(GPIOD,GPIO_Pin_13,0); GPIO_WriteBit(GPIOD,GPIO_Pin_14,0); GPIO_WriteBit(GPIOD,GPIO_Pin_15,0); }; }