AnsweredAssumed Answered

STM32F100RCT6TR TIM12 issues

Question asked by eriksson.emil.001 on Jan 6, 2016
Latest reply on Feb 4, 2018 by dhenry
Hi!

I am trying to control two motors using an STM32F100RCT6TR microcontroller and the standard peripheral library version 3.5.0. I want to use TIM3 channel 1&2 and TIM12 channel 1&2. TIM3 works perfectly but I get nothing out of TIM12. None of the TIM12 registers that I look at using the debugging mode of IAR embedded workbench even seem to change form being 0. I figure since I initialize TIM12 the same way as TIM3 it should work. 

Does anyone know where I am doing it wrong?

This is my initializing code:
     GPIO_InitTypeDef GPIO_InitStruct;
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE );

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; 
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; 
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_Init(GPIOA, &GPIO_InitStruct);
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
    GPIO_Init(GPIOC, &GPIO_InitStruct);

TIM_TimeBaseInitTypeDef TIM_BaseStruct; //make a struct
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12, ENABLE); //turn on clock for TIM12
    TIM_BaseStruct.TIM_Prescaler = 0; //No prescaler
    TIM_BaseStruct.TIM_CounterMode = TIM_CounterMode_Up; //count up
    TIM_BaseStruct.TIM_Period = PWM_PERIOD; //(PWM_PERIOD=24000)
    TIM_BaseStruct.TIM_ClockDivision = TIM_CKD_DIV1; 
    TIM_BaseStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM12, &TIM_BaseStruct); //init
    TIM_Cmd(TIM12, ENABLE); //enable TIM12
    
    TIM_OCInitTypeDef TIM_OCStruct; //Output compare struct
    TIM_OCStruct.TIM_OCMode = TIM_OCMode_PWM1; //clear on compare match
    TIM_OCStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCStruct.TIM_OutputNState = TIM_OutputNState_Disable;
    TIM_OCStruct.TIM_Pulse = (uint32_t)(PWM_PERIOD*0.6); //60% duty cycle
    TIM_OCStruct.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OCStruct.TIM_OCNPolarity = TIM_OCNPolarity_High; 
    TIM_OCStruct.TIM_OCIdleState = TIM_OCIdleState_Reset;
    TIM_OCStruct.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
    TIM_OC1Init(TIM12, &TIM_OCStruct); //Initialize TIM12 channel 1
    TIM_OC1PreloadConfig(TIM12, TIM_OCPreload_Enable);
    TIM_ARRPreloadConfig(TIM12, ENABLE);
    
    TIM_OCStruct.TIM_Pulse = (uint32_t)(PWM_PERIOD*0.6);
    TIM_OC2Init(TIM12, &TIM_OCStruct); //Initialize TIM12 channel 2
    TIM_OC2PreloadConfig(TIM12, TIM_OCPreload_Enable); 
    
    
    //Steering
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //turn on clock for TIM3
    TIM_TimeBaseInit(TIM3, &TIM_BaseStruct); //init
    TIM_Cmd(TIM3, ENABLE); //enable TIM3
    
    TIM_OCStruct.TIM_Pulse = (uint32_t)(PWM_PERIOD*0.5); //50% duty cycle
    TIM_OC1Init(TIM3, &TIM_OCStruct); //Initialize TIM3 channel 1
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
    
    TIM_OCStruct.TIM_Pulse = (uint32_t)(PWM_PERIOD*0.5);
    TIM_OC2Init(TIM3, &TIM_OCStruct); //Initialize TIM3 channel 2
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); 
    

Best regards,
Emil

Outcomes