title: Stm32 Time Base Interrupt (without any library)
body: <p>I use Stm32f103c8t6 processor and I want to make 1 second counter. Normaly I dont use tımer update ınterrupt and my counter counts correctly but when I add timer update ınterrupt in code my counter doesnt count correctly. So fast increment. And ı cant remove update interrupt flag (UIF) in TIM1's SR register. If I remove this flag my code is entering infinty loop. I cant any solution for this problem. Thanks for help</p>
  
  <p>This part is bring in startup_stm32f10x_md.s</p>
  
  <pre><code>/**
   * @brief  This is the code that gets called when the processor receives an
   *         unexpected interrupt.  This simply enters an infinite loop, preserving
   *         the system state for examination by a debugger.
   *
   * @param  None
   * @retval : None
  */
      .section    .text.Default_Handler,"ax",%progbits
  Default_Handler:
  Infinite_Loop:
      b   Infinite_Loop -----------------------------&gt; my code is stuck here
      .size   Default_Handler, .-Default_Handler
  </code></pre>
  
  <p>And this is a my code.</p>
  
  <pre><code>#include &lt;stddef.h&gt;
  #include "stm32f10x.h"
  
  void CLK_Config(){
       RCC-&gt; CR = 0x00000000;             //CR Reset
       RCC-&gt; CR |= 0x00010000;            //HSE enable
       while(!(RCC-&gt; CR &amp; 0x00020000));   //HSE FLAG control
       RCC-&gt; CR |= 0x00080000;            //CSS enable
       RCC-&gt; CR |= 0x01000000;            //PLL On
       RCC-&gt; CFGR |= 0x00010000;          //HSE Select PLL input
       RCC-&gt; CFGR |= 0x001C0000;          //PLL Multi With 9 = 72 Mhz
       RCC-&gt; CFGR |= 0x00000002;          //PLL Select as SYSCLK
       RCC-&gt; CFGR |= 0x00000400;          //APB1 Clock divided by 2
  
       RCC-&gt; APB1ENR |= 0x18000000;       //APB1 BKP Clock Enable
       PWR-&gt; CR |= 0x0100;                //PWR BKP Access Enable
  
       RCC-&gt; APB2ENR |= 0x00000001;       //APB2 AFIO Clock Enable
  }
  
  void TIM1_Config(){
       RCC-&gt; APB2ENR  |= 0x00000800;      //TIM1 CLK Enable
       TIM1-&gt; CR1     |= 0x0085;          //Update Request Source, Counter Enable
       TIM1-&gt; DIER     = 0x0001;          //Update Interrupt Enable
       TIM1-&gt; ARR      = 0x1F40;          //8000 is set as Auto Reload Value
       TIM1-&gt; PSC      = 0x2327;          //9000 is set as Prescaler Value for 1 sn Formula: 1sn=1Hz=(PCLK/(PSC*ARR))
  }
  
  void USART1_Config(){
      RCC-&gt; APB2ENR   |= 0x00000004;      //GPIOA CLK Enable
      GPIOA-&gt; CRH     |= 0x00000AA0;      //GPIOA 10,9 Push-Pull Alternate Function 2Mhz
      RCC-&gt; APB2ENR   |= 0x00004000;      //USART1 CLK Enable
      USART1-&gt; BRR    |= 0x00001D4C;      //USART1 Baund Rate 9600
      USART1-&gt; CR1    |= 0x000020C8;      //USART, TXE Interrupt, TC Interrupt, Transmitter Enable
  }
  
  void Interrupt_Config(){
      NVIC-&gt; ISER[0]  |= 0x02000000;      //NVIC TIM1 UP
      NVIC-&gt; ISER[1]  |= 0x00000020;      //NVIC USART1 Global Interrupt
  
      NVIC-&gt; IP[25]   = 0x10;             //TIM1 UP Interrupt Priority 2.         25th Interrupt
      NVIC-&gt; IP[37]   = 0x40;             //USART1 Global Interrupt Priority 5.   37th Interrupt
  
  }
  
  uint8_t count1sec; // Global variable
  
  int main(void)
  {
      CLK_Config();
      Interrupt_Config();
      TIM1_Config();
      USART1_Config();
  
    while (1)
    {
        if(count1sec != 0){
            USART1-&gt; DR = count1sec;
        }
    }
  
  }
  
  void TIM1_UP_IRQHandler(){
      TIM1-&gt; SR = 0x00000000;
      NVIC-&gt; ICPR[0] = 0x02000000;
      count1sec += 1;
  }
  </code></pre>