Skip to main content
jgnoss
Associate III
January 24, 2013
Question

howto get tim_16 Frequ. to PB8

  • January 24, 2013
  • 2 replies
  • 704 views
Posted on January 24, 2013 at 21:54

Hello,

My problem is that I don't get the Frequency produced by Timer 16  seen on PB8.

Somebody can have a look at the code and tell me what's missing or wrong.

 static void setup_Timer16(void) {

    TIM_OCInitTypeDef  TIM_OCInitStructure;

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    GPIO_InitTypeDef GPIO_InitStructure;

    // clock on Timer 16 enable

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);

    // GPIOB clocks enable

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

    // GPIOB Configuration: Pin PB8 AF2 connecting to TIM16 Ch1

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

    GPIO_InitStructure.GPIO_Mode = /*GPIO_Mode_OUT*/GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

    GPIO_Init(GPIOB, &GPIO_InitStructure);

    // Connect TIM16 pins to AF2

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_2);

    TIM_DeInit(TIM16 );

    TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);

    /* Time base configuration */

    TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / 50000/*lcd_Contrast*/) - 1;

    TIM_TimeBaseStructure.TIM_Prescaler = 0x0;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);

    // Timer output compare configuration

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OCInitStructure.TIM_OCMode = /*TIM_OCMode_Timing*/TIM_OCMode_Toggle;

    TIM_OCInitStructure.TIM_Pulse = 500;

    TIM_OC1Init(TIM16, &TIM_OCInitStructure);

/*

    NVIC_InitTypeDef nvic;

    nvic.NVIC_IRQChannel = TIM16_IRQn;

    nvic.NVIC_IRQChannelCmd = ENABLE;

    nvic.NVIC_IRQChannelPriority = 3;

    NVIC_Init(&nvic);

    TIM_ITConfig(TIM16, TIM_IT_CC1 | TIM_IT_Update, ENABLE);

*/

    /* TIM16 enable counter */

    TIM_Cmd(TIM16, ENABLE);

}

    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    January 25, 2013
    Posted on January 25, 2013 at 03:00

    Make sure to initialize ALL fields of TIM_OCInitStructure as TIM16_CH1 has a negated output channel CH1N

    Short of testing it myself, it otherwise looks reasonable.
    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    jgnoss
    jgnossAuthor
    Associate III
    January 25, 2013
    Posted on January 25, 2013 at 14:15

    Thanks clive1,

    after having a couple hours of sleep I got it working.

    The missing call was

     

    TIM_CtrlPWMOutputs(TIM16, ENABLE);

    now he creates a nice glitch free square wave of 100kHz on PB8 without the ISR.

    Thanks again

    Ju