cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F030R8 Time base setup issue

countach
Associate II
Posted on July 05, 2016 at 12:44

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.

0690X00000603ADQAY.jpg

But, I used it to set time flag in main.c

It's worked abnormal.

The waveform isn't regular(not 10ms waveform).

0690X000006039jQAA.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

&sharpdefine 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);

}

#timer
6 REPLIES 6
Posted on July 05, 2016 at 14:29

Not sure exactly what's going on here, but your variable needs to be volatile. I'd recommend toggling the GPIO at each event.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Walid FTITI_O
Senior II
Posted on July 05, 2016 at 15:26

Hi Ben,

Even I don't know why you are waiting 10 times to set the flags in the IRQ_Handler, you should ensure that the declaration ofCCR variable in stm32f0xx_it.c fileas follow:

extern __IO uint16_t TIM14_CCR_VAL;

-Hannibal-
Radosław
Senior II
Posted on July 05, 2016 at 15:44

Hannibal do you think??? TIM14_CCR_VAL is macro.

Only flag variable nedd to be volatile.

But anyway this code is mess.  TIM14_CCR_VAL isn't visiblefor all files where is used.

So this code will never compile succesfull.

Walid FTITI_O
Senior II
Posted on July 05, 2016 at 16:53

Hi , you would change it to a volatile vaiable.

To get more help, I recommend to get a look to ''TIM_TimeBase'' example in

http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef0.html

at this path STM32Cube_FW_F0_V1.2.0\Projects\STM32072B_EVAL\Examples\TIM\TIM_TimeBase

You can aslo use

http://www.st.com/content/st_com/en/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-configurators-and-code-generators/stm32cubemx.html

tool to generate correctly your initialization code and start you project development.

-Hannibal-

Posted on July 05, 2016 at 18:44

The OP is clearly not using HAL/Cube. Examples exist within the SPL, and on the forum.

I'd test this by toggling a GPIO, and not having it running under a debugger. I'm not sure why the trace has missing pulses, but I'm not willing to spend my time diagnosing it.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
countach
Associate II
Posted on July 06, 2016 at 10:22

Hi clive1, Hannibal and dembek.radoslaw.001

I make a mistake in TIM14_CCR_VAL.

It should be set volatile not define in a file. (But, IAR don't show any error or warning before change the variable).

After that, it still lose the pulse waveform(I used the example code from en.stsw-stm32140).

Thank you for your reply.