cancel
Showing results for 
Search instead for 
Did you mean: 

Create a precision timer with HAL and interrupt

matteo239955_st
Associate II
Posted on November 20, 2015 at 12:41

Hi guys,

I'm using CubeMx with HAL STM32F051.

I need to activate a one shot timer with interrupt.

Something likr this:

void StartTimer(unsigned short microSecond){

...

...

}

volatile unsigned char TimerIsOver = 0;

 void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef *htim) {

TimerIsOver = 1;

}

void main(void) {

LedOff();

TimerIsOver = 0;

StartTimer(20);

while(!TimerIsOver);

LedOn();

TimerIsOver = 0;

HAL_Delay(50);

LedOff();

StartTimer(340);

while(!TimerIsOver);

LedOn();

}

#stm32-hal-tim
4 REPLIES 4
Posted on November 20, 2015 at 19:20

Any question?

Posted on November 20, 2015 at 22:33

For micro-seconds, where you're going to idle in a loop, why not just have that timer free run, and delta the tick count from TIMx->CNT ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
matteo239955_st
Associate II
Posted on November 23, 2015 at 08:18

Please could you write for me StartTimer function?

Thank you very much...

Posted on November 23, 2015 at 15:14

Sorry, I'm not writing HAL examples.

A free running timer would be a matter of configuring the time base in a maximal count mode, in your case probably with a 1us (1MHz) prescaled clock. Read the count at the start of the loop, and delta it over the duration. Interrupting at 1 MHz is NOT viable, so software counters aren't a solution.

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