cancel
Showing results for 
Search instead for 
Did you mean: 

PROBLEM WITH TIME IN STM32F2 SERIES

Arman Ilmak
Senior
Posted on February 21, 2018 at 18:25

Hey guys.

I'm working with an ultra sound piece for measuring the distance.

When i set the timer1 to count every 1 ms it works(i set PSC 59999 and ARR 1000 to get 1 second with 120M cpu clock.).

But when i set the PSC 11 and ARR 1 to get a 0.1 us interrupt its not correct and it doesn't count correctly. 

Do you know what is the problem?

15 REPLIES 15
Posted on February 21, 2018 at 18:31

You aren't going to be able to interrupt at 10 MHz (100ns), you're going to need to be realistic, and do things at high rates in hardware.

Depending on the load the interrupt causes, you might expect to support several hundred KHz.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
henry.dick
Senior II
Posted on February 21, 2018 at 18:37

Your interval is too short for the mcu.

Much better for you to ask for a better approach to do what you want to do, than to try the impossible.

Posted on February 21, 2018 at 20:53

I want to work with this HC_SR04 module to measure distance.

The  ultra sound speed is 340 m/s so for a centimeter  accuracy i need at least 30 us .

What should i do?

Posted on February 21, 2018 at 21:09

What if i do not use interrupt and just use the timer?

In this case am i able to count every at least 1 us(1MHz clock)?

Posted on February 21, 2018 at 21:31

TIM2 is 32-bit, have it count maximally, ie ARR=0xFFFFFFFF. You can set the prescaler to count at 1 MHz, 10 MHz or 60 MHz (ie 16.67ns ticks)

If you have an input pulse you could use one of the channels and latch the count, ie timestamp on your chosen timebase.

The DWT_CYCCNT is also available, it is 32-bit wide and counts 120 MHz core ticks.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Arman Ilmak
Senior
Posted on February 21, 2018 at 22:38

The other problem i have is that when i use HAL DRIVERS to generate the code i don't know how to reset the timer.I have seen the whole hal libraries PDF and searched through it but i just found start and stop timer.There was no reset option.I decided to do it with TIM1->CNT but it was 0 all the time the timer was working and it didn't   increment.

Posted on February 21, 2018 at 21:58

Depends on the nature of the signal you are trying to capture. For example if you are trying to measure the distance between two pulses, you can use the capture function of the timer.

You have to understand the signal before you decide on an approach. Not the other way around.

Posted on February 21, 2018 at 22:42

Writing CNT should work, if it is not counting you have other issues, you'd want to inspect the TIM registers, and make sure the peripheral clock was enabled in the APB2ENR and not held in reset in APB2RST

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 22, 2018 at 12:37

The problem is that i was working on LPC series about  a year and it wasn't as complex as stm mcu's are especially i used only registers to write my code without any problem.But these mcu's have a lot of registers and working with them is not easy and they are a little different.For example in lpc series i just enabled the timer and the timer counter counted until it reaches its final value,but in stm series you need to set ARR to set the max value and i thought its just for generating interrupt.So the problem was when i didn't use interrupt i didn't give ARR any number so the CNT did not count.

Thank u guys for the help,