cancel
Showing results for 
Search instead for 
Did you mean: 

delay function Mcs o mls

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

Hi, I'm looking how to perform a  delay function whith microsecondor milisecande 

Who can help me ?

thank you alot
8 REPLIES 8
haythem
Associate II
Posted on March 18, 2014 at 22:41

sorry I do not quite understand the position that you send me, can you explain me because I'm still a beginner

arganasss
Associate II
Posted on March 18, 2014 at 22:52

It depends on how you want to do the delay. If you want to do it with timers, u can do it pretty easily. You just need to know the frequency of your timers. Then you can  divide it wih the prescaler to reach the frequency you want. For example, this is how i did the delay function with timer. Maybe it is not the best way, but it is working. It's stm32f3 example.

void

Timer3_config(

void

)

{

TIM_TimeBaseInitStructure.TIM_Period =

0xFFFF

;

//

MAksimalus

delay'us

yra

65535

us.

TIM_TimeBaseInitStructure.TIM_Prescaler =

72

-

1

;

//

Padalina

clocko

72Mhz

is

72.

Vadinasi

1

clocko

tick'as

yra

1us

TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure );

TIM_Cmd(TIM3, ENABLE);

}

void

Delay_us(uint16_t us)

{

TIM_SetCounter(TIM3,

0x00

);

uint16_t start = TIM_GetCounter(TIM3);

while

((TIM_GetCounter(TIM3) - start) < us);

}

//

Example

Delay_us(

100

);

//

100

us

delay

if you want to make a milisecond delay, you have to change the prescaler value, your you could just create another function , something like this.

void

delay_ms(

int

ms)

{

while

(ms--)

{

Delay_us(

1000

);

1ms delay

}

}

so delay_ms(500) would make 500ms delay. 

of course, this delay_ms function would only work with the Timer3 configuration above, but i think u will get the idea.

haythem
Associate II
Posted on March 18, 2014 at 23:03

Thank you for your reply. I get an error that appears in my code

..\main.c(89): error:  #20: identifier ''TIM_TimeBaseInitStructure'' is undefined

..\main.c(100): error:  #268: declaration may not appear after executable statement in block

this is my code

/**

  ******************************************************************************

  * @file    IO_Toggle/main.c 

  * @author  MCD Application Team

  * @version V1.0.0

  * @date    19-September-2011

  * @brief   Main program body

  ******************************************************************************

  * @attention

  *

  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS

  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE

  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY

  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING

  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE

  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.

  *

  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>

  ******************************************************************************  

  */ 

/* Includes ------------------------------------------------------------------*/

#include ''stm32f4_discovery.h''

/** @addtogroup STM32F4_Discovery_Peripheral_Examples

  * @{

  */

/** @addtogroup IO_Toggle

  * @{

  */ 

/* Private typedef -----------------------------------------------------------*/

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_OCInitTypeDef  TIM_OCInitStructure;

TIM_ICInitTypeDef  TIM_ICInitStructure;

GPIO_InitTypeDef  GPIO_InitStructure;

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

void Timer3_config(void);

void Delay_us(uint16_t us);

/* Private functions ---------------------------------------------------------*/

/**

  * @brief  Main program

  * @param  None

  * @retval None

  */

int main(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);

Timer3_config();

  while (1)

  {

GPIO_SetBits(GPIOD, GPIO_Pin_15);

    

    /* Insert delay */

    Delay_us(5000);

    

    GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);

    

    /* Insert delay */

    Delay_us(1000);;

}

}

/*------------------------------------------------------------------------------

  Delay function

  delays number of tick Systicks (happens every 10 ms)

*------------------------------------------------------------------------------*/

void Timer3_config(void)

{

  TIM_TimeBaseInitStructure.TIM_Period = 0xFFFF; // MAksimalus delay'us yra 65535 us.

  TIM_TimeBaseInitStructure.TIM_Prescaler = 72-1; // Padalina clocko 72Mhz is 72. Vadinasi 1 clocko tick'as yra 1us

  TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure );

  

  TIM_Cmd(TIM3, ENABLE);

}

void Delay_us(uint16_t us)

{

  TIM_SetCounter(TIM3, 0x00);

  uint16_t start = TIM_GetCounter(TIM3);

  while((TIM_GetCounter(TIM3) - start) < us);

}

Posted on March 18, 2014 at 23:04

Or just use some other, existing, free running counter, with the granularity required.

I'm not even sure of the merits of resetting them, as it's pretty easy to mark time.

A counter running at the processor frequency (say 168 MHz) is quite capable of delaying micro-seconds, in this case 168 ticks of the clock. For milli-seconds, well that would be 168000, or SystemCoreClock / 1000

The core has a DWT_CYCCNT register that is 32-bit and ticks at the core frequency.

[DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/compensating%20latencies%20on%20STM32F4%20interrupts&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=1844]https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2Fcortex_mx_stm32%2Fcompensating%20latencies%20on%20STM32F4%20interrupts&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B¤tviews=1844
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 18, 2014 at 23:25

Thank you alot for your help, but I have a small question: what delay is expressed in ms or us

Posted on March 19, 2014 at 01:15

It is expressed as clock ticks

ticks = (SystemCoreClock / 1000) * milliseconds;

ticks = (SystemCoreClock / 1000000) * microseconds;

ticks = SystemCoreClock * seconds;

You understand the concept of Frequency and Period?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
haythem
Associate II
Posted on March 20, 2014 at 16:23

it works thank you alot for your help