Skip to main content
d4ng3r09
Associate III
June 4, 2014
Question

Delay function using timers

  • June 4, 2014
  • 2 replies
  • 557 views
Posted on June 04, 2014 at 18:42

Hi, 

i want to make a  delay function using timers using stm32f4 discovery.Unfortunatelly i have no idea the start. I read the timers doucmentation but it didn''t help me a lot.

Can anyone guide me please?
    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    June 4, 2014
    Posted on June 04, 2014 at 20:24

    Spin loops, or interrupting?

    [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/Delay%20function%20with%20timer&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&currentviews=1743]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FDelay%20function%20with%20timer&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F¤tviews=1743

    [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/TIMERS%20used%20as%20DELAY%20Function%20For%20Beginners%20on%20STM32F3&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=280]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2FTIMERS%20used%20as%20DELAY%20Function%20For%20Beginners%20on%20STM32F3&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=280

    https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/compensating%20latencies%20on%20STM32F4%20interrupts&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=2061

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    d4ng3r09
    d4ng3r09Author
    Associate III
    June 4, 2014
    Posted on June 04, 2014 at 22:46

    Actually i managed to get something work but not like i wanted to . i am supposed to make a delay of 10 secs and i am getting a delay of 28 secs.This is how thought getting working things

    Since my bus clock is 42 MHZ i managed to reduce it to 1 KHZ .It means that have a period of 10^(-3).So to get 10 seconds , i have to make a counter with 10000 as a value. Can you tell what i missing? Thank you for your concern.

    #include <stm32f4xx_gpio.h>
    #include <stm32f4xx_rcc.h>
    #include <stm32f4xx_tim.h>
    void
    configLED() {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
    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(GPIOA, &GPIO_InitStruct);
    }
    void
    configTimerAndGpio() {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    GPIO_InitTypeDef GPIO_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    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_TimeBaseInitStruct.TIM_Period = 1;
    TIM_TimeBaseInitStruct.TIM_Prescaler = 42000-1;
    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Down;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
    //TIM_PrescalerConfig()
    }
    void
    delay() {
    TIM_SetCounter(TIM2, 10000);
    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(GPIOA, GPIO_Pin_9);
    delay();
    GPIO_ResetBits(GPIOA, GPIO_Pin_9);
    delay();
    }
    }