Skip to main content
pedro
Associate III
June 26, 2015
Question

TIM8 Toggle STM32F4

  • June 26, 2015
  • 3 replies
  • 879 views
Posted on June 26, 2015 at 12:32

Hi,

I'm trying to toggle some pins when timer 8 overflows. The problem I get is that I don´t see the pins toogle but I can see the timer counting on the debugger, so I think I might be missing some mistereous configuration on the GPIO. I'm using all 4 channels just for testing and I put a pulse value of half the timer period for testing aswell. The code I have so far:

void TIM8_Init(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8 ,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6 ,GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7 ,GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8 ,GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9 ,GPIO_AF_TIM8);
TIM_TimeBaseStructure.TIM_Period = 100; 
TIM_TimeBaseStructure.TIM_Prescaler = 0; 
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
//Channel 1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_Pulse = 50;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
TIM_OC2Init(TIM8, &TIM_OCInitStructure);
TIM_OC3Init(TIM8, &TIM_OCInitStructure);
TIM_OC4Init(TIM8, &TIM_OCInitStructure);
//TIM_DMACmd(TIM8, TIM_DMA_Update, ENABLE ); /* Enable TIM8_Updates DMA Requests */
//SET COUNTER
TIM8->CR1 = 0x1; 
}

Thanks :)
    This topic has been closed for replies.

    3 replies

    kai_mauer
    Associate
    June 26, 2015
    Posted on June 26, 2015 at 15:36

    You write:
     

    GPIO_Init(GPIOC,&GPIO_InitStructure);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6 ,GPIO_AF_TIM8);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource7 ,GPIO_AF_TIM8);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource8 ,GPIO_AF_TIM8);
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource9 ,GPIO_AF_TIM8);
     This itches me a little. Wrong order?
     First I would config the pins, then do GPIO_Init().
     I use HAL, so I explicitly cannot say whether the order is right or wrong. The HAL function set all regs at once...
     Have you looked in the GPIO AF reg with the debugger? 
    

    Tesla DeLorean
    Guru
    June 26, 2015
    Posted on June 26, 2015 at 15:43

    Not sure the pin ordering matters, provided the GPIO clock is enabled, it's pins/mux are configurable.

      /* TIM8 (special) enable pins */

      TIM_CtrlPWMOutputs(TIM8, ENABLE);

      /* TIM8 enable counter */

      TIM_Cmd(TIM8, ENABLE);

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    pedro
    pedroAuthor
    Associate III
    July 1, 2015
    Posted on July 01, 2015 at 22:57

    Thanks clive1!

    I knew it had to be something simple that I was missing.