2013-05-20 07:04 AM
Hi Folks
I have this simple example program that I am modifying and by moving one statement into a while statement it breaks it. I'm working on the STM32F4 Discovery boardHere is the code:- /* =============================================================================== Name : main.cpp Author : Version : Copyright : Copyright (C) Description : main definition =============================================================================== */&sharpifdef __USE_CMSIS&sharpinclude ''stm32f4xx.h''&sharpendif&sharpinclude <stdio.h>/* Private typedef -----------------------------------------------------------*/GPIO_InitTypeDef GPIO_InitStructure;TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;/* Private define ------------------------------------------------------------*//* Private macro -------------------------------------------------------------*//* Private variables ---------------------------------------------------------*//* Private function prototypes -----------------------------------------------*/voidLED_Config(void);voidINTTIM_Config(void);/* Private functions ---------------------------------------------------------*//** * @brief Main program * @param None * @retval None */intmain(void){ /* LED Configuration */ LED_Config(); INTTIM_Config(); while (1) { //INTTIM_Config(); while (TIM_GetFlagStatus(TIM2, TIM_FLAG_Update ) == RESET) { }; TIM_ClearFlag(TIM2, TIM_IT_Update ); GPIO_ToggleBits(GPIOD, GPIO_Pin_13 ); }}/** * @brief Setup an interval timer * @param None * @retval None */voidINTTIM_Config(void){ /* TIM2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 200 - 1; // 1 MHz down to 10 KHz (0.1 ms) between 0x0000 and 0xFFFF. TIM_TimeBaseStructure.TIM_Prescaler = 134 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock) between 0x0000 and 0xFFFF TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM2 enable counter */ TIM_Cmd(TIM2, ENABLE);}voidLED_Config(void){ /* GPIOD Periph clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure);}As it stands the code works fine - however if I commit out the active INTTIM_Config() and make the INTTIM_Config() in the loop active the time base init does not work and the timer stays at default values. Any thoughts welcome.CS #timer-set-up-stm32f42013-05-20 02:57 PM
I'm not sure why you are surprised by this behaviour. After the initialization, all the timer flags are likely cleared. Therefore it would never enter the inner while{} loop. Once you complete the outer/main while loop, you re-initialize the timer, resetting its counter value, clearing flags etc. Of course your LED should toggle - very quickly probably!
2013-05-21 01:44 AM
Hi Finkelmeyer
I think I understand what you are saying, however why on the first loop does it not work if the INTTIM_Config(); is inside the while(1) statement. In the working case (first time) it executes INTTIM_Config(); then while(1) and then even thing is fine, however in the second case it executes while(1) then INTTIM_Config(); and the values are not loaded. This morning I tried running a number of INTTIM_Config(); outside the loop to see if that would break it - but it didn't. So next I'm going to simulate a number of the loops with in line code to see what happens. CS2013-05-21 03:42 AM
Hi Folks
Interestingly this looks like a timing issue, if I change the loop code to while (1) { INTTIM_Config(); int i = 0; while (i < 1000) { i++; }; while (TIM_GetFlagStatus(TIM2, TIM_FLAG_Update ) == RESET) { }; TIM_ClearFlag(TIM2, TIM_IT_Update ); GPIO_ToggleBits(GPIOD, GPIO_Pin_13 ); }This works, however if I change it to ''while (i < 100)'' it breaks, so it shows there needs to be a time between setting up the timer and using it. Probably some parrellalism going on in the compiler, which is flushed by the 'while' statement. ..Mmm interesting!!2013-05-21 04:48 AM
Hi are you try for the Channel 3 and 4 of timer 2...?