cancel
Showing results for 
Search instead for 
Did you mean: 

How to make microsecond delay using timer?

BBart.16
Associate II

Im using F030F4P6 controller. Here's TIM16 configuration:

0690X000009akJnQAI.png

Here's clock configuration:

0690X000009akJiQAI.png

main.c:

  /* USER CODE BEGIN WHILE */
while (1) {
 
		toggleAll();
		delayUs(1000);
 
/* USER CODE END WHILE */

...

delayUs function:

void delayUs(uint32_t delay) {
 
	usCounter = 0;
 
	HAL_TIM_Base_Start(&htim16);
 
	while(usCounter<delay){
		usCounter = __HAL_TIM_GET_COUNTER(&htim16);
	}
 
	HAL_TIM_Base_Stop(&htim16);
 
	usCounter = 0;
 
}

Complete code at:

http://github.com/bartoszp1992/ProjectPhantom

Missing desc:

  1. toggleAll() - function that toggle all LEDs connected to UC(L0-L6)
  2. I haven't any problem to see blinking at 1ms, becouse my PCB are spinning at ~35rps.

Problem:

Not working. Program is turning on all LEDs once, and stucks at delayUs() function. Doesn't know why.

Thanks for response!

5 REPLIES 5

>>Doesn't know why

Start by using the debugger, and check to see that TIM16->CNT increments.

htim16.Init.Prescaler = 1; // Needs to be MHz of TIMCLK -1 (here assumes TIMCLK = 2 MHz, is that correct?)

htim16.Init.CounterMode = TIM_COUNTERMODE_UP;

htim16.Init.Period = 23; // Needs to be maximal, ie 0xFFFF

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

Okey, thanks for response. That was stupid. But i tried other method. I just turned on interrupts from timer 16 in cube MX(NVIC tab).

And now, my interrupt looks like this:

if (htim->Instance == TIM16) {
		usCounter++;
	}

(of course in Period elapsed callback).

And my delayUs() looks like this:

void delayUs(uint32_t delay) {
 
	usCounter = 0;
 
	HAL_TIM_Base_Start_IT(&htim16);
 
 
	while(usCounter<=delay);
 
 
	HAL_TIM_Base_Stop_IT(&htim16);
 
	usCounter = 0;
}

I turned off GPIOS for LED 5 and 6(CLK and DIO also:) ) and turn on debugger.

Debugger says, that program stucks at interrput and incrementing usCounter integer to 423987209837042301 or something. Why it cant stops while loop when reach e.g 10, set as delayUs argument?

BBart.16
Associate II

Edit:

while i remove while, uc also stucks and only incrementing usCounter.

Unfortunately 1 MHz is impractically fast to interrupt, which is why the TIM->CNT method is normally used.

I would tend just to initialize the TIM once, and read the TIM->CNT start value, and then delta the count as it increments.

ie

uint16_t start = TIM16->CNT;

while(((uint16_t)TIM16->CNT - start) < delay);

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

Hi bartoszp,

If you only need a delay function that waste us CPU time, you do not need to activate timer interrupts.

Consider my example on STM32F0 with Timer 2 (32 bit) as an attached file. Leave me your comment if it could help you.

Regards

rrom

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.