AnsweredAssumed Answered

STM32F030R8 Time base setup issue

Question asked by Ben on Jul 5, 2016
Latest reply on Jul 6, 2016 by Ben
Hi Sir/Madam:

I've encounter a issue about setting Time base on STM32F030R8.
I used TIM14 to be time base.
When I check the 10ms time base in stm32f0xx_it.c.
That was worked normally.
a.jpg

But, I used it to set time flag in main.c
It's worked abnormal.
The waveform isn't regular(not 10ms waveform).
155797711.jpg
How do I solve it?
Please help me.
Thank you.


Code is as below:

Main.c

extern uint8_t flag;


int main()
{


    Timer14_Initial();
    GPIO_Initial();


    while (1)
    {
        if(flag == 1)
        {
            flag = 0;
            GPIO_SetBits(GPIOC, GPIO_Pin_8);
        }
        else
        {
            GPIO_ResetBits(GPIOC, GPIO_Pin_8);
        }


    }
}






stm32f0xx_it.c


uint16_t Capture_Timer14 = 0;
uint8_t flag = 0;


void TIM14_IRQHandler(void)
{
    static uint8_t CC1_Cnt = 0;
    if (TIM_GetITStatus(TIM14, TIM_IT_CC1) != RESET)
    {
        TIM_ClearITPendingBit(TIM14, TIM_IT_CC1);


        Capture_Timer14 = TIM_GetCapture1(TIM14);
        TIM_SetCompare1(TIM14, Capture_Timer14 + TIM14_CCR_VAL);
        if(++CC1_Cnt >= 10){
            CC1_Cnt = 0;
            flag = 1;


        }
    }


}






Timer.c


#define TIM14_CCR_VAL 1000


void Timer14_Initial(void){
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;


    uint16_t PrescalerValue = 0;




    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);    




    PrescalerValue = (uint16_t)((SystemCoreClock)/1000000)-1;


    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 65535;
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);


    TIM_PrescalerConfig(TIM14, PrescalerValue, TIM_PSCReloadMode_Immediate);
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = TIM14_CCR_VAL;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;


    TIM_OC1Init(TIM14, &TIM_OCInitStructure);               
    TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Disable);




    TIM_ITConfig(TIM14, TIM_IT_CC1, ENABLE);


    TIM_Cmd(TIM14, ENABLE);



    NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);


}

Outcomes