cancel
Showing results for 
Search instead for 
Did you mean: 

how to make 500nS delay function using timer3

luisfynn1
Associate II
Posted on August 11, 2015 at 07:04

I wanna to make 500ns delay function using timer3. I can't use systick timer because FreeRTOS use systick timer. 

I wrote my code below.. please help me !!!

13 void TimerInit(void)

 14 {

 15     NVIC_InitTypeDef NVIC_InitStructure;

 16     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

 17

 18     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);            //TIM2 clock enable

 19     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);            //TIM3 clock enable

 20

 21     NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;                 /* Enable the TIM2 Interrupt */

 22     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 14;

 23     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

 24     NVIC_Init(&NVIC_InitStructure);

 25

 26     NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;                 /* Enable the TIM3 Interrupt */

 27     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 14;

 28     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

 29     NVIC_Init(&NVIC_InitStructure);

 30

 31     //Time base configuration

 32     //time of 1 clock count = (Period * Prescaler) / (System Core Clock)

 33     //(10000*72)/72000000 = 0.01(10mS)

 34     TIM_TimeBaseStructure.TIM_Period = 10000-1;     // Interrupt occured, every 10000 Clock count.

 35     TIM_TimeBaseStructure.TIM_Prescaler = 72-1;     // Timer clock setting. Up to 1s, Clock Count need to 1,000,000(72/System Clock(=72Mhz))

 36     TIM_TimeBaseStructure.TIM_ClockDivision = 0;

 37     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

 38     TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

 39     TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);      /* Enable TIM2 Update interrupt */

 40     //  TIM_Cmd(TIM2, ENABLE);                      // TIM2 enable counter

 41

 42     //Time base configuration

 43     //time of 1 clock count = (Period * Prescaler) / (System Core Clock)

 44     //(24*3)/72000000 = 0.00001(10uS)

 45     TIM_TimeBaseStructure.TIM_Period = 1-1;     // Interrupt occured, every 1000 Clock count.

 46     TIM_TimeBaseStructure.TIM_Prescaler = 72-1;     // Timer clock setting. Up to 1s, Clock Count need to 1,000,000(72/System Clock(=72Mhz))

 47     TIM_TimeBaseStructure.TIM_ClockDivision = 0;

 48     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

 49     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

 50     TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);      /* Enable TIM2 Update interrupt */

 51     TIM_Cmd(TIM3, ENABLE);                          // TIM3 enable counter

 52 }

 53

#stm32 #stm32f103 #500ns-delay
7 REPLIES 7
RomainR.
ST Employee
Posted on August 11, 2015 at 11:00

Check your configuration of the 3 TIMER structure.

You select a autoreload period of 1-1 = 0, your TIM3_CNT counter may never generate interrupt?

// Time base configuration

// Time of 1 count = clock (Prescaler Period *) / (System Core Clock)

// (24 * 3) / 72000000 = 0.00001 (10us)

TIM_TimeBaseStructure.TIM_Period = 1-1;

// Interrupt occured, 1000 every clock count.

TIM_TimeBaseStructure.TIM_Prescaler = 72-1;

// Timer clock setting.

Up to 1s, Clock Count need to 1,000,000 (72 / System Clock (= 72Mhz))

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit (TIM3, & TIM_TimeBaseStructure);

TIM_ITConfig (TIM3, TIM_IT_Update, ENABLE);

/ * Enable TIM2 Update interrupt * /

TIM_Cmd (TIM3, ENABLE);

// Enable TIM3 counter

You should also observe two important points:

1- Always begin the configuration with TIM_DeInit (timx);

and fill the default structure TIM_TimeBaseStructInit ();

2- You should first configure the structure of the TIMER and after the NVIC

Good luck

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

RomainR.
ST Employee
Posted on August 11, 2015 at 11:29

here my example to generate 500ns Time Base interrupt:

void
TIMER_Configuration(
void
){ 

/* Stuctures instance for used peripherals */

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 

NVIC_InitTypeDef NVIC_InitStructure; 


/* Enable Peripheral clock for TIMER3 */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); 
// To be checked into your STD_Lib !! 

/* TIM4 Peripheral Configuration */

TIM_DeInit(TIM4); 


/* TIM4 Base configuration PCLK = 72MHz */

TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); 
// Very Important ! 

TIM_TimeBaseStructure.TIM_Prescaler = 11; 
// PSC = (CK_PSC/CK_CNT)-1 = (72MHz/6MHz)-1 = 11 

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 

TIM_TimeBaseStructure.TIM_Period = 2; 
// TIM3 Resolution = 7ns * 3 (Autoreload Period +1) = 500ns update Rate 

TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; 

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; 

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 


TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); 
// To be checked into your STD_Lib !! 

TIM_Cmd(TIM3, ENABLE); 


NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; 

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 14; 
// To be checked into your STD_Lib !! 

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 

NVIC_Init(&NVIC_InitStructure); 


}

Check your configuration of the 3 TIMER structure.

You select a autoreload period of 1-1 = 0, your TIM3_CNT counter may never generate interrupt?

// Time base configuration

// Time of 1 count = clock (Prescaler Period *) / (System Core Clock)

// (24 * 3) / 72000000 = 0.00001 (10us)

TIM_TimeBaseStructure.TIM_Period = 1-1;

// Interrupt occured, 1000 every clock count.

TIM_TimeBaseStructure.TIM_Prescaler = 72-1;

// Timer clock setting.

Up to 1s, Clock Count need to 1,000,000 (72 / System Clock (= 72Mhz))

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit (TIM3, & TIM_TimeBaseStructure);

TIM_ITConfig (TIM3, TIM_IT_Update, ENABLE);

/ * Enable TIM2 Update interrupt * /

TIM_Cmd (TIM3, ENABLE);

// Enable TIM3 counter

You should also observe two important points:

1- Always begin the configuration with TIM_DeInit (timx);

and fill the default structure TIM_TimeBaseStructInit ();

2- You should first configure the structure of the TIMER and after the NVIC

Good luck

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Posted on August 11, 2015 at 15:20

Somehow interrupting at 2 MHz seems rather impractical.

You should probably just clock the timer at 72 MHz, at maximal length, and monitor the count in a loop. There are other core level 32-bit counters that could be used.

For signal generation look at doing it in hardware.

The Prescaler should typically be the smaller factor, and can be zero. The Period the larger, and always non-zero, and ideally >1. This will permit the finest granularity in the tick periodicity.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ensafatef
Associate
Posted on February 23, 2016 at 14:55

ensafatef
Associate
Posted on February 23, 2016 at 14:58

Radosław
Senior II
Posted on February 23, 2016 at 15:54

1. When change prescaler uptade generetion is needed.

2. 0,5us is  very small time, so use preskaler 1.

3. If it is only delay not period generator be notice that cnfiguration of timer take some time and with SPL it will be wveeeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrryyyyyyyy LONG.

Why you need timer for so small delay?

Posted on February 23, 2016 at 20:48

@happy.ich_bin

The forum does not support mobile platforms well. Please avoid posting to old threads (>1 year), start your own thread, state your own question/situation *fully*. If you think an old thread has salient information, cite it.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..