title: Square wave generation with STM32 and timer
body: <p>I'm trying to generate a square wave / quadrature signal ( 2 square wave with 90 degres offset). The board is a STM32F103C8
  I'm not focused on the frequency yet, I just want to have a clean quadrature signal.
  My code is not complicated at the moment and here are the 2 mains fonctions in order to initialize the timer:</p>
  
  <pre class="lang-c prettyprint-override"><code>void init_SW()
  {
      GPIO_InitTypeDef GPIO_InitStruct;
      // Step 1: Initialize GPIO as input for rotary encoder
      // PB7 (TIM4_CH2) (encoder pin A), PB6 (TIM4_CH1) (encoder pin B)
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
      GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6;
      GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
      GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
      GPIO_Init(GPIOB, &amp;GPIO_InitStruct);
      // Step 2: Setup TIM4 for encoder input
  
          RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
          TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
          TIM_TimeBaseStructInit (&amp;TIM_TimeBaseStructure);
          TIM_TimeBaseStructure.TIM_Period = 3;
          TIM_TimeBaseStructure.TIM_Prescaler = 0;
      TIM_Cmd(TIM4, ENABLE);
          TIM_TimeBaseInit (TIM4, &amp;TIM_TimeBaseStructure);
  /*  
    TIM4-&gt;CCR3=0 ;
          TIM4-&gt;CCR4=(TIM4-&gt;ARR+1)/2;
          TIM4-&gt;CCER;
          */
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre class="lang-c prettyprint-override"><code>void timer_ccr_init (void)
  {
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    /* always initialise local variables before use */
    TIM_OCStructInit (&amp;TIM_OCInitStructure);
  
    /* Common settings for all channels */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  
    /* Channel1 */
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OC1Init (TIM4, &amp;TIM_OCInitStructure);
  
    /* Channel2 - 90 degres after*/
    TIM_OCInitStructure.TIM_Pulse = 1;
    TIM_OC2Init (TIM4, &amp;TIM_OCInitStructure);
      TIM4-&gt;CCER;
  }
  </code></pre>
  
  <p>Do you have any idea where I messed up?</p>