AnsweredAssumed Answered

Timer OC problem

Question asked by paroulek.lukas on Apr 19, 2016
Latest reply on Apr 29, 2016 by FTITI.Walid
Hi,
I´m using TIM3(output compare mode) in stm32f072 to generate waveform via GPIO. My function principle is:

- timer initializing
- timer is still running, only is enabled TIM_IT_CC3 a output TIM_CCER_CC3E (BUS_Set_Tx)
- when is CNT equal CCR3, pin is toggled and I set register CCR3 to next pulse lenght
- when is message sended, TIM_IT_CC3 and TIM_CCER_CC3E ale disabled (BUS_Stop_Tx)

Message is sended correctly but sometimes is output pin toggled immediately after enabling TIM_CCER_CC3E in function BUS_Set_Tx (pin state is toggled though CNT < CCR3 and interrupt is not called). Next time pin isnt´t already toggled. Timer is running, register CCR3 is correctly setable, interrupt is generated correctly but output pin is not toggled.

The problem occurs randomly. All timer registers are equal in the wrong and correct state.

Please where is problem?



1) timer initialization
void TimerInit() {

    //.......
    GPIO_InitStruct.Pin   = GPIO_PIN_0;
    GPIO_InitStruct.Mode  = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Alternate = GPIOB;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    __TIM3_CLK_ENABLE();
    TimHand.Instance          = TIM3;
    TimHand.Init.Period        = 65535;      
    TimHand.Init.Prescaler     = 0;
    TimHand.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    TimHand.Init.CounterMode   = TIM_COUNTERMODE_UP;
    TimHand.Init.RepetitionCounter = 0;
    TimHand.Channel               = HAL_TIM_ACTIVE_CHANNEL_3;
    TimHand.Lock               = HAL_UNLOCKED;
    TimHand.State               = HAL_TIM_STATE_READY;

    HAL_TIM_Base_Init(&TimHand);
    HAL_TIM_OC_Init(&TimHand);

    __HAL_TIM_SET_COUNTER(&TimHand, 0);      
    __HAL_TIM_CLEAR_IT(&TimHand, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_UPDATE);


    sConfig.OCMode     = TIM_OCMODE_TOGGLE;
    sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfig.Pulse = 480;   

    HAL_TIM_OC_ConfigChannel(&TimHand, &sConfig, TIM_CHANNEL_3);
    HAL_TIM_OC_Start_IT(&TimHand, TIM_CHANNEL_3);
    &TimHand.Instance->CCER &= ~TIM_CCER_CC3E; disable   //  "Capture/Compare 3 output enable"

    __HAL_TIM_DISABLE_IT(&TimHand, TIM_IT_UPDATE | TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4);
    __HAL_TIM_CLEAR_IT(&TimHand, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4 | TIM_IT_UPDATE);

    HAL_NVIC_SetPriority(TIM3_IRQn, 0, 1);
    HAL_NVIC_EnableIRQ(TIM3_IRQn);
}

2) start Tx function

void BUS_Set_Tx(uint32_t messLen_) {
    __HAL_TIM_CLEAR_IT(&TimHand, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4 | TIM_IT_UPDATE);
    __HAL_TIM_SET_COUNTER(&TimHand, 0);
    __HAL_TIM_ENABLE_IT(&TimHand, TIM_IT_CC3);  

    &TimHand.Instance->CCR3 = 480;
    &TimHand.Instance->CCER |= TIM_CCER_CC3E;   // enable "Capture/Compare 3 output enable"
}


3) stop Tx function

void BUS_Stop_Tx() {
    __HAL_TIM_DISABLE_IT(&TimHand, TIM_IT_CC3);
    __HAL_TIM_CLEAR_IT(&TimHand, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4 | TIM_IT_UPDATE);
    &TimHand.Instance->CCER &= ~TIM_CCER_CC3E;        // disable "Capture/Compare 3 output enable"
}

Outcomes