2014-03-18 02:06 PM
Hi, I'm looking how to perform a delay function whith microsecondor milisecande
Who can help me ?thank you alot2014-03-18 02:26 PM
2014-03-18 02:41 PM
sorry I do not quite understand the position that you send me, can you explain me because I'm still a beginner
2014-03-18 02:52 PM
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.2014-03-18 03:03 PM
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 blockthis 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>© 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);}2014-03-18 03:04 PM
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¤tviews=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=18442014-03-18 03:25 PM
Thank you alot for your help, but I have a small question: what delay is expressed in ms or us
2014-03-18 05:15 PM
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?2014-03-20 08:23 AM