AnsweredAssumed Answered

Using all channels of Timer 3

Question asked by bujak.dan on Jun 23, 2014
Latest reply on Jun 24, 2014 by Brutte
I'd like to use all channels of Timer 3 to generate different clocks and timers.

Channel 1- 10ms timer interrupt
Channel 2- 250us timer interrupt
Channel 3- 50kHz clock (50%) on PB0
Channel 4- 256kHz clock (50%) on PB1

The problems I'm having are:

       
  1. I believe I need to use the CCR for this and I don't know how to configure that without getting it to do the output compare and toggle the I/O
  2.    
  3. I can't get a clock over 37.5kHz on any of the I/Os (75kHz update rate)

void Timer3_Configuration(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
   
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 
  /* GPIOB clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 
  /* GPIOB Configuration: TIM3 CH3 (PB0) and TIM3 CH4 (PB1) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN ;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
 
  /* Connect TIM Channels to AF1 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_1);
 
  /* Enable the TIM3 global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
  /* ---------------------------------------------------------------------------
    TIM3 Configuration: Output Compare Toggle Mode:
     
    In this example TIM3 input clock (TIM3CLK) is set to APB1 clock (PCLK1).
      => TIM3CLK = PCLK1 = 48 MHz
                                               
     CC3 update rate = TIM3 counter clock / CCR3_Val = 75000 Hz
       ==> So the TIM3 Channel 3 generates a periodic signal with a
           frequency equal to 37500 Hz.
 
     CC4 update rate = TIM3 counter clock / CCR4_Val = 9375 Hz
       ==> So the TIM3 Channel 4 generates a periodic signal with a
           frequency equal to 4687.5 Hz.
  --------------------------------------------------------------------------- */  
 
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
 
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
 
  /* Output Compare Toggle Mode configuration: Generic */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
 
  /* Output Compare Toggle Mode configuration: Channel 3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);
  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Disable);
 
  /* Output Compare Toggle Mode configuration: Channel 4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Disable);
 
  /* TIM enable counter */
  TIM_Cmd(TIM3, ENABLE);
 
  /* TIM IT enable */
  TIM_ITConfig(TIM3, TIM_IT_CC3 | TIM_IT_CC4, ENABLE);
}

Outcomes