title: STM32F10X timer
body: <p>I have a problem regarding the timer-interrupt with my stm32f10 controller.
  When I enable the counter of the timer the status register is set to 
  0x1F because the following flags are set: UIF, CC1IF, CC2IF, CC3IF, CC4IF</p>
  
  <p>Here is what i did:</p>
  
  <pre><code>#include &lt;Compiler.h&gt;
  #include &lt;Debug.h&gt;
  #include &lt;STM32F10xxx.h&gt;
  
  #include &lt;Reset.h&gt;
  #include "Example.h"
  #include &lt;HW_TIM.h&gt;
  #include &lt;HW_NVIC.h&gt;
  #include &lt;HW_RCC.h&gt;
  #include &lt;RegBit.h&gt;
  
  unsigned volatile int sr =0;
  
  static void SetBASEPRI(WORD const basepriVal) {
    register WORD basepriReg __asm("basepri");
  
      basepriReg = basepriVal;
  }
  
  static void SetCONTROL(WORD const controlVal) {
    register WORD controlReg __asm("control");
  
      controlReg = controlVal;
  }
  
  static void MainInit(void) {
   STM32F10xxx_Config();
   SetBASEPRI(0U);
  
   PeripheryEnable(RCC_TIM2);
  
   SET_RBINDEX_REG_BIT(TIM, 2, DIER, UIE);
  
   SET_RBINDEX_REG_BIT(TIM, 2, DIER, CC1IE);
   SET_RBINDEX_REG_BIT(TIM, 2, DIER, CC2IE);
   SET_RBINDEX_REG_BIT(TIM, 2, DIER, CC3IE);
   SET_RBINDEX_REG_BIT(TIM, 2, DIER, CC4IE);
  
   InterruptEnable(NVIC_TIM2);
  
   SetCONTROL(1U);
  }
  
  void timInit(void){
  
   TIM2.PSC = 0x0024;
   TIM2.ARR = 0xFFFF;
  
   TIM2.CCR[0] = 0x00FF; 
   TIM2.CCR[1] = 0x0A00; 
   TIM2.CCR[2] = 0x0F00; 
   TIM2.CCR[3] = 0xF000;
  
   SET_RBINDEX_REG_BIT(TIM, 2, CR1, CEN);
  
  }
  
  __declspec(noreturn) int main() {
  
    MainInit();
  
    timInit();
  
    while(1);
  }
  
  void IRQ_TIM2(void){
   //Delet IFs in SR
   //trying to clear SR by reading the register
   sr = TIM2.SR;
   sr= GET_RBINDEX_REG_BIT(TIM,2,SR,UIF);
  
   //trying to clear SR by resetting the single bits
   RES_RBINDEX_REG_BIT(TIM, 2, SR, CC1IF);
   RES_RBINDEX_REG_BIT(TIM, 2, SR, CC2IF);
   RES_RBINDEX_REG_BIT(TIM, 2, SR, CC3IF);
   RES_RBINDEX_REG_BIT(TIM, 2, SR, CC4IF);
   RES_RBINDEX_REG_BIT(TIM, 2, SR, UIF);
  
   //trying to clear SR bei assigning 0 to SR
   TIM2.SR=0;
  
  }
  
  void IRQ_SysTick(void){
  }
  
  void IRQ_HrdFault(void) {   for ( ;; ) DEBUG_BREAK(1); }
  void IRQ_UsgFault(void) {   for ( ;; ) DEBUG_BREAK(1); }
  void IRQ_BusFault(void) {   for ( ;; ) DEBUG_BREAK(1); }
  void IRQ_MemFault(void) {   for ( ;; ) DEBUG_BREAK(1); }
  void IRQ_NMI(void)      {   for ( ;; ) DEBUG_BREAK(1); }
  </code></pre>
  
  <p>As I said, the problem is that all the flags are set when the Counter-Enable-Bit(CEN) is set. And I can't delete them in the subroutine.</p>