2016-01-12 07:53 PM
Hi Everyone.
I've looked through some of the older posts re: timer delay function, and I have used one of those posts as a base to what I have implemented. The problem is, when I run the delay function in a loop to test, it runs for about one second then it stops (crashes?). From what I've read so far, my set up should be a free running 16bit counter.Can anyone see what the problem is.
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);TIM_TimeBaseStructure.TIM_Prescaler = 0x0;
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);TIM_Cmd(TIM2, ENABLE);
while(1)
{ GPIOC->BRR = GPIO_Pin_11; delay(100); GPIOC->BSRR = GPIO_Pin_11; delay(100); } void delay(uint16_t ticks) { uint16_t start = TIM2->CNT; do { } while((TIM2->CNT - start) < ticks); }2016-01-13 01:25 AM
do { } while((uint16_t)(TIM2->CNT - start) < ticks);
2016-01-13 03:43 AM
Stops where? Use a debugger.
Is this an F1 part? What specific STM32If an F2/F4 TIM2 is 32-bit, suggest you configure it as such, and use uint32_t, it will be more efficient that 16-bit, and won't potentially need casting2016-01-13 05:48 AM
Hi b.m.002,
If you only want to make a timer based delay function then I would recommend to usesystick.
It uses very less configurations. Easy to use. If need help for TIMER problem then mention your STM32 part number. So that specific help can be provided.2016-01-13 07:37 AM
Hi b.m.002,
You have to enable the Timer counter the moment of start ( begin of delay function), and not before. -Hannibal-2016-01-13 03:34 PM
Thanks everyone for your input. I have a STM32VLDISCOVERY fitted with the STM32F100RBT6B microcontroller.
I've made some changes, and what I want to do is have a delay routine available that will give me 1uS granularity. Generally most applications need delays in the low mS, but for this exercise I want to figure out why this setup is not working. When I changed the location the timer was enabled, this worked for the previous setup, but it counted some other delay value which for testing at the time was not important. The code below is hopefully set up to produce 1uS events, and this does not seem to work at the moment, the timer does not seem to want to start at all, maybe its something I don't understand about the way timers work in the stm32? If I got it correct, then TIMER2 roll over should occur at 1/24Mhz * 65535 = 2.7mS, so in the main loop delaying for 100 should work ie: 100uS.void TIMER2_Configuration_new(void)
{ TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);TIM_TimeBaseStructure.TIM_Prescaler = 23;
TIM_TimeBaseStructure.TIM_Period = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); }void delay(uint16_t ticks)
{ TIM_Cmd(TIM2, ENABLE); uint16_t start = TIM2->CNT; do { } while((TIM2->CNT - start) < ticks); TIM_Cmd(TIM2,DISABLE); } Thanks Martin.2016-01-13 06:45 PM
TIM_TimeBaseStructure.TIM_Period = 0; // No going to work
TIM_TimeBaseStructure.TIM_Period = 0xFFFF; // Maximal 0..655352016-01-13 07:36 PM
Hi clive, I change the period setting to 0xFFFF, but its still wont work? so far I haven't figured out what else to try. It works briefly and then stops.
My scope trace seems to show the period to be correct for the setup, that is, its a 100uS dely.