cancel
Showing results for 
Search instead for 
Did you mean: 

tim2 interrupt

novotny
Associate
Posted on June 02, 2011 at 12:23

Hello,

i try get interrupt from timer 2 in compare mode. I want up count mode and generate interrupt in case match compare_register and counter and in case overflow counter. I'm using TrueStudio.

Here is my code:

void EXTI0_IRQHandler(void)

{

  /*-----LOCAL-VARIABLES-----------------*/

  int i=100000;

  if(EXTI_GetITStatus(USER_BUTTON_EXTI_LINE) != RESET)

  {

     if(k){SetFreqViaHSI(24);k=0;}

     else{SetFreqViaHSI(2);k=1;}

      while(i){i--;}

    /* Clear the User Button EXTI line pending bit */

    EXTI_ClearITPendingBit(USER_BUTTON_EXTI_LINE);

  }

}

void TIM2_IRQHandler(void)

{

    if(TIM2->SR&&TIM_SR_CC1IF){STM32vldiscovery_LEDOn(LED4);}

    else{STM32vldiscovery_LEDOn(LED4);}

 TIM2->SR=0x0000;

 TIM2->CR1|=0X0001;

}

/*-----MAIN-FUNCTION-------------------*/

int main(void)

{

  /*-----LOCAL-VARIABLES-----------------*/

  unsigned long int i=0;

  /*-----CONFIGURACE---------------------*/

  SetFreqViaHSI(24);                            //set HSI and PLL to 24MHz

  STM32vldiscovery_LEDInit(LED3);        //init LED3 (0)

  STM32vldiscovery_LEDInit(LED4);        //init LED4 (1)

  STM32vldiscovery_LEDOn(LED3);

  STM32vldiscovery_LEDOff(LED4);

  RCC->APB1RSTR=RCC_APB1RSTR_TIM2RST;    //peripheral reset

  RCC->APB2ENR=RCC_APB2ENR_IOPCEN;        //portc Clock enable

  RCC->APB1ENR=RCC_APB1ENR_TIM2EN;        //peripheral enable

  NVIC_EnableIRQ(TIM2_IRQn);            //nvic configure

TIM2->DIER=TIM_DIER_CC1IE|TIM_DIER_UIE;    //enable interrupt

  TIM2->CCMR1=TIM_CCMR1_OC1M_0;

  TIM2->EGR=TIM_EGR_UG;                    //Update Generation

  TIM2->CCR1=0xBF;

  TIM2->CCER=TIM_CCER_CC1E;              //Capture/Compare 1 output enable

  TIM2->CR1=TIM_CR1_CEN;                //enable counter

  STM32vldiscovery_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);    // init button - interrupt

  /*-----MAIN-LOOP-----------------------*/

  while (1)

  {

  }

}

void SetFreqViaHSI(int freq)

{

  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/

      int i=400;

    RCC_DeInit();                            //default settings

    RCC_HSICmd(ENABLE);                        //enable HSI

    RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);    //HSI - clock source

    while (RCC_GetSYSCLKSource() != 0x00)    //check HSI is clock source

            {

            }

    while(i){i--;}                            //delay

    FLASH_SetLatency(FLASH_Latency_0);        //flash 0 wait state

    RCC_HCLKConfig(RCC_SYSCLK_Div1);        //HCLK = SYSCLK

    RCC_PCLK2Config(RCC_HCLK_Div1);            //PCLK2 = HCLK

    RCC_PCLK1Config(RCC_HCLK_Div1);            //PCLK1 = HCLK

        RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_6);

        RCC->CFGR&=0xFFFFFF0F;

        RCC->CFGR|=RCC_SYSCLK_Div1;

    RCC_PLLCmd(ENABLE);                        //Enable PLL

    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)        //Wait till PLL is ready

    {

    }

    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);                //Select PLL as system clock source

    while (RCC_GetSYSCLKSource() != 0x08)    //Wait till PLL is used as system clock source

    {

    }

}

so that is it. I have breakpoint on ''void TIM2_IRQHandler(void)'' but it newer fall in tim2 interrupt. Do you see any mistake? or do you have some example, to study how to set timer and interrupt?

thanks
2 REPLIES 2
Posted on June 02, 2011 at 19:01

Do you see any mistake? or do you have some example, to study how to set timer and interrupt?

 

Several obvious flaws, and yes several examples have been posted on the forum.

    if(TIM2->SR&&TIM_SR_CC1IF){STM32vldiscovery_LEDOn(LED4);}

    else{STM32vldiscovery_LEDOn(LED4);}

One too many ampersands there to do what you want.

  RCC->APB1RSTR=RCC_APB1RSTR_TIM2RST;    //peripheral reset

Holding TIM2 in reset, I think.

Suggest you use the library, unless you enjoy debugging register minutia.

If you want to use register directly, don't write in absolute values, mask in the bits you want to change.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
novotny
Associate
Posted on June 04, 2011 at 11:03

so, you were right, ''RCC->APB1RSTR=RCC_APB1RSTR_TIM2RST;'' holded device in reset. Now it's working:

  RCC->APB1RSTR|=RCC_APB1RSTR_TIM2RST;    //peripheral reset

  RCC->APB1RSTR&=~RCC_APB1RSTR_TIM2RST;    //peripheral reset off

thanks