-1
\$\begingroup\$

I am trying to make a 10 seconds delay function using STM32F4 Discovery board. Unfortunately, it is not working and I don't know why. I have been two days on this problem. Can anyone help me please?

Here is my code:

#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_tim.h>

void configLED() {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOD, &GPIO_InitStruct);
}

void configTimerAndGpio() {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    /*
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &GPIO_InitStruct);
    GPIO_PinAFConfig(GPIOB, GPIO_Pin_3, GPIO_AF_TIM2);
    */

    //TIM_TimeBaseInitStruct.TIM_Period = 10000;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    TIM_TimeBaseInitStruct.TIM_Period = 2000-1;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 21000-1;
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
    TIM_SetClockDivision(TIM_CKD_DIV1);
    TIM_PrescalerConfig(TIM2,Presacler,TIM_PSCReloadMode_Immediate);
    TIM_ITConfig(TIM_IT_UPDATE);
}

void delay() {
    TIM_Cmd(TIM2, ENABLE);
    while (TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) != RESET) {
        TIM_ClearFlag(TIM2, TIM_FLAG_Update);
    }
    //while (TIM_GetCounter(TIM2) > 1) {
    //}
    TIM_Cmd(TIM2, DISABLE);
}

int main(void) {
    configTimerAndGpio();
    configLED();
    while (1) {
        GPIO_SetBits(GPIOD, GPIO_Pin_13);
        delay();
        GPIO_ResetBits(GPIOD, GPIO_Pin_13);
        delay();
    }
}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ What are the symptoms? "Is not working" is not sufficient here. What is the expected behavior and the resulting behavior? What oscillator do you use and at which frequency? \$\endgroup\$
    – Blup1980
    Commented Jun 6, 2014 at 9:07

2 Answers 2

1
\$\begingroup\$

Actually ,i fixed that by using st's clock configuration tool to set the used clock to 84 MHZ then i did some calculation and modified the prescaler.

\$\endgroup\$
0
\$\begingroup\$

On the link below you have tutorial on how to use Systick timer for delay.

Actually, library is written which do that for you.

http://stm32f4-discovery.com/2014/04/03-stm32f429-discovery-system-clock-and-pretty-precise-delays/

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Please post a summary of the details from that link, otherwise the answer will become useless if the link dies. \$\endgroup\$
    – PeterJ
    Commented Jul 7, 2014 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.