2016-07-05 03:44 AM
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.But, I used it to set time flag in main.cIt's worked abnormal.The waveform isn't regular(not 10ms waveform).How do I solve it?Please help me.Thank you.Code is as below:Main.cextern 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.cuint16_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 1000void 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);} #timer2016-07-05 05:29 AM
Not sure exactly what's going on here, but your variable needs to be volatile. I'd recommend toggling the GPIO at each event.
2016-07-05 06:26 AM
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-
2016-07-05 06:44 AM
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.2016-07-05 07:53 AM
Hi , you would change it to a volatile vaiable.
To get more help, I recommend to get a look to ''TIM_TimeBase'' example in at this path STM32Cube_FW_F0_V1.2.0\Projects\STM32072B_EVAL\Examples\TIM\TIM_TimeBase You can aslo use tool to generate correctly your initialization code and start you project development. -Hannibal-2016-07-05 09:44 AM
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.2016-07-06 01:22 AM
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.