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();
}
}