cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4Discovery simple timer for delay

raimondas
Associate II
Posted on April 04, 2013 at 20:11

I have problems with creation of simple timer for delay. For a week I am searching some code examples where timers will working without interrupts, examples are working but not quit correct. One of examples http://amarkham.com/?p=12 said what it must have output 10 KHz but wit oscilloscope I got only 1.6 KHz without any code changes. Libraries are 1.1.0.

If I use demo project TIME_base it's working correctly, but after changing main.c code by example working become incorrect.

#stm32f4-timers
6 REPLIES 6
Posted on April 04, 2013 at 21:12

Then presumably you do not have the clock speeds for the processor and buses set up correctly in system_stm32f4xx.c in your project. The value defined for HSE_VALUE may also be incorrect, as the STM32F4-Discovery board uses an 8 MHz crystal, and the other F4 EVAL boards use a 25 MHz crystal.

So, perhaps explain your development environment, tool chain, etc.

For simple periodic delays consider also SysTick.

What time_base example are you talking about, what do you change to make it stop working as you want.

The Period and Prescaler value take an N-1 form. The both act as dividers of the input clock to the timer.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
raimondas
Associate II
Posted on April 05, 2013 at 10:43

So here is my project, where I want to generate 200 Hz. Formula Hz=Clock/((Prescaler+1)*(Period+1)) and in result I get 03 Hz. Systicks are ok, but I want to understand timers ! Maybe some help ?

I have attached my Keil project.

#include ''stm32f4xx_gpio.h''

#include ''stm32f4xx_rcc.h''

#include ''stm32f4xx_tim.h''

GPIO_InitTypeDef GPIO_InitStructure;

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

void INTTIM_Config(void);

void LED_Config(void);

int main(void){

INTTIM_Config();

LED_Config();

while (1){

if (TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) != RESET){

TIM_ClearFlag(TIM2, TIM_IT_Update);

GPIO_ToggleBits(GPIOD, GPIO_Pin_13);

}

}

}

void INTTIM_Config(void)

{

/* TIM2 clock enable */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

/* Time base configuration */

TIM_TimeBaseStructure.TIM_Period = 10000 - 1;

TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

/* TIM2 enable counter */

TIM_Cmd(TIM2, ENABLE);

}

void LED_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);

}

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line)

{

while (1)

{}

}

#endif

in stm32f4 firmware are peripheral demo projects, and one of them is called TIM_TimeBase and it's working correctly for me, but in this example are used channels and interrupts in which I am not interesting at this time.

________________

Attachments :

81_Timers.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HzkA&d=%2Fa%2F0X0000000bOv%2FTJvN_Gh50x2YvokpAnXa9jcqYDWu.BAm.0aUZE8NSwY&asPdf=false
crt2
Associate II
Posted on April 05, 2013 at 12:38

I played a bit Sherlock Holmes with your formula and that you get 16.03 Hz out as result-> then you should have a 52.4 kHz clock on system. Seems funny because clive1 did mention 8 MHz and 25 MHz clocks so there might be something wrong with some other settings or with formula (or with my maths) or with prescaler and period settings?  And where are you setting system clock in your project?

If it helps: in TIM_TimeBase example you have a comment at start of program:

/*!< At this stage the microcontroller clock setting is already configured,  

       this is done through SystemInit() function which is called from startup

       file (startup_stm32f2xx.s) before to branch to application main.

       To reconfigure the default setting of SystemInit() function, refer to

       system_stm32f2xx.c file

     */

raimondas
Associate II
Posted on April 05, 2013 at 20:21

the math is simple it's practycaly from formula without +1

1/200*168000000=840000, so Prescaler = 84 and Period = 10000

I have checked system and startup files and for me everything is in places. Project files could be checked, they are attached in previous comment
Posted on April 05, 2013 at 21:10

Your settings in system_stm32f4xx.c reflect the use of a 25 MHz crystal, the board has an 8 MHz crystal. What speed you end up with, I'm not sure, as you place the PLL comparison frequency way below the 1-2 MHz it's designed for.

You need to remedy this,

  *-----------------------------------------------------------------------------

  *        HSE Frequency(Hz)                      | 25000000

  *-----------------------------------------------------------------------------

  *        PLL_M                                  | 25

  *-----------------------------------------------------------------------------

/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */

#define PLL_M      25

#define PLL_N      336

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
raimondas
Associate II
Posted on April 08, 2013 at 10:54

Thank You. I have missed this. I have changed to 8 and I have checked examples they also set to 8.  Now I got 50 Hz it's better result but not 200 Hz. Maybe some more tips ?

I found. APB1 prescaler was 4 so I took it into account and everything works perfectly.