cancel
Showing results for 
Search instead for 
Did you mean: 

DELAY help

timothy
Associate II
Posted on July 19, 2013 at 07:34

Im trying to create a delay but im lost on how to make one, any code would be great.

-Tim
4 REPLIES 4
sonurobots
Associate III
Posted on July 19, 2013 at 08:24

just call Delay(500);

void Delay(int count)

{

while(count--);

}

if you want some precise delay then go for timers .that's why they are made for .
zzdz2
Associate II
Posted on July 19, 2013 at 08:58

#define TICKSGN ((SysTick_VAL_CURRENT_Msk+1)>>1)
#define WAITMAX (SysTick_VAL_CURRENT_Msk >> 1)
static inline void waittick(uint32_t step)
{
static uint32_t tick0;
if (!step)
{
tick0 = SysTick->VAL;
return;
}
tick0 -= step;
while ((tick0 - SysTick->VAL) & TICKSGN)
{
__NOP();
__NOP();
}
}
static inline void sleep(uint32_t delay)
{
waittick(0);
delay++;
while (delay > WAITMAX)
{
waittick(WAITMAX);
delay -= WAITMAX;
}
waittick(delay);
}

haythem
Associate II
Posted on March 18, 2014 at 22:47

How can I use the delay function in U.S.

francescatodiego
Associate II
Posted on March 19, 2014 at 02:09

My hw delay routine

#include <
stdbool.h
>
#include <
stdint.h
>
#include ''stm32f4xx.h''
#include ''LIBhwtmr.h''
#define TIMER_HW_FREQ 1000000 // 1MHz
// default config APB1clock=Coreclock/4 -> APB1timerclock=2*APB1clock=Coreclock/2
///////////////////////////////////////////////////////////////////////////////////////////////
bool Timer_HW_delay_set (uint32_t time)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint32_t PrescalerFreq = TIMER_HW_FREQ ;
uint32_t PrescalerValue = 0 ;
uint32_t PresetValue = time ;
while ((PresetValue >= 0x00010000) && (PrescalerFreq > 0))
{
PresetValue >>= 1 ;
PrescalerFreq >>= 1 ;
}
if (PrescalerFreq > 0)
{
PrescalerValue = ((SystemCoreClock / 2) / PrescalerFreq) - 1;
if ((PresetValue < 0x00010000) && (PrescalerValue < 0x00010000))
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
TIM_TimeBaseStructure.TIM_Period = (uint16_t) PresetValue ;
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) PrescalerValue ;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit (TIM6, &TIM_TimeBaseStructure);
TIM_ClearFlag (TIM6, TIM_FLAG_Update) ;
TIM_Cmd(TIM6, ENABLE);
return true ;
}
}
return false ;
}
///////////////////////////////////////////////////////////////////////////////////////////////
bool Timer_HW_expired ()
{
return (bool) (TIM_GetFlagStatus(TIM6, TIM_FLAG_Update) != RESET) ;
}
///////////////////////////////////////////////////////////////////////////////////////////////
void Timer_HW_reset ()
{
TIM_Cmd(TIM6, DISABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, DISABLE);
}
///////////////////////////////////////////////////////////////////////////////////////////////
void Timer_HW_wait_delay (uint32_t time)
{
Timer_HW_delay_set(time) ;
while (TIM_GetFlagStatus(TIM6, TIM_FLAG_Update) == RESET) ;
Timer_HW_reset ()
}

You can define the hw timer frequency in TIMER_HW_FREQ define if you use external clock define HSE_VALUE = clock frequency in compiler otherwise SystemCoreClock contain wrong value Delay use TMR6 and if possible use tic 1us (set by define TIMER_HW_FREQ in my code) If delay value > 16bit

try to

reduce

tic frequency and delay until < 16bit and return true if timer start if division fail (very long delay) return false